blob: ea7245c5fb69bacc0f72bdd9186bbe4e49e5689a [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
608static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
609{
610 id_tbl->start = start_id;
611 id_tbl->max = size;
612 id_tbl->next = 0;
613 spin_lock_init(&id_tbl->lock);
614 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
615 if (!id_tbl->table)
616 return -ENOMEM;
617
618 return 0;
619}
620
621static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
622{
623 kfree(id_tbl->table);
624 id_tbl->table = NULL;
625}
626
627static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
628{
629 int ret = -1;
630
631 id -= id_tbl->start;
632 if (id >= id_tbl->max)
633 return ret;
634
635 spin_lock(&id_tbl->lock);
636 if (!test_bit(id, id_tbl->table)) {
637 set_bit(id, id_tbl->table);
638 ret = 0;
639 }
640 spin_unlock(&id_tbl->lock);
641 return ret;
642}
643
644/* Returns -1 if not successful */
645static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
646{
647 u32 id;
648
649 spin_lock(&id_tbl->lock);
650 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
651 if (id >= id_tbl->max) {
652 id = -1;
653 if (id_tbl->next != 0) {
654 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
655 if (id >= id_tbl->next)
656 id = -1;
657 }
658 }
659
660 if (id < id_tbl->max) {
661 set_bit(id, id_tbl->table);
662 id_tbl->next = (id + 1) & (id_tbl->max - 1);
663 id += id_tbl->start;
664 }
665
666 spin_unlock(&id_tbl->lock);
667
668 return id;
669}
670
671static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
672{
673 if (id == -1)
674 return;
675
676 id -= id_tbl->start;
677 if (id >= id_tbl->max)
678 return;
679
680 clear_bit(id, id_tbl->table);
681}
682
683static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
684{
685 int i;
686
687 if (!dma->pg_arr)
688 return;
689
690 for (i = 0; i < dma->num_pages; i++) {
691 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000692 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
693 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700694 dma->pg_arr[i] = NULL;
695 }
696 }
697 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000698 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
699 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700700 dma->pgtbl = NULL;
701 }
702 kfree(dma->pg_arr);
703 dma->pg_arr = NULL;
704 dma->num_pages = 0;
705}
706
707static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
708{
709 int i;
Michael Chan51388262011-01-25 22:14:50 +0000710 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700711
712 for (i = 0; i < dma->num_pages; i++) {
713 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000714 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700715 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000716 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700717 page_table++;
718 }
719}
720
Michael Chan71034ba2009-10-10 13:46:59 +0000721static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
722{
723 int i;
Michael Chan51388262011-01-25 22:14:50 +0000724 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000725
726 for (i = 0; i < dma->num_pages; i++) {
727 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000728 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000729 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000730 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000731 page_table++;
732 }
733}
734
Michael Chana4636962009-06-08 18:14:43 -0700735static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
736 int pages, int use_pg_tbl)
737{
738 int i, size;
739 struct cnic_local *cp = dev->cnic_priv;
740
741 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
742 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
743 if (dma->pg_arr == NULL)
744 return -ENOMEM;
745
746 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
747 dma->num_pages = pages;
748
749 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000750 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
751 BCM_PAGE_SIZE,
752 &dma->pg_map_arr[i],
753 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700754 if (dma->pg_arr[i] == NULL)
755 goto error;
756 }
757 if (!use_pg_tbl)
758 return 0;
759
760 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
761 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000762 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
763 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700764 if (dma->pgtbl == NULL)
765 goto error;
766
767 cp->setup_pgtbl(dev, dma);
768
769 return 0;
770
771error:
772 cnic_free_dma(dev, dma);
773 return -ENOMEM;
774}
775
Michael Chan86b53602009-10-10 13:46:57 +0000776static void cnic_free_context(struct cnic_dev *dev)
777{
778 struct cnic_local *cp = dev->cnic_priv;
779 int i;
780
781 for (i = 0; i < cp->ctx_blks; i++) {
782 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000783 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
784 cp->ctx_arr[i].ctx,
785 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000786 cp->ctx_arr[i].ctx = NULL;
787 }
788 }
789}
790
Michael Chancd801532010-10-13 14:06:49 +0000791static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700792{
Michael Chancd801532010-10-13 14:06:49 +0000793 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700794
Michael Chancd801532010-10-13 14:06:49 +0000795 if (udev->l2_buf) {
796 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
797 udev->l2_buf, udev->l2_buf_map);
798 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700799 }
800
Michael Chancd801532010-10-13 14:06:49 +0000801 if (udev->l2_ring) {
802 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
803 udev->l2_ring, udev->l2_ring_map);
804 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700805 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000806
807 pci_dev_put(udev->pdev);
808 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000809}
810
Michael Chancd801532010-10-13 14:06:49 +0000811static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000812{
Michael Chancd801532010-10-13 14:06:49 +0000813 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000814 return;
815
Michael Chana3ceeeb2010-10-13 14:06:50 +0000816 write_lock(&cnic_dev_lock);
817 list_del_init(&udev->list);
818 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000819 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000820}
821
822static void cnic_free_resc(struct cnic_dev *dev)
823{
824 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000825 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000826
Michael Chancd801532010-10-13 14:06:49 +0000827 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000828 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000829 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000830 }
Michael Chana4636962009-06-08 18:14:43 -0700831
Michael Chan86b53602009-10-10 13:46:57 +0000832 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700833 kfree(cp->ctx_arr);
834 cp->ctx_arr = NULL;
835 cp->ctx_blks = 0;
836
837 cnic_free_dma(dev, &cp->gbl_buf_info);
838 cnic_free_dma(dev, &cp->conn_buf_info);
839 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000840 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000841 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000842 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700843 kfree(cp->iscsi_tbl);
844 cp->iscsi_tbl = NULL;
845 kfree(cp->ctx_tbl);
846 cp->ctx_tbl = NULL;
847
Michael Chane1928c82010-12-23 07:43:04 +0000848 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700849 cnic_free_id_tbl(&cp->cid_tbl);
850}
851
852static int cnic_alloc_context(struct cnic_dev *dev)
853{
854 struct cnic_local *cp = dev->cnic_priv;
855
856 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
857 int i, k, arr_size;
858
859 cp->ctx_blk_size = BCM_PAGE_SIZE;
860 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
861 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
862 sizeof(struct cnic_ctx);
863 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
864 if (cp->ctx_arr == NULL)
865 return -ENOMEM;
866
867 k = 0;
868 for (i = 0; i < 2; i++) {
869 u32 j, reg, off, lo, hi;
870
871 if (i == 0)
872 off = BNX2_PG_CTX_MAP;
873 else
874 off = BNX2_ISCSI_CTX_MAP;
875
876 reg = cnic_reg_rd_ind(dev, off);
877 lo = reg >> 16;
878 hi = reg & 0xffff;
879 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
880 cp->ctx_arr[k].cid = j;
881 }
882
883 cp->ctx_blks = k;
884 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
885 cp->ctx_blks = 0;
886 return -ENOMEM;
887 }
888
889 for (i = 0; i < cp->ctx_blks; i++) {
890 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000891 dma_alloc_coherent(&dev->pcidev->dev,
892 BCM_PAGE_SIZE,
893 &cp->ctx_arr[i].mapping,
894 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700895 if (cp->ctx_arr[i].ctx == NULL)
896 return -ENOMEM;
897 }
898 }
899 return 0;
900}
901
Michael Chane6c28892010-06-24 14:58:39 +0000902static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
903{
904 int err, i, is_bnx2 = 0;
905 struct kcqe **kcq;
906
907 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
908 is_bnx2 = 1;
909
910 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
911 if (err)
912 return err;
913
914 kcq = (struct kcqe **) info->dma.pg_arr;
915 info->kcq = kcq;
916
917 if (is_bnx2)
918 return 0;
919
920 for (i = 0; i < KCQ_PAGE_CNT; i++) {
921 struct bnx2x_bd_chain_next *next =
922 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
923 int j = i + 1;
924
925 if (j >= KCQ_PAGE_CNT)
926 j = 0;
927 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
928 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
929 }
930 return 0;
931}
932
Michael Chancd801532010-10-13 14:06:49 +0000933static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000934{
935 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000936 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000937
Michael Chana3ceeeb2010-10-13 14:06:50 +0000938 read_lock(&cnic_dev_lock);
939 list_for_each_entry(udev, &cnic_udev_list, list) {
940 if (udev->pdev == dev->pcidev) {
941 udev->dev = dev;
942 cp->udev = udev;
943 read_unlock(&cnic_dev_lock);
944 return 0;
945 }
946 }
947 read_unlock(&cnic_dev_lock);
948
Michael Chancd801532010-10-13 14:06:49 +0000949 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
950 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +0000951 return -ENOMEM;
952
Michael Chancd801532010-10-13 14:06:49 +0000953 udev->uio_dev = -1;
954
955 udev->dev = dev;
956 udev->pdev = dev->pcidev;
957 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
958 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
959 &udev->l2_ring_map,
960 GFP_KERNEL | __GFP_COMP);
961 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800962 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000963
Michael Chancd801532010-10-13 14:06:49 +0000964 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
965 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
966 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
967 &udev->l2_buf_map,
968 GFP_KERNEL | __GFP_COMP);
969 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800970 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +0000971
Michael Chana3ceeeb2010-10-13 14:06:50 +0000972 write_lock(&cnic_dev_lock);
973 list_add(&udev->list, &cnic_udev_list);
974 write_unlock(&cnic_dev_lock);
975
976 pci_dev_get(udev->pdev);
977
Michael Chancd801532010-10-13 14:06:49 +0000978 cp->udev = udev;
979
Michael Chanec0248e2009-08-26 09:49:22 +0000980 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800981 err_dma:
982 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
983 udev->l2_ring, udev->l2_ring_map);
984 err_udev:
985 kfree(udev);
986 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +0000987}
988
Michael Chancd801532010-10-13 14:06:49 +0000989static int cnic_init_uio(struct cnic_dev *dev)
990{
Michael Chan5e9b2db2009-08-26 09:49:23 +0000991 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000992 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000993 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +0000994 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000995
Michael Chancd801532010-10-13 14:06:49 +0000996 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +0000997 return -ENOMEM;
998
Michael Chancd801532010-10-13 14:06:49 +0000999 uinfo = &udev->cnic_uinfo;
1000
Michael Chan5e9b2db2009-08-26 09:49:23 +00001001 uinfo->mem[0].addr = dev->netdev->base_addr;
1002 uinfo->mem[0].internal_addr = dev->regview;
1003 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
1004 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1005
Michael Chan5e9b2db2009-08-26 09:49:23 +00001006 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001007 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001008 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001009 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1010 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1011 else
1012 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1013
1014 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001015 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1016 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1017 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001018 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001019
1020 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001021 }
1022
1023 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1024
Michael Chancd801532010-10-13 14:06:49 +00001025 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1026 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001027 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1028
Michael Chancd801532010-10-13 14:06:49 +00001029 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1030 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001031 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1032
1033 uinfo->version = CNIC_MODULE_VERSION;
1034 uinfo->irq = UIO_IRQ_CUSTOM;
1035
1036 uinfo->open = cnic_uio_open;
1037 uinfo->release = cnic_uio_close;
1038
Michael Chana3ceeeb2010-10-13 14:06:50 +00001039 if (udev->uio_dev == -1) {
1040 if (!uinfo->priv) {
1041 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001042
Michael Chana3ceeeb2010-10-13 14:06:50 +00001043 ret = uio_register_device(&udev->pdev->dev, uinfo);
1044 }
1045 } else {
1046 cnic_init_rings(dev);
1047 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001048
Michael Chancd801532010-10-13 14:06:49 +00001049 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001050}
1051
Michael Chana4636962009-06-08 18:14:43 -07001052static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1053{
1054 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001055 int ret;
1056
1057 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1058 if (ret)
1059 goto error;
1060 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1061
Michael Chane6c28892010-06-24 14:58:39 +00001062 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chana4636962009-06-08 18:14:43 -07001063 if (ret)
1064 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001065
1066 ret = cnic_alloc_context(dev);
1067 if (ret)
1068 goto error;
1069
Michael Chancd801532010-10-13 14:06:49 +00001070 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001071 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001072 goto error;
1073
Michael Chancd801532010-10-13 14:06:49 +00001074 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001075 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001076 goto error;
1077
Michael Chana4636962009-06-08 18:14:43 -07001078 return 0;
1079
1080error:
1081 cnic_free_resc(dev);
1082 return ret;
1083}
1084
Michael Chan71034ba2009-10-10 13:46:59 +00001085static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1086{
1087 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001088 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001089 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001090
Michael Chan520efdf2010-06-24 14:58:37 +00001091 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001092 blks = total_mem / ctx_blk_size;
1093 if (total_mem % ctx_blk_size)
1094 blks++;
1095
1096 if (blks > cp->ethdev->ctx_tbl_len)
1097 return -ENOMEM;
1098
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001099 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001100 if (cp->ctx_arr == NULL)
1101 return -ENOMEM;
1102
1103 cp->ctx_blks = blks;
1104 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001105 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001106 cp->ctx_align = 0;
1107 else
1108 cp->ctx_align = ctx_blk_size;
1109
1110 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1111
1112 for (i = 0; i < blks; i++) {
1113 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001114 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1115 &cp->ctx_arr[i].mapping,
1116 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001117 if (cp->ctx_arr[i].ctx == NULL)
1118 return -ENOMEM;
1119
1120 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1121 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1122 cnic_free_context(dev);
1123 cp->ctx_blk_size += cp->ctx_align;
1124 i = -1;
1125 continue;
1126 }
1127 }
1128 }
1129 return 0;
1130}
1131
1132static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1133{
1134 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001135 struct cnic_eth_dev *ethdev = cp->ethdev;
1136 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001137 int i, j, n, ret, pages;
1138 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1139
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001140 cp->iro_arr = ethdev->iro_arr;
1141
Michael Chane1928c82010-12-23 07:43:04 +00001142 cp->max_cid_space = MAX_ISCSI_TBL_SZ + BNX2X_FCOE_NUM_CONNECTIONS;
Michael Chan520efdf2010-06-24 14:58:37 +00001143 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001144 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1145
1146 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1147 cp->max_cid_space += BNX2X_FCOE_NUM_CONNECTIONS;
1148 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1149 if (!cp->fcoe_init_cid)
1150 cp->fcoe_init_cid = 0x10;
1151 }
1152
Michael Chan520efdf2010-06-24 14:58:37 +00001153 if (start_cid < BNX2X_ISCSI_START_CID) {
1154 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1155
1156 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
Michael Chane1928c82010-12-23 07:43:04 +00001157 cp->fcoe_start_cid += delta;
Michael Chan520efdf2010-06-24 14:58:37 +00001158 cp->max_cid_space += delta;
1159 }
1160
Michael Chan71034ba2009-10-10 13:46:59 +00001161 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1162 GFP_KERNEL);
1163 if (!cp->iscsi_tbl)
1164 goto error;
1165
1166 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001167 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001168 if (!cp->ctx_tbl)
1169 goto error;
1170
1171 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1172 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1173 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1174 }
1175
Michael Chane1928c82010-12-23 07:43:04 +00001176 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1177 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1178
Michael Chan520efdf2010-06-24 14:58:37 +00001179 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001180 PAGE_SIZE;
1181
1182 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1183 if (ret)
1184 return -ENOMEM;
1185
1186 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001187 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001188 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1189
1190 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1191 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1192 off;
1193
1194 if ((i % n) == (n - 1))
1195 j++;
1196 }
1197
Michael Chane6c28892010-06-24 14:58:39 +00001198 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00001199 if (ret)
1200 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001201
Michael Chane21ba412010-12-23 07:43:03 +00001202 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1203 ret = cnic_alloc_kcq(dev, &cp->kcq2);
1204 if (ret)
1205 goto error;
1206 }
1207
Michael Chan71034ba2009-10-10 13:46:59 +00001208 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1209 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1210 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1211 if (ret)
1212 goto error;
1213
1214 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1215 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1216 if (ret)
1217 goto error;
1218
1219 ret = cnic_alloc_bnx2x_context(dev);
1220 if (ret)
1221 goto error;
1222
Michael Chan71034ba2009-10-10 13:46:59 +00001223 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1224
1225 cp->l2_rx_ring_size = 15;
1226
Michael Chancd801532010-10-13 14:06:49 +00001227 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001228 if (ret)
1229 goto error;
1230
Michael Chancd801532010-10-13 14:06:49 +00001231 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001232 if (ret)
1233 goto error;
1234
1235 return 0;
1236
1237error:
1238 cnic_free_resc(dev);
1239 return -ENOMEM;
1240}
1241
Michael Chana4636962009-06-08 18:14:43 -07001242static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1243{
1244 return cp->max_kwq_idx -
1245 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1246}
1247
1248static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1249 u32 num_wqes)
1250{
1251 struct cnic_local *cp = dev->cnic_priv;
1252 struct kwqe *prod_qe;
1253 u16 prod, sw_prod, i;
1254
1255 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1256 return -EAGAIN; /* bnx2 is down */
1257
1258 spin_lock_bh(&cp->cnic_ulp_lock);
1259 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001260 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001261 spin_unlock_bh(&cp->cnic_ulp_lock);
1262 return -EAGAIN;
1263 }
1264
Michael Chan1f1332a2010-05-18 11:32:52 +00001265 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001266
1267 prod = cp->kwq_prod_idx;
1268 sw_prod = prod & MAX_KWQ_IDX;
1269 for (i = 0; i < num_wqes; i++) {
1270 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1271 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1272 prod++;
1273 sw_prod = prod & MAX_KWQ_IDX;
1274 }
1275 cp->kwq_prod_idx = prod;
1276
1277 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1278
1279 spin_unlock_bh(&cp->cnic_ulp_lock);
1280 return 0;
1281}
1282
Michael Chan71034ba2009-10-10 13:46:59 +00001283static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1284 union l5cm_specific_data *l5_data)
1285{
1286 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1287 dma_addr_t map;
1288
1289 map = ctx->kwqe_data_mapping;
1290 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1291 l5_data->phy_address.hi = (u64) map >> 32;
1292 return ctx->kwqe_data;
1293}
1294
1295static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1296 u32 type, union l5cm_specific_data *l5_data)
1297{
1298 struct cnic_local *cp = dev->cnic_priv;
1299 struct l5cm_spe kwqe;
1300 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001301 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001302 int ret;
1303
1304 kwqe.hdr.conn_and_cmd_data =
1305 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001306 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001307
1308 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1309 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1310 SPE_HDR_FUNCTION_ID;
1311
1312 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001313 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001314 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1315 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1316
1317 kwq[0] = (struct kwqe_16 *) &kwqe;
1318
1319 spin_lock_bh(&cp->cnic_ulp_lock);
1320 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1321 spin_unlock_bh(&cp->cnic_ulp_lock);
1322
1323 if (ret == 1)
1324 return 0;
1325
1326 return -EBUSY;
1327}
1328
1329static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1330 struct kcqe *cqes[], u32 num_cqes)
1331{
1332 struct cnic_local *cp = dev->cnic_priv;
1333 struct cnic_ulp_ops *ulp_ops;
1334
1335 rcu_read_lock();
1336 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1337 if (likely(ulp_ops)) {
1338 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1339 cqes, num_cqes);
1340 }
1341 rcu_read_unlock();
1342}
1343
1344static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1345{
1346 struct cnic_local *cp = dev->cnic_priv;
1347 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001348 int hq_bds, pages;
1349 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001350
1351 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1352 cp->num_ccells = req1->num_ccells_per_conn;
1353 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1354 cp->num_iscsi_tasks;
1355 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1356 BNX2X_ISCSI_R2TQE_SIZE;
1357 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1358 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1359 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1360 cp->num_cqs = req1->num_cqs;
1361
1362 if (!dev->max_iscsi_conn)
1363 return 0;
1364
1365 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001366 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001367 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001368 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001369 PAGE_SIZE);
1370 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001371 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001372 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001373 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001374 req1->num_tasks_per_conn);
1375
1376 /* init Ustorm RAM */
1377 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001378 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001379 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001380 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001381 PAGE_SIZE);
1382 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001383 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001384 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001385 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001386 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001387 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001388 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001389 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001390 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001391 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001392 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1393
1394 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001395 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001396 PAGE_SIZE);
1397 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001398 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001399 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001400 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001401 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001402 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001403 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001404 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001405 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001406 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001407 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1408
1409 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001410 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001411 PAGE_SIZE);
1412 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001413 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001414 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001415 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001416 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001417 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001418 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001419 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001420 hq_bds);
1421
1422 return 0;
1423}
1424
1425static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1426{
1427 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1428 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001429 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001430 struct iscsi_kcqe kcqe;
1431 struct kcqe *cqes[1];
1432
1433 memset(&kcqe, 0, sizeof(kcqe));
1434 if (!dev->max_iscsi_conn) {
1435 kcqe.completion_status =
1436 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1437 goto done;
1438 }
1439
1440 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001441 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001442 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001443 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001444 req2->error_bit_map[1]);
1445
1446 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001447 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001448 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001449 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001450 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001451 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001452 req2->error_bit_map[1]);
1453
1454 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001455 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001456
1457 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1458
1459done:
1460 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1461 cqes[0] = (struct kcqe *) &kcqe;
1462 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1463
1464 return 0;
1465}
1466
1467static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1468{
1469 struct cnic_local *cp = dev->cnic_priv;
1470 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1471
1472 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1473 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1474
1475 cnic_free_dma(dev, &iscsi->hq_info);
1476 cnic_free_dma(dev, &iscsi->r2tq_info);
1477 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001478 cnic_free_id(&cp->cid_tbl, ctx->cid);
1479 } else {
1480 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001481 }
Michael Chane1928c82010-12-23 07:43:04 +00001482
Michael Chan71034ba2009-10-10 13:46:59 +00001483 ctx->cid = 0;
1484}
1485
1486static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1487{
1488 u32 cid;
1489 int ret, pages;
1490 struct cnic_local *cp = dev->cnic_priv;
1491 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1492 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1493
Michael Chane1928c82010-12-23 07:43:04 +00001494 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1495 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1496 if (cid == -1) {
1497 ret = -ENOMEM;
1498 goto error;
1499 }
1500 ctx->cid = cid;
1501 return 0;
1502 }
1503
Michael Chan71034ba2009-10-10 13:46:59 +00001504 cid = cnic_alloc_new_id(&cp->cid_tbl);
1505 if (cid == -1) {
1506 ret = -ENOMEM;
1507 goto error;
1508 }
1509
1510 ctx->cid = cid;
1511 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1512
1513 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1514 if (ret)
1515 goto error;
1516
1517 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1518 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1519 if (ret)
1520 goto error;
1521
1522 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1523 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1524 if (ret)
1525 goto error;
1526
1527 return 0;
1528
1529error:
1530 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1531 return ret;
1532}
1533
1534static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1535 struct regpair *ctx_addr)
1536{
1537 struct cnic_local *cp = dev->cnic_priv;
1538 struct cnic_eth_dev *ethdev = cp->ethdev;
1539 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1540 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1541 unsigned long align_off = 0;
1542 dma_addr_t ctx_map;
1543 void *ctx;
1544
1545 if (cp->ctx_align) {
1546 unsigned long mask = cp->ctx_align - 1;
1547
1548 if (cp->ctx_arr[blk].mapping & mask)
1549 align_off = cp->ctx_align -
1550 (cp->ctx_arr[blk].mapping & mask);
1551 }
1552 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1553 (off * BNX2X_CONTEXT_MEM_SIZE);
1554 ctx = cp->ctx_arr[blk].ctx + align_off +
1555 (off * BNX2X_CONTEXT_MEM_SIZE);
1556 if (init)
1557 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1558
1559 ctx_addr->lo = ctx_map & 0xffffffff;
1560 ctx_addr->hi = (u64) ctx_map >> 32;
1561 return ctx;
1562}
1563
1564static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1565 u32 num)
1566{
1567 struct cnic_local *cp = dev->cnic_priv;
1568 struct iscsi_kwqe_conn_offload1 *req1 =
1569 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1570 struct iscsi_kwqe_conn_offload2 *req2 =
1571 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1572 struct iscsi_kwqe_conn_offload3 *req3;
1573 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1574 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1575 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001576 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001577 struct iscsi_context *ictx;
1578 struct regpair context_addr;
1579 int i, j, n = 2, n_max;
1580
1581 ctx->ctx_flags = 0;
1582 if (!req2->num_additional_wqes)
1583 return -EINVAL;
1584
1585 n_max = req2->num_additional_wqes + 2;
1586
1587 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1588 if (ictx == NULL)
1589 return -ENOMEM;
1590
1591 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1592
1593 ictx->xstorm_ag_context.hq_prod = 1;
1594
1595 ictx->xstorm_st_context.iscsi.first_burst_length =
1596 ISCSI_DEF_FIRST_BURST_LEN;
1597 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1598 ISCSI_DEF_MAX_RECV_SEG_LEN;
1599 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1600 req1->sq_page_table_addr_lo;
1601 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1602 req1->sq_page_table_addr_hi;
1603 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1604 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1605 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1606 iscsi->hq_info.pgtbl_map & 0xffffffff;
1607 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1608 (u64) iscsi->hq_info.pgtbl_map >> 32;
1609 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1610 iscsi->hq_info.pgtbl[0];
1611 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1612 iscsi->hq_info.pgtbl[1];
1613 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1614 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1615 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1616 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1617 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1618 iscsi->r2tq_info.pgtbl[0];
1619 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1620 iscsi->r2tq_info.pgtbl[1];
1621 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1622 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1623 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1624 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1625 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1626 BNX2X_ISCSI_PBL_NOT_CACHED;
1627 ictx->xstorm_st_context.iscsi.flags.flags |=
1628 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1629 ictx->xstorm_st_context.iscsi.flags.flags |=
1630 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1631
1632 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1633 /* TSTORM requires the base address of RQ DB & not PTE */
1634 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1635 req2->rq_page_table_addr_lo & PAGE_MASK;
1636 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1637 req2->rq_page_table_addr_hi;
1638 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1639 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1640 ictx->tstorm_st_context.tcp.flags2 |=
1641 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001642 ictx->tstorm_st_context.tcp.ooo_support_mode =
1643 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001644
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001645 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001646
1647 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001648 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001649 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001650 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001651 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1652 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1653 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1654 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1655 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1656 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1657 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1658 iscsi->r2tq_info.pgtbl[0];
1659 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1660 iscsi->r2tq_info.pgtbl[1];
1661 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1662 req1->cq_page_table_addr_lo;
1663 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1664 req1->cq_page_table_addr_hi;
1665 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1666 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1667 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1668 ictx->ustorm_st_context.task_pbe_cache_index =
1669 BNX2X_ISCSI_PBL_NOT_CACHED;
1670 ictx->ustorm_st_context.task_pdu_cache_index =
1671 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1672
1673 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1674 if (j == 3) {
1675 if (n >= n_max)
1676 break;
1677 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1678 j = 0;
1679 }
1680 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1681 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1682 req3->qp_first_pte[j].hi;
1683 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1684 req3->qp_first_pte[j].lo;
1685 }
1686
1687 ictx->ustorm_st_context.task_pbl_base.lo =
1688 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1689 ictx->ustorm_st_context.task_pbl_base.hi =
1690 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1691 ictx->ustorm_st_context.tce_phy_addr.lo =
1692 iscsi->task_array_info.pgtbl[0];
1693 ictx->ustorm_st_context.tce_phy_addr.hi =
1694 iscsi->task_array_info.pgtbl[1];
1695 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1696 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1697 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1698 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1699 ISCSI_DEF_MAX_BURST_LEN;
1700 ictx->ustorm_st_context.negotiated_rx |=
1701 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1702 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1703
1704 ictx->cstorm_st_context.hq_pbl_base.lo =
1705 iscsi->hq_info.pgtbl_map & 0xffffffff;
1706 ictx->cstorm_st_context.hq_pbl_base.hi =
1707 (u64) iscsi->hq_info.pgtbl_map >> 32;
1708 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1709 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1710 ictx->cstorm_st_context.task_pbl_base.lo =
1711 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1712 ictx->cstorm_st_context.task_pbl_base.hi =
1713 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1714 /* CSTORM and USTORM initialization is different, CSTORM requires
1715 * CQ DB base & not PTE addr */
1716 ictx->cstorm_st_context.cq_db_base.lo =
1717 req1->cq_page_table_addr_lo & PAGE_MASK;
1718 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1719 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1720 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1721 for (i = 0; i < cp->num_cqs; i++) {
1722 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1723 ISCSI_INITIAL_SN;
1724 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1725 ISCSI_INITIAL_SN;
1726 }
1727
1728 ictx->xstorm_ag_context.cdu_reserved =
1729 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1730 ISCSI_CONNECTION_TYPE);
1731 ictx->ustorm_ag_context.cdu_usage =
1732 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1733 ISCSI_CONNECTION_TYPE);
1734 return 0;
1735
1736}
1737
1738static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1739 u32 num, int *work)
1740{
1741 struct iscsi_kwqe_conn_offload1 *req1;
1742 struct iscsi_kwqe_conn_offload2 *req2;
1743 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001744 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001745 struct iscsi_kcqe kcqe;
1746 struct kcqe *cqes[1];
1747 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001748 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001749
1750 if (num < 2) {
1751 *work = num;
1752 return -EINVAL;
1753 }
1754
1755 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1756 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1757 if ((num - 2) < req2->num_additional_wqes) {
1758 *work = num;
1759 return -EINVAL;
1760 }
Joe Perches779bb412010-11-14 17:04:37 +00001761 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001762
1763 l5_cid = req1->iscsi_conn_id;
1764 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1765 return -EINVAL;
1766
1767 memset(&kcqe, 0, sizeof(kcqe));
1768 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1769 kcqe.iscsi_conn_id = l5_cid;
1770 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1771
Michael Chanfdf24082010-10-13 14:06:47 +00001772 ctx = &cp->ctx_tbl[l5_cid];
1773 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1774 kcqe.completion_status =
1775 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1776 goto done;
1777 }
1778
Michael Chan71034ba2009-10-10 13:46:59 +00001779 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1780 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001781 goto done;
1782 }
1783 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1784 if (ret) {
1785 atomic_dec(&cp->iscsi_conn);
1786 ret = 0;
1787 goto done;
1788 }
1789 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1790 if (ret < 0) {
1791 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1792 atomic_dec(&cp->iscsi_conn);
1793 goto done;
1794 }
1795
1796 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001797 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001798
1799done:
1800 cqes[0] = (struct kcqe *) &kcqe;
1801 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1802 return ret;
1803}
1804
1805
1806static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1807{
1808 struct cnic_local *cp = dev->cnic_priv;
1809 struct iscsi_kwqe_conn_update *req =
1810 (struct iscsi_kwqe_conn_update *) kwqe;
1811 void *data;
1812 union l5cm_specific_data l5_data;
1813 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1814 int ret;
1815
1816 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1817 return -EINVAL;
1818
1819 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1820 if (!data)
1821 return -ENOMEM;
1822
1823 memcpy(data, kwqe, sizeof(struct kwqe));
1824
1825 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1826 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1827 return ret;
1828}
1829
Michael Chana2c9e762010-10-13 14:06:46 +00001830static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001831{
1832 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001833 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001834 union l5cm_specific_data l5_data;
1835 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001836 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001837
Michael Chan71034ba2009-10-10 13:46:59 +00001838 init_waitqueue_head(&ctx->waitq);
1839 ctx->wait_cond = 0;
1840 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001841 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001842
1843 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001844 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001845
Michael Chan71034ba2009-10-10 13:46:59 +00001846 if (ret == 0)
1847 wait_event(ctx->waitq, ctx->wait_cond);
1848
Michael Chana2c9e762010-10-13 14:06:46 +00001849 return ret;
1850}
1851
1852static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1853{
1854 struct cnic_local *cp = dev->cnic_priv;
1855 struct iscsi_kwqe_conn_destroy *req =
1856 (struct iscsi_kwqe_conn_destroy *) kwqe;
1857 u32 l5_cid = req->reserved0;
1858 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1859 int ret = 0;
1860 struct iscsi_kcqe kcqe;
1861 struct kcqe *cqes[1];
1862
1863 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1864 goto skip_cfc_delete;
1865
Michael Chanfdf24082010-10-13 14:06:47 +00001866 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1867 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1868
1869 if (delta > (2 * HZ))
1870 delta = 0;
1871
1872 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1873 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1874 goto destroy_reply;
1875 }
Michael Chana2c9e762010-10-13 14:06:46 +00001876
1877 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1878
Michael Chan71034ba2009-10-10 13:46:59 +00001879skip_cfc_delete:
1880 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1881
1882 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00001883 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001884
Michael Chanfdf24082010-10-13 14:06:47 +00001885destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001886 memset(&kcqe, 0, sizeof(kcqe));
1887 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1888 kcqe.iscsi_conn_id = l5_cid;
1889 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1890 kcqe.iscsi_conn_context_id = req->context_id;
1891
1892 cqes[0] = (struct kcqe *) &kcqe;
1893 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1894
1895 return ret;
1896}
1897
1898static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1899 struct l4_kwq_connect_req1 *kwqe1,
1900 struct l4_kwq_connect_req3 *kwqe3,
1901 struct l5cm_active_conn_buffer *conn_buf)
1902{
1903 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1904 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1905 &conn_buf->xstorm_conn_buffer;
1906 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1907 &conn_buf->tstorm_conn_buffer;
1908 struct regpair context_addr;
1909 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1910 struct in6_addr src_ip, dst_ip;
1911 int i;
1912 u32 *addrp;
1913
1914 addrp = (u32 *) &conn_addr->local_ip_addr;
1915 for (i = 0; i < 4; i++, addrp++)
1916 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1917
1918 addrp = (u32 *) &conn_addr->remote_ip_addr;
1919 for (i = 0; i < 4; i++, addrp++)
1920 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1921
1922 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1923
1924 xstorm_buf->context_addr.hi = context_addr.hi;
1925 xstorm_buf->context_addr.lo = context_addr.lo;
1926 xstorm_buf->mss = 0xffff;
1927 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1928 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1929 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1930 xstorm_buf->pseudo_header_checksum =
1931 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1932
1933 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1934 tstorm_buf->params |=
1935 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1936 if (kwqe3->ka_timeout) {
1937 tstorm_buf->ka_enable = 1;
1938 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1939 tstorm_buf->ka_interval = kwqe3->ka_interval;
1940 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1941 }
1942 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1943 tstorm_buf->snd_buf = kwqe3->snd_buf;
1944 tstorm_buf->max_rt_time = 0xffffffff;
1945}
1946
1947static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1948{
1949 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001950 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001951 u8 *mac = dev->mac_addr;
1952
1953 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001954 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001955 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001956 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001957 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001958 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001959 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001960 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001961 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001962 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001963 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001964 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001965
1966 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001967 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001968 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001969 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001970 mac[4]);
1971 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001972 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001973 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001974 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001975 mac[2]);
1976 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001977 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00001978 mac[1]);
1979 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001980 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00001981 mac[0]);
1982}
1983
1984static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1985{
1986 struct cnic_local *cp = dev->cnic_priv;
1987 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1988 u16 tstorm_flags = 0;
1989
1990 if (tcp_ts) {
1991 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1992 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1993 }
1994
1995 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001996 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001997
1998 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001999 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002000}
2001
2002static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2003 u32 num, int *work)
2004{
2005 struct cnic_local *cp = dev->cnic_priv;
2006 struct l4_kwq_connect_req1 *kwqe1 =
2007 (struct l4_kwq_connect_req1 *) wqes[0];
2008 struct l4_kwq_connect_req3 *kwqe3;
2009 struct l5cm_active_conn_buffer *conn_buf;
2010 struct l5cm_conn_addr_params *conn_addr;
2011 union l5cm_specific_data l5_data;
2012 u32 l5_cid = kwqe1->pg_cid;
2013 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2014 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2015 int ret;
2016
2017 if (num < 2) {
2018 *work = num;
2019 return -EINVAL;
2020 }
2021
2022 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2023 *work = 3;
2024 else
2025 *work = 2;
2026
2027 if (num < *work) {
2028 *work = num;
2029 return -EINVAL;
2030 }
2031
2032 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002033 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002034 return -ENOMEM;
2035 }
2036 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2037 if (!conn_buf)
2038 return -ENOMEM;
2039
2040 memset(conn_buf, 0, sizeof(*conn_buf));
2041
2042 conn_addr = &conn_buf->conn_addr_buf;
2043 conn_addr->remote_addr_0 = csk->ha[0];
2044 conn_addr->remote_addr_1 = csk->ha[1];
2045 conn_addr->remote_addr_2 = csk->ha[2];
2046 conn_addr->remote_addr_3 = csk->ha[3];
2047 conn_addr->remote_addr_4 = csk->ha[4];
2048 conn_addr->remote_addr_5 = csk->ha[5];
2049
2050 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2051 struct l4_kwq_connect_req2 *kwqe2 =
2052 (struct l4_kwq_connect_req2 *) wqes[1];
2053
2054 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2055 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2056 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2057
2058 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2059 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2060 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2061 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2062 }
2063 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2064
2065 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2066 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2067 conn_addr->local_tcp_port = kwqe1->src_port;
2068 conn_addr->remote_tcp_port = kwqe1->dst_port;
2069
2070 conn_addr->pmtu = kwqe3->pmtu;
2071 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2072
2073 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002074 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002075
2076 cnic_bnx2x_set_tcp_timestamp(dev,
2077 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2078
2079 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2080 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2081 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002082 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002083
2084 return ret;
2085}
2086
2087static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2088{
2089 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2090 union l5cm_specific_data l5_data;
2091 int ret;
2092
2093 memset(&l5_data, 0, sizeof(l5_data));
2094 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2095 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2096 return ret;
2097}
2098
2099static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2100{
2101 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2102 union l5cm_specific_data l5_data;
2103 int ret;
2104
2105 memset(&l5_data, 0, sizeof(l5_data));
2106 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2107 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2108 return ret;
2109}
2110static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2111{
2112 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2113 struct l4_kcq kcqe;
2114 struct kcqe *cqes[1];
2115
2116 memset(&kcqe, 0, sizeof(kcqe));
2117 kcqe.pg_host_opaque = req->host_opaque;
2118 kcqe.pg_cid = req->host_opaque;
2119 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2120 cqes[0] = (struct kcqe *) &kcqe;
2121 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2122 return 0;
2123}
2124
2125static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2126{
2127 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2128 struct l4_kcq kcqe;
2129 struct kcqe *cqes[1];
2130
2131 memset(&kcqe, 0, sizeof(kcqe));
2132 kcqe.pg_host_opaque = req->pg_host_opaque;
2133 kcqe.pg_cid = req->pg_cid;
2134 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2135 cqes[0] = (struct kcqe *) &kcqe;
2136 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2137 return 0;
2138}
2139
Michael Chane1928c82010-12-23 07:43:04 +00002140static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2141{
2142 struct fcoe_kwqe_stat *req;
2143 struct fcoe_stat_ramrod_params *fcoe_stat;
2144 union l5cm_specific_data l5_data;
2145 struct cnic_local *cp = dev->cnic_priv;
2146 int ret;
2147 u32 cid;
2148
2149 req = (struct fcoe_kwqe_stat *) kwqe;
2150 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2151
2152 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2153 if (!fcoe_stat)
2154 return -ENOMEM;
2155
2156 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2157 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2158
2159 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT, cid,
2160 FCOE_CONNECTION_TYPE, &l5_data);
2161 return ret;
2162}
2163
2164static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2165 u32 num, int *work)
2166{
2167 int ret;
2168 struct cnic_local *cp = dev->cnic_priv;
2169 u32 cid;
2170 struct fcoe_init_ramrod_params *fcoe_init;
2171 struct fcoe_kwqe_init1 *req1;
2172 struct fcoe_kwqe_init2 *req2;
2173 struct fcoe_kwqe_init3 *req3;
2174 union l5cm_specific_data l5_data;
2175
2176 if (num < 3) {
2177 *work = num;
2178 return -EINVAL;
2179 }
2180 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2181 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2182 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2183 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2184 *work = 1;
2185 return -EINVAL;
2186 }
2187 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2188 *work = 2;
2189 return -EINVAL;
2190 }
2191
2192 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2193 netdev_err(dev->netdev, "fcoe_init size too big\n");
2194 return -ENOMEM;
2195 }
2196 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2197 if (!fcoe_init)
2198 return -ENOMEM;
2199
2200 memset(fcoe_init, 0, sizeof(*fcoe_init));
2201 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2202 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2203 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
2204 fcoe_init->eq_addr.lo = cp->kcq2.dma.pg_map_arr[0] & 0xffffffff;
2205 fcoe_init->eq_addr.hi = (u64) cp->kcq2.dma.pg_map_arr[0] >> 32;
2206 fcoe_init->eq_next_page_addr.lo =
2207 cp->kcq2.dma.pg_map_arr[1] & 0xffffffff;
2208 fcoe_init->eq_next_page_addr.hi =
2209 (u64) cp->kcq2.dma.pg_map_arr[1] >> 32;
2210
2211 fcoe_init->sb_num = cp->status_blk_num;
2212 fcoe_init->eq_prod = MAX_KCQ_IDX;
2213 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2214 cp->kcq2.sw_prod_idx = 0;
2215
2216 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Michael Chane1928c82010-12-23 07:43:04 +00002217 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT, cid,
2218 FCOE_CONNECTION_TYPE, &l5_data);
2219 *work = 3;
2220 return ret;
2221}
2222
2223static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2224 u32 num, int *work)
2225{
2226 int ret = 0;
2227 u32 cid = -1, l5_cid;
2228 struct cnic_local *cp = dev->cnic_priv;
2229 struct fcoe_kwqe_conn_offload1 *req1;
2230 struct fcoe_kwqe_conn_offload2 *req2;
2231 struct fcoe_kwqe_conn_offload3 *req3;
2232 struct fcoe_kwqe_conn_offload4 *req4;
2233 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2234 struct cnic_context *ctx;
2235 struct fcoe_context *fctx;
2236 struct regpair ctx_addr;
2237 union l5cm_specific_data l5_data;
2238 struct fcoe_kcqe kcqe;
2239 struct kcqe *cqes[1];
2240
2241 if (num < 4) {
2242 *work = num;
2243 return -EINVAL;
2244 }
2245 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2246 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2247 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2248 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2249
2250 *work = 4;
2251
2252 l5_cid = req1->fcoe_conn_id;
2253 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2254 goto err_reply;
2255
2256 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2257
2258 ctx = &cp->ctx_tbl[l5_cid];
2259 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2260 goto err_reply;
2261
2262 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2263 if (ret) {
2264 ret = 0;
2265 goto err_reply;
2266 }
2267 cid = ctx->cid;
2268
2269 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2270 if (fctx) {
2271 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2272 u32 val;
2273
2274 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2275 FCOE_CONNECTION_TYPE);
2276 fctx->xstorm_ag_context.cdu_reserved = val;
2277 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2278 FCOE_CONNECTION_TYPE);
2279 fctx->ustorm_ag_context.cdu_usage = val;
2280 }
2281 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2282 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2283 goto err_reply;
2284 }
2285 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2286 if (!fcoe_offload)
2287 goto err_reply;
2288
2289 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2290 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2291 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2292 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2293 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2294
2295 cid = BNX2X_HW_CID(cp, cid);
2296 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2297 FCOE_CONNECTION_TYPE, &l5_data);
2298 if (!ret)
2299 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2300
2301 return ret;
2302
2303err_reply:
2304 if (cid != -1)
2305 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2306
2307 memset(&kcqe, 0, sizeof(kcqe));
2308 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2309 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2310 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2311
2312 cqes[0] = (struct kcqe *) &kcqe;
2313 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2314 return ret;
2315}
2316
2317static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2318{
2319 struct fcoe_kwqe_conn_enable_disable *req;
2320 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2321 union l5cm_specific_data l5_data;
2322 int ret;
2323 u32 cid, l5_cid;
2324 struct cnic_local *cp = dev->cnic_priv;
2325
2326 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2327 cid = req->context_id;
2328 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2329
2330 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2331 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2332 return -ENOMEM;
2333 }
2334 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2335 if (!fcoe_enable)
2336 return -ENOMEM;
2337
2338 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2339 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2340 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2341 FCOE_CONNECTION_TYPE, &l5_data);
2342 return ret;
2343}
2344
2345static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2346{
2347 struct fcoe_kwqe_conn_enable_disable *req;
2348 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2349 union l5cm_specific_data l5_data;
2350 int ret;
2351 u32 cid, l5_cid;
2352 struct cnic_local *cp = dev->cnic_priv;
2353
2354 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2355 cid = req->context_id;
2356 l5_cid = req->conn_id;
2357 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2358 return -EINVAL;
2359
2360 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2361
2362 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2363 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2364 return -ENOMEM;
2365 }
2366 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2367 if (!fcoe_disable)
2368 return -ENOMEM;
2369
2370 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2371 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2372 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2373 FCOE_CONNECTION_TYPE, &l5_data);
2374 return ret;
2375}
2376
2377static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2378{
2379 struct fcoe_kwqe_conn_destroy *req;
2380 union l5cm_specific_data l5_data;
2381 int ret;
2382 u32 cid, l5_cid;
2383 struct cnic_local *cp = dev->cnic_priv;
2384 struct cnic_context *ctx;
2385 struct fcoe_kcqe kcqe;
2386 struct kcqe *cqes[1];
2387
2388 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2389 cid = req->context_id;
2390 l5_cid = req->conn_id;
2391 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2392 return -EINVAL;
2393
2394 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2395
2396 ctx = &cp->ctx_tbl[l5_cid];
2397
2398 init_waitqueue_head(&ctx->waitq);
2399 ctx->wait_cond = 0;
2400
2401 memset(&l5_data, 0, sizeof(l5_data));
2402 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2403 FCOE_CONNECTION_TYPE, &l5_data);
2404 if (ret == 0) {
2405 wait_event(ctx->waitq, ctx->wait_cond);
2406 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2407 queue_delayed_work(cnic_wq, &cp->delete_task,
2408 msecs_to_jiffies(2000));
2409 }
2410
2411 memset(&kcqe, 0, sizeof(kcqe));
2412 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2413 kcqe.fcoe_conn_id = req->conn_id;
2414 kcqe.fcoe_conn_context_id = cid;
2415
2416 cqes[0] = (struct kcqe *) &kcqe;
2417 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2418 return ret;
2419}
2420
2421static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2422{
2423 struct fcoe_kwqe_destroy *req;
2424 union l5cm_specific_data l5_data;
2425 struct cnic_local *cp = dev->cnic_priv;
2426 int ret;
2427 u32 cid;
2428
2429 req = (struct fcoe_kwqe_destroy *) kwqe;
2430 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2431
2432 memset(&l5_data, 0, sizeof(l5_data));
2433 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY, cid,
2434 FCOE_CONNECTION_TYPE, &l5_data);
2435 return ret;
2436}
2437
2438static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2439 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002440{
2441 int i, work, ret;
2442 u32 opcode;
2443 struct kwqe *kwqe;
2444
2445 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2446 return -EAGAIN; /* bnx2 is down */
2447
2448 for (i = 0; i < num_wqes; ) {
2449 kwqe = wqes[i];
2450 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2451 work = 1;
2452
2453 switch (opcode) {
2454 case ISCSI_KWQE_OPCODE_INIT1:
2455 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2456 break;
2457 case ISCSI_KWQE_OPCODE_INIT2:
2458 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2459 break;
2460 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2461 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2462 num_wqes - i, &work);
2463 break;
2464 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2465 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2466 break;
2467 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2468 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2469 break;
2470 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2471 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2472 &work);
2473 break;
2474 case L4_KWQE_OPCODE_VALUE_CLOSE:
2475 ret = cnic_bnx2x_close(dev, kwqe);
2476 break;
2477 case L4_KWQE_OPCODE_VALUE_RESET:
2478 ret = cnic_bnx2x_reset(dev, kwqe);
2479 break;
2480 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2481 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2482 break;
2483 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2484 ret = cnic_bnx2x_update_pg(dev, kwqe);
2485 break;
2486 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2487 ret = 0;
2488 break;
2489 default:
2490 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002491 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2492 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002493 break;
2494 }
2495 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002496 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2497 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002498 i += work;
2499 }
2500 return 0;
2501}
2502
Michael Chane1928c82010-12-23 07:43:04 +00002503static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2504 struct kwqe *wqes[], u32 num_wqes)
2505{
2506 struct cnic_local *cp = dev->cnic_priv;
2507 int i, work, ret;
2508 u32 opcode;
2509 struct kwqe *kwqe;
2510
2511 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2512 return -EAGAIN; /* bnx2 is down */
2513
2514 if (BNX2X_CHIP_NUM(cp->chip_id) == BNX2X_CHIP_NUM_57710)
2515 return -EINVAL;
2516
2517 for (i = 0; i < num_wqes; ) {
2518 kwqe = wqes[i];
2519 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2520 work = 1;
2521
2522 switch (opcode) {
2523 case FCOE_KWQE_OPCODE_INIT1:
2524 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2525 num_wqes - i, &work);
2526 break;
2527 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2528 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2529 num_wqes - i, &work);
2530 break;
2531 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2532 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2533 break;
2534 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2535 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2536 break;
2537 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2538 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2539 break;
2540 case FCOE_KWQE_OPCODE_DESTROY:
2541 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2542 break;
2543 case FCOE_KWQE_OPCODE_STAT:
2544 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2545 break;
2546 default:
2547 ret = 0;
2548 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2549 opcode);
2550 break;
2551 }
2552 if (ret < 0)
2553 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2554 opcode);
2555 i += work;
2556 }
2557 return 0;
2558}
2559
2560static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2561 u32 num_wqes)
2562{
2563 int ret = -EINVAL;
2564 u32 layer_code;
2565
2566 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2567 return -EAGAIN; /* bnx2x is down */
2568
2569 if (!num_wqes)
2570 return 0;
2571
2572 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2573 switch (layer_code) {
2574 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2575 case KWQE_FLAGS_LAYER_MASK_L4:
2576 case KWQE_FLAGS_LAYER_MASK_L2:
2577 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2578 break;
2579
2580 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2581 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2582 break;
2583 }
2584 return ret;
2585}
2586
2587static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2588{
2589 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2590 return KCQE_FLAGS_LAYER_MASK_L4;
2591
2592 return opflag & KCQE_FLAGS_LAYER_MASK;
2593}
2594
Michael Chana4636962009-06-08 18:14:43 -07002595static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2596{
2597 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002598 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002599
2600 i = 0;
2601 j = 1;
2602 while (num_cqes) {
2603 struct cnic_ulp_ops *ulp_ops;
2604 int ulp_type;
2605 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002606 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002607
2608 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002609 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002610
2611 while (j < num_cqes) {
2612 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2613
Michael Chane1928c82010-12-23 07:43:04 +00002614 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002615 break;
2616
2617 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002618 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002619 j++;
2620 }
2621
2622 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2623 ulp_type = CNIC_ULP_RDMA;
2624 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2625 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002626 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2627 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002628 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2629 ulp_type = CNIC_ULP_L4;
2630 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2631 goto end;
2632 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002633 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2634 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002635 goto end;
2636 }
2637
2638 rcu_read_lock();
2639 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2640 if (likely(ulp_ops)) {
2641 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2642 cp->completed_kcq + i, j);
2643 }
2644 rcu_read_unlock();
2645end:
2646 num_cqes -= j;
2647 i += j;
2648 j = 1;
2649 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002650 if (unlikely(comp))
2651 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002652}
2653
2654static u16 cnic_bnx2_next_idx(u16 idx)
2655{
2656 return idx + 1;
2657}
2658
2659static u16 cnic_bnx2_hw_idx(u16 idx)
2660{
2661 return idx;
2662}
2663
Michael Chan71034ba2009-10-10 13:46:59 +00002664static u16 cnic_bnx2x_next_idx(u16 idx)
2665{
2666 idx++;
2667 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2668 idx++;
2669
2670 return idx;
2671}
2672
2673static u16 cnic_bnx2x_hw_idx(u16 idx)
2674{
2675 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2676 idx++;
2677 return idx;
2678}
2679
Michael Chan644b9d42010-06-24 14:58:40 +00002680static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002681{
2682 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002683 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002684 struct kcqe *kcqe;
2685 int kcqe_cnt = 0, last_cnt = 0;
2686
Michael Chan644b9d42010-06-24 14:58:40 +00002687 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002688 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002689 hw_prod = *info->hw_prod_idx_ptr;
2690 hw_prod = cp->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002691
2692 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002693 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002694 cp->completed_kcq[kcqe_cnt++] = kcqe;
2695 i = cp->next_idx(i);
2696 ri = i & MAX_KCQ_IDX;
2697 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2698 last_cnt = kcqe_cnt;
2699 last = i;
2700 }
2701 }
2702
Michael Chan644b9d42010-06-24 14:58:40 +00002703 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002704 return last_cnt;
2705}
2706
Michael Chan48f753d2010-05-18 11:32:53 +00002707static int cnic_l2_completion(struct cnic_local *cp)
2708{
2709 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002710 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002711 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002712 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002713 u32 cmd;
2714 int comp = 0;
2715
2716 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2717 return 0;
2718
2719 hw_cons = *cp->rx_cons_ptr;
2720 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2721 hw_cons++;
2722
2723 sw_cons = cp->rx_cons;
2724 while (sw_cons != hw_cons) {
2725 u8 cqe_fp_flags;
2726
2727 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2728 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2729 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2730 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2731 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2732 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2733 cmd == RAMROD_CMD_ID_ETH_HALT)
2734 comp++;
2735 }
2736 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2737 }
2738 return comp;
2739}
2740
Michael Chan86b53602009-10-10 13:46:57 +00002741static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002742{
Michael Chan541a7812010-10-06 03:17:22 +00002743 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002744 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002745
Michael Chan541a7812010-10-06 03:17:22 +00002746 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002747 return;
2748
Michael Chan541a7812010-10-06 03:17:22 +00002749 rx_cons = *cp->rx_cons_ptr;
2750 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002751 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002752 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2753 comp = cnic_l2_completion(cp);
2754
Michael Chana4636962009-06-08 18:14:43 -07002755 cp->tx_cons = tx_cons;
2756 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002757
Michael Chancd801532010-10-13 14:06:49 +00002758 if (cp->udev)
2759 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002760 }
Michael Chan48f753d2010-05-18 11:32:53 +00002761 if (comp)
2762 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002763}
2764
Michael Chanb177a5d52010-06-24 14:58:41 +00002765static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002766{
Michael Chana4636962009-06-08 18:14:43 -07002767 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002768 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002769 int kcqe_cnt;
2770
Michael Chan107c3f42011-03-02 13:00:49 +00002771 /* status block index must be read before reading other fields */
2772 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002773 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2774
Michael Chan644b9d42010-06-24 14:58:40 +00002775 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002776
2777 service_kcqes(dev, kcqe_cnt);
2778
2779 /* Tell compiler that status_blk fields can change. */
2780 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002781 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2782 /* status block index must be read first */
2783 rmb();
2784 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002785 }
2786
Michael Chan644b9d42010-06-24 14:58:40 +00002787 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002788
Michael Chan86b53602009-10-10 13:46:57 +00002789 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002790
Michael Chana4636962009-06-08 18:14:43 -07002791 return status_idx;
2792}
2793
Michael Chanb177a5d52010-06-24 14:58:41 +00002794static int cnic_service_bnx2(void *data, void *status_blk)
2795{
2796 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002797
Michael Chaneaaa6e92010-12-23 08:38:30 +00002798 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2799 struct status_block *sblk = status_blk;
2800
2801 return sblk->status_idx;
2802 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002803
2804 return cnic_service_bnx2_queues(dev);
2805}
2806
Michael Chana4636962009-06-08 18:14:43 -07002807static void cnic_service_bnx2_msix(unsigned long data)
2808{
2809 struct cnic_dev *dev = (struct cnic_dev *) data;
2810 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002811
Michael Chanb177a5d52010-06-24 14:58:41 +00002812 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002813
Michael Chana4636962009-06-08 18:14:43 -07002814 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2815 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2816}
2817
Michael Chan66fee9e2010-06-24 14:58:38 +00002818static void cnic_doirq(struct cnic_dev *dev)
2819{
2820 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002821
2822 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002823 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2824
Michael Chan66fee9e2010-06-24 14:58:38 +00002825 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002826 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002827
2828 tasklet_schedule(&cp->cnic_irq_task);
2829 }
2830}
2831
Michael Chana4636962009-06-08 18:14:43 -07002832static irqreturn_t cnic_irq(int irq, void *dev_instance)
2833{
2834 struct cnic_dev *dev = dev_instance;
2835 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002836
2837 if (cp->ack_int)
2838 cp->ack_int(dev);
2839
Michael Chan66fee9e2010-06-24 14:58:38 +00002840 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002841
2842 return IRQ_HANDLED;
2843}
2844
Michael Chan71034ba2009-10-10 13:46:59 +00002845static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2846 u16 index, u8 op, u8 update)
2847{
2848 struct cnic_local *cp = dev->cnic_priv;
2849 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2850 COMMAND_REG_INT_ACK);
2851 struct igu_ack_register igu_ack;
2852
2853 igu_ack.status_block_index = index;
2854 igu_ack.sb_id_and_flags =
2855 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2856 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2857 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2858 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2859
2860 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2861}
2862
Michael Chanee87a822010-10-13 14:06:51 +00002863static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2864 u16 index, u8 op, u8 update)
2865{
2866 struct igu_regular cmd_data;
2867 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2868
2869 cmd_data.sb_id_and_flags =
2870 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2871 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2872 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2873 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2874
2875
2876 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2877}
2878
Michael Chan71034ba2009-10-10 13:46:59 +00002879static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2880{
2881 struct cnic_local *cp = dev->cnic_priv;
2882
Dmitry Kravkov523224a2010-10-06 03:23:26 +00002883 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00002884 IGU_INT_DISABLE, 0);
2885}
2886
Michael Chanee87a822010-10-13 14:06:51 +00002887static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2888{
2889 struct cnic_local *cp = dev->cnic_priv;
2890
2891 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2892 IGU_INT_DISABLE, 0);
2893}
2894
Michael Chanb177a5d52010-06-24 14:58:41 +00002895static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002896{
Michael Chanb177a5d52010-06-24 14:58:41 +00002897 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002898 int kcqe_cnt;
2899
Michael Chan107c3f42011-03-02 13:00:49 +00002900 /* status block index must be read before reading the KCQ */
2901 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00002902 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002903
2904 service_kcqes(dev, kcqe_cnt);
2905
2906 /* Tell compiler that sblk fields can change. */
2907 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00002908
Michael Chanb177a5d52010-06-24 14:58:41 +00002909 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00002910 /* status block index must be read before reading the KCQ */
2911 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00002912 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002913 return last_status;
2914}
2915
2916static void cnic_service_bnx2x_bh(unsigned long data)
2917{
2918 struct cnic_dev *dev = (struct cnic_dev *) data;
2919 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00002920 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00002921
2922 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2923 return;
2924
Michael Chan0197b082011-03-02 13:00:50 +00002925 while (1) {
2926 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002927
Michael Chan0197b082011-03-02 13:00:50 +00002928 CNIC_WR16(dev, cp->kcq1.io_addr,
2929 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00002930
Michael Chan0197b082011-03-02 13:00:50 +00002931 if (!BNX2X_CHIP_IS_E2(cp->chip_id)) {
2932 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2933 status_idx, IGU_INT_ENABLE, 1);
2934 break;
2935 }
2936
2937 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
2938
2939 if (new_status_idx != status_idx)
2940 continue;
Michael Chane21ba412010-12-23 07:43:03 +00002941
2942 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
2943 MAX_KCQ_IDX);
2944
Michael Chanee87a822010-10-13 14:06:51 +00002945 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2946 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00002947
2948 break;
Michael Chane21ba412010-12-23 07:43:03 +00002949 }
Michael Chan71034ba2009-10-10 13:46:59 +00002950}
2951
2952static int cnic_service_bnx2x(void *data, void *status_blk)
2953{
2954 struct cnic_dev *dev = data;
2955 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002956
Michael Chan66fee9e2010-06-24 14:58:38 +00002957 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2958 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002959
Michael Chan66fee9e2010-06-24 14:58:38 +00002960 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002961
2962 return 0;
2963}
2964
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07002965static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
2966{
2967 struct cnic_ulp_ops *ulp_ops;
2968
2969 if (if_type == CNIC_ULP_ISCSI)
2970 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
2971
2972 mutex_lock(&cnic_lock);
2973 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
2974 lockdep_is_held(&cnic_lock));
2975 if (!ulp_ops) {
2976 mutex_unlock(&cnic_lock);
2977 return;
2978 }
2979 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2980 mutex_unlock(&cnic_lock);
2981
2982 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2983 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
2984
2985 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2986}
2987
Michael Chana4636962009-06-08 18:14:43 -07002988static void cnic_ulp_stop(struct cnic_dev *dev)
2989{
2990 struct cnic_local *cp = dev->cnic_priv;
2991 int if_type;
2992
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07002993 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
2994 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07002995}
2996
2997static void cnic_ulp_start(struct cnic_dev *dev)
2998{
2999 struct cnic_local *cp = dev->cnic_priv;
3000 int if_type;
3001
Michael Chana4636962009-06-08 18:14:43 -07003002 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3003 struct cnic_ulp_ops *ulp_ops;
3004
Michael Chan681dbd72009-08-14 15:49:46 +00003005 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003006 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3007 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003008 if (!ulp_ops || !ulp_ops->cnic_start) {
3009 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003010 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003011 }
3012 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3013 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003014
3015 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3016 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003017
3018 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003019 }
Michael Chana4636962009-06-08 18:14:43 -07003020}
3021
3022static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3023{
3024 struct cnic_dev *dev = data;
3025
3026 switch (info->cmd) {
3027 case CNIC_CTL_STOP_CMD:
3028 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003029
3030 cnic_ulp_stop(dev);
3031 cnic_stop_hw(dev);
3032
Michael Chana4636962009-06-08 18:14:43 -07003033 cnic_put(dev);
3034 break;
3035 case CNIC_CTL_START_CMD:
3036 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003037
3038 if (!cnic_start_hw(dev))
3039 cnic_ulp_start(dev);
3040
Michael Chana4636962009-06-08 18:14:43 -07003041 cnic_put(dev);
3042 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003043 case CNIC_CTL_STOP_ISCSI_CMD: {
3044 struct cnic_local *cp = dev->cnic_priv;
3045 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3046 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3047 break;
3048 }
Michael Chan71034ba2009-10-10 13:46:59 +00003049 case CNIC_CTL_COMPLETION_CMD: {
3050 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
3051 u32 l5_cid;
3052 struct cnic_local *cp = dev->cnic_priv;
3053
3054 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3055 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3056
3057 ctx->wait_cond = 1;
3058 wake_up(&ctx->waitq);
3059 }
3060 break;
3061 }
Michael Chana4636962009-06-08 18:14:43 -07003062 default:
3063 return -EINVAL;
3064 }
3065 return 0;
3066}
3067
3068static void cnic_ulp_init(struct cnic_dev *dev)
3069{
3070 int i;
3071 struct cnic_local *cp = dev->cnic_priv;
3072
Michael Chana4636962009-06-08 18:14:43 -07003073 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3074 struct cnic_ulp_ops *ulp_ops;
3075
Michael Chan7fc1ece2009-08-14 15:49:47 +00003076 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003077 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003078 if (!ulp_ops || !ulp_ops->cnic_init) {
3079 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003080 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003081 }
3082 ulp_get(ulp_ops);
3083 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003084
3085 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3086 ulp_ops->cnic_init(dev);
3087
Michael Chan7fc1ece2009-08-14 15:49:47 +00003088 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003089 }
Michael Chana4636962009-06-08 18:14:43 -07003090}
3091
3092static void cnic_ulp_exit(struct cnic_dev *dev)
3093{
3094 int i;
3095 struct cnic_local *cp = dev->cnic_priv;
3096
Michael Chana4636962009-06-08 18:14:43 -07003097 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3098 struct cnic_ulp_ops *ulp_ops;
3099
Michael Chan7fc1ece2009-08-14 15:49:47 +00003100 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003101 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003102 if (!ulp_ops || !ulp_ops->cnic_exit) {
3103 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003104 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003105 }
3106 ulp_get(ulp_ops);
3107 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003108
3109 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3110 ulp_ops->cnic_exit(dev);
3111
Michael Chan7fc1ece2009-08-14 15:49:47 +00003112 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003113 }
Michael Chana4636962009-06-08 18:14:43 -07003114}
3115
3116static int cnic_cm_offload_pg(struct cnic_sock *csk)
3117{
3118 struct cnic_dev *dev = csk->dev;
3119 struct l4_kwq_offload_pg *l4kwqe;
3120 struct kwqe *wqes[1];
3121
3122 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3123 memset(l4kwqe, 0, sizeof(*l4kwqe));
3124 wqes[0] = (struct kwqe *) l4kwqe;
3125
3126 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3127 l4kwqe->flags =
3128 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3129 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3130
3131 l4kwqe->da0 = csk->ha[0];
3132 l4kwqe->da1 = csk->ha[1];
3133 l4kwqe->da2 = csk->ha[2];
3134 l4kwqe->da3 = csk->ha[3];
3135 l4kwqe->da4 = csk->ha[4];
3136 l4kwqe->da5 = csk->ha[5];
3137
3138 l4kwqe->sa0 = dev->mac_addr[0];
3139 l4kwqe->sa1 = dev->mac_addr[1];
3140 l4kwqe->sa2 = dev->mac_addr[2];
3141 l4kwqe->sa3 = dev->mac_addr[3];
3142 l4kwqe->sa4 = dev->mac_addr[4];
3143 l4kwqe->sa5 = dev->mac_addr[5];
3144
3145 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003146 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003147 l4kwqe->host_opaque = csk->l5_cid;
3148
3149 if (csk->vlan_id) {
3150 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3151 l4kwqe->vlan_tag = csk->vlan_id;
3152 l4kwqe->l2hdr_nbytes += 4;
3153 }
3154
3155 return dev->submit_kwqes(dev, wqes, 1);
3156}
3157
3158static int cnic_cm_update_pg(struct cnic_sock *csk)
3159{
3160 struct cnic_dev *dev = csk->dev;
3161 struct l4_kwq_update_pg *l4kwqe;
3162 struct kwqe *wqes[1];
3163
3164 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3165 memset(l4kwqe, 0, sizeof(*l4kwqe));
3166 wqes[0] = (struct kwqe *) l4kwqe;
3167
3168 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3169 l4kwqe->flags =
3170 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3171 l4kwqe->pg_cid = csk->pg_cid;
3172
3173 l4kwqe->da0 = csk->ha[0];
3174 l4kwqe->da1 = csk->ha[1];
3175 l4kwqe->da2 = csk->ha[2];
3176 l4kwqe->da3 = csk->ha[3];
3177 l4kwqe->da4 = csk->ha[4];
3178 l4kwqe->da5 = csk->ha[5];
3179
3180 l4kwqe->pg_host_opaque = csk->l5_cid;
3181 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3182
3183 return dev->submit_kwqes(dev, wqes, 1);
3184}
3185
3186static int cnic_cm_upload_pg(struct cnic_sock *csk)
3187{
3188 struct cnic_dev *dev = csk->dev;
3189 struct l4_kwq_upload *l4kwqe;
3190 struct kwqe *wqes[1];
3191
3192 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3193 memset(l4kwqe, 0, sizeof(*l4kwqe));
3194 wqes[0] = (struct kwqe *) l4kwqe;
3195
3196 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3197 l4kwqe->flags =
3198 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3199 l4kwqe->cid = csk->pg_cid;
3200
3201 return dev->submit_kwqes(dev, wqes, 1);
3202}
3203
3204static int cnic_cm_conn_req(struct cnic_sock *csk)
3205{
3206 struct cnic_dev *dev = csk->dev;
3207 struct l4_kwq_connect_req1 *l4kwqe1;
3208 struct l4_kwq_connect_req2 *l4kwqe2;
3209 struct l4_kwq_connect_req3 *l4kwqe3;
3210 struct kwqe *wqes[3];
3211 u8 tcp_flags = 0;
3212 int num_wqes = 2;
3213
3214 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3215 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3216 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3217 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3218 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3219 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3220
3221 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3222 l4kwqe3->flags =
3223 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3224 l4kwqe3->ka_timeout = csk->ka_timeout;
3225 l4kwqe3->ka_interval = csk->ka_interval;
3226 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3227 l4kwqe3->tos = csk->tos;
3228 l4kwqe3->ttl = csk->ttl;
3229 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3230 l4kwqe3->pmtu = csk->mtu;
3231 l4kwqe3->rcv_buf = csk->rcv_buf;
3232 l4kwqe3->snd_buf = csk->snd_buf;
3233 l4kwqe3->seed = csk->seed;
3234
3235 wqes[0] = (struct kwqe *) l4kwqe1;
3236 if (test_bit(SK_F_IPV6, &csk->flags)) {
3237 wqes[1] = (struct kwqe *) l4kwqe2;
3238 wqes[2] = (struct kwqe *) l4kwqe3;
3239 num_wqes = 3;
3240
3241 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3242 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3243 l4kwqe2->flags =
3244 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3245 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3246 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3247 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3248 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3249 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3250 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3251 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3252 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3253 sizeof(struct tcphdr);
3254 } else {
3255 wqes[1] = (struct kwqe *) l4kwqe3;
3256 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3257 sizeof(struct tcphdr);
3258 }
3259
3260 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3261 l4kwqe1->flags =
3262 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3263 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3264 l4kwqe1->cid = csk->cid;
3265 l4kwqe1->pg_cid = csk->pg_cid;
3266 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3267 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3268 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3269 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3270 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3271 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3272 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3273 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3274 if (csk->tcp_flags & SK_TCP_NAGLE)
3275 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3276 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3277 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3278 if (csk->tcp_flags & SK_TCP_SACK)
3279 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3280 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3281 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3282
3283 l4kwqe1->tcp_flags = tcp_flags;
3284
3285 return dev->submit_kwqes(dev, wqes, num_wqes);
3286}
3287
3288static int cnic_cm_close_req(struct cnic_sock *csk)
3289{
3290 struct cnic_dev *dev = csk->dev;
3291 struct l4_kwq_close_req *l4kwqe;
3292 struct kwqe *wqes[1];
3293
3294 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3295 memset(l4kwqe, 0, sizeof(*l4kwqe));
3296 wqes[0] = (struct kwqe *) l4kwqe;
3297
3298 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3299 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3300 l4kwqe->cid = csk->cid;
3301
3302 return dev->submit_kwqes(dev, wqes, 1);
3303}
3304
3305static int cnic_cm_abort_req(struct cnic_sock *csk)
3306{
3307 struct cnic_dev *dev = csk->dev;
3308 struct l4_kwq_reset_req *l4kwqe;
3309 struct kwqe *wqes[1];
3310
3311 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3312 memset(l4kwqe, 0, sizeof(*l4kwqe));
3313 wqes[0] = (struct kwqe *) l4kwqe;
3314
3315 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3316 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3317 l4kwqe->cid = csk->cid;
3318
3319 return dev->submit_kwqes(dev, wqes, 1);
3320}
3321
3322static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3323 u32 l5_cid, struct cnic_sock **csk, void *context)
3324{
3325 struct cnic_local *cp = dev->cnic_priv;
3326 struct cnic_sock *csk1;
3327
3328 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3329 return -EINVAL;
3330
Michael Chanfdf24082010-10-13 14:06:47 +00003331 if (cp->ctx_tbl) {
3332 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3333
3334 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3335 return -EAGAIN;
3336 }
3337
Michael Chana4636962009-06-08 18:14:43 -07003338 csk1 = &cp->csk_tbl[l5_cid];
3339 if (atomic_read(&csk1->ref_count))
3340 return -EAGAIN;
3341
3342 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3343 return -EBUSY;
3344
3345 csk1->dev = dev;
3346 csk1->cid = cid;
3347 csk1->l5_cid = l5_cid;
3348 csk1->ulp_type = ulp_type;
3349 csk1->context = context;
3350
3351 csk1->ka_timeout = DEF_KA_TIMEOUT;
3352 csk1->ka_interval = DEF_KA_INTERVAL;
3353 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3354 csk1->tos = DEF_TOS;
3355 csk1->ttl = DEF_TTL;
3356 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3357 csk1->rcv_buf = DEF_RCV_BUF;
3358 csk1->snd_buf = DEF_SND_BUF;
3359 csk1->seed = DEF_SEED;
3360
3361 *csk = csk1;
3362 return 0;
3363}
3364
3365static void cnic_cm_cleanup(struct cnic_sock *csk)
3366{
3367 if (csk->src_port) {
3368 struct cnic_dev *dev = csk->dev;
3369 struct cnic_local *cp = dev->cnic_priv;
3370
Michael Chan9b093362010-12-23 07:42:56 +00003371 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003372 csk->src_port = 0;
3373 }
3374}
3375
3376static void cnic_close_conn(struct cnic_sock *csk)
3377{
3378 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3379 cnic_cm_upload_pg(csk);
3380 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3381 }
3382 cnic_cm_cleanup(csk);
3383}
3384
3385static int cnic_cm_destroy(struct cnic_sock *csk)
3386{
3387 if (!cnic_in_use(csk))
3388 return -EINVAL;
3389
3390 csk_hold(csk);
3391 clear_bit(SK_F_INUSE, &csk->flags);
3392 smp_mb__after_clear_bit();
3393 while (atomic_read(&csk->ref_count) != 1)
3394 msleep(1);
3395 cnic_cm_cleanup(csk);
3396
3397 csk->flags = 0;
3398 csk_put(csk);
3399 return 0;
3400}
3401
3402static inline u16 cnic_get_vlan(struct net_device *dev,
3403 struct net_device **vlan_dev)
3404{
3405 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3406 *vlan_dev = vlan_dev_real_dev(dev);
3407 return vlan_dev_vlan_id(dev);
3408 }
3409 *vlan_dev = dev;
3410 return 0;
3411}
3412
3413static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3414 struct dst_entry **dst)
3415{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003416#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003417 struct rtable *rt;
3418
David S. Miller78fbfd82011-03-12 00:00:52 -05003419 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3420 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003421 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003422 return 0;
3423 }
3424 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003425#else
3426 return -ENETUNREACH;
3427#endif
Michael Chana4636962009-06-08 18:14:43 -07003428}
3429
3430static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3431 struct dst_entry **dst)
3432{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003433#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003434 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003435
David S. Miller4c9483b2011-03-12 16:22:43 -05003436 memset(&fl6, 0, sizeof(fl6));
3437 ipv6_addr_copy(&fl6.daddr, &dst_addr->sin6_addr);
3438 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3439 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003440
David S. Miller4c9483b2011-03-12 16:22:43 -05003441 *dst = ip6_route_output(&init_net, NULL, &fl6);
Michael Chana4636962009-06-08 18:14:43 -07003442 if (*dst)
3443 return 0;
3444#endif
3445
3446 return -ENETUNREACH;
3447}
3448
3449static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3450 int ulp_type)
3451{
3452 struct cnic_dev *dev = NULL;
3453 struct dst_entry *dst;
3454 struct net_device *netdev = NULL;
3455 int err = -ENETUNREACH;
3456
3457 if (dst_addr->sin_family == AF_INET)
3458 err = cnic_get_v4_route(dst_addr, &dst);
3459 else if (dst_addr->sin_family == AF_INET6) {
3460 struct sockaddr_in6 *dst_addr6 =
3461 (struct sockaddr_in6 *) dst_addr;
3462
3463 err = cnic_get_v6_route(dst_addr6, &dst);
3464 } else
3465 return NULL;
3466
3467 if (err)
3468 return NULL;
3469
3470 if (!dst->dev)
3471 goto done;
3472
3473 cnic_get_vlan(dst->dev, &netdev);
3474
3475 dev = cnic_from_netdev(netdev);
3476
3477done:
3478 dst_release(dst);
3479 if (dev)
3480 cnic_put(dev);
3481 return dev;
3482}
3483
3484static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3485{
3486 struct cnic_dev *dev = csk->dev;
3487 struct cnic_local *cp = dev->cnic_priv;
3488
3489 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3490}
3491
3492static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3493{
3494 struct cnic_dev *dev = csk->dev;
3495 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003496 int is_v6, rc = 0;
3497 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003498 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003499 __be16 local_port;
3500 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003501
3502 if (saddr->local.v6.sin6_family == AF_INET6 &&
3503 saddr->remote.v6.sin6_family == AF_INET6)
3504 is_v6 = 1;
3505 else if (saddr->local.v4.sin_family == AF_INET &&
3506 saddr->remote.v4.sin_family == AF_INET)
3507 is_v6 = 0;
3508 else
3509 return -EINVAL;
3510
3511 clear_bit(SK_F_IPV6, &csk->flags);
3512
3513 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003514 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003515 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003516
3517 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3518 sizeof(struct in6_addr));
3519 csk->dst_port = saddr->remote.v6.sin6_port;
3520 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003521
3522 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003523 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003524
3525 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3526 csk->dst_port = saddr->remote.v4.sin_port;
3527 local_port = saddr->local.v4.sin_port;
3528 }
3529
Michael Chanc76284a2010-02-24 14:42:07 +00003530 csk->vlan_id = 0;
3531 csk->mtu = dev->netdev->mtu;
3532 if (dst && dst->dev) {
3533 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3534 if (realdev == dev->netdev) {
3535 csk->vlan_id = vlan;
3536 csk->mtu = dst_mtu(dst);
3537 }
3538 }
Michael Chana4636962009-06-08 18:14:43 -07003539
Michael Chan9b093362010-12-23 07:42:56 +00003540 port_id = be16_to_cpu(local_port);
3541 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3542 port_id < CNIC_LOCAL_PORT_MAX) {
3543 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3544 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003545 } else
Michael Chan9b093362010-12-23 07:42:56 +00003546 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003547
Michael Chan9b093362010-12-23 07:42:56 +00003548 if (!port_id) {
3549 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3550 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003551 rc = -ENOMEM;
3552 goto err_out;
3553 }
Michael Chan9b093362010-12-23 07:42:56 +00003554 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003555 }
3556 csk->src_port = local_port;
3557
Michael Chana4636962009-06-08 18:14:43 -07003558err_out:
3559 dst_release(dst);
3560 return rc;
3561}
3562
3563static void cnic_init_csk_state(struct cnic_sock *csk)
3564{
3565 csk->state = 0;
3566 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3567 clear_bit(SK_F_CLOSING, &csk->flags);
3568}
3569
3570static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3571{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003572 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003573 int err = 0;
3574
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003575 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3576 return -EOPNOTSUPP;
3577
Michael Chana4636962009-06-08 18:14:43 -07003578 if (!cnic_in_use(csk))
3579 return -EINVAL;
3580
3581 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3582 return -EINVAL;
3583
3584 cnic_init_csk_state(csk);
3585
3586 err = cnic_get_route(csk, saddr);
3587 if (err)
3588 goto err_out;
3589
3590 err = cnic_resolve_addr(csk, saddr);
3591 if (!err)
3592 return 0;
3593
3594err_out:
3595 clear_bit(SK_F_CONNECT_START, &csk->flags);
3596 return err;
3597}
3598
3599static int cnic_cm_abort(struct cnic_sock *csk)
3600{
3601 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003602 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003603
3604 if (!cnic_in_use(csk))
3605 return -EINVAL;
3606
3607 if (cnic_abort_prep(csk))
3608 return cnic_cm_abort_req(csk);
3609
3610 /* Getting here means that we haven't started connect, or
3611 * connect was not successful.
3612 */
3613
Michael Chana4636962009-06-08 18:14:43 -07003614 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003615 if (csk->state != opcode)
3616 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003617
3618 return 0;
3619}
3620
3621static int cnic_cm_close(struct cnic_sock *csk)
3622{
3623 if (!cnic_in_use(csk))
3624 return -EINVAL;
3625
3626 if (cnic_close_prep(csk)) {
3627 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3628 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003629 } else {
3630 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003631 }
3632 return 0;
3633}
3634
3635static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3636 u8 opcode)
3637{
3638 struct cnic_ulp_ops *ulp_ops;
3639 int ulp_type = csk->ulp_type;
3640
3641 rcu_read_lock();
3642 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3643 if (ulp_ops) {
3644 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3645 ulp_ops->cm_connect_complete(csk);
3646 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3647 ulp_ops->cm_close_complete(csk);
3648 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3649 ulp_ops->cm_remote_abort(csk);
3650 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3651 ulp_ops->cm_abort_complete(csk);
3652 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3653 ulp_ops->cm_remote_close(csk);
3654 }
3655 rcu_read_unlock();
3656}
3657
3658static int cnic_cm_set_pg(struct cnic_sock *csk)
3659{
3660 if (cnic_offld_prep(csk)) {
3661 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3662 cnic_cm_update_pg(csk);
3663 else
3664 cnic_cm_offload_pg(csk);
3665 }
3666 return 0;
3667}
3668
3669static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3670{
3671 struct cnic_local *cp = dev->cnic_priv;
3672 u32 l5_cid = kcqe->pg_host_opaque;
3673 u8 opcode = kcqe->op_code;
3674 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3675
3676 csk_hold(csk);
3677 if (!cnic_in_use(csk))
3678 goto done;
3679
3680 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3681 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3682 goto done;
3683 }
Eddie Waia9736c02010-02-24 14:42:04 +00003684 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3685 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3686 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3687 cnic_cm_upcall(cp, csk,
3688 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3689 goto done;
3690 }
3691
Michael Chana4636962009-06-08 18:14:43 -07003692 csk->pg_cid = kcqe->pg_cid;
3693 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3694 cnic_cm_conn_req(csk);
3695
3696done:
3697 csk_put(csk);
3698}
3699
Michael Chane1928c82010-12-23 07:43:04 +00003700static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3701{
3702 struct cnic_local *cp = dev->cnic_priv;
3703 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3704 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3705 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3706
3707 ctx->timestamp = jiffies;
3708 ctx->wait_cond = 1;
3709 wake_up(&ctx->waitq);
3710}
3711
Michael Chana4636962009-06-08 18:14:43 -07003712static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3713{
3714 struct cnic_local *cp = dev->cnic_priv;
3715 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3716 u8 opcode = l4kcqe->op_code;
3717 u32 l5_cid;
3718 struct cnic_sock *csk;
3719
Michael Chane1928c82010-12-23 07:43:04 +00003720 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3721 cnic_process_fcoe_term_conn(dev, kcqe);
3722 return;
3723 }
Michael Chana4636962009-06-08 18:14:43 -07003724 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3725 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3726 cnic_cm_process_offld_pg(dev, l4kcqe);
3727 return;
3728 }
3729
3730 l5_cid = l4kcqe->conn_id;
3731 if (opcode & 0x80)
3732 l5_cid = l4kcqe->cid;
3733 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3734 return;
3735
3736 csk = &cp->csk_tbl[l5_cid];
3737 csk_hold(csk);
3738
3739 if (!cnic_in_use(csk)) {
3740 csk_put(csk);
3741 return;
3742 }
3743
3744 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003745 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3746 if (l4kcqe->status != 0) {
3747 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3748 cnic_cm_upcall(cp, csk,
3749 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3750 }
3751 break;
Michael Chana4636962009-06-08 18:14:43 -07003752 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3753 if (l4kcqe->status == 0)
3754 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3755
3756 smp_mb__before_clear_bit();
3757 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3758 cnic_cm_upcall(cp, csk, opcode);
3759 break;
3760
3761 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003762 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3763 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003764 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3765 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003766 cp->close_conn(csk, opcode);
3767 break;
3768
3769 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003770 /* after we already sent CLOSE_REQ */
3771 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3772 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3773 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3774 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3775 else
3776 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003777 break;
3778 }
3779 csk_put(csk);
3780}
3781
3782static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3783{
3784 struct cnic_dev *dev = data;
3785 int i;
3786
3787 for (i = 0; i < num; i++)
3788 cnic_cm_process_kcqe(dev, kcqe[i]);
3789}
3790
3791static struct cnic_ulp_ops cm_ulp_ops = {
3792 .indicate_kcqes = cnic_cm_indicate_kcqe,
3793};
3794
3795static void cnic_cm_free_mem(struct cnic_dev *dev)
3796{
3797 struct cnic_local *cp = dev->cnic_priv;
3798
3799 kfree(cp->csk_tbl);
3800 cp->csk_tbl = NULL;
3801 cnic_free_id_tbl(&cp->csk_port_tbl);
3802}
3803
3804static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3805{
3806 struct cnic_local *cp = dev->cnic_priv;
3807
3808 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3809 GFP_KERNEL);
3810 if (!cp->csk_tbl)
3811 return -ENOMEM;
3812
3813 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
3814 CNIC_LOCAL_PORT_MIN)) {
3815 cnic_cm_free_mem(dev);
3816 return -ENOMEM;
3817 }
3818 return 0;
3819}
3820
3821static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3822{
Michael Chan943189f2010-06-15 08:57:02 +00003823 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3824 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3825 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3826 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003827 }
Michael Chan943189f2010-06-15 08:57:02 +00003828
3829 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00003830 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
3831 * event
Michael Chan7b34a462010-06-15 08:57:03 +00003832 * 3. If the expected event is 0, meaning the connection was never
3833 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003834 */
Michael Chan7b34a462010-06-15 08:57:03 +00003835 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00003836 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
3837 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00003838 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3839 if (csk->state == 0)
3840 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003841 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003842 }
Michael Chana4636962009-06-08 18:14:43 -07003843 }
3844 return 0;
3845}
3846
3847static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3848{
3849 struct cnic_dev *dev = csk->dev;
3850 struct cnic_local *cp = dev->cnic_priv;
3851
Michael Chana1e621b2010-06-15 08:57:01 +00003852 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3853 cnic_cm_upcall(cp, csk, opcode);
3854 return;
3855 }
3856
Michael Chana4636962009-06-08 18:14:43 -07003857 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003858 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003859 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003860 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003861}
3862
3863static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3864{
3865}
3866
3867static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3868{
3869 u32 seed;
3870
3871 get_random_bytes(&seed, 4);
3872 cnic_ctx_wr(dev, 45, 0, seed);
3873 return 0;
3874}
3875
Michael Chan71034ba2009-10-10 13:46:59 +00003876static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3877{
3878 struct cnic_dev *dev = csk->dev;
3879 struct cnic_local *cp = dev->cnic_priv;
3880 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3881 union l5cm_specific_data l5_data;
3882 u32 cmd = 0;
3883 int close_complete = 0;
3884
3885 switch (opcode) {
3886 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3887 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3888 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003889 if (cnic_ready_to_close(csk, opcode)) {
3890 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3891 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3892 else
3893 close_complete = 1;
3894 }
Michael Chan71034ba2009-10-10 13:46:59 +00003895 break;
3896 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3897 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3898 break;
3899 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3900 close_complete = 1;
3901 break;
3902 }
3903 if (cmd) {
3904 memset(&l5_data, 0, sizeof(l5_data));
3905
3906 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3907 &l5_data);
3908 } else if (close_complete) {
3909 ctx->timestamp = jiffies;
3910 cnic_close_conn(csk);
3911 cnic_cm_upcall(cp, csk, csk->state);
3912 }
3913}
3914
3915static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3916{
Michael Chanfdf24082010-10-13 14:06:47 +00003917 struct cnic_local *cp = dev->cnic_priv;
3918 int i;
3919
3920 if (!cp->ctx_tbl)
3921 return;
3922
3923 if (!netif_running(dev->netdev))
3924 return;
3925
3926 for (i = 0; i < cp->max_cid_space; i++) {
3927 struct cnic_context *ctx = &cp->ctx_tbl[i];
3928
3929 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3930 msleep(10);
3931
3932 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3933 netdev_warn(dev->netdev, "CID %x not deleted\n",
3934 ctx->cid);
3935 }
3936
3937 cancel_delayed_work(&cp->delete_task);
3938 flush_workqueue(cnic_wq);
3939
3940 if (atomic_read(&cp->iscsi_conn) != 0)
3941 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3942 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00003943}
3944
3945static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3946{
3947 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003948 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003949 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003950
3951 cnic_init_bnx2x_mac(dev);
3952 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3953
3954 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003955 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003956
3957 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003958 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003959 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003960 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00003961 DEF_MAX_DA_COUNT);
3962
3963 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003964 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003965 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003966 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003967 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003968 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003969 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003970 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003971
Michael Chan14203982010-10-06 03:16:06 +00003972 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003973 DEF_MAX_CWND);
3974 return 0;
3975}
3976
Michael Chanfdf24082010-10-13 14:06:47 +00003977static void cnic_delete_task(struct work_struct *work)
3978{
3979 struct cnic_local *cp;
3980 struct cnic_dev *dev;
3981 u32 i;
3982 int need_resched = 0;
3983
3984 cp = container_of(work, struct cnic_local, delete_task.work);
3985 dev = cp->dev;
3986
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003987 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
3988 struct drv_ctl_info info;
3989
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003990 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003991
3992 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
3993 cp->ethdev->drv_ctl(dev->netdev, &info);
3994 }
3995
Michael Chanfdf24082010-10-13 14:06:47 +00003996 for (i = 0; i < cp->max_cid_space; i++) {
3997 struct cnic_context *ctx = &cp->ctx_tbl[i];
3998
3999 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4000 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4001 continue;
4002
4003 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4004 need_resched = 1;
4005 continue;
4006 }
4007
4008 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4009 continue;
4010
4011 cnic_bnx2x_destroy_ramrod(dev, i);
4012
4013 cnic_free_bnx2x_conn_resc(dev, i);
4014 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4015 atomic_dec(&cp->iscsi_conn);
4016
4017 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4018 }
4019
4020 if (need_resched)
4021 queue_delayed_work(cnic_wq, &cp->delete_task,
4022 msecs_to_jiffies(10));
4023
4024}
4025
Michael Chana4636962009-06-08 18:14:43 -07004026static int cnic_cm_open(struct cnic_dev *dev)
4027{
4028 struct cnic_local *cp = dev->cnic_priv;
4029 int err;
4030
4031 err = cnic_cm_alloc_mem(dev);
4032 if (err)
4033 return err;
4034
4035 err = cp->start_cm(dev);
4036
4037 if (err)
4038 goto err_out;
4039
Michael Chanfdf24082010-10-13 14:06:47 +00004040 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4041
Michael Chana4636962009-06-08 18:14:43 -07004042 dev->cm_create = cnic_cm_create;
4043 dev->cm_destroy = cnic_cm_destroy;
4044 dev->cm_connect = cnic_cm_connect;
4045 dev->cm_abort = cnic_cm_abort;
4046 dev->cm_close = cnic_cm_close;
4047 dev->cm_select_dev = cnic_cm_select_dev;
4048
4049 cp->ulp_handle[CNIC_ULP_L4] = dev;
4050 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4051 return 0;
4052
4053err_out:
4054 cnic_cm_free_mem(dev);
4055 return err;
4056}
4057
4058static int cnic_cm_shutdown(struct cnic_dev *dev)
4059{
4060 struct cnic_local *cp = dev->cnic_priv;
4061 int i;
4062
4063 cp->stop_cm(dev);
4064
4065 if (!cp->csk_tbl)
4066 return 0;
4067
4068 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4069 struct cnic_sock *csk = &cp->csk_tbl[i];
4070
4071 clear_bit(SK_F_INUSE, &csk->flags);
4072 cnic_cm_cleanup(csk);
4073 }
4074 cnic_cm_free_mem(dev);
4075
4076 return 0;
4077}
4078
4079static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4080{
Michael Chana4636962009-06-08 18:14:43 -07004081 u32 cid_addr;
4082 int i;
4083
Michael Chana4636962009-06-08 18:14:43 -07004084 cid_addr = GET_CID_ADDR(cid);
4085
4086 for (i = 0; i < CTX_SIZE; i += 4)
4087 cnic_ctx_wr(dev, cid_addr, i, 0);
4088}
4089
4090static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4091{
4092 struct cnic_local *cp = dev->cnic_priv;
4093 int ret = 0, i;
4094 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4095
4096 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4097 return 0;
4098
4099 for (i = 0; i < cp->ctx_blks; i++) {
4100 int j;
4101 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4102 u32 val;
4103
4104 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4105
4106 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4107 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4108 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4109 (u64) cp->ctx_arr[i].mapping >> 32);
4110 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4111 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4112 for (j = 0; j < 10; j++) {
4113
4114 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4115 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4116 break;
4117 udelay(5);
4118 }
4119 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4120 ret = -EBUSY;
4121 break;
4122 }
4123 }
4124 return ret;
4125}
4126
4127static void cnic_free_irq(struct cnic_dev *dev)
4128{
4129 struct cnic_local *cp = dev->cnic_priv;
4130 struct cnic_eth_dev *ethdev = cp->ethdev;
4131
4132 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4133 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004134 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004135 free_irq(ethdev->irq_arr[0].vector, dev);
4136 }
4137}
4138
Michael Chan6e0dc642010-10-13 14:06:44 +00004139static int cnic_request_irq(struct cnic_dev *dev)
4140{
4141 struct cnic_local *cp = dev->cnic_priv;
4142 struct cnic_eth_dev *ethdev = cp->ethdev;
4143 int err;
4144
4145 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4146 if (err)
4147 tasklet_disable(&cp->cnic_irq_task);
4148
4149 return err;
4150}
4151
Michael Chana4636962009-06-08 18:14:43 -07004152static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4153{
4154 struct cnic_local *cp = dev->cnic_priv;
4155 struct cnic_eth_dev *ethdev = cp->ethdev;
4156
4157 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4158 int err, i = 0;
4159 int sblk_num = cp->status_blk_num;
4160 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4161 BNX2_HC_SB_CONFIG_1;
4162
4163 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4164
4165 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4166 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4167 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4168
Michael Chana4dde3a2010-02-24 14:42:08 +00004169 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004170 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004171 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004172 err = cnic_request_irq(dev);
4173 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004174 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004175
Michael Chana4dde3a2010-02-24 14:42:08 +00004176 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004177 i < 10) {
4178 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4179 1 << (11 + sblk_num));
4180 udelay(10);
4181 i++;
4182 barrier();
4183 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004184 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004185 cnic_free_irq(dev);
4186 goto failed;
4187 }
4188
4189 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004190 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004191 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4192 int i = 0;
4193
4194 while (sblk->status_completion_producer_index && i < 10) {
4195 CNIC_WR(dev, BNX2_HC_COMMAND,
4196 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4197 udelay(10);
4198 i++;
4199 barrier();
4200 }
4201 if (sblk->status_completion_producer_index)
4202 goto failed;
4203
4204 }
4205 return 0;
4206
4207failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004208 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004209 return -EBUSY;
4210}
4211
4212static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4213{
4214 struct cnic_local *cp = dev->cnic_priv;
4215 struct cnic_eth_dev *ethdev = cp->ethdev;
4216
4217 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4218 return;
4219
4220 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4221 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4222}
4223
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00004224static void cnic_get_bnx2_iscsi_info(struct cnic_dev *dev)
4225{
4226 u32 max_conn;
4227
4228 max_conn = cnic_reg_rd_ind(dev, BNX2_FW_MAX_ISCSI_CONN);
4229 dev->max_iscsi_conn = max_conn;
4230}
4231
Michael Chana4636962009-06-08 18:14:43 -07004232static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4233{
4234 struct cnic_local *cp = dev->cnic_priv;
4235 struct cnic_eth_dev *ethdev = cp->ethdev;
4236
4237 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4238 return;
4239
4240 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4241 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4242 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4243 synchronize_irq(ethdev->irq_arr[0].vector);
4244}
4245
4246static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4247{
4248 struct cnic_local *cp = dev->cnic_priv;
4249 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004250 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004251 u32 cid_addr, tx_cid, sb_id;
4252 u32 val, offset0, offset1, offset2, offset3;
4253 int i;
4254 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004255 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004256 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004257
4258 sb_id = cp->status_blk_num;
4259 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004260 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4261 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004262 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004263
4264 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004265 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4266 (TX_TSS_CID << 7));
4267 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4268 }
4269 cp->tx_cons = *cp->tx_cons_ptr;
4270
4271 cid_addr = GET_CID_ADDR(tx_cid);
4272 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4273 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4274
4275 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4276 cnic_ctx_wr(dev, cid_addr2, i, 0);
4277
4278 offset0 = BNX2_L2CTX_TYPE_XI;
4279 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4280 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4281 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4282 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004283 cnic_init_context(dev, tx_cid);
4284 cnic_init_context(dev, tx_cid + 1);
4285
Michael Chana4636962009-06-08 18:14:43 -07004286 offset0 = BNX2_L2CTX_TYPE;
4287 offset1 = BNX2_L2CTX_CMD_TYPE;
4288 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4289 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4290 }
4291 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4292 cnic_ctx_wr(dev, cid_addr, offset0, val);
4293
4294 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4295 cnic_ctx_wr(dev, cid_addr, offset1, val);
4296
Michael Chancd801532010-10-13 14:06:49 +00004297 txbd = (struct tx_bd *) udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004298
Michael Chancd801532010-10-13 14:06:49 +00004299 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004300 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4301 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4302 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4303 }
Michael Chancd801532010-10-13 14:06:49 +00004304 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004305 cnic_ctx_wr(dev, cid_addr, offset2, val);
4306 txbd->tx_bd_haddr_hi = val;
4307
Michael Chancd801532010-10-13 14:06:49 +00004308 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004309 cnic_ctx_wr(dev, cid_addr, offset3, val);
4310 txbd->tx_bd_haddr_lo = val;
4311}
4312
4313static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4314{
4315 struct cnic_local *cp = dev->cnic_priv;
4316 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004317 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004318 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4319 int i;
4320 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004321 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004322 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004323
4324 sb_id = cp->status_blk_num;
4325 cnic_init_context(dev, 2);
4326 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4327 coal_reg = BNX2_HC_COMMAND;
4328 coal_val = CNIC_RD(dev, coal_reg);
4329 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004330 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004331
4332 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4333 coal_reg = BNX2_HC_COALESCE_NOW;
4334 coal_val = 1 << (11 + sb_id);
4335 }
4336 i = 0;
4337 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4338 CNIC_WR(dev, coal_reg, coal_val);
4339 udelay(10);
4340 i++;
4341 barrier();
4342 }
4343 cp->rx_cons = *cp->rx_cons_ptr;
4344
4345 cid_addr = GET_CID_ADDR(2);
4346 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4347 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4348 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4349
4350 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004351 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004352 else
Michael Chand0549382009-10-28 03:41:59 -07004353 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004354 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4355
Michael Chancd801532010-10-13 14:06:49 +00004356 rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
Michael Chana4636962009-06-08 18:14:43 -07004357 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4358 dma_addr_t buf_map;
4359 int n = (i % cp->l2_rx_ring_size) + 1;
4360
Michael Chancd801532010-10-13 14:06:49 +00004361 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004362 rxbd->rx_bd_len = cp->l2_single_buf_size;
4363 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4364 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4365 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4366 }
Michael Chancd801532010-10-13 14:06:49 +00004367 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004368 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4369 rxbd->rx_bd_haddr_hi = val;
4370
Michael Chancd801532010-10-13 14:06:49 +00004371 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004372 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4373 rxbd->rx_bd_haddr_lo = val;
4374
4375 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4376 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4377}
4378
4379static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4380{
4381 struct kwqe *wqes[1], l2kwqe;
4382
4383 memset(&l2kwqe, 0, sizeof(l2kwqe));
4384 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004385 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004386 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4387 KWQE_OPCODE_SHIFT) | 2;
4388 dev->submit_kwqes(dev, wqes, 1);
4389}
4390
4391static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4392{
4393 struct cnic_local *cp = dev->cnic_priv;
4394 u32 val;
4395
4396 val = cp->func << 2;
4397
4398 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4399
4400 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4401 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4402 dev->mac_addr[0] = (u8) (val >> 8);
4403 dev->mac_addr[1] = (u8) val;
4404
4405 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4406
4407 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4408 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4409 dev->mac_addr[2] = (u8) (val >> 24);
4410 dev->mac_addr[3] = (u8) (val >> 16);
4411 dev->mac_addr[4] = (u8) (val >> 8);
4412 dev->mac_addr[5] = (u8) val;
4413
4414 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4415
4416 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4417 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4418 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4419
4420 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4421 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4422 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4423}
4424
4425static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4426{
4427 struct cnic_local *cp = dev->cnic_priv;
4428 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004429 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004430 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004431 int err;
4432
4433 cnic_set_bnx2_mac(dev);
4434
4435 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4436 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4437 if (BCM_PAGE_BITS > 12)
4438 val |= (12 - 8) << 4;
4439 else
4440 val |= (BCM_PAGE_BITS - 8) << 4;
4441
4442 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4443
4444 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4445 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4446 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4447
4448 err = cnic_setup_5709_context(dev, 1);
4449 if (err)
4450 return err;
4451
4452 cnic_init_context(dev, KWQ_CID);
4453 cnic_init_context(dev, KCQ_CID);
4454
Michael Chane6c28892010-06-24 14:58:39 +00004455 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004456 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4457
4458 cp->max_kwq_idx = MAX_KWQ_IDX;
4459 cp->kwq_prod_idx = 0;
4460 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004461 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004462
4463 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4464 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4465 else
4466 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4467
4468 /* Initialize the kernel work queue context. */
4469 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4470 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004471 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004472
4473 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004474 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004475
4476 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004477 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004478
4479 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004480 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004481
4482 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004483 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004484
Michael Chane6c28892010-06-24 14:58:39 +00004485 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4486 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004487
Michael Chane6c28892010-06-24 14:58:39 +00004488 cp->kcq1.sw_prod_idx = 0;
4489 cp->kcq1.hw_prod_idx_ptr =
4490 (u16 *) &sblk->status_completion_producer_index;
4491
4492 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004493
4494 /* Initialize the kernel complete queue context. */
4495 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4496 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004497 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004498
4499 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004500 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004501
4502 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004503 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004504
Michael Chane6c28892010-06-24 14:58:39 +00004505 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4506 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004507
Michael Chane6c28892010-06-24 14:58:39 +00004508 val = (u32) cp->kcq1.dma.pgtbl_map;
4509 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004510
4511 cp->int_num = 0;
4512 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004513 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004514 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004515 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004516
Michael Chane6c28892010-06-24 14:58:39 +00004517 cp->kcq1.hw_prod_idx_ptr =
4518 (u16 *) &msblk->status_completion_producer_index;
4519 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004520 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004521 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004522 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4523 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004524 }
4525
4526 /* Enable Commnad Scheduler notification when we write to the
4527 * host producer index of the kernel contexts. */
4528 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4529
4530 /* Enable Command Scheduler notification when we write to either
4531 * the Send Queue or Receive Queue producer indexes of the kernel
4532 * bypass contexts. */
4533 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4534 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4535
4536 /* Notify COM when the driver post an application buffer. */
4537 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4538
4539 /* Set the CP and COM doorbells. These two processors polls the
4540 * doorbell for a non zero value before running. This must be done
4541 * after setting up the kernel queue contexts. */
4542 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4543 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4544
4545 cnic_init_bnx2_tx_ring(dev);
4546 cnic_init_bnx2_rx_ring(dev);
4547
4548 err = cnic_init_bnx2_irq(dev);
4549 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004550 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004551 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4552 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4553 return err;
4554 }
4555
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00004556 cnic_get_bnx2_iscsi_info(dev);
4557
Michael Chana4636962009-06-08 18:14:43 -07004558 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,
Michael Chan520efdf2010-06-24 14:58:37 +00004832 cp->iscsi_start_cid);
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,
4840 cp->fcoe_start_cid);
4841
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
5223 cp->cnic_ops = &cnic_bnx2_ops;
5224 cp->start_hw = cnic_start_bnx2_hw;
5225 cp->stop_hw = cnic_stop_bnx2_hw;
5226 cp->setup_pgtbl = cnic_setup_page_tbl;
5227 cp->alloc_resc = cnic_alloc_bnx2_resc;
5228 cp->free_resc = cnic_free_resc;
5229 cp->start_cm = cnic_cm_init_bnx2_hw;
5230 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5231 cp->enable_int = cnic_enable_bnx2_int;
5232 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5233 cp->close_conn = cnic_close_bnx2_conn;
5234 cp->next_idx = cnic_bnx2_next_idx;
5235 cp->hw_idx = cnic_bnx2_hw_idx;
5236 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;
5301 cp->next_idx = cnic_bnx2x_next_idx;
5302 cp->hw_idx = cnic_bnx2x_hw_idx;
5303 return cdev;
5304}
5305
Michael Chana4636962009-06-08 18:14:43 -07005306static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5307{
5308 struct ethtool_drvinfo drvinfo;
5309 struct cnic_dev *cdev = NULL;
5310
5311 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5312 memset(&drvinfo, 0, sizeof(drvinfo));
5313 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5314
5315 if (!strcmp(drvinfo.driver, "bnx2"))
5316 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005317 if (!strcmp(drvinfo.driver, "bnx2x"))
5318 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005319 if (cdev) {
5320 write_lock(&cnic_dev_lock);
5321 list_add(&cdev->list, &cnic_dev_list);
5322 write_unlock(&cnic_dev_lock);
5323 }
5324 }
5325 return cdev;
5326}
5327
5328/**
5329 * netdev event handler
5330 */
5331static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5332 void *ptr)
5333{
5334 struct net_device *netdev = ptr;
5335 struct cnic_dev *dev;
5336 int if_type;
5337 int new_dev = 0;
5338
5339 dev = cnic_from_netdev(netdev);
5340
5341 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
5342 /* Check for the hot-plug device */
5343 dev = is_cnic_dev(netdev);
5344 if (dev) {
5345 new_dev = 1;
5346 cnic_hold(dev);
5347 }
5348 }
5349 if (dev) {
5350 struct cnic_local *cp = dev->cnic_priv;
5351
5352 if (new_dev)
5353 cnic_ulp_init(dev);
5354 else if (event == NETDEV_UNREGISTER)
5355 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005356
5357 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00005358 if (cnic_register_netdev(dev) != 0) {
5359 cnic_put(dev);
5360 goto done;
5361 }
Michael Chana4636962009-06-08 18:14:43 -07005362 if (!cnic_start_hw(dev))
5363 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005364 }
5365
5366 rcu_read_lock();
5367 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5368 struct cnic_ulp_ops *ulp_ops;
5369 void *ctx;
5370
5371 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5372 if (!ulp_ops || !ulp_ops->indicate_netevent)
5373 continue;
5374
5375 ctx = cp->ulp_handle[if_type];
5376
5377 ulp_ops->indicate_netevent(ctx, event);
5378 }
5379 rcu_read_unlock();
5380
5381 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005382 cnic_ulp_stop(dev);
5383 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005384 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005385 } else if (event == NETDEV_UNREGISTER) {
5386 write_lock(&cnic_dev_lock);
5387 list_del_init(&dev->list);
5388 write_unlock(&cnic_dev_lock);
5389
5390 cnic_put(dev);
5391 cnic_free_dev(dev);
5392 goto done;
5393 }
5394 cnic_put(dev);
5395 }
5396done:
5397 return NOTIFY_DONE;
5398}
5399
5400static struct notifier_block cnic_netdev_notifier = {
5401 .notifier_call = cnic_netdev_event
5402};
5403
5404static void cnic_release(void)
5405{
5406 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005407 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005408
5409 while (!list_empty(&cnic_dev_list)) {
5410 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5411 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5412 cnic_ulp_stop(dev);
5413 cnic_stop_hw(dev);
5414 }
5415
5416 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005417 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005418 list_del_init(&dev->list);
5419 cnic_free_dev(dev);
5420 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005421 while (!list_empty(&cnic_udev_list)) {
5422 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5423 list);
5424 cnic_free_uio(udev);
5425 }
Michael Chana4636962009-06-08 18:14:43 -07005426}
5427
5428static int __init cnic_init(void)
5429{
5430 int rc = 0;
5431
Joe Perchesddf79b22010-02-17 15:01:54 +00005432 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005433
5434 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5435 if (rc) {
5436 cnic_release();
5437 return rc;
5438 }
5439
Michael Chanfdf24082010-10-13 14:06:47 +00005440 cnic_wq = create_singlethread_workqueue("cnic_wq");
5441 if (!cnic_wq) {
5442 cnic_release();
5443 unregister_netdevice_notifier(&cnic_netdev_notifier);
5444 return -ENOMEM;
5445 }
5446
Michael Chana4636962009-06-08 18:14:43 -07005447 return 0;
5448}
5449
5450static void __exit cnic_exit(void)
5451{
5452 unregister_netdevice_notifier(&cnic_netdev_notifier);
5453 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005454 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005455}
5456
5457module_init(cnic_init);
5458module_exit(cnic_exit);