blob: a529bde03c560ddc022de52143e7f959c04a1285 [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 Chan8adc9242010-12-23 07:42:57 +000063/* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
Michael Chana4636962009-06-08 18:14:43 -070064static LIST_HEAD(cnic_dev_list);
Michael Chana3ceeeb2010-10-13 14:06:50 +000065static LIST_HEAD(cnic_udev_list);
Michael Chana4636962009-06-08 18:14:43 -070066static DEFINE_RWLOCK(cnic_dev_lock);
67static DEFINE_MUTEX(cnic_lock);
68
Eric Dumazet13707f92011-01-26 19:28:23 +000069static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
70
71/* helper function, assuming cnic_lock is held */
72static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
73{
74 return rcu_dereference_protected(cnic_ulp_tbl[type],
75 lockdep_is_held(&cnic_lock));
76}
Michael Chana4636962009-06-08 18:14:43 -070077
78static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000079static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070080static int cnic_ctl(void *, struct cnic_ctl_info *);
81
82static struct cnic_ops cnic_bnx2_ops = {
83 .cnic_owner = THIS_MODULE,
84 .cnic_handler = cnic_service_bnx2,
85 .cnic_ctl = cnic_ctl,
86};
87
Michael Chan71034ba2009-10-10 13:46:59 +000088static struct cnic_ops cnic_bnx2x_ops = {
89 .cnic_owner = THIS_MODULE,
90 .cnic_handler = cnic_service_bnx2x,
91 .cnic_ctl = cnic_ctl,
92};
93
Michael Chanfdf24082010-10-13 14:06:47 +000094static struct workqueue_struct *cnic_wq;
95
Michael Chan86b53602009-10-10 13:46:57 +000096static void cnic_shutdown_rings(struct cnic_dev *);
97static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070098static int cnic_cm_set_pg(struct cnic_sock *);
99
100static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
101{
Michael Chancd801532010-10-13 14:06:49 +0000102 struct cnic_uio_dev *udev = uinfo->priv;
103 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -0700104
105 if (!capable(CAP_NET_ADMIN))
106 return -EPERM;
107
Michael Chancd801532010-10-13 14:06:49 +0000108 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700109 return -EBUSY;
110
Michael Chan86b53602009-10-10 13:46:57 +0000111 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000112 dev = udev->dev;
113
Michael Chana3ceeeb2010-10-13 14:06:50 +0000114 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000115 rtnl_unlock();
116 return -ENODEV;
117 }
118
Michael Chancd801532010-10-13 14:06:49 +0000119 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700120
Michael Chana3ceeeb2010-10-13 14:06:50 +0000121 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000122 cnic_init_rings(dev);
123 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700124
125 return 0;
126}
127
128static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
129{
Michael Chancd801532010-10-13 14:06:49 +0000130 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000131
Michael Chancd801532010-10-13 14:06:49 +0000132 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700133 return 0;
134}
135
136static inline void cnic_hold(struct cnic_dev *dev)
137{
138 atomic_inc(&dev->ref_count);
139}
140
141static inline void cnic_put(struct cnic_dev *dev)
142{
143 atomic_dec(&dev->ref_count);
144}
145
146static inline void csk_hold(struct cnic_sock *csk)
147{
148 atomic_inc(&csk->ref_count);
149}
150
151static inline void csk_put(struct cnic_sock *csk)
152{
153 atomic_dec(&csk->ref_count);
154}
155
156static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
157{
158 struct cnic_dev *cdev;
159
160 read_lock(&cnic_dev_lock);
161 list_for_each_entry(cdev, &cnic_dev_list, list) {
162 if (netdev == cdev->netdev) {
163 cnic_hold(cdev);
164 read_unlock(&cnic_dev_lock);
165 return cdev;
166 }
167 }
168 read_unlock(&cnic_dev_lock);
169 return NULL;
170}
171
Michael Chan7fc1ece2009-08-14 15:49:47 +0000172static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
173{
174 atomic_inc(&ulp_ops->ref_count);
175}
176
177static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
178{
179 atomic_dec(&ulp_ops->ref_count);
180}
181
Michael Chana4636962009-06-08 18:14:43 -0700182static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
183{
184 struct cnic_local *cp = dev->cnic_priv;
185 struct cnic_eth_dev *ethdev = cp->ethdev;
186 struct drv_ctl_info info;
187 struct drv_ctl_io *io = &info.data.io;
188
189 info.cmd = DRV_CTL_CTX_WR_CMD;
190 io->cid_addr = cid_addr;
191 io->offset = off;
192 io->data = val;
193 ethdev->drv_ctl(dev->netdev, &info);
194}
195
Michael Chan71034ba2009-10-10 13:46:59 +0000196static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
197{
198 struct cnic_local *cp = dev->cnic_priv;
199 struct cnic_eth_dev *ethdev = cp->ethdev;
200 struct drv_ctl_info info;
201 struct drv_ctl_io *io = &info.data.io;
202
203 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
204 io->offset = off;
205 io->dma_addr = addr;
206 ethdev->drv_ctl(dev->netdev, &info);
207}
208
209static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
210{
211 struct cnic_local *cp = dev->cnic_priv;
212 struct cnic_eth_dev *ethdev = cp->ethdev;
213 struct drv_ctl_info info;
214 struct drv_ctl_l2_ring *ring = &info.data.ring;
215
216 if (start)
217 info.cmd = DRV_CTL_START_L2_CMD;
218 else
219 info.cmd = DRV_CTL_STOP_L2_CMD;
220
221 ring->cid = cid;
222 ring->client_id = cl_id;
223 ethdev->drv_ctl(dev->netdev, &info);
224}
225
Michael Chana4636962009-06-08 18:14:43 -0700226static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
227{
228 struct cnic_local *cp = dev->cnic_priv;
229 struct cnic_eth_dev *ethdev = cp->ethdev;
230 struct drv_ctl_info info;
231 struct drv_ctl_io *io = &info.data.io;
232
233 info.cmd = DRV_CTL_IO_WR_CMD;
234 io->offset = off;
235 io->data = val;
236 ethdev->drv_ctl(dev->netdev, &info);
237}
238
239static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
240{
241 struct cnic_local *cp = dev->cnic_priv;
242 struct cnic_eth_dev *ethdev = cp->ethdev;
243 struct drv_ctl_info info;
244 struct drv_ctl_io *io = &info.data.io;
245
246 info.cmd = DRV_CTL_IO_RD_CMD;
247 io->offset = off;
248 ethdev->drv_ctl(dev->netdev, &info);
249 return io->data;
250}
251
252static int cnic_in_use(struct cnic_sock *csk)
253{
254 return test_bit(SK_F_INUSE, &csk->flags);
255}
256
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000257static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700258{
259 struct cnic_local *cp = dev->cnic_priv;
260 struct cnic_eth_dev *ethdev = cp->ethdev;
261 struct drv_ctl_info info;
262
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000263 info.cmd = cmd;
264 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700265 ethdev->drv_ctl(dev->netdev, &info);
266}
267
Michael Chan71034ba2009-10-10 13:46:59 +0000268static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
269{
270 u32 i;
271
Michael Chan520efdf2010-06-24 14:58:37 +0000272 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000273 if (cp->ctx_tbl[i].cid == cid) {
274 *l5_cid = i;
275 return 0;
276 }
277 }
278 return -EINVAL;
279}
280
Michael Chana4636962009-06-08 18:14:43 -0700281static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
282 struct cnic_sock *csk)
283{
284 struct iscsi_path path_req;
285 char *buf = NULL;
286 u16 len = 0;
287 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
288 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000289 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000290 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700291
Michael Chancd801532010-10-13 14:06:49 +0000292 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700293 return -ENODEV;
294
295 if (csk) {
296 len = sizeof(path_req);
297 buf = (char *) &path_req;
298 memset(&path_req, 0, len);
299
300 msg_type = ISCSI_KEVENT_PATH_REQ;
301 path_req.handle = (u64) csk->l5_cid;
302 if (test_bit(SK_F_IPV6, &csk->flags)) {
303 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
304 sizeof(struct in6_addr));
305 path_req.ip_addr_len = 16;
306 } else {
307 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
308 sizeof(struct in_addr));
309 path_req.ip_addr_len = 4;
310 }
311 path_req.vlan_id = csk->vlan_id;
312 path_req.pmtu = csk->mtu;
313 }
314
Michael Chan939b82e2010-12-23 07:42:58 +0000315 while (retry < 3) {
316 rc = 0;
317 rcu_read_lock();
318 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
319 if (ulp_ops)
320 rc = ulp_ops->iscsi_nl_send_msg(
321 cp->ulp_handle[CNIC_ULP_ISCSI],
322 msg_type, buf, len);
323 rcu_read_unlock();
324 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
325 break;
326
327 msleep(100);
328 retry++;
329 }
Michael Chana4636962009-06-08 18:14:43 -0700330 return 0;
331}
332
Eddie Wai42ecbb82010-12-23 07:43:02 +0000333static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
334
Michael Chana4636962009-06-08 18:14:43 -0700335static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
336 char *buf, u16 len)
337{
338 int rc = -EINVAL;
339
340 switch (msg_type) {
341 case ISCSI_UEVENT_PATH_UPDATE: {
342 struct cnic_local *cp;
343 u32 l5_cid;
344 struct cnic_sock *csk;
345 struct iscsi_path *path_resp;
346
347 if (len < sizeof(*path_resp))
348 break;
349
350 path_resp = (struct iscsi_path *) buf;
351 cp = dev->cnic_priv;
352 l5_cid = (u32) path_resp->handle;
353 if (l5_cid >= MAX_CM_SK_TBL_SZ)
354 break;
355
Michael Chand02a5e62010-02-24 14:42:06 +0000356 rcu_read_lock();
357 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
358 rc = -ENODEV;
359 rcu_read_unlock();
360 break;
361 }
Michael Chana4636962009-06-08 18:14:43 -0700362 csk = &cp->csk_tbl[l5_cid];
363 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000364 if (cnic_in_use(csk) &&
365 test_bit(SK_F_CONNECT_START, &csk->flags)) {
366
Michael Chana4636962009-06-08 18:14:43 -0700367 memcpy(csk->ha, path_resp->mac_addr, 6);
368 if (test_bit(SK_F_IPV6, &csk->flags))
369 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
370 sizeof(struct in6_addr));
371 else
372 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
373 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000374
375 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700376 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000377 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
378 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
379
380 cnic_cm_upcall(cp, csk,
381 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
382 clear_bit(SK_F_CONNECT_START, &csk->flags);
383 }
Michael Chana4636962009-06-08 18:14:43 -0700384 }
385 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000386 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700387 rc = 0;
388 }
389 }
390
391 return rc;
392}
393
394static int cnic_offld_prep(struct cnic_sock *csk)
395{
396 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
397 return 0;
398
399 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
400 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
401 return 0;
402 }
403
404 return 1;
405}
406
407static int cnic_close_prep(struct cnic_sock *csk)
408{
409 clear_bit(SK_F_CONNECT_START, &csk->flags);
410 smp_mb__after_clear_bit();
411
412 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
413 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
414 msleep(1);
415
416 return 1;
417 }
418 return 0;
419}
420
421static int cnic_abort_prep(struct cnic_sock *csk)
422{
423 clear_bit(SK_F_CONNECT_START, &csk->flags);
424 smp_mb__after_clear_bit();
425
426 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
427 msleep(1);
428
429 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
430 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
431 return 1;
432 }
433
434 return 0;
435}
436
437int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
438{
439 struct cnic_dev *dev;
440
roel kluin0d37f362009-11-02 06:53:44 +0000441 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000442 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700443 return -EINVAL;
444 }
445 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000446 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000447 pr_err("%s: Type %d has already been registered\n",
448 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700449 mutex_unlock(&cnic_lock);
450 return -EBUSY;
451 }
452
453 read_lock(&cnic_dev_lock);
454 list_for_each_entry(dev, &cnic_dev_list, list) {
455 struct cnic_local *cp = dev->cnic_priv;
456
457 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
458 }
459 read_unlock(&cnic_dev_lock);
460
Michael Chan7fc1ece2009-08-14 15:49:47 +0000461 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700462 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
463 mutex_unlock(&cnic_lock);
464
465 /* Prevent race conditions with netdev_event */
466 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700467 list_for_each_entry(dev, &cnic_dev_list, list) {
468 struct cnic_local *cp = dev->cnic_priv;
469
470 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
471 ulp_ops->cnic_init(dev);
472 }
Michael Chana4636962009-06-08 18:14:43 -0700473 rtnl_unlock();
474
475 return 0;
476}
477
478int cnic_unregister_driver(int ulp_type)
479{
480 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000481 struct cnic_ulp_ops *ulp_ops;
482 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700483
roel kluin0d37f362009-11-02 06:53:44 +0000484 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000485 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700486 return -EINVAL;
487 }
488 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000489 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000490 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000491 pr_err("%s: Type %d has not been registered\n",
492 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700493 goto out_unlock;
494 }
495 read_lock(&cnic_dev_lock);
496 list_for_each_entry(dev, &cnic_dev_list, list) {
497 struct cnic_local *cp = dev->cnic_priv;
498
499 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000500 pr_err("%s: Type %d still has devices registered\n",
501 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700502 read_unlock(&cnic_dev_lock);
503 goto out_unlock;
504 }
505 }
506 read_unlock(&cnic_dev_lock);
507
508 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
509
510 mutex_unlock(&cnic_lock);
511 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000512 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
513 msleep(100);
514 i++;
515 }
516
517 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000518 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700519 return 0;
520
521out_unlock:
522 mutex_unlock(&cnic_lock);
523 return -EINVAL;
524}
525
526static int cnic_start_hw(struct cnic_dev *);
527static void cnic_stop_hw(struct cnic_dev *);
528
529static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
530 void *ulp_ctx)
531{
532 struct cnic_local *cp = dev->cnic_priv;
533 struct cnic_ulp_ops *ulp_ops;
534
roel kluin0d37f362009-11-02 06:53:44 +0000535 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000536 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700537 return -EINVAL;
538 }
539 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000540 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000541 pr_err("%s: Driver with type %d has not been registered\n",
542 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700543 mutex_unlock(&cnic_lock);
544 return -EAGAIN;
545 }
546 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000547 pr_err("%s: Type %d has already been registered to this device\n",
548 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700549 mutex_unlock(&cnic_lock);
550 return -EBUSY;
551 }
552
553 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
554 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000555 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700556 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
557 cnic_hold(dev);
558
559 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
560 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
561 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
562
563 mutex_unlock(&cnic_lock);
564
565 return 0;
566
567}
568EXPORT_SYMBOL(cnic_register_driver);
569
570static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
571{
572 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000573 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700574
roel kluin0d37f362009-11-02 06:53:44 +0000575 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000576 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700577 return -EINVAL;
578 }
579 mutex_lock(&cnic_lock);
580 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
581 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
582 cnic_put(dev);
583 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000584 pr_err("%s: device not registered to this ulp type %d\n",
585 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700586 mutex_unlock(&cnic_lock);
587 return -EINVAL;
588 }
589 mutex_unlock(&cnic_lock);
590
Michael Chan42bb8d52011-01-03 15:21:46 +0000591 if (ulp_type == CNIC_ULP_ISCSI)
592 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
593
Michael Chana4636962009-06-08 18:14:43 -0700594 synchronize_rcu();
595
Michael Chan681dbd72009-08-14 15:49:46 +0000596 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
597 i < 20) {
598 msleep(100);
599 i++;
600 }
601 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000602 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000603
Michael Chana4636962009-06-08 18:14:43 -0700604 return 0;
605}
606EXPORT_SYMBOL(cnic_unregister_driver);
607
Eddie Wai11f23aa2011-06-08 19:29:34 +0000608static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
609 u32 next)
Michael Chana4636962009-06-08 18:14:43 -0700610{
611 id_tbl->start = start_id;
612 id_tbl->max = size;
Eddie Wai11f23aa2011-06-08 19:29:34 +0000613 id_tbl->next = next;
Michael Chana4636962009-06-08 18:14:43 -0700614 spin_lock_init(&id_tbl->lock);
615 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
616 if (!id_tbl->table)
617 return -ENOMEM;
618
619 return 0;
620}
621
622static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
623{
624 kfree(id_tbl->table);
625 id_tbl->table = NULL;
626}
627
628static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
629{
630 int ret = -1;
631
632 id -= id_tbl->start;
633 if (id >= id_tbl->max)
634 return ret;
635
636 spin_lock(&id_tbl->lock);
637 if (!test_bit(id, id_tbl->table)) {
638 set_bit(id, id_tbl->table);
639 ret = 0;
640 }
641 spin_unlock(&id_tbl->lock);
642 return ret;
643}
644
645/* Returns -1 if not successful */
646static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
647{
648 u32 id;
649
650 spin_lock(&id_tbl->lock);
651 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
652 if (id >= id_tbl->max) {
653 id = -1;
654 if (id_tbl->next != 0) {
655 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
656 if (id >= id_tbl->next)
657 id = -1;
658 }
659 }
660
661 if (id < id_tbl->max) {
662 set_bit(id, id_tbl->table);
663 id_tbl->next = (id + 1) & (id_tbl->max - 1);
664 id += id_tbl->start;
665 }
666
667 spin_unlock(&id_tbl->lock);
668
669 return id;
670}
671
672static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
673{
674 if (id == -1)
675 return;
676
677 id -= id_tbl->start;
678 if (id >= id_tbl->max)
679 return;
680
681 clear_bit(id, id_tbl->table);
682}
683
684static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
685{
686 int i;
687
688 if (!dma->pg_arr)
689 return;
690
691 for (i = 0; i < dma->num_pages; i++) {
692 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000693 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
694 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700695 dma->pg_arr[i] = NULL;
696 }
697 }
698 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000699 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
700 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700701 dma->pgtbl = NULL;
702 }
703 kfree(dma->pg_arr);
704 dma->pg_arr = NULL;
705 dma->num_pages = 0;
706}
707
708static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
709{
710 int i;
Michael Chan51388262011-01-25 22:14:50 +0000711 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700712
713 for (i = 0; i < dma->num_pages; i++) {
714 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000715 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700716 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000717 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700718 page_table++;
719 }
720}
721
Michael Chan71034ba2009-10-10 13:46:59 +0000722static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
723{
724 int i;
Michael Chan51388262011-01-25 22:14:50 +0000725 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000726
727 for (i = 0; i < dma->num_pages; i++) {
728 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000729 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000730 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000731 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000732 page_table++;
733 }
734}
735
Michael Chana4636962009-06-08 18:14:43 -0700736static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
737 int pages, int use_pg_tbl)
738{
739 int i, size;
740 struct cnic_local *cp = dev->cnic_priv;
741
742 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
743 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
744 if (dma->pg_arr == NULL)
745 return -ENOMEM;
746
747 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
748 dma->num_pages = pages;
749
750 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000751 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
752 BCM_PAGE_SIZE,
753 &dma->pg_map_arr[i],
754 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700755 if (dma->pg_arr[i] == NULL)
756 goto error;
757 }
758 if (!use_pg_tbl)
759 return 0;
760
761 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
762 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000763 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
764 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700765 if (dma->pgtbl == NULL)
766 goto error;
767
768 cp->setup_pgtbl(dev, dma);
769
770 return 0;
771
772error:
773 cnic_free_dma(dev, dma);
774 return -ENOMEM;
775}
776
Michael Chan86b53602009-10-10 13:46:57 +0000777static void cnic_free_context(struct cnic_dev *dev)
778{
779 struct cnic_local *cp = dev->cnic_priv;
780 int i;
781
782 for (i = 0; i < cp->ctx_blks; i++) {
783 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000784 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
785 cp->ctx_arr[i].ctx,
786 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000787 cp->ctx_arr[i].ctx = NULL;
788 }
789 }
790}
791
Michael Chancd801532010-10-13 14:06:49 +0000792static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700793{
Michael Chancd801532010-10-13 14:06:49 +0000794 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700795
Michael Chancd801532010-10-13 14:06:49 +0000796 if (udev->l2_buf) {
797 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
798 udev->l2_buf, udev->l2_buf_map);
799 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700800 }
801
Michael Chancd801532010-10-13 14:06:49 +0000802 if (udev->l2_ring) {
803 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
804 udev->l2_ring, udev->l2_ring_map);
805 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700806 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000807
808 pci_dev_put(udev->pdev);
809 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000810}
811
Michael Chancd801532010-10-13 14:06:49 +0000812static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000813{
Michael Chancd801532010-10-13 14:06:49 +0000814 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000815 return;
816
Michael Chana3ceeeb2010-10-13 14:06:50 +0000817 write_lock(&cnic_dev_lock);
818 list_del_init(&udev->list);
819 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000820 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000821}
822
823static void cnic_free_resc(struct cnic_dev *dev)
824{
825 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000826 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000827
Michael Chancd801532010-10-13 14:06:49 +0000828 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000829 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000830 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000831 }
Michael Chana4636962009-06-08 18:14:43 -0700832
Michael Chan86b53602009-10-10 13:46:57 +0000833 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700834 kfree(cp->ctx_arr);
835 cp->ctx_arr = NULL;
836 cp->ctx_blks = 0;
837
838 cnic_free_dma(dev, &cp->gbl_buf_info);
839 cnic_free_dma(dev, &cp->conn_buf_info);
840 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000841 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000842 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000843 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700844 kfree(cp->iscsi_tbl);
845 cp->iscsi_tbl = NULL;
846 kfree(cp->ctx_tbl);
847 cp->ctx_tbl = NULL;
848
Michael Chane1928c82010-12-23 07:43:04 +0000849 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700850 cnic_free_id_tbl(&cp->cid_tbl);
851}
852
853static int cnic_alloc_context(struct cnic_dev *dev)
854{
855 struct cnic_local *cp = dev->cnic_priv;
856
857 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
858 int i, k, arr_size;
859
860 cp->ctx_blk_size = BCM_PAGE_SIZE;
861 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
862 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
863 sizeof(struct cnic_ctx);
864 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
865 if (cp->ctx_arr == NULL)
866 return -ENOMEM;
867
868 k = 0;
869 for (i = 0; i < 2; i++) {
870 u32 j, reg, off, lo, hi;
871
872 if (i == 0)
873 off = BNX2_PG_CTX_MAP;
874 else
875 off = BNX2_ISCSI_CTX_MAP;
876
877 reg = cnic_reg_rd_ind(dev, off);
878 lo = reg >> 16;
879 hi = reg & 0xffff;
880 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
881 cp->ctx_arr[k].cid = j;
882 }
883
884 cp->ctx_blks = k;
885 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
886 cp->ctx_blks = 0;
887 return -ENOMEM;
888 }
889
890 for (i = 0; i < cp->ctx_blks; i++) {
891 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000892 dma_alloc_coherent(&dev->pcidev->dev,
893 BCM_PAGE_SIZE,
894 &cp->ctx_arr[i].mapping,
895 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700896 if (cp->ctx_arr[i].ctx == NULL)
897 return -ENOMEM;
898 }
899 }
900 return 0;
901}
902
Michael Chane6c28892010-06-24 14:58:39 +0000903static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
904{
905 int err, i, is_bnx2 = 0;
906 struct kcqe **kcq;
907
908 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
909 is_bnx2 = 1;
910
911 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
912 if (err)
913 return err;
914
915 kcq = (struct kcqe **) info->dma.pg_arr;
916 info->kcq = kcq;
917
918 if (is_bnx2)
919 return 0;
920
921 for (i = 0; i < KCQ_PAGE_CNT; i++) {
922 struct bnx2x_bd_chain_next *next =
923 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
924 int j = i + 1;
925
926 if (j >= KCQ_PAGE_CNT)
927 j = 0;
928 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
929 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
930 }
931 return 0;
932}
933
Michael Chancd801532010-10-13 14:06:49 +0000934static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000935{
936 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000937 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000938
Michael Chana3ceeeb2010-10-13 14:06:50 +0000939 read_lock(&cnic_dev_lock);
940 list_for_each_entry(udev, &cnic_udev_list, list) {
941 if (udev->pdev == dev->pcidev) {
942 udev->dev = dev;
943 cp->udev = udev;
944 read_unlock(&cnic_dev_lock);
945 return 0;
946 }
947 }
948 read_unlock(&cnic_dev_lock);
949
Michael Chancd801532010-10-13 14:06:49 +0000950 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
951 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +0000952 return -ENOMEM;
953
Michael Chancd801532010-10-13 14:06:49 +0000954 udev->uio_dev = -1;
955
956 udev->dev = dev;
957 udev->pdev = dev->pcidev;
958 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
959 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
960 &udev->l2_ring_map,
961 GFP_KERNEL | __GFP_COMP);
962 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800963 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000964
Michael Chancd801532010-10-13 14:06:49 +0000965 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
966 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
967 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
968 &udev->l2_buf_map,
969 GFP_KERNEL | __GFP_COMP);
970 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800971 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +0000972
Michael Chana3ceeeb2010-10-13 14:06:50 +0000973 write_lock(&cnic_dev_lock);
974 list_add(&udev->list, &cnic_udev_list);
975 write_unlock(&cnic_dev_lock);
976
977 pci_dev_get(udev->pdev);
978
Michael Chancd801532010-10-13 14:06:49 +0000979 cp->udev = udev;
980
Michael Chanec0248e2009-08-26 09:49:22 +0000981 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800982 err_dma:
983 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
984 udev->l2_ring, udev->l2_ring_map);
985 err_udev:
986 kfree(udev);
987 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +0000988}
989
Michael Chancd801532010-10-13 14:06:49 +0000990static int cnic_init_uio(struct cnic_dev *dev)
991{
Michael Chan5e9b2db2009-08-26 09:49:23 +0000992 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000993 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000994 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +0000995 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000996
Michael Chancd801532010-10-13 14:06:49 +0000997 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +0000998 return -ENOMEM;
999
Michael Chancd801532010-10-13 14:06:49 +00001000 uinfo = &udev->cnic_uinfo;
1001
Michael Chan5e9b2db2009-08-26 09:49:23 +00001002 uinfo->mem[0].addr = dev->netdev->base_addr;
1003 uinfo->mem[0].internal_addr = dev->regview;
1004 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
1005 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1006
Michael Chan5e9b2db2009-08-26 09:49:23 +00001007 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001008 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001009 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001010 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1011 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1012 else
1013 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1014
1015 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001016 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1017 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1018 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001019 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001020
1021 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001022 }
1023
1024 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1025
Michael Chancd801532010-10-13 14:06:49 +00001026 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1027 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001028 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1029
Michael Chancd801532010-10-13 14:06:49 +00001030 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1031 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001032 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1033
1034 uinfo->version = CNIC_MODULE_VERSION;
1035 uinfo->irq = UIO_IRQ_CUSTOM;
1036
1037 uinfo->open = cnic_uio_open;
1038 uinfo->release = cnic_uio_close;
1039
Michael Chana3ceeeb2010-10-13 14:06:50 +00001040 if (udev->uio_dev == -1) {
1041 if (!uinfo->priv) {
1042 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001043
Michael Chana3ceeeb2010-10-13 14:06:50 +00001044 ret = uio_register_device(&udev->pdev->dev, uinfo);
1045 }
1046 } else {
1047 cnic_init_rings(dev);
1048 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001049
Michael Chancd801532010-10-13 14:06:49 +00001050 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001051}
1052
Michael Chana4636962009-06-08 18:14:43 -07001053static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1054{
1055 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001056 int ret;
1057
1058 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1059 if (ret)
1060 goto error;
1061 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1062
Michael Chane6c28892010-06-24 14:58:39 +00001063 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chana4636962009-06-08 18:14:43 -07001064 if (ret)
1065 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001066
1067 ret = cnic_alloc_context(dev);
1068 if (ret)
1069 goto error;
1070
Michael Chancd801532010-10-13 14:06:49 +00001071 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001072 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001073 goto error;
1074
Michael Chancd801532010-10-13 14:06:49 +00001075 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001076 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001077 goto error;
1078
Michael Chana4636962009-06-08 18:14:43 -07001079 return 0;
1080
1081error:
1082 cnic_free_resc(dev);
1083 return ret;
1084}
1085
Michael Chan71034ba2009-10-10 13:46:59 +00001086static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1087{
1088 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001089 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001090 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001091
Michael Chan520efdf2010-06-24 14:58:37 +00001092 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001093 blks = total_mem / ctx_blk_size;
1094 if (total_mem % ctx_blk_size)
1095 blks++;
1096
1097 if (blks > cp->ethdev->ctx_tbl_len)
1098 return -ENOMEM;
1099
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001100 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001101 if (cp->ctx_arr == NULL)
1102 return -ENOMEM;
1103
1104 cp->ctx_blks = blks;
1105 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001106 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001107 cp->ctx_align = 0;
1108 else
1109 cp->ctx_align = ctx_blk_size;
1110
1111 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1112
1113 for (i = 0; i < blks; i++) {
1114 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001115 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1116 &cp->ctx_arr[i].mapping,
1117 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001118 if (cp->ctx_arr[i].ctx == NULL)
1119 return -ENOMEM;
1120
1121 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1122 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1123 cnic_free_context(dev);
1124 cp->ctx_blk_size += cp->ctx_align;
1125 i = -1;
1126 continue;
1127 }
1128 }
1129 }
1130 return 0;
1131}
1132
1133static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1134{
1135 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001136 struct cnic_eth_dev *ethdev = cp->ethdev;
1137 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001138 int i, j, n, ret, pages;
1139 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1140
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001141 cp->iro_arr = ethdev->iro_arr;
1142
Michael Chane1928c82010-12-23 07:43:04 +00001143 cp->max_cid_space = MAX_ISCSI_TBL_SZ + BNX2X_FCOE_NUM_CONNECTIONS;
Michael Chan520efdf2010-06-24 14:58:37 +00001144 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001145 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1146
1147 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1148 cp->max_cid_space += BNX2X_FCOE_NUM_CONNECTIONS;
1149 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1150 if (!cp->fcoe_init_cid)
1151 cp->fcoe_init_cid = 0x10;
1152 }
1153
Michael Chan520efdf2010-06-24 14:58:37 +00001154 if (start_cid < BNX2X_ISCSI_START_CID) {
1155 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1156
1157 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
Michael Chane1928c82010-12-23 07:43:04 +00001158 cp->fcoe_start_cid += delta;
Michael Chan520efdf2010-06-24 14:58:37 +00001159 cp->max_cid_space += delta;
1160 }
1161
Michael Chan71034ba2009-10-10 13:46:59 +00001162 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1163 GFP_KERNEL);
1164 if (!cp->iscsi_tbl)
1165 goto error;
1166
1167 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001168 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001169 if (!cp->ctx_tbl)
1170 goto error;
1171
1172 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1173 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1174 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1175 }
1176
Michael Chane1928c82010-12-23 07:43:04 +00001177 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1178 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1179
Michael Chan520efdf2010-06-24 14:58:37 +00001180 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001181 PAGE_SIZE;
1182
1183 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1184 if (ret)
1185 return -ENOMEM;
1186
1187 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001188 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001189 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1190
1191 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1192 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1193 off;
1194
1195 if ((i % n) == (n - 1))
1196 j++;
1197 }
1198
Michael Chane6c28892010-06-24 14:58:39 +00001199 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00001200 if (ret)
1201 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001202
Michael Chane21ba412010-12-23 07:43:03 +00001203 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1204 ret = cnic_alloc_kcq(dev, &cp->kcq2);
1205 if (ret)
1206 goto error;
1207 }
1208
Michael Chan71034ba2009-10-10 13:46:59 +00001209 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1210 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1211 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1212 if (ret)
1213 goto error;
1214
1215 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1216 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1217 if (ret)
1218 goto error;
1219
1220 ret = cnic_alloc_bnx2x_context(dev);
1221 if (ret)
1222 goto error;
1223
Michael Chan71034ba2009-10-10 13:46:59 +00001224 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1225
1226 cp->l2_rx_ring_size = 15;
1227
Michael Chancd801532010-10-13 14:06:49 +00001228 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001229 if (ret)
1230 goto error;
1231
Michael Chancd801532010-10-13 14:06:49 +00001232 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001233 if (ret)
1234 goto error;
1235
1236 return 0;
1237
1238error:
1239 cnic_free_resc(dev);
1240 return -ENOMEM;
1241}
1242
Michael Chana4636962009-06-08 18:14:43 -07001243static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1244{
1245 return cp->max_kwq_idx -
1246 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1247}
1248
1249static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1250 u32 num_wqes)
1251{
1252 struct cnic_local *cp = dev->cnic_priv;
1253 struct kwqe *prod_qe;
1254 u16 prod, sw_prod, i;
1255
1256 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1257 return -EAGAIN; /* bnx2 is down */
1258
1259 spin_lock_bh(&cp->cnic_ulp_lock);
1260 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001261 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001262 spin_unlock_bh(&cp->cnic_ulp_lock);
1263 return -EAGAIN;
1264 }
1265
Michael Chan1f1332a2010-05-18 11:32:52 +00001266 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001267
1268 prod = cp->kwq_prod_idx;
1269 sw_prod = prod & MAX_KWQ_IDX;
1270 for (i = 0; i < num_wqes; i++) {
1271 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1272 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1273 prod++;
1274 sw_prod = prod & MAX_KWQ_IDX;
1275 }
1276 cp->kwq_prod_idx = prod;
1277
1278 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1279
1280 spin_unlock_bh(&cp->cnic_ulp_lock);
1281 return 0;
1282}
1283
Michael Chan71034ba2009-10-10 13:46:59 +00001284static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1285 union l5cm_specific_data *l5_data)
1286{
1287 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1288 dma_addr_t map;
1289
1290 map = ctx->kwqe_data_mapping;
1291 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1292 l5_data->phy_address.hi = (u64) map >> 32;
1293 return ctx->kwqe_data;
1294}
1295
1296static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1297 u32 type, union l5cm_specific_data *l5_data)
1298{
1299 struct cnic_local *cp = dev->cnic_priv;
1300 struct l5cm_spe kwqe;
1301 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001302 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001303 int ret;
1304
1305 kwqe.hdr.conn_and_cmd_data =
1306 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001307 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001308
1309 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1310 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1311 SPE_HDR_FUNCTION_ID;
1312
1313 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001314 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001315 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1316 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1317
1318 kwq[0] = (struct kwqe_16 *) &kwqe;
1319
1320 spin_lock_bh(&cp->cnic_ulp_lock);
1321 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1322 spin_unlock_bh(&cp->cnic_ulp_lock);
1323
1324 if (ret == 1)
1325 return 0;
1326
1327 return -EBUSY;
1328}
1329
1330static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1331 struct kcqe *cqes[], u32 num_cqes)
1332{
1333 struct cnic_local *cp = dev->cnic_priv;
1334 struct cnic_ulp_ops *ulp_ops;
1335
1336 rcu_read_lock();
1337 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1338 if (likely(ulp_ops)) {
1339 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1340 cqes, num_cqes);
1341 }
1342 rcu_read_unlock();
1343}
1344
1345static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1346{
1347 struct cnic_local *cp = dev->cnic_priv;
1348 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001349 int hq_bds, pages;
1350 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001351
1352 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1353 cp->num_ccells = req1->num_ccells_per_conn;
1354 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1355 cp->num_iscsi_tasks;
1356 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1357 BNX2X_ISCSI_R2TQE_SIZE;
1358 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1359 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1360 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1361 cp->num_cqs = req1->num_cqs;
1362
1363 if (!dev->max_iscsi_conn)
1364 return 0;
1365
1366 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001367 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001368 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001369 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001370 PAGE_SIZE);
1371 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001372 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001373 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001374 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001375 req1->num_tasks_per_conn);
1376
1377 /* init Ustorm RAM */
1378 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001379 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001380 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001381 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001382 PAGE_SIZE);
1383 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001384 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001385 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001386 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001387 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001388 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001389 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001390 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001391 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001392 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001393 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1394
1395 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001396 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001397 PAGE_SIZE);
1398 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001399 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001400 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001401 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001402 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001403 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001404 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001405 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001406 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001407 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001408 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1409
1410 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001411 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001412 PAGE_SIZE);
1413 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001414 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001415 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001416 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001417 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001418 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001419 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001420 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001421 hq_bds);
1422
1423 return 0;
1424}
1425
1426static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1427{
1428 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1429 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001430 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001431 struct iscsi_kcqe kcqe;
1432 struct kcqe *cqes[1];
1433
1434 memset(&kcqe, 0, sizeof(kcqe));
1435 if (!dev->max_iscsi_conn) {
1436 kcqe.completion_status =
1437 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1438 goto done;
1439 }
1440
1441 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001442 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001443 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001444 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001445 req2->error_bit_map[1]);
1446
1447 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001448 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001449 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001450 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001451 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001452 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001453 req2->error_bit_map[1]);
1454
1455 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001456 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001457
1458 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1459
1460done:
1461 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1462 cqes[0] = (struct kcqe *) &kcqe;
1463 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1464
1465 return 0;
1466}
1467
1468static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1469{
1470 struct cnic_local *cp = dev->cnic_priv;
1471 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1472
1473 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1474 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1475
1476 cnic_free_dma(dev, &iscsi->hq_info);
1477 cnic_free_dma(dev, &iscsi->r2tq_info);
1478 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001479 cnic_free_id(&cp->cid_tbl, ctx->cid);
1480 } else {
1481 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001482 }
Michael Chane1928c82010-12-23 07:43:04 +00001483
Michael Chan71034ba2009-10-10 13:46:59 +00001484 ctx->cid = 0;
1485}
1486
1487static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1488{
1489 u32 cid;
1490 int ret, pages;
1491 struct cnic_local *cp = dev->cnic_priv;
1492 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1493 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1494
Michael Chane1928c82010-12-23 07:43:04 +00001495 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1496 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1497 if (cid == -1) {
1498 ret = -ENOMEM;
1499 goto error;
1500 }
1501 ctx->cid = cid;
1502 return 0;
1503 }
1504
Michael Chan71034ba2009-10-10 13:46:59 +00001505 cid = cnic_alloc_new_id(&cp->cid_tbl);
1506 if (cid == -1) {
1507 ret = -ENOMEM;
1508 goto error;
1509 }
1510
1511 ctx->cid = cid;
1512 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1513
1514 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1515 if (ret)
1516 goto error;
1517
1518 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1519 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1520 if (ret)
1521 goto error;
1522
1523 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1524 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1525 if (ret)
1526 goto error;
1527
1528 return 0;
1529
1530error:
1531 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1532 return ret;
1533}
1534
1535static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1536 struct regpair *ctx_addr)
1537{
1538 struct cnic_local *cp = dev->cnic_priv;
1539 struct cnic_eth_dev *ethdev = cp->ethdev;
1540 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1541 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1542 unsigned long align_off = 0;
1543 dma_addr_t ctx_map;
1544 void *ctx;
1545
1546 if (cp->ctx_align) {
1547 unsigned long mask = cp->ctx_align - 1;
1548
1549 if (cp->ctx_arr[blk].mapping & mask)
1550 align_off = cp->ctx_align -
1551 (cp->ctx_arr[blk].mapping & mask);
1552 }
1553 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1554 (off * BNX2X_CONTEXT_MEM_SIZE);
1555 ctx = cp->ctx_arr[blk].ctx + align_off +
1556 (off * BNX2X_CONTEXT_MEM_SIZE);
1557 if (init)
1558 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1559
1560 ctx_addr->lo = ctx_map & 0xffffffff;
1561 ctx_addr->hi = (u64) ctx_map >> 32;
1562 return ctx;
1563}
1564
1565static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1566 u32 num)
1567{
1568 struct cnic_local *cp = dev->cnic_priv;
1569 struct iscsi_kwqe_conn_offload1 *req1 =
1570 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1571 struct iscsi_kwqe_conn_offload2 *req2 =
1572 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1573 struct iscsi_kwqe_conn_offload3 *req3;
1574 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1575 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1576 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001577 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001578 struct iscsi_context *ictx;
1579 struct regpair context_addr;
1580 int i, j, n = 2, n_max;
1581
1582 ctx->ctx_flags = 0;
1583 if (!req2->num_additional_wqes)
1584 return -EINVAL;
1585
1586 n_max = req2->num_additional_wqes + 2;
1587
1588 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1589 if (ictx == NULL)
1590 return -ENOMEM;
1591
1592 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1593
1594 ictx->xstorm_ag_context.hq_prod = 1;
1595
1596 ictx->xstorm_st_context.iscsi.first_burst_length =
1597 ISCSI_DEF_FIRST_BURST_LEN;
1598 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1599 ISCSI_DEF_MAX_RECV_SEG_LEN;
1600 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1601 req1->sq_page_table_addr_lo;
1602 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1603 req1->sq_page_table_addr_hi;
1604 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1605 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1606 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1607 iscsi->hq_info.pgtbl_map & 0xffffffff;
1608 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1609 (u64) iscsi->hq_info.pgtbl_map >> 32;
1610 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1611 iscsi->hq_info.pgtbl[0];
1612 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1613 iscsi->hq_info.pgtbl[1];
1614 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1615 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1616 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1617 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1618 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1619 iscsi->r2tq_info.pgtbl[0];
1620 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1621 iscsi->r2tq_info.pgtbl[1];
1622 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1623 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1624 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1625 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1626 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1627 BNX2X_ISCSI_PBL_NOT_CACHED;
1628 ictx->xstorm_st_context.iscsi.flags.flags |=
1629 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1630 ictx->xstorm_st_context.iscsi.flags.flags |=
1631 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1632
1633 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1634 /* TSTORM requires the base address of RQ DB & not PTE */
1635 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1636 req2->rq_page_table_addr_lo & PAGE_MASK;
1637 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1638 req2->rq_page_table_addr_hi;
1639 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1640 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1641 ictx->tstorm_st_context.tcp.flags2 |=
1642 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001643 ictx->tstorm_st_context.tcp.ooo_support_mode =
1644 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001645
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001646 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001647
1648 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001649 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001650 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001651 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001652 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1653 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1654 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1655 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1656 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1657 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1658 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1659 iscsi->r2tq_info.pgtbl[0];
1660 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1661 iscsi->r2tq_info.pgtbl[1];
1662 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1663 req1->cq_page_table_addr_lo;
1664 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1665 req1->cq_page_table_addr_hi;
1666 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1667 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1668 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1669 ictx->ustorm_st_context.task_pbe_cache_index =
1670 BNX2X_ISCSI_PBL_NOT_CACHED;
1671 ictx->ustorm_st_context.task_pdu_cache_index =
1672 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1673
1674 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1675 if (j == 3) {
1676 if (n >= n_max)
1677 break;
1678 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1679 j = 0;
1680 }
1681 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1682 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1683 req3->qp_first_pte[j].hi;
1684 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1685 req3->qp_first_pte[j].lo;
1686 }
1687
1688 ictx->ustorm_st_context.task_pbl_base.lo =
1689 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1690 ictx->ustorm_st_context.task_pbl_base.hi =
1691 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1692 ictx->ustorm_st_context.tce_phy_addr.lo =
1693 iscsi->task_array_info.pgtbl[0];
1694 ictx->ustorm_st_context.tce_phy_addr.hi =
1695 iscsi->task_array_info.pgtbl[1];
1696 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1697 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1698 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1699 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1700 ISCSI_DEF_MAX_BURST_LEN;
1701 ictx->ustorm_st_context.negotiated_rx |=
1702 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1703 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1704
1705 ictx->cstorm_st_context.hq_pbl_base.lo =
1706 iscsi->hq_info.pgtbl_map & 0xffffffff;
1707 ictx->cstorm_st_context.hq_pbl_base.hi =
1708 (u64) iscsi->hq_info.pgtbl_map >> 32;
1709 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1710 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1711 ictx->cstorm_st_context.task_pbl_base.lo =
1712 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1713 ictx->cstorm_st_context.task_pbl_base.hi =
1714 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1715 /* CSTORM and USTORM initialization is different, CSTORM requires
1716 * CQ DB base & not PTE addr */
1717 ictx->cstorm_st_context.cq_db_base.lo =
1718 req1->cq_page_table_addr_lo & PAGE_MASK;
1719 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1720 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1721 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1722 for (i = 0; i < cp->num_cqs; i++) {
1723 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1724 ISCSI_INITIAL_SN;
1725 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1726 ISCSI_INITIAL_SN;
1727 }
1728
1729 ictx->xstorm_ag_context.cdu_reserved =
1730 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1731 ISCSI_CONNECTION_TYPE);
1732 ictx->ustorm_ag_context.cdu_usage =
1733 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1734 ISCSI_CONNECTION_TYPE);
1735 return 0;
1736
1737}
1738
1739static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1740 u32 num, int *work)
1741{
1742 struct iscsi_kwqe_conn_offload1 *req1;
1743 struct iscsi_kwqe_conn_offload2 *req2;
1744 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001745 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001746 struct iscsi_kcqe kcqe;
1747 struct kcqe *cqes[1];
1748 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001749 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001750
1751 if (num < 2) {
1752 *work = num;
1753 return -EINVAL;
1754 }
1755
1756 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1757 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1758 if ((num - 2) < req2->num_additional_wqes) {
1759 *work = num;
1760 return -EINVAL;
1761 }
Joe Perches779bb412010-11-14 17:04:37 +00001762 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001763
1764 l5_cid = req1->iscsi_conn_id;
1765 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1766 return -EINVAL;
1767
1768 memset(&kcqe, 0, sizeof(kcqe));
1769 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1770 kcqe.iscsi_conn_id = l5_cid;
1771 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1772
Michael Chanfdf24082010-10-13 14:06:47 +00001773 ctx = &cp->ctx_tbl[l5_cid];
1774 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1775 kcqe.completion_status =
1776 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1777 goto done;
1778 }
1779
Michael Chan71034ba2009-10-10 13:46:59 +00001780 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1781 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001782 goto done;
1783 }
1784 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1785 if (ret) {
1786 atomic_dec(&cp->iscsi_conn);
1787 ret = 0;
1788 goto done;
1789 }
1790 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1791 if (ret < 0) {
1792 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1793 atomic_dec(&cp->iscsi_conn);
1794 goto done;
1795 }
1796
1797 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001798 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001799
1800done:
1801 cqes[0] = (struct kcqe *) &kcqe;
1802 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1803 return ret;
1804}
1805
1806
1807static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1808{
1809 struct cnic_local *cp = dev->cnic_priv;
1810 struct iscsi_kwqe_conn_update *req =
1811 (struct iscsi_kwqe_conn_update *) kwqe;
1812 void *data;
1813 union l5cm_specific_data l5_data;
1814 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1815 int ret;
1816
1817 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1818 return -EINVAL;
1819
1820 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1821 if (!data)
1822 return -ENOMEM;
1823
1824 memcpy(data, kwqe, sizeof(struct kwqe));
1825
1826 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1827 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1828 return ret;
1829}
1830
Michael Chana2c9e762010-10-13 14:06:46 +00001831static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001832{
1833 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001834 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001835 union l5cm_specific_data l5_data;
1836 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001837 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001838
Michael Chan71034ba2009-10-10 13:46:59 +00001839 init_waitqueue_head(&ctx->waitq);
1840 ctx->wait_cond = 0;
1841 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001842 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001843
1844 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001845 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001846
Michael Chan71034ba2009-10-10 13:46:59 +00001847 if (ret == 0)
1848 wait_event(ctx->waitq, ctx->wait_cond);
1849
Michael Chana2c9e762010-10-13 14:06:46 +00001850 return ret;
1851}
1852
1853static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1854{
1855 struct cnic_local *cp = dev->cnic_priv;
1856 struct iscsi_kwqe_conn_destroy *req =
1857 (struct iscsi_kwqe_conn_destroy *) kwqe;
1858 u32 l5_cid = req->reserved0;
1859 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1860 int ret = 0;
1861 struct iscsi_kcqe kcqe;
1862 struct kcqe *cqes[1];
1863
1864 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1865 goto skip_cfc_delete;
1866
Michael Chanfdf24082010-10-13 14:06:47 +00001867 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1868 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1869
1870 if (delta > (2 * HZ))
1871 delta = 0;
1872
1873 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1874 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1875 goto destroy_reply;
1876 }
Michael Chana2c9e762010-10-13 14:06:46 +00001877
1878 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1879
Michael Chan71034ba2009-10-10 13:46:59 +00001880skip_cfc_delete:
1881 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1882
1883 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00001884 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001885
Michael Chanfdf24082010-10-13 14:06:47 +00001886destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001887 memset(&kcqe, 0, sizeof(kcqe));
1888 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1889 kcqe.iscsi_conn_id = l5_cid;
1890 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1891 kcqe.iscsi_conn_context_id = req->context_id;
1892
1893 cqes[0] = (struct kcqe *) &kcqe;
1894 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1895
1896 return ret;
1897}
1898
1899static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1900 struct l4_kwq_connect_req1 *kwqe1,
1901 struct l4_kwq_connect_req3 *kwqe3,
1902 struct l5cm_active_conn_buffer *conn_buf)
1903{
1904 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1905 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1906 &conn_buf->xstorm_conn_buffer;
1907 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1908 &conn_buf->tstorm_conn_buffer;
1909 struct regpair context_addr;
1910 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1911 struct in6_addr src_ip, dst_ip;
1912 int i;
1913 u32 *addrp;
1914
1915 addrp = (u32 *) &conn_addr->local_ip_addr;
1916 for (i = 0; i < 4; i++, addrp++)
1917 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1918
1919 addrp = (u32 *) &conn_addr->remote_ip_addr;
1920 for (i = 0; i < 4; i++, addrp++)
1921 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1922
1923 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1924
1925 xstorm_buf->context_addr.hi = context_addr.hi;
1926 xstorm_buf->context_addr.lo = context_addr.lo;
1927 xstorm_buf->mss = 0xffff;
1928 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1929 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1930 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1931 xstorm_buf->pseudo_header_checksum =
1932 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1933
1934 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1935 tstorm_buf->params |=
1936 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1937 if (kwqe3->ka_timeout) {
1938 tstorm_buf->ka_enable = 1;
1939 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1940 tstorm_buf->ka_interval = kwqe3->ka_interval;
1941 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1942 }
1943 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1944 tstorm_buf->snd_buf = kwqe3->snd_buf;
1945 tstorm_buf->max_rt_time = 0xffffffff;
1946}
1947
1948static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1949{
1950 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001951 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001952 u8 *mac = dev->mac_addr;
1953
1954 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001955 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001956 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001957 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001958 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001959 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001960 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001961 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001962 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001963 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001964 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001965 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001966
1967 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001968 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001969 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001970 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001971 mac[4]);
1972 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001973 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001974 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001975 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001976 mac[2]);
1977 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001978 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00001979 mac[1]);
1980 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001981 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00001982 mac[0]);
1983}
1984
1985static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1986{
1987 struct cnic_local *cp = dev->cnic_priv;
1988 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1989 u16 tstorm_flags = 0;
1990
1991 if (tcp_ts) {
1992 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1993 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1994 }
1995
1996 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001997 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001998
1999 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002000 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002001}
2002
2003static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2004 u32 num, int *work)
2005{
2006 struct cnic_local *cp = dev->cnic_priv;
2007 struct l4_kwq_connect_req1 *kwqe1 =
2008 (struct l4_kwq_connect_req1 *) wqes[0];
2009 struct l4_kwq_connect_req3 *kwqe3;
2010 struct l5cm_active_conn_buffer *conn_buf;
2011 struct l5cm_conn_addr_params *conn_addr;
2012 union l5cm_specific_data l5_data;
2013 u32 l5_cid = kwqe1->pg_cid;
2014 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2015 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2016 int ret;
2017
2018 if (num < 2) {
2019 *work = num;
2020 return -EINVAL;
2021 }
2022
2023 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2024 *work = 3;
2025 else
2026 *work = 2;
2027
2028 if (num < *work) {
2029 *work = num;
2030 return -EINVAL;
2031 }
2032
2033 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002034 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002035 return -ENOMEM;
2036 }
2037 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2038 if (!conn_buf)
2039 return -ENOMEM;
2040
2041 memset(conn_buf, 0, sizeof(*conn_buf));
2042
2043 conn_addr = &conn_buf->conn_addr_buf;
2044 conn_addr->remote_addr_0 = csk->ha[0];
2045 conn_addr->remote_addr_1 = csk->ha[1];
2046 conn_addr->remote_addr_2 = csk->ha[2];
2047 conn_addr->remote_addr_3 = csk->ha[3];
2048 conn_addr->remote_addr_4 = csk->ha[4];
2049 conn_addr->remote_addr_5 = csk->ha[5];
2050
2051 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2052 struct l4_kwq_connect_req2 *kwqe2 =
2053 (struct l4_kwq_connect_req2 *) wqes[1];
2054
2055 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2056 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2057 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2058
2059 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2060 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2061 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2062 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2063 }
2064 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2065
2066 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2067 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2068 conn_addr->local_tcp_port = kwqe1->src_port;
2069 conn_addr->remote_tcp_port = kwqe1->dst_port;
2070
2071 conn_addr->pmtu = kwqe3->pmtu;
2072 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2073
2074 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002075 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002076
2077 cnic_bnx2x_set_tcp_timestamp(dev,
2078 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2079
2080 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2081 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2082 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002083 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002084
2085 return ret;
2086}
2087
2088static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2089{
2090 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2091 union l5cm_specific_data l5_data;
2092 int ret;
2093
2094 memset(&l5_data, 0, sizeof(l5_data));
2095 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2096 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2097 return ret;
2098}
2099
2100static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2101{
2102 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2103 union l5cm_specific_data l5_data;
2104 int ret;
2105
2106 memset(&l5_data, 0, sizeof(l5_data));
2107 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2108 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2109 return ret;
2110}
2111static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2112{
2113 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2114 struct l4_kcq kcqe;
2115 struct kcqe *cqes[1];
2116
2117 memset(&kcqe, 0, sizeof(kcqe));
2118 kcqe.pg_host_opaque = req->host_opaque;
2119 kcqe.pg_cid = req->host_opaque;
2120 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2121 cqes[0] = (struct kcqe *) &kcqe;
2122 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2123 return 0;
2124}
2125
2126static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2127{
2128 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2129 struct l4_kcq kcqe;
2130 struct kcqe *cqes[1];
2131
2132 memset(&kcqe, 0, sizeof(kcqe));
2133 kcqe.pg_host_opaque = req->pg_host_opaque;
2134 kcqe.pg_cid = req->pg_cid;
2135 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2136 cqes[0] = (struct kcqe *) &kcqe;
2137 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2138 return 0;
2139}
2140
Michael Chane1928c82010-12-23 07:43:04 +00002141static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2142{
2143 struct fcoe_kwqe_stat *req;
2144 struct fcoe_stat_ramrod_params *fcoe_stat;
2145 union l5cm_specific_data l5_data;
2146 struct cnic_local *cp = dev->cnic_priv;
2147 int ret;
2148 u32 cid;
2149
2150 req = (struct fcoe_kwqe_stat *) kwqe;
2151 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2152
2153 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2154 if (!fcoe_stat)
2155 return -ENOMEM;
2156
2157 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2158 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2159
2160 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT, cid,
2161 FCOE_CONNECTION_TYPE, &l5_data);
2162 return ret;
2163}
2164
2165static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2166 u32 num, int *work)
2167{
2168 int ret;
2169 struct cnic_local *cp = dev->cnic_priv;
2170 u32 cid;
2171 struct fcoe_init_ramrod_params *fcoe_init;
2172 struct fcoe_kwqe_init1 *req1;
2173 struct fcoe_kwqe_init2 *req2;
2174 struct fcoe_kwqe_init3 *req3;
2175 union l5cm_specific_data l5_data;
2176
2177 if (num < 3) {
2178 *work = num;
2179 return -EINVAL;
2180 }
2181 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2182 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2183 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2184 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2185 *work = 1;
2186 return -EINVAL;
2187 }
2188 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2189 *work = 2;
2190 return -EINVAL;
2191 }
2192
2193 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2194 netdev_err(dev->netdev, "fcoe_init size too big\n");
2195 return -ENOMEM;
2196 }
2197 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2198 if (!fcoe_init)
2199 return -ENOMEM;
2200
2201 memset(fcoe_init, 0, sizeof(*fcoe_init));
2202 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2203 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2204 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
2205 fcoe_init->eq_addr.lo = cp->kcq2.dma.pg_map_arr[0] & 0xffffffff;
2206 fcoe_init->eq_addr.hi = (u64) cp->kcq2.dma.pg_map_arr[0] >> 32;
2207 fcoe_init->eq_next_page_addr.lo =
2208 cp->kcq2.dma.pg_map_arr[1] & 0xffffffff;
2209 fcoe_init->eq_next_page_addr.hi =
2210 (u64) cp->kcq2.dma.pg_map_arr[1] >> 32;
2211
2212 fcoe_init->sb_num = cp->status_blk_num;
2213 fcoe_init->eq_prod = MAX_KCQ_IDX;
2214 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2215 cp->kcq2.sw_prod_idx = 0;
2216
2217 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Michael Chane1928c82010-12-23 07:43:04 +00002218 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT, cid,
2219 FCOE_CONNECTION_TYPE, &l5_data);
2220 *work = 3;
2221 return ret;
2222}
2223
2224static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2225 u32 num, int *work)
2226{
2227 int ret = 0;
2228 u32 cid = -1, l5_cid;
2229 struct cnic_local *cp = dev->cnic_priv;
2230 struct fcoe_kwqe_conn_offload1 *req1;
2231 struct fcoe_kwqe_conn_offload2 *req2;
2232 struct fcoe_kwqe_conn_offload3 *req3;
2233 struct fcoe_kwqe_conn_offload4 *req4;
2234 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2235 struct cnic_context *ctx;
2236 struct fcoe_context *fctx;
2237 struct regpair ctx_addr;
2238 union l5cm_specific_data l5_data;
2239 struct fcoe_kcqe kcqe;
2240 struct kcqe *cqes[1];
2241
2242 if (num < 4) {
2243 *work = num;
2244 return -EINVAL;
2245 }
2246 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2247 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2248 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2249 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2250
2251 *work = 4;
2252
2253 l5_cid = req1->fcoe_conn_id;
2254 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2255 goto err_reply;
2256
2257 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2258
2259 ctx = &cp->ctx_tbl[l5_cid];
2260 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2261 goto err_reply;
2262
2263 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2264 if (ret) {
2265 ret = 0;
2266 goto err_reply;
2267 }
2268 cid = ctx->cid;
2269
2270 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2271 if (fctx) {
2272 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2273 u32 val;
2274
2275 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2276 FCOE_CONNECTION_TYPE);
2277 fctx->xstorm_ag_context.cdu_reserved = val;
2278 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2279 FCOE_CONNECTION_TYPE);
2280 fctx->ustorm_ag_context.cdu_usage = val;
2281 }
2282 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2283 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2284 goto err_reply;
2285 }
2286 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2287 if (!fcoe_offload)
2288 goto err_reply;
2289
2290 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2291 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2292 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2293 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2294 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2295
2296 cid = BNX2X_HW_CID(cp, cid);
2297 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2298 FCOE_CONNECTION_TYPE, &l5_data);
2299 if (!ret)
2300 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2301
2302 return ret;
2303
2304err_reply:
2305 if (cid != -1)
2306 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2307
2308 memset(&kcqe, 0, sizeof(kcqe));
2309 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2310 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2311 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2312
2313 cqes[0] = (struct kcqe *) &kcqe;
2314 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2315 return ret;
2316}
2317
2318static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2319{
2320 struct fcoe_kwqe_conn_enable_disable *req;
2321 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2322 union l5cm_specific_data l5_data;
2323 int ret;
2324 u32 cid, l5_cid;
2325 struct cnic_local *cp = dev->cnic_priv;
2326
2327 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2328 cid = req->context_id;
2329 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2330
2331 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2332 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2333 return -ENOMEM;
2334 }
2335 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2336 if (!fcoe_enable)
2337 return -ENOMEM;
2338
2339 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2340 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2341 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2342 FCOE_CONNECTION_TYPE, &l5_data);
2343 return ret;
2344}
2345
2346static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2347{
2348 struct fcoe_kwqe_conn_enable_disable *req;
2349 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2350 union l5cm_specific_data l5_data;
2351 int ret;
2352 u32 cid, l5_cid;
2353 struct cnic_local *cp = dev->cnic_priv;
2354
2355 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2356 cid = req->context_id;
2357 l5_cid = req->conn_id;
2358 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2359 return -EINVAL;
2360
2361 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2362
2363 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2364 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2365 return -ENOMEM;
2366 }
2367 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2368 if (!fcoe_disable)
2369 return -ENOMEM;
2370
2371 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2372 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2373 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2374 FCOE_CONNECTION_TYPE, &l5_data);
2375 return ret;
2376}
2377
2378static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2379{
2380 struct fcoe_kwqe_conn_destroy *req;
2381 union l5cm_specific_data l5_data;
2382 int ret;
2383 u32 cid, l5_cid;
2384 struct cnic_local *cp = dev->cnic_priv;
2385 struct cnic_context *ctx;
2386 struct fcoe_kcqe kcqe;
2387 struct kcqe *cqes[1];
2388
2389 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2390 cid = req->context_id;
2391 l5_cid = req->conn_id;
2392 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2393 return -EINVAL;
2394
2395 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2396
2397 ctx = &cp->ctx_tbl[l5_cid];
2398
2399 init_waitqueue_head(&ctx->waitq);
2400 ctx->wait_cond = 0;
2401
2402 memset(&l5_data, 0, sizeof(l5_data));
2403 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2404 FCOE_CONNECTION_TYPE, &l5_data);
2405 if (ret == 0) {
2406 wait_event(ctx->waitq, ctx->wait_cond);
2407 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2408 queue_delayed_work(cnic_wq, &cp->delete_task,
2409 msecs_to_jiffies(2000));
2410 }
2411
2412 memset(&kcqe, 0, sizeof(kcqe));
2413 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2414 kcqe.fcoe_conn_id = req->conn_id;
2415 kcqe.fcoe_conn_context_id = cid;
2416
2417 cqes[0] = (struct kcqe *) &kcqe;
2418 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2419 return ret;
2420}
2421
2422static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2423{
2424 struct fcoe_kwqe_destroy *req;
2425 union l5cm_specific_data l5_data;
2426 struct cnic_local *cp = dev->cnic_priv;
2427 int ret;
2428 u32 cid;
2429
2430 req = (struct fcoe_kwqe_destroy *) kwqe;
2431 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2432
2433 memset(&l5_data, 0, sizeof(l5_data));
2434 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY, cid,
2435 FCOE_CONNECTION_TYPE, &l5_data);
2436 return ret;
2437}
2438
2439static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2440 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002441{
2442 int i, work, ret;
2443 u32 opcode;
2444 struct kwqe *kwqe;
2445
2446 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2447 return -EAGAIN; /* bnx2 is down */
2448
2449 for (i = 0; i < num_wqes; ) {
2450 kwqe = wqes[i];
2451 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2452 work = 1;
2453
2454 switch (opcode) {
2455 case ISCSI_KWQE_OPCODE_INIT1:
2456 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2457 break;
2458 case ISCSI_KWQE_OPCODE_INIT2:
2459 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2460 break;
2461 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2462 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2463 num_wqes - i, &work);
2464 break;
2465 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2466 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2467 break;
2468 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2469 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2470 break;
2471 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2472 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2473 &work);
2474 break;
2475 case L4_KWQE_OPCODE_VALUE_CLOSE:
2476 ret = cnic_bnx2x_close(dev, kwqe);
2477 break;
2478 case L4_KWQE_OPCODE_VALUE_RESET:
2479 ret = cnic_bnx2x_reset(dev, kwqe);
2480 break;
2481 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2482 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2483 break;
2484 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2485 ret = cnic_bnx2x_update_pg(dev, kwqe);
2486 break;
2487 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2488 ret = 0;
2489 break;
2490 default:
2491 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002492 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2493 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002494 break;
2495 }
2496 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002497 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2498 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002499 i += work;
2500 }
2501 return 0;
2502}
2503
Michael Chane1928c82010-12-23 07:43:04 +00002504static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2505 struct kwqe *wqes[], u32 num_wqes)
2506{
2507 struct cnic_local *cp = dev->cnic_priv;
2508 int i, work, ret;
2509 u32 opcode;
2510 struct kwqe *kwqe;
2511
2512 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2513 return -EAGAIN; /* bnx2 is down */
2514
2515 if (BNX2X_CHIP_NUM(cp->chip_id) == BNX2X_CHIP_NUM_57710)
2516 return -EINVAL;
2517
2518 for (i = 0; i < num_wqes; ) {
2519 kwqe = wqes[i];
2520 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2521 work = 1;
2522
2523 switch (opcode) {
2524 case FCOE_KWQE_OPCODE_INIT1:
2525 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2526 num_wqes - i, &work);
2527 break;
2528 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2529 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2530 num_wqes - i, &work);
2531 break;
2532 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2533 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2534 break;
2535 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2536 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2537 break;
2538 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2539 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2540 break;
2541 case FCOE_KWQE_OPCODE_DESTROY:
2542 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2543 break;
2544 case FCOE_KWQE_OPCODE_STAT:
2545 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2546 break;
2547 default:
2548 ret = 0;
2549 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2550 opcode);
2551 break;
2552 }
2553 if (ret < 0)
2554 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2555 opcode);
2556 i += work;
2557 }
2558 return 0;
2559}
2560
2561static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2562 u32 num_wqes)
2563{
2564 int ret = -EINVAL;
2565 u32 layer_code;
2566
2567 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2568 return -EAGAIN; /* bnx2x is down */
2569
2570 if (!num_wqes)
2571 return 0;
2572
2573 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2574 switch (layer_code) {
2575 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2576 case KWQE_FLAGS_LAYER_MASK_L4:
2577 case KWQE_FLAGS_LAYER_MASK_L2:
2578 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2579 break;
2580
2581 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2582 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2583 break;
2584 }
2585 return ret;
2586}
2587
2588static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2589{
2590 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2591 return KCQE_FLAGS_LAYER_MASK_L4;
2592
2593 return opflag & KCQE_FLAGS_LAYER_MASK;
2594}
2595
Michael Chana4636962009-06-08 18:14:43 -07002596static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2597{
2598 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002599 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002600
2601 i = 0;
2602 j = 1;
2603 while (num_cqes) {
2604 struct cnic_ulp_ops *ulp_ops;
2605 int ulp_type;
2606 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002607 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002608
2609 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002610 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002611
2612 while (j < num_cqes) {
2613 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2614
Michael Chane1928c82010-12-23 07:43:04 +00002615 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002616 break;
2617
2618 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002619 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002620 j++;
2621 }
2622
2623 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2624 ulp_type = CNIC_ULP_RDMA;
2625 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2626 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002627 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2628 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002629 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2630 ulp_type = CNIC_ULP_L4;
2631 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2632 goto end;
2633 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002634 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2635 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002636 goto end;
2637 }
2638
2639 rcu_read_lock();
2640 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2641 if (likely(ulp_ops)) {
2642 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2643 cp->completed_kcq + i, j);
2644 }
2645 rcu_read_unlock();
2646end:
2647 num_cqes -= j;
2648 i += j;
2649 j = 1;
2650 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002651 if (unlikely(comp))
2652 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002653}
2654
2655static u16 cnic_bnx2_next_idx(u16 idx)
2656{
2657 return idx + 1;
2658}
2659
2660static u16 cnic_bnx2_hw_idx(u16 idx)
2661{
2662 return idx;
2663}
2664
Michael Chan71034ba2009-10-10 13:46:59 +00002665static u16 cnic_bnx2x_next_idx(u16 idx)
2666{
2667 idx++;
2668 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2669 idx++;
2670
2671 return idx;
2672}
2673
2674static u16 cnic_bnx2x_hw_idx(u16 idx)
2675{
2676 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2677 idx++;
2678 return idx;
2679}
2680
Michael Chan644b9d42010-06-24 14:58:40 +00002681static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002682{
2683 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002684 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002685 struct kcqe *kcqe;
2686 int kcqe_cnt = 0, last_cnt = 0;
2687
Michael Chan644b9d42010-06-24 14:58:40 +00002688 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002689 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002690 hw_prod = *info->hw_prod_idx_ptr;
2691 hw_prod = cp->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002692
2693 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002694 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002695 cp->completed_kcq[kcqe_cnt++] = kcqe;
2696 i = cp->next_idx(i);
2697 ri = i & MAX_KCQ_IDX;
2698 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2699 last_cnt = kcqe_cnt;
2700 last = i;
2701 }
2702 }
2703
Michael Chan644b9d42010-06-24 14:58:40 +00002704 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002705 return last_cnt;
2706}
2707
Michael Chan48f753d2010-05-18 11:32:53 +00002708static int cnic_l2_completion(struct cnic_local *cp)
2709{
2710 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002711 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002712 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002713 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002714 u32 cmd;
2715 int comp = 0;
2716
2717 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2718 return 0;
2719
2720 hw_cons = *cp->rx_cons_ptr;
2721 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2722 hw_cons++;
2723
2724 sw_cons = cp->rx_cons;
2725 while (sw_cons != hw_cons) {
2726 u8 cqe_fp_flags;
2727
2728 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2729 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2730 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2731 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2732 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2733 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2734 cmd == RAMROD_CMD_ID_ETH_HALT)
2735 comp++;
2736 }
2737 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2738 }
2739 return comp;
2740}
2741
Michael Chan86b53602009-10-10 13:46:57 +00002742static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002743{
Michael Chan541a7812010-10-06 03:17:22 +00002744 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002745 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002746
Michael Chan541a7812010-10-06 03:17:22 +00002747 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002748 return;
2749
Michael Chan541a7812010-10-06 03:17:22 +00002750 rx_cons = *cp->rx_cons_ptr;
2751 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002752 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002753 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2754 comp = cnic_l2_completion(cp);
2755
Michael Chana4636962009-06-08 18:14:43 -07002756 cp->tx_cons = tx_cons;
2757 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002758
Michael Chancd801532010-10-13 14:06:49 +00002759 if (cp->udev)
2760 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002761 }
Michael Chan48f753d2010-05-18 11:32:53 +00002762 if (comp)
2763 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002764}
2765
Michael Chanb177a5d52010-06-24 14:58:41 +00002766static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002767{
Michael Chana4636962009-06-08 18:14:43 -07002768 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002769 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002770 int kcqe_cnt;
2771
Michael Chan107c3f42011-03-02 13:00:49 +00002772 /* status block index must be read before reading other fields */
2773 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002774 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2775
Michael Chan644b9d42010-06-24 14:58:40 +00002776 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002777
2778 service_kcqes(dev, kcqe_cnt);
2779
2780 /* Tell compiler that status_blk fields can change. */
2781 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002782 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2783 /* status block index must be read first */
2784 rmb();
2785 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002786 }
2787
Michael Chan644b9d42010-06-24 14:58:40 +00002788 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002789
Michael Chan86b53602009-10-10 13:46:57 +00002790 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002791
Michael Chana4636962009-06-08 18:14:43 -07002792 return status_idx;
2793}
2794
Michael Chanb177a5d52010-06-24 14:58:41 +00002795static int cnic_service_bnx2(void *data, void *status_blk)
2796{
2797 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002798
Michael Chaneaaa6e92010-12-23 08:38:30 +00002799 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2800 struct status_block *sblk = status_blk;
2801
2802 return sblk->status_idx;
2803 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002804
2805 return cnic_service_bnx2_queues(dev);
2806}
2807
Michael Chana4636962009-06-08 18:14:43 -07002808static void cnic_service_bnx2_msix(unsigned long data)
2809{
2810 struct cnic_dev *dev = (struct cnic_dev *) data;
2811 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002812
Michael Chanb177a5d52010-06-24 14:58:41 +00002813 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002814
Michael Chana4636962009-06-08 18:14:43 -07002815 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2816 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2817}
2818
Michael Chan66fee9e2010-06-24 14:58:38 +00002819static void cnic_doirq(struct cnic_dev *dev)
2820{
2821 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002822
2823 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002824 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2825
Michael Chan66fee9e2010-06-24 14:58:38 +00002826 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002827 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002828
2829 tasklet_schedule(&cp->cnic_irq_task);
2830 }
2831}
2832
Michael Chana4636962009-06-08 18:14:43 -07002833static irqreturn_t cnic_irq(int irq, void *dev_instance)
2834{
2835 struct cnic_dev *dev = dev_instance;
2836 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002837
2838 if (cp->ack_int)
2839 cp->ack_int(dev);
2840
Michael Chan66fee9e2010-06-24 14:58:38 +00002841 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002842
2843 return IRQ_HANDLED;
2844}
2845
Michael Chan71034ba2009-10-10 13:46:59 +00002846static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2847 u16 index, u8 op, u8 update)
2848{
2849 struct cnic_local *cp = dev->cnic_priv;
2850 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2851 COMMAND_REG_INT_ACK);
2852 struct igu_ack_register igu_ack;
2853
2854 igu_ack.status_block_index = index;
2855 igu_ack.sb_id_and_flags =
2856 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2857 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2858 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2859 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2860
2861 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2862}
2863
Michael Chanee87a822010-10-13 14:06:51 +00002864static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2865 u16 index, u8 op, u8 update)
2866{
2867 struct igu_regular cmd_data;
2868 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2869
2870 cmd_data.sb_id_and_flags =
2871 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2872 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2873 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2874 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2875
2876
2877 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2878}
2879
Michael Chan71034ba2009-10-10 13:46:59 +00002880static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2881{
2882 struct cnic_local *cp = dev->cnic_priv;
2883
Dmitry Kravkov523224a2010-10-06 03:23:26 +00002884 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00002885 IGU_INT_DISABLE, 0);
2886}
2887
Michael Chanee87a822010-10-13 14:06:51 +00002888static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2889{
2890 struct cnic_local *cp = dev->cnic_priv;
2891
2892 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2893 IGU_INT_DISABLE, 0);
2894}
2895
Michael Chanb177a5d52010-06-24 14:58:41 +00002896static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002897{
Michael Chanb177a5d52010-06-24 14:58:41 +00002898 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002899 int kcqe_cnt;
2900
Michael Chan107c3f42011-03-02 13:00:49 +00002901 /* status block index must be read before reading the KCQ */
2902 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00002903 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002904
2905 service_kcqes(dev, kcqe_cnt);
2906
2907 /* Tell compiler that sblk fields can change. */
2908 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00002909
Michael Chanb177a5d52010-06-24 14:58:41 +00002910 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00002911 /* status block index must be read before reading the KCQ */
2912 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00002913 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002914 return last_status;
2915}
2916
2917static void cnic_service_bnx2x_bh(unsigned long data)
2918{
2919 struct cnic_dev *dev = (struct cnic_dev *) data;
2920 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00002921 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00002922
2923 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2924 return;
2925
Michael Chan0197b082011-03-02 13:00:50 +00002926 while (1) {
2927 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002928
Michael Chan0197b082011-03-02 13:00:50 +00002929 CNIC_WR16(dev, cp->kcq1.io_addr,
2930 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00002931
Michael Chan0197b082011-03-02 13:00:50 +00002932 if (!BNX2X_CHIP_IS_E2(cp->chip_id)) {
2933 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2934 status_idx, IGU_INT_ENABLE, 1);
2935 break;
2936 }
2937
2938 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
2939
2940 if (new_status_idx != status_idx)
2941 continue;
Michael Chane21ba412010-12-23 07:43:03 +00002942
2943 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
2944 MAX_KCQ_IDX);
2945
Michael Chanee87a822010-10-13 14:06:51 +00002946 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2947 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00002948
2949 break;
Michael Chane21ba412010-12-23 07:43:03 +00002950 }
Michael Chan71034ba2009-10-10 13:46:59 +00002951}
2952
2953static int cnic_service_bnx2x(void *data, void *status_blk)
2954{
2955 struct cnic_dev *dev = data;
2956 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002957
Michael Chan66fee9e2010-06-24 14:58:38 +00002958 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2959 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002960
Michael Chan66fee9e2010-06-24 14:58:38 +00002961 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002962
2963 return 0;
2964}
2965
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07002966static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
2967{
2968 struct cnic_ulp_ops *ulp_ops;
2969
2970 if (if_type == CNIC_ULP_ISCSI)
2971 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
2972
2973 mutex_lock(&cnic_lock);
2974 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
2975 lockdep_is_held(&cnic_lock));
2976 if (!ulp_ops) {
2977 mutex_unlock(&cnic_lock);
2978 return;
2979 }
2980 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2981 mutex_unlock(&cnic_lock);
2982
2983 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2984 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
2985
2986 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2987}
2988
Michael Chana4636962009-06-08 18:14:43 -07002989static void cnic_ulp_stop(struct cnic_dev *dev)
2990{
2991 struct cnic_local *cp = dev->cnic_priv;
2992 int if_type;
2993
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07002994 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
2995 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07002996}
2997
2998static void cnic_ulp_start(struct cnic_dev *dev)
2999{
3000 struct cnic_local *cp = dev->cnic_priv;
3001 int if_type;
3002
Michael Chana4636962009-06-08 18:14:43 -07003003 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3004 struct cnic_ulp_ops *ulp_ops;
3005
Michael Chan681dbd72009-08-14 15:49:46 +00003006 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003007 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3008 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003009 if (!ulp_ops || !ulp_ops->cnic_start) {
3010 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003011 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003012 }
3013 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3014 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003015
3016 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3017 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003018
3019 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003020 }
Michael Chana4636962009-06-08 18:14:43 -07003021}
3022
3023static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3024{
3025 struct cnic_dev *dev = data;
3026
3027 switch (info->cmd) {
3028 case CNIC_CTL_STOP_CMD:
3029 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003030
3031 cnic_ulp_stop(dev);
3032 cnic_stop_hw(dev);
3033
Michael Chana4636962009-06-08 18:14:43 -07003034 cnic_put(dev);
3035 break;
3036 case CNIC_CTL_START_CMD:
3037 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003038
3039 if (!cnic_start_hw(dev))
3040 cnic_ulp_start(dev);
3041
Michael Chana4636962009-06-08 18:14:43 -07003042 cnic_put(dev);
3043 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003044 case CNIC_CTL_STOP_ISCSI_CMD: {
3045 struct cnic_local *cp = dev->cnic_priv;
3046 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3047 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3048 break;
3049 }
Michael Chan71034ba2009-10-10 13:46:59 +00003050 case CNIC_CTL_COMPLETION_CMD: {
3051 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
3052 u32 l5_cid;
3053 struct cnic_local *cp = dev->cnic_priv;
3054
3055 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3056 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3057
3058 ctx->wait_cond = 1;
3059 wake_up(&ctx->waitq);
3060 }
3061 break;
3062 }
Michael Chana4636962009-06-08 18:14:43 -07003063 default:
3064 return -EINVAL;
3065 }
3066 return 0;
3067}
3068
3069static void cnic_ulp_init(struct cnic_dev *dev)
3070{
3071 int i;
3072 struct cnic_local *cp = dev->cnic_priv;
3073
Michael Chana4636962009-06-08 18:14:43 -07003074 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3075 struct cnic_ulp_ops *ulp_ops;
3076
Michael Chan7fc1ece2009-08-14 15:49:47 +00003077 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003078 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003079 if (!ulp_ops || !ulp_ops->cnic_init) {
3080 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003081 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003082 }
3083 ulp_get(ulp_ops);
3084 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003085
3086 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3087 ulp_ops->cnic_init(dev);
3088
Michael Chan7fc1ece2009-08-14 15:49:47 +00003089 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003090 }
Michael Chana4636962009-06-08 18:14:43 -07003091}
3092
3093static void cnic_ulp_exit(struct cnic_dev *dev)
3094{
3095 int i;
3096 struct cnic_local *cp = dev->cnic_priv;
3097
Michael Chana4636962009-06-08 18:14:43 -07003098 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3099 struct cnic_ulp_ops *ulp_ops;
3100
Michael Chan7fc1ece2009-08-14 15:49:47 +00003101 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003102 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003103 if (!ulp_ops || !ulp_ops->cnic_exit) {
3104 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003105 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003106 }
3107 ulp_get(ulp_ops);
3108 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003109
3110 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3111 ulp_ops->cnic_exit(dev);
3112
Michael Chan7fc1ece2009-08-14 15:49:47 +00003113 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003114 }
Michael Chana4636962009-06-08 18:14:43 -07003115}
3116
3117static int cnic_cm_offload_pg(struct cnic_sock *csk)
3118{
3119 struct cnic_dev *dev = csk->dev;
3120 struct l4_kwq_offload_pg *l4kwqe;
3121 struct kwqe *wqes[1];
3122
3123 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3124 memset(l4kwqe, 0, sizeof(*l4kwqe));
3125 wqes[0] = (struct kwqe *) l4kwqe;
3126
3127 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3128 l4kwqe->flags =
3129 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3130 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3131
3132 l4kwqe->da0 = csk->ha[0];
3133 l4kwqe->da1 = csk->ha[1];
3134 l4kwqe->da2 = csk->ha[2];
3135 l4kwqe->da3 = csk->ha[3];
3136 l4kwqe->da4 = csk->ha[4];
3137 l4kwqe->da5 = csk->ha[5];
3138
3139 l4kwqe->sa0 = dev->mac_addr[0];
3140 l4kwqe->sa1 = dev->mac_addr[1];
3141 l4kwqe->sa2 = dev->mac_addr[2];
3142 l4kwqe->sa3 = dev->mac_addr[3];
3143 l4kwqe->sa4 = dev->mac_addr[4];
3144 l4kwqe->sa5 = dev->mac_addr[5];
3145
3146 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003147 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003148 l4kwqe->host_opaque = csk->l5_cid;
3149
3150 if (csk->vlan_id) {
3151 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3152 l4kwqe->vlan_tag = csk->vlan_id;
3153 l4kwqe->l2hdr_nbytes += 4;
3154 }
3155
3156 return dev->submit_kwqes(dev, wqes, 1);
3157}
3158
3159static int cnic_cm_update_pg(struct cnic_sock *csk)
3160{
3161 struct cnic_dev *dev = csk->dev;
3162 struct l4_kwq_update_pg *l4kwqe;
3163 struct kwqe *wqes[1];
3164
3165 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3166 memset(l4kwqe, 0, sizeof(*l4kwqe));
3167 wqes[0] = (struct kwqe *) l4kwqe;
3168
3169 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3170 l4kwqe->flags =
3171 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3172 l4kwqe->pg_cid = csk->pg_cid;
3173
3174 l4kwqe->da0 = csk->ha[0];
3175 l4kwqe->da1 = csk->ha[1];
3176 l4kwqe->da2 = csk->ha[2];
3177 l4kwqe->da3 = csk->ha[3];
3178 l4kwqe->da4 = csk->ha[4];
3179 l4kwqe->da5 = csk->ha[5];
3180
3181 l4kwqe->pg_host_opaque = csk->l5_cid;
3182 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3183
3184 return dev->submit_kwqes(dev, wqes, 1);
3185}
3186
3187static int cnic_cm_upload_pg(struct cnic_sock *csk)
3188{
3189 struct cnic_dev *dev = csk->dev;
3190 struct l4_kwq_upload *l4kwqe;
3191 struct kwqe *wqes[1];
3192
3193 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3194 memset(l4kwqe, 0, sizeof(*l4kwqe));
3195 wqes[0] = (struct kwqe *) l4kwqe;
3196
3197 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3198 l4kwqe->flags =
3199 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3200 l4kwqe->cid = csk->pg_cid;
3201
3202 return dev->submit_kwqes(dev, wqes, 1);
3203}
3204
3205static int cnic_cm_conn_req(struct cnic_sock *csk)
3206{
3207 struct cnic_dev *dev = csk->dev;
3208 struct l4_kwq_connect_req1 *l4kwqe1;
3209 struct l4_kwq_connect_req2 *l4kwqe2;
3210 struct l4_kwq_connect_req3 *l4kwqe3;
3211 struct kwqe *wqes[3];
3212 u8 tcp_flags = 0;
3213 int num_wqes = 2;
3214
3215 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3216 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3217 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3218 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3219 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3220 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3221
3222 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3223 l4kwqe3->flags =
3224 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3225 l4kwqe3->ka_timeout = csk->ka_timeout;
3226 l4kwqe3->ka_interval = csk->ka_interval;
3227 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3228 l4kwqe3->tos = csk->tos;
3229 l4kwqe3->ttl = csk->ttl;
3230 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3231 l4kwqe3->pmtu = csk->mtu;
3232 l4kwqe3->rcv_buf = csk->rcv_buf;
3233 l4kwqe3->snd_buf = csk->snd_buf;
3234 l4kwqe3->seed = csk->seed;
3235
3236 wqes[0] = (struct kwqe *) l4kwqe1;
3237 if (test_bit(SK_F_IPV6, &csk->flags)) {
3238 wqes[1] = (struct kwqe *) l4kwqe2;
3239 wqes[2] = (struct kwqe *) l4kwqe3;
3240 num_wqes = 3;
3241
3242 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3243 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3244 l4kwqe2->flags =
3245 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3246 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3247 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3248 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3249 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3250 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3251 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3252 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3253 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3254 sizeof(struct tcphdr);
3255 } else {
3256 wqes[1] = (struct kwqe *) l4kwqe3;
3257 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3258 sizeof(struct tcphdr);
3259 }
3260
3261 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3262 l4kwqe1->flags =
3263 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3264 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3265 l4kwqe1->cid = csk->cid;
3266 l4kwqe1->pg_cid = csk->pg_cid;
3267 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3268 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3269 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3270 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3271 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3272 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3273 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3274 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3275 if (csk->tcp_flags & SK_TCP_NAGLE)
3276 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3277 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3278 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3279 if (csk->tcp_flags & SK_TCP_SACK)
3280 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3281 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3282 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3283
3284 l4kwqe1->tcp_flags = tcp_flags;
3285
3286 return dev->submit_kwqes(dev, wqes, num_wqes);
3287}
3288
3289static int cnic_cm_close_req(struct cnic_sock *csk)
3290{
3291 struct cnic_dev *dev = csk->dev;
3292 struct l4_kwq_close_req *l4kwqe;
3293 struct kwqe *wqes[1];
3294
3295 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3296 memset(l4kwqe, 0, sizeof(*l4kwqe));
3297 wqes[0] = (struct kwqe *) l4kwqe;
3298
3299 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3300 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3301 l4kwqe->cid = csk->cid;
3302
3303 return dev->submit_kwqes(dev, wqes, 1);
3304}
3305
3306static int cnic_cm_abort_req(struct cnic_sock *csk)
3307{
3308 struct cnic_dev *dev = csk->dev;
3309 struct l4_kwq_reset_req *l4kwqe;
3310 struct kwqe *wqes[1];
3311
3312 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3313 memset(l4kwqe, 0, sizeof(*l4kwqe));
3314 wqes[0] = (struct kwqe *) l4kwqe;
3315
3316 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3317 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3318 l4kwqe->cid = csk->cid;
3319
3320 return dev->submit_kwqes(dev, wqes, 1);
3321}
3322
3323static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3324 u32 l5_cid, struct cnic_sock **csk, void *context)
3325{
3326 struct cnic_local *cp = dev->cnic_priv;
3327 struct cnic_sock *csk1;
3328
3329 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3330 return -EINVAL;
3331
Michael Chanfdf24082010-10-13 14:06:47 +00003332 if (cp->ctx_tbl) {
3333 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3334
3335 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3336 return -EAGAIN;
3337 }
3338
Michael Chana4636962009-06-08 18:14:43 -07003339 csk1 = &cp->csk_tbl[l5_cid];
3340 if (atomic_read(&csk1->ref_count))
3341 return -EAGAIN;
3342
3343 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3344 return -EBUSY;
3345
3346 csk1->dev = dev;
3347 csk1->cid = cid;
3348 csk1->l5_cid = l5_cid;
3349 csk1->ulp_type = ulp_type;
3350 csk1->context = context;
3351
3352 csk1->ka_timeout = DEF_KA_TIMEOUT;
3353 csk1->ka_interval = DEF_KA_INTERVAL;
3354 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3355 csk1->tos = DEF_TOS;
3356 csk1->ttl = DEF_TTL;
3357 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3358 csk1->rcv_buf = DEF_RCV_BUF;
3359 csk1->snd_buf = DEF_SND_BUF;
3360 csk1->seed = DEF_SEED;
3361
3362 *csk = csk1;
3363 return 0;
3364}
3365
3366static void cnic_cm_cleanup(struct cnic_sock *csk)
3367{
3368 if (csk->src_port) {
3369 struct cnic_dev *dev = csk->dev;
3370 struct cnic_local *cp = dev->cnic_priv;
3371
Michael Chan9b093362010-12-23 07:42:56 +00003372 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003373 csk->src_port = 0;
3374 }
3375}
3376
3377static void cnic_close_conn(struct cnic_sock *csk)
3378{
3379 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3380 cnic_cm_upload_pg(csk);
3381 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3382 }
3383 cnic_cm_cleanup(csk);
3384}
3385
3386static int cnic_cm_destroy(struct cnic_sock *csk)
3387{
3388 if (!cnic_in_use(csk))
3389 return -EINVAL;
3390
3391 csk_hold(csk);
3392 clear_bit(SK_F_INUSE, &csk->flags);
3393 smp_mb__after_clear_bit();
3394 while (atomic_read(&csk->ref_count) != 1)
3395 msleep(1);
3396 cnic_cm_cleanup(csk);
3397
3398 csk->flags = 0;
3399 csk_put(csk);
3400 return 0;
3401}
3402
3403static inline u16 cnic_get_vlan(struct net_device *dev,
3404 struct net_device **vlan_dev)
3405{
3406 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3407 *vlan_dev = vlan_dev_real_dev(dev);
3408 return vlan_dev_vlan_id(dev);
3409 }
3410 *vlan_dev = dev;
3411 return 0;
3412}
3413
3414static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3415 struct dst_entry **dst)
3416{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003417#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003418 struct rtable *rt;
3419
David S. Miller78fbfd82011-03-12 00:00:52 -05003420 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3421 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003422 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003423 return 0;
3424 }
3425 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003426#else
3427 return -ENETUNREACH;
3428#endif
Michael Chana4636962009-06-08 18:14:43 -07003429}
3430
3431static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3432 struct dst_entry **dst)
3433{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003434#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003435 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003436
David S. Miller4c9483b2011-03-12 16:22:43 -05003437 memset(&fl6, 0, sizeof(fl6));
3438 ipv6_addr_copy(&fl6.daddr, &dst_addr->sin6_addr);
3439 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3440 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003441
David S. Miller4c9483b2011-03-12 16:22:43 -05003442 *dst = ip6_route_output(&init_net, NULL, &fl6);
Michael Chana4636962009-06-08 18:14:43 -07003443 if (*dst)
3444 return 0;
3445#endif
3446
3447 return -ENETUNREACH;
3448}
3449
3450static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3451 int ulp_type)
3452{
3453 struct cnic_dev *dev = NULL;
3454 struct dst_entry *dst;
3455 struct net_device *netdev = NULL;
3456 int err = -ENETUNREACH;
3457
3458 if (dst_addr->sin_family == AF_INET)
3459 err = cnic_get_v4_route(dst_addr, &dst);
3460 else if (dst_addr->sin_family == AF_INET6) {
3461 struct sockaddr_in6 *dst_addr6 =
3462 (struct sockaddr_in6 *) dst_addr;
3463
3464 err = cnic_get_v6_route(dst_addr6, &dst);
3465 } else
3466 return NULL;
3467
3468 if (err)
3469 return NULL;
3470
3471 if (!dst->dev)
3472 goto done;
3473
3474 cnic_get_vlan(dst->dev, &netdev);
3475
3476 dev = cnic_from_netdev(netdev);
3477
3478done:
3479 dst_release(dst);
3480 if (dev)
3481 cnic_put(dev);
3482 return dev;
3483}
3484
3485static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3486{
3487 struct cnic_dev *dev = csk->dev;
3488 struct cnic_local *cp = dev->cnic_priv;
3489
3490 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3491}
3492
3493static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3494{
3495 struct cnic_dev *dev = csk->dev;
3496 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003497 int is_v6, rc = 0;
3498 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003499 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003500 __be16 local_port;
3501 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003502
3503 if (saddr->local.v6.sin6_family == AF_INET6 &&
3504 saddr->remote.v6.sin6_family == AF_INET6)
3505 is_v6 = 1;
3506 else if (saddr->local.v4.sin_family == AF_INET &&
3507 saddr->remote.v4.sin_family == AF_INET)
3508 is_v6 = 0;
3509 else
3510 return -EINVAL;
3511
3512 clear_bit(SK_F_IPV6, &csk->flags);
3513
3514 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003515 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003516 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003517
3518 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3519 sizeof(struct in6_addr));
3520 csk->dst_port = saddr->remote.v6.sin6_port;
3521 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003522
3523 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003524 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003525
3526 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3527 csk->dst_port = saddr->remote.v4.sin_port;
3528 local_port = saddr->local.v4.sin_port;
3529 }
3530
Michael Chanc76284a2010-02-24 14:42:07 +00003531 csk->vlan_id = 0;
3532 csk->mtu = dev->netdev->mtu;
3533 if (dst && dst->dev) {
3534 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3535 if (realdev == dev->netdev) {
3536 csk->vlan_id = vlan;
3537 csk->mtu = dst_mtu(dst);
3538 }
3539 }
Michael Chana4636962009-06-08 18:14:43 -07003540
Michael Chan9b093362010-12-23 07:42:56 +00003541 port_id = be16_to_cpu(local_port);
3542 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3543 port_id < CNIC_LOCAL_PORT_MAX) {
3544 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3545 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003546 } else
Michael Chan9b093362010-12-23 07:42:56 +00003547 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003548
Michael Chan9b093362010-12-23 07:42:56 +00003549 if (!port_id) {
3550 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3551 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003552 rc = -ENOMEM;
3553 goto err_out;
3554 }
Michael Chan9b093362010-12-23 07:42:56 +00003555 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003556 }
3557 csk->src_port = local_port;
3558
Michael Chana4636962009-06-08 18:14:43 -07003559err_out:
3560 dst_release(dst);
3561 return rc;
3562}
3563
3564static void cnic_init_csk_state(struct cnic_sock *csk)
3565{
3566 csk->state = 0;
3567 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3568 clear_bit(SK_F_CLOSING, &csk->flags);
3569}
3570
3571static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3572{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003573 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003574 int err = 0;
3575
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003576 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3577 return -EOPNOTSUPP;
3578
Michael Chana4636962009-06-08 18:14:43 -07003579 if (!cnic_in_use(csk))
3580 return -EINVAL;
3581
3582 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3583 return -EINVAL;
3584
3585 cnic_init_csk_state(csk);
3586
3587 err = cnic_get_route(csk, saddr);
3588 if (err)
3589 goto err_out;
3590
3591 err = cnic_resolve_addr(csk, saddr);
3592 if (!err)
3593 return 0;
3594
3595err_out:
3596 clear_bit(SK_F_CONNECT_START, &csk->flags);
3597 return err;
3598}
3599
3600static int cnic_cm_abort(struct cnic_sock *csk)
3601{
3602 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003603 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003604
3605 if (!cnic_in_use(csk))
3606 return -EINVAL;
3607
3608 if (cnic_abort_prep(csk))
3609 return cnic_cm_abort_req(csk);
3610
3611 /* Getting here means that we haven't started connect, or
3612 * connect was not successful.
3613 */
3614
Michael Chana4636962009-06-08 18:14:43 -07003615 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003616 if (csk->state != opcode)
3617 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003618
3619 return 0;
3620}
3621
3622static int cnic_cm_close(struct cnic_sock *csk)
3623{
3624 if (!cnic_in_use(csk))
3625 return -EINVAL;
3626
3627 if (cnic_close_prep(csk)) {
3628 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3629 return cnic_cm_close_req(csk);
Michael Chaned99daa2010-06-15 08:57:00 +00003630 } else {
3631 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003632 }
3633 return 0;
3634}
3635
3636static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3637 u8 opcode)
3638{
3639 struct cnic_ulp_ops *ulp_ops;
3640 int ulp_type = csk->ulp_type;
3641
3642 rcu_read_lock();
3643 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3644 if (ulp_ops) {
3645 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3646 ulp_ops->cm_connect_complete(csk);
3647 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3648 ulp_ops->cm_close_complete(csk);
3649 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3650 ulp_ops->cm_remote_abort(csk);
3651 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3652 ulp_ops->cm_abort_complete(csk);
3653 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3654 ulp_ops->cm_remote_close(csk);
3655 }
3656 rcu_read_unlock();
3657}
3658
3659static int cnic_cm_set_pg(struct cnic_sock *csk)
3660{
3661 if (cnic_offld_prep(csk)) {
3662 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3663 cnic_cm_update_pg(csk);
3664 else
3665 cnic_cm_offload_pg(csk);
3666 }
3667 return 0;
3668}
3669
3670static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3671{
3672 struct cnic_local *cp = dev->cnic_priv;
3673 u32 l5_cid = kcqe->pg_host_opaque;
3674 u8 opcode = kcqe->op_code;
3675 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3676
3677 csk_hold(csk);
3678 if (!cnic_in_use(csk))
3679 goto done;
3680
3681 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3682 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3683 goto done;
3684 }
Eddie Waia9736c02010-02-24 14:42:04 +00003685 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3686 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3687 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3688 cnic_cm_upcall(cp, csk,
3689 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3690 goto done;
3691 }
3692
Michael Chana4636962009-06-08 18:14:43 -07003693 csk->pg_cid = kcqe->pg_cid;
3694 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3695 cnic_cm_conn_req(csk);
3696
3697done:
3698 csk_put(csk);
3699}
3700
Michael Chane1928c82010-12-23 07:43:04 +00003701static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3702{
3703 struct cnic_local *cp = dev->cnic_priv;
3704 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3705 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3706 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3707
3708 ctx->timestamp = jiffies;
3709 ctx->wait_cond = 1;
3710 wake_up(&ctx->waitq);
3711}
3712
Michael Chana4636962009-06-08 18:14:43 -07003713static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3714{
3715 struct cnic_local *cp = dev->cnic_priv;
3716 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3717 u8 opcode = l4kcqe->op_code;
3718 u32 l5_cid;
3719 struct cnic_sock *csk;
3720
Michael Chane1928c82010-12-23 07:43:04 +00003721 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3722 cnic_process_fcoe_term_conn(dev, kcqe);
3723 return;
3724 }
Michael Chana4636962009-06-08 18:14:43 -07003725 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3726 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3727 cnic_cm_process_offld_pg(dev, l4kcqe);
3728 return;
3729 }
3730
3731 l5_cid = l4kcqe->conn_id;
3732 if (opcode & 0x80)
3733 l5_cid = l4kcqe->cid;
3734 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3735 return;
3736
3737 csk = &cp->csk_tbl[l5_cid];
3738 csk_hold(csk);
3739
3740 if (!cnic_in_use(csk)) {
3741 csk_put(csk);
3742 return;
3743 }
3744
3745 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003746 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3747 if (l4kcqe->status != 0) {
3748 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3749 cnic_cm_upcall(cp, csk,
3750 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3751 }
3752 break;
Michael Chana4636962009-06-08 18:14:43 -07003753 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3754 if (l4kcqe->status == 0)
3755 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3756
3757 smp_mb__before_clear_bit();
3758 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3759 cnic_cm_upcall(cp, csk, opcode);
3760 break;
3761
3762 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003763 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3764 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003765 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3766 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003767 cp->close_conn(csk, opcode);
3768 break;
3769
3770 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003771 /* after we already sent CLOSE_REQ */
3772 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3773 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3774 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3775 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3776 else
3777 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003778 break;
3779 }
3780 csk_put(csk);
3781}
3782
3783static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3784{
3785 struct cnic_dev *dev = data;
3786 int i;
3787
3788 for (i = 0; i < num; i++)
3789 cnic_cm_process_kcqe(dev, kcqe[i]);
3790}
3791
3792static struct cnic_ulp_ops cm_ulp_ops = {
3793 .indicate_kcqes = cnic_cm_indicate_kcqe,
3794};
3795
3796static void cnic_cm_free_mem(struct cnic_dev *dev)
3797{
3798 struct cnic_local *cp = dev->cnic_priv;
3799
3800 kfree(cp->csk_tbl);
3801 cp->csk_tbl = NULL;
3802 cnic_free_id_tbl(&cp->csk_port_tbl);
3803}
3804
3805static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3806{
3807 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00003808 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003809
3810 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3811 GFP_KERNEL);
3812 if (!cp->csk_tbl)
3813 return -ENOMEM;
3814
Eddie Wai11f23aa2011-06-08 19:29:34 +00003815 get_random_bytes(&port_id, sizeof(port_id));
3816 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07003817 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00003818 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07003819 cnic_cm_free_mem(dev);
3820 return -ENOMEM;
3821 }
3822 return 0;
3823}
3824
3825static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3826{
Michael Chan943189f2010-06-15 08:57:02 +00003827 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3828 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3829 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3830 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003831 }
Michael Chan943189f2010-06-15 08:57:02 +00003832
3833 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00003834 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
3835 * event
Michael Chan7b34a462010-06-15 08:57:03 +00003836 * 3. If the expected event is 0, meaning the connection was never
3837 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003838 */
Michael Chan7b34a462010-06-15 08:57:03 +00003839 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00003840 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
3841 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00003842 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3843 if (csk->state == 0)
3844 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003845 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003846 }
Michael Chana4636962009-06-08 18:14:43 -07003847 }
3848 return 0;
3849}
3850
3851static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3852{
3853 struct cnic_dev *dev = csk->dev;
3854 struct cnic_local *cp = dev->cnic_priv;
3855
Michael Chana1e621b2010-06-15 08:57:01 +00003856 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3857 cnic_cm_upcall(cp, csk, opcode);
3858 return;
3859 }
3860
Michael Chana4636962009-06-08 18:14:43 -07003861 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003862 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003863 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003864 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003865}
3866
3867static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3868{
3869}
3870
3871static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3872{
3873 u32 seed;
3874
3875 get_random_bytes(&seed, 4);
3876 cnic_ctx_wr(dev, 45, 0, seed);
3877 return 0;
3878}
3879
Michael Chan71034ba2009-10-10 13:46:59 +00003880static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3881{
3882 struct cnic_dev *dev = csk->dev;
3883 struct cnic_local *cp = dev->cnic_priv;
3884 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3885 union l5cm_specific_data l5_data;
3886 u32 cmd = 0;
3887 int close_complete = 0;
3888
3889 switch (opcode) {
3890 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3891 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3892 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003893 if (cnic_ready_to_close(csk, opcode)) {
3894 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3895 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3896 else
3897 close_complete = 1;
3898 }
Michael Chan71034ba2009-10-10 13:46:59 +00003899 break;
3900 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3901 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3902 break;
3903 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3904 close_complete = 1;
3905 break;
3906 }
3907 if (cmd) {
3908 memset(&l5_data, 0, sizeof(l5_data));
3909
3910 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3911 &l5_data);
3912 } else if (close_complete) {
3913 ctx->timestamp = jiffies;
3914 cnic_close_conn(csk);
3915 cnic_cm_upcall(cp, csk, csk->state);
3916 }
3917}
3918
3919static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3920{
Michael Chanfdf24082010-10-13 14:06:47 +00003921 struct cnic_local *cp = dev->cnic_priv;
3922 int i;
3923
3924 if (!cp->ctx_tbl)
3925 return;
3926
3927 if (!netif_running(dev->netdev))
3928 return;
3929
3930 for (i = 0; i < cp->max_cid_space; i++) {
3931 struct cnic_context *ctx = &cp->ctx_tbl[i];
3932
3933 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3934 msleep(10);
3935
3936 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3937 netdev_warn(dev->netdev, "CID %x not deleted\n",
3938 ctx->cid);
3939 }
3940
3941 cancel_delayed_work(&cp->delete_task);
3942 flush_workqueue(cnic_wq);
3943
3944 if (atomic_read(&cp->iscsi_conn) != 0)
3945 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3946 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00003947}
3948
3949static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3950{
3951 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003952 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003953 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003954
3955 cnic_init_bnx2x_mac(dev);
3956 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3957
3958 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003959 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003960
3961 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003962 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003963 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003964 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00003965 DEF_MAX_DA_COUNT);
3966
3967 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003968 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003969 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003970 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003971 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003972 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003973 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003974 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003975
Michael Chan14203982010-10-06 03:16:06 +00003976 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003977 DEF_MAX_CWND);
3978 return 0;
3979}
3980
Michael Chanfdf24082010-10-13 14:06:47 +00003981static void cnic_delete_task(struct work_struct *work)
3982{
3983 struct cnic_local *cp;
3984 struct cnic_dev *dev;
3985 u32 i;
3986 int need_resched = 0;
3987
3988 cp = container_of(work, struct cnic_local, delete_task.work);
3989 dev = cp->dev;
3990
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003991 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
3992 struct drv_ctl_info info;
3993
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003994 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003995
3996 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
3997 cp->ethdev->drv_ctl(dev->netdev, &info);
3998 }
3999
Michael Chanfdf24082010-10-13 14:06:47 +00004000 for (i = 0; i < cp->max_cid_space; i++) {
4001 struct cnic_context *ctx = &cp->ctx_tbl[i];
4002
4003 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4004 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4005 continue;
4006
4007 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4008 need_resched = 1;
4009 continue;
4010 }
4011
4012 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4013 continue;
4014
4015 cnic_bnx2x_destroy_ramrod(dev, i);
4016
4017 cnic_free_bnx2x_conn_resc(dev, i);
4018 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4019 atomic_dec(&cp->iscsi_conn);
4020
4021 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4022 }
4023
4024 if (need_resched)
4025 queue_delayed_work(cnic_wq, &cp->delete_task,
4026 msecs_to_jiffies(10));
4027
4028}
4029
Michael Chana4636962009-06-08 18:14:43 -07004030static int cnic_cm_open(struct cnic_dev *dev)
4031{
4032 struct cnic_local *cp = dev->cnic_priv;
4033 int err;
4034
4035 err = cnic_cm_alloc_mem(dev);
4036 if (err)
4037 return err;
4038
4039 err = cp->start_cm(dev);
4040
4041 if (err)
4042 goto err_out;
4043
Michael Chanfdf24082010-10-13 14:06:47 +00004044 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4045
Michael Chana4636962009-06-08 18:14:43 -07004046 dev->cm_create = cnic_cm_create;
4047 dev->cm_destroy = cnic_cm_destroy;
4048 dev->cm_connect = cnic_cm_connect;
4049 dev->cm_abort = cnic_cm_abort;
4050 dev->cm_close = cnic_cm_close;
4051 dev->cm_select_dev = cnic_cm_select_dev;
4052
4053 cp->ulp_handle[CNIC_ULP_L4] = dev;
4054 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4055 return 0;
4056
4057err_out:
4058 cnic_cm_free_mem(dev);
4059 return err;
4060}
4061
4062static int cnic_cm_shutdown(struct cnic_dev *dev)
4063{
4064 struct cnic_local *cp = dev->cnic_priv;
4065 int i;
4066
4067 cp->stop_cm(dev);
4068
4069 if (!cp->csk_tbl)
4070 return 0;
4071
4072 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4073 struct cnic_sock *csk = &cp->csk_tbl[i];
4074
4075 clear_bit(SK_F_INUSE, &csk->flags);
4076 cnic_cm_cleanup(csk);
4077 }
4078 cnic_cm_free_mem(dev);
4079
4080 return 0;
4081}
4082
4083static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4084{
Michael Chana4636962009-06-08 18:14:43 -07004085 u32 cid_addr;
4086 int i;
4087
Michael Chana4636962009-06-08 18:14:43 -07004088 cid_addr = GET_CID_ADDR(cid);
4089
4090 for (i = 0; i < CTX_SIZE; i += 4)
4091 cnic_ctx_wr(dev, cid_addr, i, 0);
4092}
4093
4094static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4095{
4096 struct cnic_local *cp = dev->cnic_priv;
4097 int ret = 0, i;
4098 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4099
4100 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4101 return 0;
4102
4103 for (i = 0; i < cp->ctx_blks; i++) {
4104 int j;
4105 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4106 u32 val;
4107
4108 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4109
4110 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4111 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4112 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4113 (u64) cp->ctx_arr[i].mapping >> 32);
4114 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4115 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4116 for (j = 0; j < 10; j++) {
4117
4118 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4119 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4120 break;
4121 udelay(5);
4122 }
4123 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4124 ret = -EBUSY;
4125 break;
4126 }
4127 }
4128 return ret;
4129}
4130
4131static void cnic_free_irq(struct cnic_dev *dev)
4132{
4133 struct cnic_local *cp = dev->cnic_priv;
4134 struct cnic_eth_dev *ethdev = cp->ethdev;
4135
4136 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4137 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004138 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004139 free_irq(ethdev->irq_arr[0].vector, dev);
4140 }
4141}
4142
Michael Chan6e0dc642010-10-13 14:06:44 +00004143static int cnic_request_irq(struct cnic_dev *dev)
4144{
4145 struct cnic_local *cp = dev->cnic_priv;
4146 struct cnic_eth_dev *ethdev = cp->ethdev;
4147 int err;
4148
4149 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4150 if (err)
4151 tasklet_disable(&cp->cnic_irq_task);
4152
4153 return err;
4154}
4155
Michael Chana4636962009-06-08 18:14:43 -07004156static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4157{
4158 struct cnic_local *cp = dev->cnic_priv;
4159 struct cnic_eth_dev *ethdev = cp->ethdev;
4160
4161 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4162 int err, i = 0;
4163 int sblk_num = cp->status_blk_num;
4164 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4165 BNX2_HC_SB_CONFIG_1;
4166
4167 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4168
4169 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4170 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4171 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4172
Michael Chana4dde3a2010-02-24 14:42:08 +00004173 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004174 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004175 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004176 err = cnic_request_irq(dev);
4177 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004178 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004179
Michael Chana4dde3a2010-02-24 14:42:08 +00004180 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004181 i < 10) {
4182 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4183 1 << (11 + sblk_num));
4184 udelay(10);
4185 i++;
4186 barrier();
4187 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004188 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004189 cnic_free_irq(dev);
4190 goto failed;
4191 }
4192
4193 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004194 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004195 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4196 int i = 0;
4197
4198 while (sblk->status_completion_producer_index && i < 10) {
4199 CNIC_WR(dev, BNX2_HC_COMMAND,
4200 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4201 udelay(10);
4202 i++;
4203 barrier();
4204 }
4205 if (sblk->status_completion_producer_index)
4206 goto failed;
4207
4208 }
4209 return 0;
4210
4211failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004212 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004213 return -EBUSY;
4214}
4215
4216static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4217{
4218 struct cnic_local *cp = dev->cnic_priv;
4219 struct cnic_eth_dev *ethdev = cp->ethdev;
4220
4221 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4222 return;
4223
4224 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4225 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4226}
4227
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00004228static void cnic_get_bnx2_iscsi_info(struct cnic_dev *dev)
4229{
4230 u32 max_conn;
4231
4232 max_conn = cnic_reg_rd_ind(dev, BNX2_FW_MAX_ISCSI_CONN);
4233 dev->max_iscsi_conn = max_conn;
4234}
4235
Michael Chana4636962009-06-08 18:14:43 -07004236static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4237{
4238 struct cnic_local *cp = dev->cnic_priv;
4239 struct cnic_eth_dev *ethdev = cp->ethdev;
4240
4241 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4242 return;
4243
4244 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4245 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4246 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4247 synchronize_irq(ethdev->irq_arr[0].vector);
4248}
4249
4250static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4251{
4252 struct cnic_local *cp = dev->cnic_priv;
4253 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004254 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004255 u32 cid_addr, tx_cid, sb_id;
4256 u32 val, offset0, offset1, offset2, offset3;
4257 int i;
4258 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004259 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004260 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004261
4262 sb_id = cp->status_blk_num;
4263 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004264 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4265 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004266 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004267
4268 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004269 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4270 (TX_TSS_CID << 7));
4271 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4272 }
4273 cp->tx_cons = *cp->tx_cons_ptr;
4274
4275 cid_addr = GET_CID_ADDR(tx_cid);
4276 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4277 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4278
4279 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4280 cnic_ctx_wr(dev, cid_addr2, i, 0);
4281
4282 offset0 = BNX2_L2CTX_TYPE_XI;
4283 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4284 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4285 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4286 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004287 cnic_init_context(dev, tx_cid);
4288 cnic_init_context(dev, tx_cid + 1);
4289
Michael Chana4636962009-06-08 18:14:43 -07004290 offset0 = BNX2_L2CTX_TYPE;
4291 offset1 = BNX2_L2CTX_CMD_TYPE;
4292 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4293 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4294 }
4295 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4296 cnic_ctx_wr(dev, cid_addr, offset0, val);
4297
4298 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4299 cnic_ctx_wr(dev, cid_addr, offset1, val);
4300
Michael Chancd801532010-10-13 14:06:49 +00004301 txbd = (struct tx_bd *) udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004302
Michael Chancd801532010-10-13 14:06:49 +00004303 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004304 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4305 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4306 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4307 }
Michael Chancd801532010-10-13 14:06:49 +00004308 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004309 cnic_ctx_wr(dev, cid_addr, offset2, val);
4310 txbd->tx_bd_haddr_hi = val;
4311
Michael Chancd801532010-10-13 14:06:49 +00004312 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004313 cnic_ctx_wr(dev, cid_addr, offset3, val);
4314 txbd->tx_bd_haddr_lo = val;
4315}
4316
4317static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4318{
4319 struct cnic_local *cp = dev->cnic_priv;
4320 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004321 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004322 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4323 int i;
4324 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004325 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004326 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004327
4328 sb_id = cp->status_blk_num;
4329 cnic_init_context(dev, 2);
4330 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4331 coal_reg = BNX2_HC_COMMAND;
4332 coal_val = CNIC_RD(dev, coal_reg);
4333 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004334 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004335
4336 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4337 coal_reg = BNX2_HC_COALESCE_NOW;
4338 coal_val = 1 << (11 + sb_id);
4339 }
4340 i = 0;
4341 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4342 CNIC_WR(dev, coal_reg, coal_val);
4343 udelay(10);
4344 i++;
4345 barrier();
4346 }
4347 cp->rx_cons = *cp->rx_cons_ptr;
4348
4349 cid_addr = GET_CID_ADDR(2);
4350 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4351 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4352 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4353
4354 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004355 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004356 else
Michael Chand0549382009-10-28 03:41:59 -07004357 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004358 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4359
Michael Chancd801532010-10-13 14:06:49 +00004360 rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
Michael Chana4636962009-06-08 18:14:43 -07004361 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4362 dma_addr_t buf_map;
4363 int n = (i % cp->l2_rx_ring_size) + 1;
4364
Michael Chancd801532010-10-13 14:06:49 +00004365 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004366 rxbd->rx_bd_len = cp->l2_single_buf_size;
4367 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4368 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4369 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4370 }
Michael Chancd801532010-10-13 14:06:49 +00004371 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004372 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4373 rxbd->rx_bd_haddr_hi = val;
4374
Michael Chancd801532010-10-13 14:06:49 +00004375 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004376 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4377 rxbd->rx_bd_haddr_lo = val;
4378
4379 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4380 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4381}
4382
4383static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4384{
4385 struct kwqe *wqes[1], l2kwqe;
4386
4387 memset(&l2kwqe, 0, sizeof(l2kwqe));
4388 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004389 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004390 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4391 KWQE_OPCODE_SHIFT) | 2;
4392 dev->submit_kwqes(dev, wqes, 1);
4393}
4394
4395static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4396{
4397 struct cnic_local *cp = dev->cnic_priv;
4398 u32 val;
4399
4400 val = cp->func << 2;
4401
4402 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4403
4404 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4405 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4406 dev->mac_addr[0] = (u8) (val >> 8);
4407 dev->mac_addr[1] = (u8) val;
4408
4409 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4410
4411 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4412 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4413 dev->mac_addr[2] = (u8) (val >> 24);
4414 dev->mac_addr[3] = (u8) (val >> 16);
4415 dev->mac_addr[4] = (u8) (val >> 8);
4416 dev->mac_addr[5] = (u8) val;
4417
4418 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4419
4420 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4421 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4422 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4423
4424 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4425 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4426 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4427}
4428
4429static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4430{
4431 struct cnic_local *cp = dev->cnic_priv;
4432 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004433 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004434 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004435 int err;
4436
4437 cnic_set_bnx2_mac(dev);
4438
4439 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4440 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4441 if (BCM_PAGE_BITS > 12)
4442 val |= (12 - 8) << 4;
4443 else
4444 val |= (BCM_PAGE_BITS - 8) << 4;
4445
4446 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4447
4448 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4449 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4450 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4451
4452 err = cnic_setup_5709_context(dev, 1);
4453 if (err)
4454 return err;
4455
4456 cnic_init_context(dev, KWQ_CID);
4457 cnic_init_context(dev, KCQ_CID);
4458
Michael Chane6c28892010-06-24 14:58:39 +00004459 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004460 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4461
4462 cp->max_kwq_idx = MAX_KWQ_IDX;
4463 cp->kwq_prod_idx = 0;
4464 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004465 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004466
4467 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4468 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4469 else
4470 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4471
4472 /* Initialize the kernel work queue context. */
4473 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4474 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004475 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004476
4477 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004478 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004479
4480 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004481 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004482
4483 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004484 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004485
4486 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004487 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004488
Michael Chane6c28892010-06-24 14:58:39 +00004489 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4490 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004491
Michael Chane6c28892010-06-24 14:58:39 +00004492 cp->kcq1.sw_prod_idx = 0;
4493 cp->kcq1.hw_prod_idx_ptr =
4494 (u16 *) &sblk->status_completion_producer_index;
4495
4496 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004497
4498 /* Initialize the kernel complete queue context. */
4499 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4500 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004501 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004502
4503 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004504 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004505
4506 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004507 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004508
Michael Chane6c28892010-06-24 14:58:39 +00004509 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4510 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004511
Michael Chane6c28892010-06-24 14:58:39 +00004512 val = (u32) cp->kcq1.dma.pgtbl_map;
4513 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004514
4515 cp->int_num = 0;
4516 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004517 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004518 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004519 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004520
Michael Chane6c28892010-06-24 14:58:39 +00004521 cp->kcq1.hw_prod_idx_ptr =
4522 (u16 *) &msblk->status_completion_producer_index;
4523 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004524 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004525 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004526 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4527 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004528 }
4529
4530 /* Enable Commnad Scheduler notification when we write to the
4531 * host producer index of the kernel contexts. */
4532 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4533
4534 /* Enable Command Scheduler notification when we write to either
4535 * the Send Queue or Receive Queue producer indexes of the kernel
4536 * bypass contexts. */
4537 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4538 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4539
4540 /* Notify COM when the driver post an application buffer. */
4541 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4542
4543 /* Set the CP and COM doorbells. These two processors polls the
4544 * doorbell for a non zero value before running. This must be done
4545 * after setting up the kernel queue contexts. */
4546 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4547 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4548
4549 cnic_init_bnx2_tx_ring(dev);
4550 cnic_init_bnx2_rx_ring(dev);
4551
4552 err = cnic_init_bnx2_irq(dev);
4553 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004554 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004555 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4556 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4557 return err;
4558 }
4559
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00004560 cnic_get_bnx2_iscsi_info(dev);
4561
Michael Chana4636962009-06-08 18:14:43 -07004562 return 0;
4563}
4564
Michael Chan71034ba2009-10-10 13:46:59 +00004565static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4566{
4567 struct cnic_local *cp = dev->cnic_priv;
4568 struct cnic_eth_dev *ethdev = cp->ethdev;
4569 u32 start_offset = ethdev->ctx_tbl_offset;
4570 int i;
4571
4572 for (i = 0; i < cp->ctx_blks; i++) {
4573 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4574 dma_addr_t map = ctx->mapping;
4575
4576 if (cp->ctx_align) {
4577 unsigned long mask = cp->ctx_align - 1;
4578
4579 map = (map + mask) & ~mask;
4580 }
4581
4582 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4583 }
4584}
4585
4586static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4587{
4588 struct cnic_local *cp = dev->cnic_priv;
4589 struct cnic_eth_dev *ethdev = cp->ethdev;
4590 int err = 0;
4591
Joe Perches164165d2009-11-19 09:30:10 +00004592 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004593 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004594 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4595 err = cnic_request_irq(dev);
4596
Michael Chan71034ba2009-10-10 13:46:59 +00004597 return err;
4598}
4599
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004600static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4601 u16 sb_id, u8 sb_index,
4602 u8 disable)
4603{
4604
4605 u32 addr = BAR_CSTRORM_INTMEM +
4606 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4607 offsetof(struct hc_status_block_data_e1x, index_data) +
4608 sizeof(struct hc_index_data)*sb_index +
4609 offsetof(struct hc_index_data, flags);
4610 u16 flags = CNIC_RD16(dev, addr);
4611 /* clear and set */
4612 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4613 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4614 HC_INDEX_DATA_HC_ENABLED);
4615 CNIC_WR16(dev, addr, flags);
4616}
4617
Michael Chan71034ba2009-10-10 13:46:59 +00004618static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4619{
4620 struct cnic_local *cp = dev->cnic_priv;
4621 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004622
4623 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004624 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4625 offsetof(struct hc_status_block_data_e1x, index_data) +
4626 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
4627 offsetof(struct hc_index_data, timeout), 64 / 12);
4628 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004629}
4630
4631static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4632{
4633}
4634
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004635static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4636 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004637{
4638 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004639 struct cnic_uio_dev *udev = cp->udev;
4640 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4641 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004642 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004643 int port = CNIC_PORT(cp);
4644 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004645 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004646 u32 val;
4647
4648 memset(txbd, 0, BCM_PAGE_SIZE);
4649
Michael Chancd801532010-10-13 14:06:49 +00004650 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004651 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4652 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4653 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4654
4655 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4656 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4657 reg_bd->addr_hi = start_bd->addr_hi;
4658 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4659 start_bd->nbytes = cpu_to_le16(0x10);
4660 start_bd->nbd = cpu_to_le16(3);
4661 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4662 start_bd->general_data = (UNICAST_ADDRESS <<
4663 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4664 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4665
4666 }
Michael Chan71034ba2009-10-10 13:46:59 +00004667
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004668 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004669 txbd->next_bd.addr_hi = cpu_to_le32(val);
4670
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004671 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004672
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004673 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004674 txbd->next_bd.addr_lo = cpu_to_le32(val);
4675
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004676 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004677
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004678 /* Other ramrod params */
4679 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4680 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004681
4682 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004683 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004684 val = BAR_XSTRORM_INTMEM +
4685 XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4686 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
4687 CNIC_WR(dev, val + i * 4, 0);
4688 }
Michael Chan71034ba2009-10-10 13:46:59 +00004689
4690 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004691 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004692}
4693
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004694static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4695 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004696{
4697 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004698 struct cnic_uio_dev *udev = cp->udev;
4699 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004700 BCM_PAGE_SIZE);
4701 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004702 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004703 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004704 int i;
4705 int port = CNIC_PORT(cp);
Michael Chan5159fdc2010-12-23 07:42:59 +00004706 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004707 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004708 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004709 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004710
4711 /* General data */
4712 data->general.client_id = cli;
4713 data->general.statistics_en_flg = 1;
4714 data->general.statistics_counter_id = cli;
4715 data->general.activate_flg = 1;
4716 data->general.sp_client_id = cli;
Michael Chan71034ba2009-10-10 13:46:59 +00004717
4718 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4719 dma_addr_t buf_map;
4720 int n = (i % cp->l2_rx_ring_size) + 1;
4721
Michael Chancd801532010-10-13 14:06:49 +00004722 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004723 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4724 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4725 }
Michael Chan71034ba2009-10-10 13:46:59 +00004726
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004727 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004728 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004729 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004730
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004731 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004732 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004733 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004734
4735 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004736 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004737 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004738 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004739
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004740 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004741 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004742 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004743
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004744 /* Other ramrod params */
4745 data->rx.client_qzone_id = cl_qzone_id;
4746 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4747 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004748
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004749 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
4750 data->rx.bd_buff_size = cpu_to_le16(cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004751
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004752 data->rx.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4753 data->rx.outer_vlan_removal_enable_flg = 1;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004754
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004755 /* reset tstorm and ustorm per client statistics */
4756 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004757 val = BAR_TSTRORM_INTMEM +
4758 TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4759 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4760 CNIC_WR(dev, val + i * 4, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004761
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004762 val = BAR_USTRORM_INTMEM +
4763 USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4764 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4765 CNIC_WR(dev, val + i * 4, 0);
4766 }
Michael Chan71034ba2009-10-10 13:46:59 +00004767
4768 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004769 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004770 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004771}
4772
Michael Chane21ba412010-12-23 07:43:03 +00004773static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4774{
4775 struct cnic_local *cp = dev->cnic_priv;
4776 u32 pfid = cp->pfid;
4777
4778 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4779 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4780 cp->kcq1.sw_prod_idx = 0;
4781
4782 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4783 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4784
4785 cp->kcq1.hw_prod_idx_ptr =
4786 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4787 cp->kcq1.status_idx_ptr =
4788 &sb->sb.running_index[SM_RX_ID];
4789 } else {
4790 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4791
4792 cp->kcq1.hw_prod_idx_ptr =
4793 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4794 cp->kcq1.status_idx_ptr =
4795 &sb->sb.running_index[SM_RX_ID];
4796 }
4797
4798 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4799 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4800
4801 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4802 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4803 cp->kcq2.sw_prod_idx = 0;
4804 cp->kcq2.hw_prod_idx_ptr =
4805 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4806 cp->kcq2.status_idx_ptr =
4807 &sb->sb.running_index[SM_RX_ID];
4808 }
4809}
4810
Michael Chan71034ba2009-10-10 13:46:59 +00004811static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4812{
4813 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004814 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chan71034ba2009-10-10 13:46:59 +00004815 int func = CNIC_FUNC(cp), ret, i;
Michael Chan14203982010-10-06 03:16:06 +00004816 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004817
Michael Chanee87a822010-10-13 14:06:51 +00004818 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4819 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4820
4821 if (!(val & 1))
4822 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4823 else
4824 val = (val >> 1) & 1;
4825
4826 if (val)
4827 cp->pfid = func >> 1;
4828 else
4829 cp->pfid = func & 0x6;
4830 } else {
4831 cp->pfid = func;
4832 }
Michael Chan14203982010-10-06 03:16:06 +00004833 pfid = cp->pfid;
4834
Michael Chan71034ba2009-10-10 13:46:59 +00004835 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004836 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004837
4838 if (ret)
4839 return -ENOMEM;
4840
Michael Chane1928c82010-12-23 07:43:04 +00004841 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4842 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl,
4843 BNX2X_FCOE_NUM_CONNECTIONS,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004844 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00004845
4846 if (ret)
4847 return -ENOMEM;
4848 }
4849
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004850 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
4851
Michael Chane21ba412010-12-23 07:43:03 +00004852 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004853
Michael Chan71034ba2009-10-10 13:46:59 +00004854 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00004855 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00004856 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004857 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004858 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004859 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004860 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004861 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004862 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004863 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004864 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004865 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004866 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004867 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004868 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004869 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
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_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004872 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004873 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00004874 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004875 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004876 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00004877
4878 for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4879 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004880 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
Michael Chan71034ba2009-10-10 13:46:59 +00004881 cp->conn_buf_info.pgtbl[2 * i]);
4882 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004883 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004884 cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4885 }
4886
4887 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004888 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004889 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4890 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004891 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004892 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4893
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004894 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4895 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
4896
Michael Chan71034ba2009-10-10 13:46:59 +00004897 cnic_setup_bnx2x_context(dev);
4898
Michael Chan71034ba2009-10-10 13:46:59 +00004899 ret = cnic_init_bnx2x_irq(dev);
4900 if (ret)
4901 return ret;
4902
Michael Chan71034ba2009-10-10 13:46:59 +00004903 return 0;
4904}
4905
Michael Chan86b53602009-10-10 13:46:57 +00004906static void cnic_init_rings(struct cnic_dev *dev)
4907{
Michael Chan541a7812010-10-06 03:17:22 +00004908 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004909 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00004910
4911 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4912 return;
4913
Michael Chan86b53602009-10-10 13:46:57 +00004914 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4915 cnic_init_bnx2_tx_ring(dev);
4916 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00004917 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00004918 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00004919 u32 cli = cp->ethdev->iscsi_l2_client_id;
4920 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00004921 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004922 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00004923 union l5cm_specific_data l5_data;
4924 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chanc7596b72009-12-02 15:15:35 +00004925 u32 off, i;
Michael Chan71034ba2009-10-10 13:46:59 +00004926
4927 rx_prods.bd_prod = 0;
4928 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4929 barrier();
4930
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004931 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4932
Michael Chanc7596b72009-12-02 15:15:35 +00004933 off = BAR_USTRORM_INTMEM +
Michael Chanee87a822010-10-13 14:06:51 +00004934 (BNX2X_CHIP_IS_E2(cp->chip_id) ?
4935 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
4936 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00004937
4938 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00004939 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00004940
Michael Chan48f753d2010-05-18 11:32:53 +00004941 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4942
Michael Chancd801532010-10-13 14:06:49 +00004943 data = udev->l2_buf;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004944
4945 memset(data, 0, sizeof(*data));
4946
4947 cnic_init_bnx2x_tx_ring(dev, data);
4948 cnic_init_bnx2x_rx_ring(dev, data);
4949
Michael Chancd801532010-10-13 14:06:49 +00004950 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
4951 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004952
Michael Chan541a7812010-10-06 03:17:22 +00004953 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4954
Michael Chan71034ba2009-10-10 13:46:59 +00004955 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00004956 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004957
Michael Chan48f753d2010-05-18 11:32:53 +00004958 i = 0;
4959 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4960 ++i < 10)
4961 msleep(1);
4962
4963 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4964 netdev_err(dev->netdev,
4965 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004966 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00004967 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chan86b53602009-10-10 13:46:57 +00004968 }
4969}
4970
4971static void cnic_shutdown_rings(struct cnic_dev *dev)
4972{
Michael Chan541a7812010-10-06 03:17:22 +00004973 struct cnic_local *cp = dev->cnic_priv;
4974
4975 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4976 return;
4977
Michael Chan86b53602009-10-10 13:46:57 +00004978 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4979 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004980 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4981 struct cnic_local *cp = dev->cnic_priv;
Michael Chan5159fdc2010-12-23 07:42:59 +00004982 u32 cli = cp->ethdev->iscsi_l2_client_id;
4983 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00004984 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00004985 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00004986
Michael Chan5159fdc2010-12-23 07:42:59 +00004987 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00004988
Michael Chan48f753d2010-05-18 11:32:53 +00004989 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4990
Michael Chan8b065b62009-12-02 15:15:36 +00004991 l5_data.phy_address.lo = cli;
4992 l5_data.phy_address.hi = 0;
4993 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00004994 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00004995 i = 0;
4996 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4997 ++i < 10)
4998 msleep(1);
4999
5000 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5001 netdev_err(dev->netdev,
5002 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005003 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005004
5005 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005006 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005007 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005008 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005009 }
Michael Chan541a7812010-10-06 03:17:22 +00005010 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan86b53602009-10-10 13:46:57 +00005011}
5012
Michael Chana3059b12009-08-14 15:49:44 +00005013static int cnic_register_netdev(struct cnic_dev *dev)
5014{
5015 struct cnic_local *cp = dev->cnic_priv;
5016 struct cnic_eth_dev *ethdev = cp->ethdev;
5017 int err;
5018
5019 if (!ethdev)
5020 return -ENODEV;
5021
5022 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5023 return 0;
5024
5025 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5026 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005027 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005028
5029 return err;
5030}
5031
5032static void cnic_unregister_netdev(struct cnic_dev *dev)
5033{
5034 struct cnic_local *cp = dev->cnic_priv;
5035 struct cnic_eth_dev *ethdev = cp->ethdev;
5036
5037 if (!ethdev)
5038 return;
5039
5040 ethdev->drv_unregister_cnic(dev->netdev);
5041}
5042
Michael Chana4636962009-06-08 18:14:43 -07005043static int cnic_start_hw(struct cnic_dev *dev)
5044{
5045 struct cnic_local *cp = dev->cnic_priv;
5046 struct cnic_eth_dev *ethdev = cp->ethdev;
5047 int err;
5048
5049 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5050 return -EALREADY;
5051
Michael Chana4636962009-06-08 18:14:43 -07005052 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005053 pci_dev_get(dev->pcidev);
5054 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005055 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005056 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5057
5058 err = cp->alloc_resc(dev);
5059 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005060 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005061 goto err1;
5062 }
5063
5064 err = cp->start_hw(dev);
5065 if (err)
5066 goto err1;
5067
5068 err = cnic_cm_open(dev);
5069 if (err)
5070 goto err1;
5071
5072 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5073
5074 cp->enable_int(dev);
5075
5076 return 0;
5077
5078err1:
Michael Chana4636962009-06-08 18:14:43 -07005079 cp->free_resc(dev);
5080 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005081 return err;
5082}
5083
5084static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5085{
Michael Chana4636962009-06-08 18:14:43 -07005086 cnic_disable_bnx2_int_sync(dev);
5087
5088 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5089 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5090
5091 cnic_init_context(dev, KWQ_CID);
5092 cnic_init_context(dev, KCQ_CID);
5093
5094 cnic_setup_5709_context(dev, 0);
5095 cnic_free_irq(dev);
5096
Michael Chana4636962009-06-08 18:14:43 -07005097 cnic_free_resc(dev);
5098}
5099
Michael Chan71034ba2009-10-10 13:46:59 +00005100
5101static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5102{
5103 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005104
5105 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005106 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005107 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005108 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005109 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005110 cnic_free_resc(dev);
5111}
5112
Michael Chana4636962009-06-08 18:14:43 -07005113static void cnic_stop_hw(struct cnic_dev *dev)
5114{
5115 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5116 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005117 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005118
Michael Chan48f753d2010-05-18 11:32:53 +00005119 /* Need to wait for the ring shutdown event to complete
5120 * before clearing the CNIC_UP flag.
5121 */
Michael Chancd801532010-10-13 14:06:49 +00005122 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005123 msleep(100);
5124 i++;
5125 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005126 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005127 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
5128 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
5129 synchronize_rcu();
5130 cnic_cm_shutdown(dev);
5131 cp->stop_hw(dev);
5132 pci_dev_put(dev->pcidev);
5133 }
5134}
5135
5136static void cnic_free_dev(struct cnic_dev *dev)
5137{
5138 int i = 0;
5139
5140 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5141 msleep(100);
5142 i++;
5143 }
5144 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005145 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005146
Joe Perchesddf79b22010-02-17 15:01:54 +00005147 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005148 dev_put(dev->netdev);
5149 kfree(dev);
5150}
5151
5152static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5153 struct pci_dev *pdev)
5154{
5155 struct cnic_dev *cdev;
5156 struct cnic_local *cp;
5157 int alloc_size;
5158
5159 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5160
5161 cdev = kzalloc(alloc_size , GFP_KERNEL);
5162 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005163 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005164 return NULL;
5165 }
5166
5167 cdev->netdev = dev;
5168 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5169 cdev->register_device = cnic_register_device;
5170 cdev->unregister_device = cnic_unregister_device;
5171 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5172
5173 cp = cdev->cnic_priv;
5174 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005175 cp->l2_single_buf_size = 0x400;
5176 cp->l2_rx_ring_size = 3;
5177
5178 spin_lock_init(&cp->cnic_ulp_lock);
5179
Joe Perchesddf79b22010-02-17 15:01:54 +00005180 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005181
5182 return cdev;
5183}
5184
5185static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5186{
5187 struct pci_dev *pdev;
5188 struct cnic_dev *cdev;
5189 struct cnic_local *cp;
5190 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005191 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005192
Michael Chane2ee3612009-06-13 17:43:02 -07005193 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005194 if (probe) {
5195 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005196 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005197 }
5198 if (!ethdev)
5199 return NULL;
5200
5201 pdev = ethdev->pdev;
5202 if (!pdev)
5203 return NULL;
5204
5205 dev_hold(dev);
5206 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005207 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5208 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5209 (pdev->revision < 0x10)) {
5210 pci_dev_put(pdev);
5211 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005212 }
5213 pci_dev_put(pdev);
5214
5215 cdev = cnic_alloc_dev(dev, pdev);
5216 if (cdev == NULL)
5217 goto cnic_err;
5218
5219 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5220 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5221
5222 cp = cdev->cnic_priv;
5223 cp->ethdev = ethdev;
5224 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005225 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005226
5227 cp->cnic_ops = &cnic_bnx2_ops;
5228 cp->start_hw = cnic_start_bnx2_hw;
5229 cp->stop_hw = cnic_stop_bnx2_hw;
5230 cp->setup_pgtbl = cnic_setup_page_tbl;
5231 cp->alloc_resc = cnic_alloc_bnx2_resc;
5232 cp->free_resc = cnic_free_resc;
5233 cp->start_cm = cnic_cm_init_bnx2_hw;
5234 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5235 cp->enable_int = cnic_enable_bnx2_int;
5236 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5237 cp->close_conn = cnic_close_bnx2_conn;
5238 cp->next_idx = cnic_bnx2_next_idx;
5239 cp->hw_idx = cnic_bnx2_hw_idx;
5240 return cdev;
5241
5242cnic_err:
5243 dev_put(dev);
5244 return NULL;
5245}
5246
Michael Chan71034ba2009-10-10 13:46:59 +00005247static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5248{
5249 struct pci_dev *pdev;
5250 struct cnic_dev *cdev;
5251 struct cnic_local *cp;
5252 struct cnic_eth_dev *ethdev = NULL;
5253 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5254
5255 probe = symbol_get(bnx2x_cnic_probe);
5256 if (probe) {
5257 ethdev = (*probe)(dev);
5258 symbol_put(bnx2x_cnic_probe);
5259 }
5260 if (!ethdev)
5261 return NULL;
5262
5263 pdev = ethdev->pdev;
5264 if (!pdev)
5265 return NULL;
5266
5267 dev_hold(dev);
5268 cdev = cnic_alloc_dev(dev, pdev);
5269 if (cdev == NULL) {
5270 dev_put(dev);
5271 return NULL;
5272 }
5273
5274 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5275 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5276
5277 cp = cdev->cnic_priv;
5278 cp->ethdev = ethdev;
5279 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005280 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005281
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005282 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5283 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5284 if (BNX2X_CHIP_IS_E2(cp->chip_id) &&
5285 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5286 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5287
5288 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5289
Michael Chan71034ba2009-10-10 13:46:59 +00005290 cp->cnic_ops = &cnic_bnx2x_ops;
5291 cp->start_hw = cnic_start_bnx2x_hw;
5292 cp->stop_hw = cnic_stop_bnx2x_hw;
5293 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5294 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5295 cp->free_resc = cnic_free_resc;
5296 cp->start_cm = cnic_cm_init_bnx2x_hw;
5297 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5298 cp->enable_int = cnic_enable_bnx2x_int;
5299 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Michael Chanee87a822010-10-13 14:06:51 +00005300 if (BNX2X_CHIP_IS_E2(cp->chip_id))
5301 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5302 else
5303 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005304 cp->close_conn = cnic_close_bnx2x_conn;
5305 cp->next_idx = cnic_bnx2x_next_idx;
5306 cp->hw_idx = cnic_bnx2x_hw_idx;
5307 return cdev;
5308}
5309
Michael Chana4636962009-06-08 18:14:43 -07005310static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5311{
5312 struct ethtool_drvinfo drvinfo;
5313 struct cnic_dev *cdev = NULL;
5314
5315 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5316 memset(&drvinfo, 0, sizeof(drvinfo));
5317 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5318
5319 if (!strcmp(drvinfo.driver, "bnx2"))
5320 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005321 if (!strcmp(drvinfo.driver, "bnx2x"))
5322 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005323 if (cdev) {
5324 write_lock(&cnic_dev_lock);
5325 list_add(&cdev->list, &cnic_dev_list);
5326 write_unlock(&cnic_dev_lock);
5327 }
5328 }
5329 return cdev;
5330}
5331
5332/**
5333 * netdev event handler
5334 */
5335static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5336 void *ptr)
5337{
5338 struct net_device *netdev = ptr;
5339 struct cnic_dev *dev;
5340 int if_type;
5341 int new_dev = 0;
5342
5343 dev = cnic_from_netdev(netdev);
5344
5345 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
5346 /* Check for the hot-plug device */
5347 dev = is_cnic_dev(netdev);
5348 if (dev) {
5349 new_dev = 1;
5350 cnic_hold(dev);
5351 }
5352 }
5353 if (dev) {
5354 struct cnic_local *cp = dev->cnic_priv;
5355
5356 if (new_dev)
5357 cnic_ulp_init(dev);
5358 else if (event == NETDEV_UNREGISTER)
5359 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005360
5361 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00005362 if (cnic_register_netdev(dev) != 0) {
5363 cnic_put(dev);
5364 goto done;
5365 }
Michael Chana4636962009-06-08 18:14:43 -07005366 if (!cnic_start_hw(dev))
5367 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005368 }
5369
5370 rcu_read_lock();
5371 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5372 struct cnic_ulp_ops *ulp_ops;
5373 void *ctx;
5374
5375 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5376 if (!ulp_ops || !ulp_ops->indicate_netevent)
5377 continue;
5378
5379 ctx = cp->ulp_handle[if_type];
5380
5381 ulp_ops->indicate_netevent(ctx, event);
5382 }
5383 rcu_read_unlock();
5384
5385 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005386 cnic_ulp_stop(dev);
5387 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005388 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005389 } else if (event == NETDEV_UNREGISTER) {
5390 write_lock(&cnic_dev_lock);
5391 list_del_init(&dev->list);
5392 write_unlock(&cnic_dev_lock);
5393
5394 cnic_put(dev);
5395 cnic_free_dev(dev);
5396 goto done;
5397 }
5398 cnic_put(dev);
5399 }
5400done:
5401 return NOTIFY_DONE;
5402}
5403
5404static struct notifier_block cnic_netdev_notifier = {
5405 .notifier_call = cnic_netdev_event
5406};
5407
5408static void cnic_release(void)
5409{
5410 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005411 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005412
5413 while (!list_empty(&cnic_dev_list)) {
5414 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5415 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5416 cnic_ulp_stop(dev);
5417 cnic_stop_hw(dev);
5418 }
5419
5420 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005421 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005422 list_del_init(&dev->list);
5423 cnic_free_dev(dev);
5424 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005425 while (!list_empty(&cnic_udev_list)) {
5426 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5427 list);
5428 cnic_free_uio(udev);
5429 }
Michael Chana4636962009-06-08 18:14:43 -07005430}
5431
5432static int __init cnic_init(void)
5433{
5434 int rc = 0;
5435
Joe Perchesddf79b22010-02-17 15:01:54 +00005436 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005437
5438 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5439 if (rc) {
5440 cnic_release();
5441 return rc;
5442 }
5443
Michael Chanfdf24082010-10-13 14:06:47 +00005444 cnic_wq = create_singlethread_workqueue("cnic_wq");
5445 if (!cnic_wq) {
5446 cnic_release();
5447 unregister_netdevice_notifier(&cnic_netdev_notifier);
5448 return -ENOMEM;
5449 }
5450
Michael Chana4636962009-06-08 18:14:43 -07005451 return 0;
5452}
5453
5454static void __exit cnic_exit(void)
5455{
5456 unregister_netdevice_notifier(&cnic_netdev_notifier);
5457 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005458 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005459}
5460
5461module_init(cnic_init);
5462module_exit(cnic_exit);