blob: 6dfa56440ac0928046d38596a52be84b7a723300 [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>
30#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
31#define BCM_VLAN 1
32#endif
33#include <net/ip.h>
34#include <net/tcp.h>
35#include <net/route.h>
36#include <net/ipv6.h>
37#include <net/ip6_route.h>
David S. Millerc05e85a2009-10-12 23:18:35 -070038#include <net/ip6_checksum.h>
Michael Chana4636962009-06-08 18:14:43 -070039#include <scsi/iscsi_if.h>
40
41#include "cnic_if.h"
42#include "bnx2.h"
Dmitry Kravkov5d1e8592010-07-27 12:31:10 +000043#include "bnx2x/bnx2x_reg.h"
44#include "bnx2x/bnx2x_fw_defs.h"
45#include "bnx2x/bnx2x_hsi.h"
Michael Chane2513062009-10-10 13:46:58 +000046#include "../scsi/bnx2i/57xx_iscsi_constants.h"
47#include "../scsi/bnx2i/57xx_iscsi_hsi.h"
Michael Chana4636962009-06-08 18:14:43 -070048#include "cnic.h"
49#include "cnic_defs.h"
50
51#define DRV_MODULE_NAME "cnic"
Michael Chana4636962009-06-08 18:14:43 -070052
53static char version[] __devinitdata =
54 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
55
56MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
57 "Chen (zongxi@broadcom.com");
58MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
59MODULE_LICENSE("GPL");
60MODULE_VERSION(CNIC_MODULE_VERSION);
61
Michael Chan8adc92402010-12-23 07:42:57 +000062/* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
Michael Chana4636962009-06-08 18:14:43 -070063static LIST_HEAD(cnic_dev_list);
Michael Chana3ceeeb2010-10-13 14:06:50 +000064static LIST_HEAD(cnic_udev_list);
Michael Chana4636962009-06-08 18:14:43 -070065static DEFINE_RWLOCK(cnic_dev_lock);
66static DEFINE_MUTEX(cnic_lock);
67
68static struct cnic_ulp_ops *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
69
70static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000071static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070072static int cnic_ctl(void *, struct cnic_ctl_info *);
73
74static struct cnic_ops cnic_bnx2_ops = {
75 .cnic_owner = THIS_MODULE,
76 .cnic_handler = cnic_service_bnx2,
77 .cnic_ctl = cnic_ctl,
78};
79
Michael Chan71034ba2009-10-10 13:46:59 +000080static struct cnic_ops cnic_bnx2x_ops = {
81 .cnic_owner = THIS_MODULE,
82 .cnic_handler = cnic_service_bnx2x,
83 .cnic_ctl = cnic_ctl,
84};
85
Michael Chanfdf24082010-10-13 14:06:47 +000086static struct workqueue_struct *cnic_wq;
87
Michael Chan86b53602009-10-10 13:46:57 +000088static void cnic_shutdown_rings(struct cnic_dev *);
89static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070090static int cnic_cm_set_pg(struct cnic_sock *);
91
92static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
93{
Michael Chancd801532010-10-13 14:06:49 +000094 struct cnic_uio_dev *udev = uinfo->priv;
95 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -070096
97 if (!capable(CAP_NET_ADMIN))
98 return -EPERM;
99
Michael Chancd801532010-10-13 14:06:49 +0000100 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700101 return -EBUSY;
102
Michael Chan86b53602009-10-10 13:46:57 +0000103 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000104 dev = udev->dev;
105
Michael Chana3ceeeb2010-10-13 14:06:50 +0000106 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000107 rtnl_unlock();
108 return -ENODEV;
109 }
110
Michael Chancd801532010-10-13 14:06:49 +0000111 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700112
Michael Chana3ceeeb2010-10-13 14:06:50 +0000113 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000114 cnic_init_rings(dev);
115 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700116
117 return 0;
118}
119
120static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
121{
Michael Chancd801532010-10-13 14:06:49 +0000122 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000123
Michael Chancd801532010-10-13 14:06:49 +0000124 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700125 return 0;
126}
127
128static inline void cnic_hold(struct cnic_dev *dev)
129{
130 atomic_inc(&dev->ref_count);
131}
132
133static inline void cnic_put(struct cnic_dev *dev)
134{
135 atomic_dec(&dev->ref_count);
136}
137
138static inline void csk_hold(struct cnic_sock *csk)
139{
140 atomic_inc(&csk->ref_count);
141}
142
143static inline void csk_put(struct cnic_sock *csk)
144{
145 atomic_dec(&csk->ref_count);
146}
147
148static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
149{
150 struct cnic_dev *cdev;
151
152 read_lock(&cnic_dev_lock);
153 list_for_each_entry(cdev, &cnic_dev_list, list) {
154 if (netdev == cdev->netdev) {
155 cnic_hold(cdev);
156 read_unlock(&cnic_dev_lock);
157 return cdev;
158 }
159 }
160 read_unlock(&cnic_dev_lock);
161 return NULL;
162}
163
Michael Chan7fc1ece2009-08-14 15:49:47 +0000164static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
165{
166 atomic_inc(&ulp_ops->ref_count);
167}
168
169static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
170{
171 atomic_dec(&ulp_ops->ref_count);
172}
173
Michael Chana4636962009-06-08 18:14:43 -0700174static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
175{
176 struct cnic_local *cp = dev->cnic_priv;
177 struct cnic_eth_dev *ethdev = cp->ethdev;
178 struct drv_ctl_info info;
179 struct drv_ctl_io *io = &info.data.io;
180
181 info.cmd = DRV_CTL_CTX_WR_CMD;
182 io->cid_addr = cid_addr;
183 io->offset = off;
184 io->data = val;
185 ethdev->drv_ctl(dev->netdev, &info);
186}
187
Michael Chan71034ba2009-10-10 13:46:59 +0000188static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
189{
190 struct cnic_local *cp = dev->cnic_priv;
191 struct cnic_eth_dev *ethdev = cp->ethdev;
192 struct drv_ctl_info info;
193 struct drv_ctl_io *io = &info.data.io;
194
195 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
196 io->offset = off;
197 io->dma_addr = addr;
198 ethdev->drv_ctl(dev->netdev, &info);
199}
200
201static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
202{
203 struct cnic_local *cp = dev->cnic_priv;
204 struct cnic_eth_dev *ethdev = cp->ethdev;
205 struct drv_ctl_info info;
206 struct drv_ctl_l2_ring *ring = &info.data.ring;
207
208 if (start)
209 info.cmd = DRV_CTL_START_L2_CMD;
210 else
211 info.cmd = DRV_CTL_STOP_L2_CMD;
212
213 ring->cid = cid;
214 ring->client_id = cl_id;
215 ethdev->drv_ctl(dev->netdev, &info);
216}
217
Michael Chana4636962009-06-08 18:14:43 -0700218static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
219{
220 struct cnic_local *cp = dev->cnic_priv;
221 struct cnic_eth_dev *ethdev = cp->ethdev;
222 struct drv_ctl_info info;
223 struct drv_ctl_io *io = &info.data.io;
224
225 info.cmd = DRV_CTL_IO_WR_CMD;
226 io->offset = off;
227 io->data = val;
228 ethdev->drv_ctl(dev->netdev, &info);
229}
230
231static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
232{
233 struct cnic_local *cp = dev->cnic_priv;
234 struct cnic_eth_dev *ethdev = cp->ethdev;
235 struct drv_ctl_info info;
236 struct drv_ctl_io *io = &info.data.io;
237
238 info.cmd = DRV_CTL_IO_RD_CMD;
239 io->offset = off;
240 ethdev->drv_ctl(dev->netdev, &info);
241 return io->data;
242}
243
244static int cnic_in_use(struct cnic_sock *csk)
245{
246 return test_bit(SK_F_INUSE, &csk->flags);
247}
248
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000249static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700250{
251 struct cnic_local *cp = dev->cnic_priv;
252 struct cnic_eth_dev *ethdev = cp->ethdev;
253 struct drv_ctl_info info;
254
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000255 info.cmd = cmd;
256 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700257 ethdev->drv_ctl(dev->netdev, &info);
258}
259
Michael Chan71034ba2009-10-10 13:46:59 +0000260static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
261{
262 u32 i;
263
Michael Chan520efdf2010-06-24 14:58:37 +0000264 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000265 if (cp->ctx_tbl[i].cid == cid) {
266 *l5_cid = i;
267 return 0;
268 }
269 }
270 return -EINVAL;
271}
272
Michael Chana4636962009-06-08 18:14:43 -0700273static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
274 struct cnic_sock *csk)
275{
276 struct iscsi_path path_req;
277 char *buf = NULL;
278 u16 len = 0;
279 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
280 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000281 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000282 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700283
Michael Chancd801532010-10-13 14:06:49 +0000284 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700285 return -ENODEV;
286
287 if (csk) {
288 len = sizeof(path_req);
289 buf = (char *) &path_req;
290 memset(&path_req, 0, len);
291
292 msg_type = ISCSI_KEVENT_PATH_REQ;
293 path_req.handle = (u64) csk->l5_cid;
294 if (test_bit(SK_F_IPV6, &csk->flags)) {
295 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
296 sizeof(struct in6_addr));
297 path_req.ip_addr_len = 16;
298 } else {
299 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
300 sizeof(struct in_addr));
301 path_req.ip_addr_len = 4;
302 }
303 path_req.vlan_id = csk->vlan_id;
304 path_req.pmtu = csk->mtu;
305 }
306
Michael Chan939b82e2010-12-23 07:42:58 +0000307 while (retry < 3) {
308 rc = 0;
309 rcu_read_lock();
310 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
311 if (ulp_ops)
312 rc = ulp_ops->iscsi_nl_send_msg(
313 cp->ulp_handle[CNIC_ULP_ISCSI],
314 msg_type, buf, len);
315 rcu_read_unlock();
316 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
317 break;
318
319 msleep(100);
320 retry++;
321 }
Michael Chana4636962009-06-08 18:14:43 -0700322 return 0;
323}
324
Eddie Wai42ecbb82010-12-23 07:43:02 +0000325static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
326
Michael Chana4636962009-06-08 18:14:43 -0700327static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
328 char *buf, u16 len)
329{
330 int rc = -EINVAL;
331
332 switch (msg_type) {
333 case ISCSI_UEVENT_PATH_UPDATE: {
334 struct cnic_local *cp;
335 u32 l5_cid;
336 struct cnic_sock *csk;
337 struct iscsi_path *path_resp;
338
339 if (len < sizeof(*path_resp))
340 break;
341
342 path_resp = (struct iscsi_path *) buf;
343 cp = dev->cnic_priv;
344 l5_cid = (u32) path_resp->handle;
345 if (l5_cid >= MAX_CM_SK_TBL_SZ)
346 break;
347
Michael Chand02a5e62010-02-24 14:42:06 +0000348 rcu_read_lock();
349 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
350 rc = -ENODEV;
351 rcu_read_unlock();
352 break;
353 }
Michael Chana4636962009-06-08 18:14:43 -0700354 csk = &cp->csk_tbl[l5_cid];
355 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000356 if (cnic_in_use(csk) &&
357 test_bit(SK_F_CONNECT_START, &csk->flags)) {
358
Michael Chana4636962009-06-08 18:14:43 -0700359 memcpy(csk->ha, path_resp->mac_addr, 6);
360 if (test_bit(SK_F_IPV6, &csk->flags))
361 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
362 sizeof(struct in6_addr));
363 else
364 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
365 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000366
367 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700368 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000369 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
370 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
371
372 cnic_cm_upcall(cp, csk,
373 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
374 clear_bit(SK_F_CONNECT_START, &csk->flags);
375 }
Michael Chana4636962009-06-08 18:14:43 -0700376 }
377 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000378 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700379 rc = 0;
380 }
381 }
382
383 return rc;
384}
385
386static int cnic_offld_prep(struct cnic_sock *csk)
387{
388 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
389 return 0;
390
391 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
392 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
393 return 0;
394 }
395
396 return 1;
397}
398
399static int cnic_close_prep(struct cnic_sock *csk)
400{
401 clear_bit(SK_F_CONNECT_START, &csk->flags);
402 smp_mb__after_clear_bit();
403
404 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
405 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
406 msleep(1);
407
408 return 1;
409 }
410 return 0;
411}
412
413static int cnic_abort_prep(struct cnic_sock *csk)
414{
415 clear_bit(SK_F_CONNECT_START, &csk->flags);
416 smp_mb__after_clear_bit();
417
418 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
419 msleep(1);
420
421 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
422 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
423 return 1;
424 }
425
426 return 0;
427}
428
429int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
430{
431 struct cnic_dev *dev;
432
roel kluin0d37f362009-11-02 06:53:44 +0000433 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000434 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700435 return -EINVAL;
436 }
437 mutex_lock(&cnic_lock);
438 if (cnic_ulp_tbl[ulp_type]) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000439 pr_err("%s: Type %d has already been registered\n",
440 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700441 mutex_unlock(&cnic_lock);
442 return -EBUSY;
443 }
444
445 read_lock(&cnic_dev_lock);
446 list_for_each_entry(dev, &cnic_dev_list, list) {
447 struct cnic_local *cp = dev->cnic_priv;
448
449 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
450 }
451 read_unlock(&cnic_dev_lock);
452
Michael Chan7fc1ece2009-08-14 15:49:47 +0000453 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700454 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
455 mutex_unlock(&cnic_lock);
456
457 /* Prevent race conditions with netdev_event */
458 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700459 list_for_each_entry(dev, &cnic_dev_list, list) {
460 struct cnic_local *cp = dev->cnic_priv;
461
462 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
463 ulp_ops->cnic_init(dev);
464 }
Michael Chana4636962009-06-08 18:14:43 -0700465 rtnl_unlock();
466
467 return 0;
468}
469
470int cnic_unregister_driver(int ulp_type)
471{
472 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000473 struct cnic_ulp_ops *ulp_ops;
474 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700475
roel kluin0d37f362009-11-02 06:53:44 +0000476 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000477 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700478 return -EINVAL;
479 }
480 mutex_lock(&cnic_lock);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000481 ulp_ops = cnic_ulp_tbl[ulp_type];
482 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000483 pr_err("%s: Type %d has not been registered\n",
484 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700485 goto out_unlock;
486 }
487 read_lock(&cnic_dev_lock);
488 list_for_each_entry(dev, &cnic_dev_list, list) {
489 struct cnic_local *cp = dev->cnic_priv;
490
491 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000492 pr_err("%s: Type %d still has devices registered\n",
493 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700494 read_unlock(&cnic_dev_lock);
495 goto out_unlock;
496 }
497 }
498 read_unlock(&cnic_dev_lock);
499
500 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
501
502 mutex_unlock(&cnic_lock);
503 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000504 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
505 msleep(100);
506 i++;
507 }
508
509 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000510 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700511 return 0;
512
513out_unlock:
514 mutex_unlock(&cnic_lock);
515 return -EINVAL;
516}
517
518static int cnic_start_hw(struct cnic_dev *);
519static void cnic_stop_hw(struct cnic_dev *);
520
521static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
522 void *ulp_ctx)
523{
524 struct cnic_local *cp = dev->cnic_priv;
525 struct cnic_ulp_ops *ulp_ops;
526
roel kluin0d37f362009-11-02 06:53:44 +0000527 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000528 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700529 return -EINVAL;
530 }
531 mutex_lock(&cnic_lock);
532 if (cnic_ulp_tbl[ulp_type] == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000533 pr_err("%s: Driver with type %d has not been registered\n",
534 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700535 mutex_unlock(&cnic_lock);
536 return -EAGAIN;
537 }
538 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000539 pr_err("%s: Type %d has already been registered to this device\n",
540 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700541 mutex_unlock(&cnic_lock);
542 return -EBUSY;
543 }
544
545 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
546 cp->ulp_handle[ulp_type] = ulp_ctx;
547 ulp_ops = cnic_ulp_tbl[ulp_type];
548 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
549 cnic_hold(dev);
550
551 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
552 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
553 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
554
555 mutex_unlock(&cnic_lock);
556
557 return 0;
558
559}
560EXPORT_SYMBOL(cnic_register_driver);
561
562static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
563{
564 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000565 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700566
roel kluin0d37f362009-11-02 06:53:44 +0000567 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000568 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700569 return -EINVAL;
570 }
571 mutex_lock(&cnic_lock);
572 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
573 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
574 cnic_put(dev);
575 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000576 pr_err("%s: device not registered to this ulp type %d\n",
577 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700578 mutex_unlock(&cnic_lock);
579 return -EINVAL;
580 }
581 mutex_unlock(&cnic_lock);
582
Michael Chan42bb8d52011-01-03 15:21:46 +0000583 if (ulp_type == CNIC_ULP_ISCSI)
584 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
585
Michael Chana4636962009-06-08 18:14:43 -0700586 synchronize_rcu();
587
Michael Chan681dbd72009-08-14 15:49:46 +0000588 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
589 i < 20) {
590 msleep(100);
591 i++;
592 }
593 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000594 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000595
Michael Chana4636962009-06-08 18:14:43 -0700596 return 0;
597}
598EXPORT_SYMBOL(cnic_unregister_driver);
599
600static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
601{
602 id_tbl->start = start_id;
603 id_tbl->max = size;
604 id_tbl->next = 0;
605 spin_lock_init(&id_tbl->lock);
606 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
607 if (!id_tbl->table)
608 return -ENOMEM;
609
610 return 0;
611}
612
613static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
614{
615 kfree(id_tbl->table);
616 id_tbl->table = NULL;
617}
618
619static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
620{
621 int ret = -1;
622
623 id -= id_tbl->start;
624 if (id >= id_tbl->max)
625 return ret;
626
627 spin_lock(&id_tbl->lock);
628 if (!test_bit(id, id_tbl->table)) {
629 set_bit(id, id_tbl->table);
630 ret = 0;
631 }
632 spin_unlock(&id_tbl->lock);
633 return ret;
634}
635
636/* Returns -1 if not successful */
637static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
638{
639 u32 id;
640
641 spin_lock(&id_tbl->lock);
642 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
643 if (id >= id_tbl->max) {
644 id = -1;
645 if (id_tbl->next != 0) {
646 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
647 if (id >= id_tbl->next)
648 id = -1;
649 }
650 }
651
652 if (id < id_tbl->max) {
653 set_bit(id, id_tbl->table);
654 id_tbl->next = (id + 1) & (id_tbl->max - 1);
655 id += id_tbl->start;
656 }
657
658 spin_unlock(&id_tbl->lock);
659
660 return id;
661}
662
663static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
664{
665 if (id == -1)
666 return;
667
668 id -= id_tbl->start;
669 if (id >= id_tbl->max)
670 return;
671
672 clear_bit(id, id_tbl->table);
673}
674
675static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
676{
677 int i;
678
679 if (!dma->pg_arr)
680 return;
681
682 for (i = 0; i < dma->num_pages; i++) {
683 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000684 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
685 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700686 dma->pg_arr[i] = NULL;
687 }
688 }
689 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000690 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
691 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700692 dma->pgtbl = NULL;
693 }
694 kfree(dma->pg_arr);
695 dma->pg_arr = NULL;
696 dma->num_pages = 0;
697}
698
699static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
700{
701 int i;
702 u32 *page_table = dma->pgtbl;
703
704 for (i = 0; i < dma->num_pages; i++) {
705 /* Each entry needs to be in big endian format. */
706 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
707 page_table++;
708 *page_table = (u32) dma->pg_map_arr[i];
709 page_table++;
710 }
711}
712
Michael Chan71034ba2009-10-10 13:46:59 +0000713static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
714{
715 int i;
716 u32 *page_table = dma->pgtbl;
717
718 for (i = 0; i < dma->num_pages; i++) {
719 /* Each entry needs to be in little endian format. */
720 *page_table = dma->pg_map_arr[i] & 0xffffffff;
721 page_table++;
722 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
723 page_table++;
724 }
725}
726
Michael Chana4636962009-06-08 18:14:43 -0700727static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
728 int pages, int use_pg_tbl)
729{
730 int i, size;
731 struct cnic_local *cp = dev->cnic_priv;
732
733 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
734 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
735 if (dma->pg_arr == NULL)
736 return -ENOMEM;
737
738 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
739 dma->num_pages = pages;
740
741 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000742 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
743 BCM_PAGE_SIZE,
744 &dma->pg_map_arr[i],
745 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700746 if (dma->pg_arr[i] == NULL)
747 goto error;
748 }
749 if (!use_pg_tbl)
750 return 0;
751
752 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
753 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000754 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
755 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700756 if (dma->pgtbl == NULL)
757 goto error;
758
759 cp->setup_pgtbl(dev, dma);
760
761 return 0;
762
763error:
764 cnic_free_dma(dev, dma);
765 return -ENOMEM;
766}
767
Michael Chan86b53602009-10-10 13:46:57 +0000768static void cnic_free_context(struct cnic_dev *dev)
769{
770 struct cnic_local *cp = dev->cnic_priv;
771 int i;
772
773 for (i = 0; i < cp->ctx_blks; i++) {
774 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000775 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
776 cp->ctx_arr[i].ctx,
777 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000778 cp->ctx_arr[i].ctx = NULL;
779 }
780 }
781}
782
Michael Chancd801532010-10-13 14:06:49 +0000783static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700784{
Michael Chancd801532010-10-13 14:06:49 +0000785 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700786
Michael Chancd801532010-10-13 14:06:49 +0000787 if (udev->l2_buf) {
788 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
789 udev->l2_buf, udev->l2_buf_map);
790 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700791 }
792
Michael Chancd801532010-10-13 14:06:49 +0000793 if (udev->l2_ring) {
794 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
795 udev->l2_ring, udev->l2_ring_map);
796 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700797 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000798
799 pci_dev_put(udev->pdev);
800 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000801}
802
Michael Chancd801532010-10-13 14:06:49 +0000803static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000804{
Michael Chancd801532010-10-13 14:06:49 +0000805 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000806 return;
807
Michael Chana3ceeeb2010-10-13 14:06:50 +0000808 write_lock(&cnic_dev_lock);
809 list_del_init(&udev->list);
810 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000811 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000812}
813
814static void cnic_free_resc(struct cnic_dev *dev)
815{
816 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000817 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000818
Michael Chancd801532010-10-13 14:06:49 +0000819 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000820 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000821 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000822 }
Michael Chana4636962009-06-08 18:14:43 -0700823
Michael Chan86b53602009-10-10 13:46:57 +0000824 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700825 kfree(cp->ctx_arr);
826 cp->ctx_arr = NULL;
827 cp->ctx_blks = 0;
828
829 cnic_free_dma(dev, &cp->gbl_buf_info);
830 cnic_free_dma(dev, &cp->conn_buf_info);
831 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000832 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000833 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000834 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700835 kfree(cp->iscsi_tbl);
836 cp->iscsi_tbl = NULL;
837 kfree(cp->ctx_tbl);
838 cp->ctx_tbl = NULL;
839
Michael Chane1928c82010-12-23 07:43:04 +0000840 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700841 cnic_free_id_tbl(&cp->cid_tbl);
842}
843
844static int cnic_alloc_context(struct cnic_dev *dev)
845{
846 struct cnic_local *cp = dev->cnic_priv;
847
848 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
849 int i, k, arr_size;
850
851 cp->ctx_blk_size = BCM_PAGE_SIZE;
852 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
853 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
854 sizeof(struct cnic_ctx);
855 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
856 if (cp->ctx_arr == NULL)
857 return -ENOMEM;
858
859 k = 0;
860 for (i = 0; i < 2; i++) {
861 u32 j, reg, off, lo, hi;
862
863 if (i == 0)
864 off = BNX2_PG_CTX_MAP;
865 else
866 off = BNX2_ISCSI_CTX_MAP;
867
868 reg = cnic_reg_rd_ind(dev, off);
869 lo = reg >> 16;
870 hi = reg & 0xffff;
871 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
872 cp->ctx_arr[k].cid = j;
873 }
874
875 cp->ctx_blks = k;
876 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
877 cp->ctx_blks = 0;
878 return -ENOMEM;
879 }
880
881 for (i = 0; i < cp->ctx_blks; i++) {
882 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000883 dma_alloc_coherent(&dev->pcidev->dev,
884 BCM_PAGE_SIZE,
885 &cp->ctx_arr[i].mapping,
886 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700887 if (cp->ctx_arr[i].ctx == NULL)
888 return -ENOMEM;
889 }
890 }
891 return 0;
892}
893
Michael Chane6c28892010-06-24 14:58:39 +0000894static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
895{
896 int err, i, is_bnx2 = 0;
897 struct kcqe **kcq;
898
899 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
900 is_bnx2 = 1;
901
902 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
903 if (err)
904 return err;
905
906 kcq = (struct kcqe **) info->dma.pg_arr;
907 info->kcq = kcq;
908
909 if (is_bnx2)
910 return 0;
911
912 for (i = 0; i < KCQ_PAGE_CNT; i++) {
913 struct bnx2x_bd_chain_next *next =
914 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
915 int j = i + 1;
916
917 if (j >= KCQ_PAGE_CNT)
918 j = 0;
919 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
920 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
921 }
922 return 0;
923}
924
Michael Chancd801532010-10-13 14:06:49 +0000925static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000926{
927 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000928 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000929
Michael Chana3ceeeb2010-10-13 14:06:50 +0000930 read_lock(&cnic_dev_lock);
931 list_for_each_entry(udev, &cnic_udev_list, list) {
932 if (udev->pdev == dev->pcidev) {
933 udev->dev = dev;
934 cp->udev = udev;
935 read_unlock(&cnic_dev_lock);
936 return 0;
937 }
938 }
939 read_unlock(&cnic_dev_lock);
940
Michael Chancd801532010-10-13 14:06:49 +0000941 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
942 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +0000943 return -ENOMEM;
944
Michael Chancd801532010-10-13 14:06:49 +0000945 udev->uio_dev = -1;
946
947 udev->dev = dev;
948 udev->pdev = dev->pcidev;
949 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
950 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
951 &udev->l2_ring_map,
952 GFP_KERNEL | __GFP_COMP);
953 if (!udev->l2_ring)
Michael Chanec0248e2009-08-26 09:49:22 +0000954 return -ENOMEM;
955
Michael Chancd801532010-10-13 14:06:49 +0000956 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
957 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
958 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
959 &udev->l2_buf_map,
960 GFP_KERNEL | __GFP_COMP);
961 if (!udev->l2_buf)
962 return -ENOMEM;
963
Michael Chana3ceeeb2010-10-13 14:06:50 +0000964 write_lock(&cnic_dev_lock);
965 list_add(&udev->list, &cnic_udev_list);
966 write_unlock(&cnic_dev_lock);
967
968 pci_dev_get(udev->pdev);
969
Michael Chancd801532010-10-13 14:06:49 +0000970 cp->udev = udev;
971
Michael Chanec0248e2009-08-26 09:49:22 +0000972 return 0;
973}
974
Michael Chancd801532010-10-13 14:06:49 +0000975static int cnic_init_uio(struct cnic_dev *dev)
976{
Michael Chan5e9b2db2009-08-26 09:49:23 +0000977 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000978 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000979 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +0000980 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000981
Michael Chancd801532010-10-13 14:06:49 +0000982 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +0000983 return -ENOMEM;
984
Michael Chancd801532010-10-13 14:06:49 +0000985 uinfo = &udev->cnic_uinfo;
986
Michael Chan5e9b2db2009-08-26 09:49:23 +0000987 uinfo->mem[0].addr = dev->netdev->base_addr;
988 uinfo->mem[0].internal_addr = dev->regview;
989 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
990 uinfo->mem[0].memtype = UIO_MEM_PHYS;
991
Michael Chan5e9b2db2009-08-26 09:49:23 +0000992 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +0000993 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +0000994 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000995 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
996 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
997 else
998 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
999
1000 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001001 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1002 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1003 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001004 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001005
1006 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001007 }
1008
1009 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1010
Michael Chancd801532010-10-13 14:06:49 +00001011 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1012 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001013 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1014
Michael Chancd801532010-10-13 14:06:49 +00001015 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1016 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001017 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1018
1019 uinfo->version = CNIC_MODULE_VERSION;
1020 uinfo->irq = UIO_IRQ_CUSTOM;
1021
1022 uinfo->open = cnic_uio_open;
1023 uinfo->release = cnic_uio_close;
1024
Michael Chana3ceeeb2010-10-13 14:06:50 +00001025 if (udev->uio_dev == -1) {
1026 if (!uinfo->priv) {
1027 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001028
Michael Chana3ceeeb2010-10-13 14:06:50 +00001029 ret = uio_register_device(&udev->pdev->dev, uinfo);
1030 }
1031 } else {
1032 cnic_init_rings(dev);
1033 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001034
Michael Chancd801532010-10-13 14:06:49 +00001035 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001036}
1037
Michael Chana4636962009-06-08 18:14:43 -07001038static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1039{
1040 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001041 int ret;
1042
1043 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1044 if (ret)
1045 goto error;
1046 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1047
Michael Chane6c28892010-06-24 14:58:39 +00001048 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chana4636962009-06-08 18:14:43 -07001049 if (ret)
1050 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001051
1052 ret = cnic_alloc_context(dev);
1053 if (ret)
1054 goto error;
1055
Michael Chancd801532010-10-13 14:06:49 +00001056 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001057 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001058 goto error;
1059
Michael Chancd801532010-10-13 14:06:49 +00001060 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001061 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001062 goto error;
1063
Michael Chana4636962009-06-08 18:14:43 -07001064 return 0;
1065
1066error:
1067 cnic_free_resc(dev);
1068 return ret;
1069}
1070
Michael Chan71034ba2009-10-10 13:46:59 +00001071static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1072{
1073 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001074 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001075 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001076
Michael Chan520efdf2010-06-24 14:58:37 +00001077 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001078 blks = total_mem / ctx_blk_size;
1079 if (total_mem % ctx_blk_size)
1080 blks++;
1081
1082 if (blks > cp->ethdev->ctx_tbl_len)
1083 return -ENOMEM;
1084
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001085 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001086 if (cp->ctx_arr == NULL)
1087 return -ENOMEM;
1088
1089 cp->ctx_blks = blks;
1090 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001091 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001092 cp->ctx_align = 0;
1093 else
1094 cp->ctx_align = ctx_blk_size;
1095
1096 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1097
1098 for (i = 0; i < blks; i++) {
1099 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001100 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1101 &cp->ctx_arr[i].mapping,
1102 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001103 if (cp->ctx_arr[i].ctx == NULL)
1104 return -ENOMEM;
1105
1106 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1107 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1108 cnic_free_context(dev);
1109 cp->ctx_blk_size += cp->ctx_align;
1110 i = -1;
1111 continue;
1112 }
1113 }
1114 }
1115 return 0;
1116}
1117
1118static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1119{
1120 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001121 struct cnic_eth_dev *ethdev = cp->ethdev;
1122 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001123 int i, j, n, ret, pages;
1124 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1125
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001126 cp->iro_arr = ethdev->iro_arr;
1127
Michael Chane1928c82010-12-23 07:43:04 +00001128 cp->max_cid_space = MAX_ISCSI_TBL_SZ + BNX2X_FCOE_NUM_CONNECTIONS;
Michael Chan520efdf2010-06-24 14:58:37 +00001129 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001130 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1131
1132 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1133 cp->max_cid_space += BNX2X_FCOE_NUM_CONNECTIONS;
1134 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1135 if (!cp->fcoe_init_cid)
1136 cp->fcoe_init_cid = 0x10;
1137 }
1138
Michael Chan520efdf2010-06-24 14:58:37 +00001139 if (start_cid < BNX2X_ISCSI_START_CID) {
1140 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1141
1142 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
Michael Chane1928c82010-12-23 07:43:04 +00001143 cp->fcoe_start_cid += delta;
Michael Chan520efdf2010-06-24 14:58:37 +00001144 cp->max_cid_space += delta;
1145 }
1146
Michael Chan71034ba2009-10-10 13:46:59 +00001147 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1148 GFP_KERNEL);
1149 if (!cp->iscsi_tbl)
1150 goto error;
1151
1152 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001153 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001154 if (!cp->ctx_tbl)
1155 goto error;
1156
1157 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1158 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1159 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1160 }
1161
Michael Chane1928c82010-12-23 07:43:04 +00001162 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1163 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1164
Michael Chan520efdf2010-06-24 14:58:37 +00001165 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001166 PAGE_SIZE;
1167
1168 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1169 if (ret)
1170 return -ENOMEM;
1171
1172 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001173 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001174 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1175
1176 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1177 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1178 off;
1179
1180 if ((i % n) == (n - 1))
1181 j++;
1182 }
1183
Michael Chane6c28892010-06-24 14:58:39 +00001184 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00001185 if (ret)
1186 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001187
Michael Chane21ba412010-12-23 07:43:03 +00001188 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1189 ret = cnic_alloc_kcq(dev, &cp->kcq2);
1190 if (ret)
1191 goto error;
1192 }
1193
Michael Chan71034ba2009-10-10 13:46:59 +00001194 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1195 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1196 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1197 if (ret)
1198 goto error;
1199
1200 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1201 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1202 if (ret)
1203 goto error;
1204
1205 ret = cnic_alloc_bnx2x_context(dev);
1206 if (ret)
1207 goto error;
1208
Michael Chan71034ba2009-10-10 13:46:59 +00001209 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1210
1211 cp->l2_rx_ring_size = 15;
1212
Michael Chancd801532010-10-13 14:06:49 +00001213 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001214 if (ret)
1215 goto error;
1216
Michael Chancd801532010-10-13 14:06:49 +00001217 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001218 if (ret)
1219 goto error;
1220
1221 return 0;
1222
1223error:
1224 cnic_free_resc(dev);
1225 return -ENOMEM;
1226}
1227
Michael Chana4636962009-06-08 18:14:43 -07001228static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1229{
1230 return cp->max_kwq_idx -
1231 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1232}
1233
1234static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1235 u32 num_wqes)
1236{
1237 struct cnic_local *cp = dev->cnic_priv;
1238 struct kwqe *prod_qe;
1239 u16 prod, sw_prod, i;
1240
1241 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1242 return -EAGAIN; /* bnx2 is down */
1243
1244 spin_lock_bh(&cp->cnic_ulp_lock);
1245 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001246 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001247 spin_unlock_bh(&cp->cnic_ulp_lock);
1248 return -EAGAIN;
1249 }
1250
Michael Chan1f1332a2010-05-18 11:32:52 +00001251 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001252
1253 prod = cp->kwq_prod_idx;
1254 sw_prod = prod & MAX_KWQ_IDX;
1255 for (i = 0; i < num_wqes; i++) {
1256 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1257 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1258 prod++;
1259 sw_prod = prod & MAX_KWQ_IDX;
1260 }
1261 cp->kwq_prod_idx = prod;
1262
1263 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1264
1265 spin_unlock_bh(&cp->cnic_ulp_lock);
1266 return 0;
1267}
1268
Michael Chan71034ba2009-10-10 13:46:59 +00001269static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1270 union l5cm_specific_data *l5_data)
1271{
1272 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1273 dma_addr_t map;
1274
1275 map = ctx->kwqe_data_mapping;
1276 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1277 l5_data->phy_address.hi = (u64) map >> 32;
1278 return ctx->kwqe_data;
1279}
1280
1281static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1282 u32 type, union l5cm_specific_data *l5_data)
1283{
1284 struct cnic_local *cp = dev->cnic_priv;
1285 struct l5cm_spe kwqe;
1286 struct kwqe_16 *kwq[1];
1287 int ret;
1288
1289 kwqe.hdr.conn_and_cmd_data =
1290 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001291 BNX2X_HW_CID(cp, cid)));
Michael Chan71034ba2009-10-10 13:46:59 +00001292 kwqe.hdr.type = cpu_to_le16(type);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001293 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001294 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1295 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1296
1297 kwq[0] = (struct kwqe_16 *) &kwqe;
1298
1299 spin_lock_bh(&cp->cnic_ulp_lock);
1300 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1301 spin_unlock_bh(&cp->cnic_ulp_lock);
1302
1303 if (ret == 1)
1304 return 0;
1305
1306 return -EBUSY;
1307}
1308
1309static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1310 struct kcqe *cqes[], u32 num_cqes)
1311{
1312 struct cnic_local *cp = dev->cnic_priv;
1313 struct cnic_ulp_ops *ulp_ops;
1314
1315 rcu_read_lock();
1316 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1317 if (likely(ulp_ops)) {
1318 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1319 cqes, num_cqes);
1320 }
1321 rcu_read_unlock();
1322}
1323
1324static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1325{
1326 struct cnic_local *cp = dev->cnic_priv;
1327 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001328 int hq_bds, pages;
1329 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001330
1331 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1332 cp->num_ccells = req1->num_ccells_per_conn;
1333 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1334 cp->num_iscsi_tasks;
1335 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1336 BNX2X_ISCSI_R2TQE_SIZE;
1337 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1338 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1339 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1340 cp->num_cqs = req1->num_cqs;
1341
1342 if (!dev->max_iscsi_conn)
1343 return 0;
1344
1345 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001346 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001347 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001348 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001349 PAGE_SIZE);
1350 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001351 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001352 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001353 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001354 req1->num_tasks_per_conn);
1355
1356 /* init Ustorm RAM */
1357 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001358 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001359 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001360 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001361 PAGE_SIZE);
1362 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001363 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001364 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001365 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001366 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001367 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_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_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001370 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001371 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001372 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1373
1374 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001375 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001376 PAGE_SIZE);
1377 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001378 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001379 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001380 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001381 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001382 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001383 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001384 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001385 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001386 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001387 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1388
1389 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001390 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001391 PAGE_SIZE);
1392 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001393 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001394 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001395 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001396 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001397 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001398 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001399 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001400 hq_bds);
1401
1402 return 0;
1403}
1404
1405static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1406{
1407 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1408 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001409 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001410 struct iscsi_kcqe kcqe;
1411 struct kcqe *cqes[1];
1412
1413 memset(&kcqe, 0, sizeof(kcqe));
1414 if (!dev->max_iscsi_conn) {
1415 kcqe.completion_status =
1416 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1417 goto done;
1418 }
1419
1420 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001421 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001422 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001423 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001424 req2->error_bit_map[1]);
1425
1426 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001427 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001428 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001429 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001430 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001431 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001432 req2->error_bit_map[1]);
1433
1434 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001435 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001436
1437 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1438
1439done:
1440 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1441 cqes[0] = (struct kcqe *) &kcqe;
1442 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1443
1444 return 0;
1445}
1446
1447static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1448{
1449 struct cnic_local *cp = dev->cnic_priv;
1450 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1451
1452 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1453 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1454
1455 cnic_free_dma(dev, &iscsi->hq_info);
1456 cnic_free_dma(dev, &iscsi->r2tq_info);
1457 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001458 cnic_free_id(&cp->cid_tbl, ctx->cid);
1459 } else {
1460 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001461 }
Michael Chane1928c82010-12-23 07:43:04 +00001462
Michael Chan71034ba2009-10-10 13:46:59 +00001463 ctx->cid = 0;
1464}
1465
1466static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1467{
1468 u32 cid;
1469 int ret, pages;
1470 struct cnic_local *cp = dev->cnic_priv;
1471 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1472 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1473
Michael Chane1928c82010-12-23 07:43:04 +00001474 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1475 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1476 if (cid == -1) {
1477 ret = -ENOMEM;
1478 goto error;
1479 }
1480 ctx->cid = cid;
1481 return 0;
1482 }
1483
Michael Chan71034ba2009-10-10 13:46:59 +00001484 cid = cnic_alloc_new_id(&cp->cid_tbl);
1485 if (cid == -1) {
1486 ret = -ENOMEM;
1487 goto error;
1488 }
1489
1490 ctx->cid = cid;
1491 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1492
1493 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1494 if (ret)
1495 goto error;
1496
1497 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1498 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1499 if (ret)
1500 goto error;
1501
1502 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1503 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1504 if (ret)
1505 goto error;
1506
1507 return 0;
1508
1509error:
1510 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1511 return ret;
1512}
1513
1514static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1515 struct regpair *ctx_addr)
1516{
1517 struct cnic_local *cp = dev->cnic_priv;
1518 struct cnic_eth_dev *ethdev = cp->ethdev;
1519 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1520 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1521 unsigned long align_off = 0;
1522 dma_addr_t ctx_map;
1523 void *ctx;
1524
1525 if (cp->ctx_align) {
1526 unsigned long mask = cp->ctx_align - 1;
1527
1528 if (cp->ctx_arr[blk].mapping & mask)
1529 align_off = cp->ctx_align -
1530 (cp->ctx_arr[blk].mapping & mask);
1531 }
1532 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1533 (off * BNX2X_CONTEXT_MEM_SIZE);
1534 ctx = cp->ctx_arr[blk].ctx + align_off +
1535 (off * BNX2X_CONTEXT_MEM_SIZE);
1536 if (init)
1537 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1538
1539 ctx_addr->lo = ctx_map & 0xffffffff;
1540 ctx_addr->hi = (u64) ctx_map >> 32;
1541 return ctx;
1542}
1543
1544static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1545 u32 num)
1546{
1547 struct cnic_local *cp = dev->cnic_priv;
1548 struct iscsi_kwqe_conn_offload1 *req1 =
1549 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1550 struct iscsi_kwqe_conn_offload2 *req2 =
1551 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1552 struct iscsi_kwqe_conn_offload3 *req3;
1553 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1554 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1555 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001556 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001557 struct iscsi_context *ictx;
1558 struct regpair context_addr;
1559 int i, j, n = 2, n_max;
1560
1561 ctx->ctx_flags = 0;
1562 if (!req2->num_additional_wqes)
1563 return -EINVAL;
1564
1565 n_max = req2->num_additional_wqes + 2;
1566
1567 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1568 if (ictx == NULL)
1569 return -ENOMEM;
1570
1571 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1572
1573 ictx->xstorm_ag_context.hq_prod = 1;
1574
1575 ictx->xstorm_st_context.iscsi.first_burst_length =
1576 ISCSI_DEF_FIRST_BURST_LEN;
1577 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1578 ISCSI_DEF_MAX_RECV_SEG_LEN;
1579 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1580 req1->sq_page_table_addr_lo;
1581 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1582 req1->sq_page_table_addr_hi;
1583 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1584 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1585 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1586 iscsi->hq_info.pgtbl_map & 0xffffffff;
1587 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1588 (u64) iscsi->hq_info.pgtbl_map >> 32;
1589 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1590 iscsi->hq_info.pgtbl[0];
1591 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1592 iscsi->hq_info.pgtbl[1];
1593 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1594 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1595 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1596 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1597 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1598 iscsi->r2tq_info.pgtbl[0];
1599 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1600 iscsi->r2tq_info.pgtbl[1];
1601 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1602 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1603 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1604 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1605 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1606 BNX2X_ISCSI_PBL_NOT_CACHED;
1607 ictx->xstorm_st_context.iscsi.flags.flags |=
1608 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1609 ictx->xstorm_st_context.iscsi.flags.flags |=
1610 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1611
1612 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1613 /* TSTORM requires the base address of RQ DB & not PTE */
1614 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1615 req2->rq_page_table_addr_lo & PAGE_MASK;
1616 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1617 req2->rq_page_table_addr_hi;
1618 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1619 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1620 ictx->tstorm_st_context.tcp.flags2 |=
1621 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001622 ictx->tstorm_st_context.tcp.ooo_support_mode =
1623 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001624
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001625 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001626
1627 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001628 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001629 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001630 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001631 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1632 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1633 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1634 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1635 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1636 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1637 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1638 iscsi->r2tq_info.pgtbl[0];
1639 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1640 iscsi->r2tq_info.pgtbl[1];
1641 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1642 req1->cq_page_table_addr_lo;
1643 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1644 req1->cq_page_table_addr_hi;
1645 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1646 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1647 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1648 ictx->ustorm_st_context.task_pbe_cache_index =
1649 BNX2X_ISCSI_PBL_NOT_CACHED;
1650 ictx->ustorm_st_context.task_pdu_cache_index =
1651 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1652
1653 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1654 if (j == 3) {
1655 if (n >= n_max)
1656 break;
1657 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1658 j = 0;
1659 }
1660 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1661 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1662 req3->qp_first_pte[j].hi;
1663 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1664 req3->qp_first_pte[j].lo;
1665 }
1666
1667 ictx->ustorm_st_context.task_pbl_base.lo =
1668 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1669 ictx->ustorm_st_context.task_pbl_base.hi =
1670 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1671 ictx->ustorm_st_context.tce_phy_addr.lo =
1672 iscsi->task_array_info.pgtbl[0];
1673 ictx->ustorm_st_context.tce_phy_addr.hi =
1674 iscsi->task_array_info.pgtbl[1];
1675 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1676 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1677 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1678 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1679 ISCSI_DEF_MAX_BURST_LEN;
1680 ictx->ustorm_st_context.negotiated_rx |=
1681 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1682 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1683
1684 ictx->cstorm_st_context.hq_pbl_base.lo =
1685 iscsi->hq_info.pgtbl_map & 0xffffffff;
1686 ictx->cstorm_st_context.hq_pbl_base.hi =
1687 (u64) iscsi->hq_info.pgtbl_map >> 32;
1688 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1689 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1690 ictx->cstorm_st_context.task_pbl_base.lo =
1691 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1692 ictx->cstorm_st_context.task_pbl_base.hi =
1693 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1694 /* CSTORM and USTORM initialization is different, CSTORM requires
1695 * CQ DB base & not PTE addr */
1696 ictx->cstorm_st_context.cq_db_base.lo =
1697 req1->cq_page_table_addr_lo & PAGE_MASK;
1698 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1699 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1700 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1701 for (i = 0; i < cp->num_cqs; i++) {
1702 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1703 ISCSI_INITIAL_SN;
1704 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1705 ISCSI_INITIAL_SN;
1706 }
1707
1708 ictx->xstorm_ag_context.cdu_reserved =
1709 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1710 ISCSI_CONNECTION_TYPE);
1711 ictx->ustorm_ag_context.cdu_usage =
1712 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1713 ISCSI_CONNECTION_TYPE);
1714 return 0;
1715
1716}
1717
1718static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1719 u32 num, int *work)
1720{
1721 struct iscsi_kwqe_conn_offload1 *req1;
1722 struct iscsi_kwqe_conn_offload2 *req2;
1723 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001724 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001725 struct iscsi_kcqe kcqe;
1726 struct kcqe *cqes[1];
1727 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001728 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001729
1730 if (num < 2) {
1731 *work = num;
1732 return -EINVAL;
1733 }
1734
1735 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1736 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1737 if ((num - 2) < req2->num_additional_wqes) {
1738 *work = num;
1739 return -EINVAL;
1740 }
Joe Perches779bb412010-11-14 17:04:37 +00001741 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001742
1743 l5_cid = req1->iscsi_conn_id;
1744 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1745 return -EINVAL;
1746
1747 memset(&kcqe, 0, sizeof(kcqe));
1748 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1749 kcqe.iscsi_conn_id = l5_cid;
1750 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1751
Michael Chanfdf24082010-10-13 14:06:47 +00001752 ctx = &cp->ctx_tbl[l5_cid];
1753 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1754 kcqe.completion_status =
1755 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1756 goto done;
1757 }
1758
Michael Chan71034ba2009-10-10 13:46:59 +00001759 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1760 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001761 goto done;
1762 }
1763 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1764 if (ret) {
1765 atomic_dec(&cp->iscsi_conn);
1766 ret = 0;
1767 goto done;
1768 }
1769 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1770 if (ret < 0) {
1771 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1772 atomic_dec(&cp->iscsi_conn);
1773 goto done;
1774 }
1775
1776 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001777 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001778
1779done:
1780 cqes[0] = (struct kcqe *) &kcqe;
1781 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1782 return ret;
1783}
1784
1785
1786static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1787{
1788 struct cnic_local *cp = dev->cnic_priv;
1789 struct iscsi_kwqe_conn_update *req =
1790 (struct iscsi_kwqe_conn_update *) kwqe;
1791 void *data;
1792 union l5cm_specific_data l5_data;
1793 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1794 int ret;
1795
1796 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1797 return -EINVAL;
1798
1799 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1800 if (!data)
1801 return -ENOMEM;
1802
1803 memcpy(data, kwqe, sizeof(struct kwqe));
1804
1805 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1806 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1807 return ret;
1808}
1809
Michael Chana2c9e762010-10-13 14:06:46 +00001810static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001811{
1812 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001813 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001814 union l5cm_specific_data l5_data;
1815 int ret;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001816 u32 hw_cid, type;
Michael Chan71034ba2009-10-10 13:46:59 +00001817
Michael Chan71034ba2009-10-10 13:46:59 +00001818 init_waitqueue_head(&ctx->waitq);
1819 ctx->wait_cond = 0;
1820 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001821 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
1822 type = (NONE_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
1823 & SPE_HDR_CONN_TYPE;
1824 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1825 SPE_HDR_FUNCTION_ID);
1826
1827 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
1828 hw_cid, type, &l5_data);
1829
Michael Chan71034ba2009-10-10 13:46:59 +00001830 if (ret == 0)
1831 wait_event(ctx->waitq, ctx->wait_cond);
1832
Michael Chana2c9e762010-10-13 14:06:46 +00001833 return ret;
1834}
1835
1836static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1837{
1838 struct cnic_local *cp = dev->cnic_priv;
1839 struct iscsi_kwqe_conn_destroy *req =
1840 (struct iscsi_kwqe_conn_destroy *) kwqe;
1841 u32 l5_cid = req->reserved0;
1842 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1843 int ret = 0;
1844 struct iscsi_kcqe kcqe;
1845 struct kcqe *cqes[1];
1846
1847 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1848 goto skip_cfc_delete;
1849
Michael Chanfdf24082010-10-13 14:06:47 +00001850 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1851 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1852
1853 if (delta > (2 * HZ))
1854 delta = 0;
1855
1856 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1857 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1858 goto destroy_reply;
1859 }
Michael Chana2c9e762010-10-13 14:06:46 +00001860
1861 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1862
Michael Chan71034ba2009-10-10 13:46:59 +00001863skip_cfc_delete:
1864 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1865
1866 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00001867 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001868
Michael Chanfdf24082010-10-13 14:06:47 +00001869destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001870 memset(&kcqe, 0, sizeof(kcqe));
1871 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1872 kcqe.iscsi_conn_id = l5_cid;
1873 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1874 kcqe.iscsi_conn_context_id = req->context_id;
1875
1876 cqes[0] = (struct kcqe *) &kcqe;
1877 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1878
1879 return ret;
1880}
1881
1882static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1883 struct l4_kwq_connect_req1 *kwqe1,
1884 struct l4_kwq_connect_req3 *kwqe3,
1885 struct l5cm_active_conn_buffer *conn_buf)
1886{
1887 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1888 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1889 &conn_buf->xstorm_conn_buffer;
1890 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1891 &conn_buf->tstorm_conn_buffer;
1892 struct regpair context_addr;
1893 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1894 struct in6_addr src_ip, dst_ip;
1895 int i;
1896 u32 *addrp;
1897
1898 addrp = (u32 *) &conn_addr->local_ip_addr;
1899 for (i = 0; i < 4; i++, addrp++)
1900 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1901
1902 addrp = (u32 *) &conn_addr->remote_ip_addr;
1903 for (i = 0; i < 4; i++, addrp++)
1904 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1905
1906 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1907
1908 xstorm_buf->context_addr.hi = context_addr.hi;
1909 xstorm_buf->context_addr.lo = context_addr.lo;
1910 xstorm_buf->mss = 0xffff;
1911 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1912 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1913 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1914 xstorm_buf->pseudo_header_checksum =
1915 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1916
1917 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1918 tstorm_buf->params |=
1919 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1920 if (kwqe3->ka_timeout) {
1921 tstorm_buf->ka_enable = 1;
1922 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1923 tstorm_buf->ka_interval = kwqe3->ka_interval;
1924 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1925 }
1926 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1927 tstorm_buf->snd_buf = kwqe3->snd_buf;
1928 tstorm_buf->max_rt_time = 0xffffffff;
1929}
1930
1931static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1932{
1933 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001934 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001935 u8 *mac = dev->mac_addr;
1936
1937 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001938 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001939 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001940 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001941 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001942 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001943 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001944 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001945 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001946 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001947 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001948 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001949
1950 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001951 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001952 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001953 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001954 mac[4]);
1955 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001956 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001957 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001958 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001959 mac[2]);
1960 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001961 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00001962 mac[1]);
1963 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001964 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00001965 mac[0]);
1966}
1967
1968static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1969{
1970 struct cnic_local *cp = dev->cnic_priv;
1971 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1972 u16 tstorm_flags = 0;
1973
1974 if (tcp_ts) {
1975 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1976 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1977 }
1978
1979 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001980 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001981
1982 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001983 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001984}
1985
1986static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
1987 u32 num, int *work)
1988{
1989 struct cnic_local *cp = dev->cnic_priv;
1990 struct l4_kwq_connect_req1 *kwqe1 =
1991 (struct l4_kwq_connect_req1 *) wqes[0];
1992 struct l4_kwq_connect_req3 *kwqe3;
1993 struct l5cm_active_conn_buffer *conn_buf;
1994 struct l5cm_conn_addr_params *conn_addr;
1995 union l5cm_specific_data l5_data;
1996 u32 l5_cid = kwqe1->pg_cid;
1997 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1998 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1999 int ret;
2000
2001 if (num < 2) {
2002 *work = num;
2003 return -EINVAL;
2004 }
2005
2006 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2007 *work = 3;
2008 else
2009 *work = 2;
2010
2011 if (num < *work) {
2012 *work = num;
2013 return -EINVAL;
2014 }
2015
2016 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002017 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002018 return -ENOMEM;
2019 }
2020 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2021 if (!conn_buf)
2022 return -ENOMEM;
2023
2024 memset(conn_buf, 0, sizeof(*conn_buf));
2025
2026 conn_addr = &conn_buf->conn_addr_buf;
2027 conn_addr->remote_addr_0 = csk->ha[0];
2028 conn_addr->remote_addr_1 = csk->ha[1];
2029 conn_addr->remote_addr_2 = csk->ha[2];
2030 conn_addr->remote_addr_3 = csk->ha[3];
2031 conn_addr->remote_addr_4 = csk->ha[4];
2032 conn_addr->remote_addr_5 = csk->ha[5];
2033
2034 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2035 struct l4_kwq_connect_req2 *kwqe2 =
2036 (struct l4_kwq_connect_req2 *) wqes[1];
2037
2038 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2039 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2040 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2041
2042 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2043 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2044 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2045 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2046 }
2047 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2048
2049 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2050 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2051 conn_addr->local_tcp_port = kwqe1->src_port;
2052 conn_addr->remote_tcp_port = kwqe1->dst_port;
2053
2054 conn_addr->pmtu = kwqe3->pmtu;
2055 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2056
2057 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002058 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002059
2060 cnic_bnx2x_set_tcp_timestamp(dev,
2061 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2062
2063 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2064 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2065 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002066 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002067
2068 return ret;
2069}
2070
2071static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2072{
2073 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2074 union l5cm_specific_data l5_data;
2075 int ret;
2076
2077 memset(&l5_data, 0, sizeof(l5_data));
2078 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2079 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2080 return ret;
2081}
2082
2083static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2084{
2085 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2086 union l5cm_specific_data l5_data;
2087 int ret;
2088
2089 memset(&l5_data, 0, sizeof(l5_data));
2090 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2091 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2092 return ret;
2093}
2094static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2095{
2096 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2097 struct l4_kcq kcqe;
2098 struct kcqe *cqes[1];
2099
2100 memset(&kcqe, 0, sizeof(kcqe));
2101 kcqe.pg_host_opaque = req->host_opaque;
2102 kcqe.pg_cid = req->host_opaque;
2103 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2104 cqes[0] = (struct kcqe *) &kcqe;
2105 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2106 return 0;
2107}
2108
2109static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2110{
2111 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2112 struct l4_kcq kcqe;
2113 struct kcqe *cqes[1];
2114
2115 memset(&kcqe, 0, sizeof(kcqe));
2116 kcqe.pg_host_opaque = req->pg_host_opaque;
2117 kcqe.pg_cid = req->pg_cid;
2118 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2119 cqes[0] = (struct kcqe *) &kcqe;
2120 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2121 return 0;
2122}
2123
Michael Chane1928c82010-12-23 07:43:04 +00002124static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2125{
2126 struct fcoe_kwqe_stat *req;
2127 struct fcoe_stat_ramrod_params *fcoe_stat;
2128 union l5cm_specific_data l5_data;
2129 struct cnic_local *cp = dev->cnic_priv;
2130 int ret;
2131 u32 cid;
2132
2133 req = (struct fcoe_kwqe_stat *) kwqe;
2134 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2135
2136 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2137 if (!fcoe_stat)
2138 return -ENOMEM;
2139
2140 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2141 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2142
2143 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT, cid,
2144 FCOE_CONNECTION_TYPE, &l5_data);
2145 return ret;
2146}
2147
2148static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2149 u32 num, int *work)
2150{
2151 int ret;
2152 struct cnic_local *cp = dev->cnic_priv;
2153 u32 cid;
2154 struct fcoe_init_ramrod_params *fcoe_init;
2155 struct fcoe_kwqe_init1 *req1;
2156 struct fcoe_kwqe_init2 *req2;
2157 struct fcoe_kwqe_init3 *req3;
2158 union l5cm_specific_data l5_data;
2159
2160 if (num < 3) {
2161 *work = num;
2162 return -EINVAL;
2163 }
2164 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2165 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2166 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2167 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2168 *work = 1;
2169 return -EINVAL;
2170 }
2171 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2172 *work = 2;
2173 return -EINVAL;
2174 }
2175
2176 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2177 netdev_err(dev->netdev, "fcoe_init size too big\n");
2178 return -ENOMEM;
2179 }
2180 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2181 if (!fcoe_init)
2182 return -ENOMEM;
2183
2184 memset(fcoe_init, 0, sizeof(*fcoe_init));
2185 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2186 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2187 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
2188 fcoe_init->eq_addr.lo = cp->kcq2.dma.pg_map_arr[0] & 0xffffffff;
2189 fcoe_init->eq_addr.hi = (u64) cp->kcq2.dma.pg_map_arr[0] >> 32;
2190 fcoe_init->eq_next_page_addr.lo =
2191 cp->kcq2.dma.pg_map_arr[1] & 0xffffffff;
2192 fcoe_init->eq_next_page_addr.hi =
2193 (u64) cp->kcq2.dma.pg_map_arr[1] >> 32;
2194
2195 fcoe_init->sb_num = cp->status_blk_num;
2196 fcoe_init->eq_prod = MAX_KCQ_IDX;
2197 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2198 cp->kcq2.sw_prod_idx = 0;
2199
2200 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2201 printk(KERN_ERR "bdbg: submitting INIT RAMROD \n");
2202 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT, cid,
2203 FCOE_CONNECTION_TYPE, &l5_data);
2204 *work = 3;
2205 return ret;
2206}
2207
2208static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2209 u32 num, int *work)
2210{
2211 int ret = 0;
2212 u32 cid = -1, l5_cid;
2213 struct cnic_local *cp = dev->cnic_priv;
2214 struct fcoe_kwqe_conn_offload1 *req1;
2215 struct fcoe_kwqe_conn_offload2 *req2;
2216 struct fcoe_kwqe_conn_offload3 *req3;
2217 struct fcoe_kwqe_conn_offload4 *req4;
2218 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2219 struct cnic_context *ctx;
2220 struct fcoe_context *fctx;
2221 struct regpair ctx_addr;
2222 union l5cm_specific_data l5_data;
2223 struct fcoe_kcqe kcqe;
2224 struct kcqe *cqes[1];
2225
2226 if (num < 4) {
2227 *work = num;
2228 return -EINVAL;
2229 }
2230 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2231 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2232 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2233 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2234
2235 *work = 4;
2236
2237 l5_cid = req1->fcoe_conn_id;
2238 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2239 goto err_reply;
2240
2241 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2242
2243 ctx = &cp->ctx_tbl[l5_cid];
2244 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2245 goto err_reply;
2246
2247 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2248 if (ret) {
2249 ret = 0;
2250 goto err_reply;
2251 }
2252 cid = ctx->cid;
2253
2254 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2255 if (fctx) {
2256 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2257 u32 val;
2258
2259 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2260 FCOE_CONNECTION_TYPE);
2261 fctx->xstorm_ag_context.cdu_reserved = val;
2262 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2263 FCOE_CONNECTION_TYPE);
2264 fctx->ustorm_ag_context.cdu_usage = val;
2265 }
2266 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2267 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2268 goto err_reply;
2269 }
2270 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2271 if (!fcoe_offload)
2272 goto err_reply;
2273
2274 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2275 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2276 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2277 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2278 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2279
2280 cid = BNX2X_HW_CID(cp, cid);
2281 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2282 FCOE_CONNECTION_TYPE, &l5_data);
2283 if (!ret)
2284 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2285
2286 return ret;
2287
2288err_reply:
2289 if (cid != -1)
2290 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2291
2292 memset(&kcqe, 0, sizeof(kcqe));
2293 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2294 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2295 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2296
2297 cqes[0] = (struct kcqe *) &kcqe;
2298 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2299 return ret;
2300}
2301
2302static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2303{
2304 struct fcoe_kwqe_conn_enable_disable *req;
2305 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2306 union l5cm_specific_data l5_data;
2307 int ret;
2308 u32 cid, l5_cid;
2309 struct cnic_local *cp = dev->cnic_priv;
2310
2311 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2312 cid = req->context_id;
2313 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2314
2315 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2316 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2317 return -ENOMEM;
2318 }
2319 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2320 if (!fcoe_enable)
2321 return -ENOMEM;
2322
2323 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2324 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2325 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2326 FCOE_CONNECTION_TYPE, &l5_data);
2327 return ret;
2328}
2329
2330static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2331{
2332 struct fcoe_kwqe_conn_enable_disable *req;
2333 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2334 union l5cm_specific_data l5_data;
2335 int ret;
2336 u32 cid, l5_cid;
2337 struct cnic_local *cp = dev->cnic_priv;
2338
2339 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2340 cid = req->context_id;
2341 l5_cid = req->conn_id;
2342 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2343 return -EINVAL;
2344
2345 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2346
2347 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2348 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2349 return -ENOMEM;
2350 }
2351 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2352 if (!fcoe_disable)
2353 return -ENOMEM;
2354
2355 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2356 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2357 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2358 FCOE_CONNECTION_TYPE, &l5_data);
2359 return ret;
2360}
2361
2362static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2363{
2364 struct fcoe_kwqe_conn_destroy *req;
2365 union l5cm_specific_data l5_data;
2366 int ret;
2367 u32 cid, l5_cid;
2368 struct cnic_local *cp = dev->cnic_priv;
2369 struct cnic_context *ctx;
2370 struct fcoe_kcqe kcqe;
2371 struct kcqe *cqes[1];
2372
2373 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2374 cid = req->context_id;
2375 l5_cid = req->conn_id;
2376 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2377 return -EINVAL;
2378
2379 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2380
2381 ctx = &cp->ctx_tbl[l5_cid];
2382
2383 init_waitqueue_head(&ctx->waitq);
2384 ctx->wait_cond = 0;
2385
2386 memset(&l5_data, 0, sizeof(l5_data));
2387 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2388 FCOE_CONNECTION_TYPE, &l5_data);
2389 if (ret == 0) {
2390 wait_event(ctx->waitq, ctx->wait_cond);
2391 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2392 queue_delayed_work(cnic_wq, &cp->delete_task,
2393 msecs_to_jiffies(2000));
2394 }
2395
2396 memset(&kcqe, 0, sizeof(kcqe));
2397 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2398 kcqe.fcoe_conn_id = req->conn_id;
2399 kcqe.fcoe_conn_context_id = cid;
2400
2401 cqes[0] = (struct kcqe *) &kcqe;
2402 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2403 return ret;
2404}
2405
2406static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2407{
2408 struct fcoe_kwqe_destroy *req;
2409 union l5cm_specific_data l5_data;
2410 struct cnic_local *cp = dev->cnic_priv;
2411 int ret;
2412 u32 cid;
2413
2414 req = (struct fcoe_kwqe_destroy *) kwqe;
2415 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2416
2417 memset(&l5_data, 0, sizeof(l5_data));
2418 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY, cid,
2419 FCOE_CONNECTION_TYPE, &l5_data);
2420 return ret;
2421}
2422
2423static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2424 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002425{
2426 int i, work, ret;
2427 u32 opcode;
2428 struct kwqe *kwqe;
2429
2430 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2431 return -EAGAIN; /* bnx2 is down */
2432
2433 for (i = 0; i < num_wqes; ) {
2434 kwqe = wqes[i];
2435 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2436 work = 1;
2437
2438 switch (opcode) {
2439 case ISCSI_KWQE_OPCODE_INIT1:
2440 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2441 break;
2442 case ISCSI_KWQE_OPCODE_INIT2:
2443 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2444 break;
2445 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2446 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2447 num_wqes - i, &work);
2448 break;
2449 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2450 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2451 break;
2452 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2453 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2454 break;
2455 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2456 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2457 &work);
2458 break;
2459 case L4_KWQE_OPCODE_VALUE_CLOSE:
2460 ret = cnic_bnx2x_close(dev, kwqe);
2461 break;
2462 case L4_KWQE_OPCODE_VALUE_RESET:
2463 ret = cnic_bnx2x_reset(dev, kwqe);
2464 break;
2465 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2466 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2467 break;
2468 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2469 ret = cnic_bnx2x_update_pg(dev, kwqe);
2470 break;
2471 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2472 ret = 0;
2473 break;
2474 default:
2475 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002476 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2477 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002478 break;
2479 }
2480 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002481 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2482 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002483 i += work;
2484 }
2485 return 0;
2486}
2487
Michael Chane1928c82010-12-23 07:43:04 +00002488static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2489 struct kwqe *wqes[], u32 num_wqes)
2490{
2491 struct cnic_local *cp = dev->cnic_priv;
2492 int i, work, ret;
2493 u32 opcode;
2494 struct kwqe *kwqe;
2495
2496 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2497 return -EAGAIN; /* bnx2 is down */
2498
2499 if (BNX2X_CHIP_NUM(cp->chip_id) == BNX2X_CHIP_NUM_57710)
2500 return -EINVAL;
2501
2502 for (i = 0; i < num_wqes; ) {
2503 kwqe = wqes[i];
2504 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2505 work = 1;
2506
2507 switch (opcode) {
2508 case FCOE_KWQE_OPCODE_INIT1:
2509 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2510 num_wqes - i, &work);
2511 break;
2512 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2513 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2514 num_wqes - i, &work);
2515 break;
2516 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2517 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2518 break;
2519 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2520 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2521 break;
2522 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2523 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2524 break;
2525 case FCOE_KWQE_OPCODE_DESTROY:
2526 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2527 break;
2528 case FCOE_KWQE_OPCODE_STAT:
2529 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2530 break;
2531 default:
2532 ret = 0;
2533 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2534 opcode);
2535 break;
2536 }
2537 if (ret < 0)
2538 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2539 opcode);
2540 i += work;
2541 }
2542 return 0;
2543}
2544
2545static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2546 u32 num_wqes)
2547{
2548 int ret = -EINVAL;
2549 u32 layer_code;
2550
2551 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2552 return -EAGAIN; /* bnx2x is down */
2553
2554 if (!num_wqes)
2555 return 0;
2556
2557 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2558 switch (layer_code) {
2559 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2560 case KWQE_FLAGS_LAYER_MASK_L4:
2561 case KWQE_FLAGS_LAYER_MASK_L2:
2562 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2563 break;
2564
2565 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2566 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2567 break;
2568 }
2569 return ret;
2570}
2571
2572static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2573{
2574 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2575 return KCQE_FLAGS_LAYER_MASK_L4;
2576
2577 return opflag & KCQE_FLAGS_LAYER_MASK;
2578}
2579
Michael Chana4636962009-06-08 18:14:43 -07002580static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2581{
2582 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002583 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002584
2585 i = 0;
2586 j = 1;
2587 while (num_cqes) {
2588 struct cnic_ulp_ops *ulp_ops;
2589 int ulp_type;
2590 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002591 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002592
2593 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002594 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002595
2596 while (j < num_cqes) {
2597 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2598
Michael Chane1928c82010-12-23 07:43:04 +00002599 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002600 break;
2601
2602 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002603 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002604 j++;
2605 }
2606
2607 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2608 ulp_type = CNIC_ULP_RDMA;
2609 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2610 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002611 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2612 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002613 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2614 ulp_type = CNIC_ULP_L4;
2615 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2616 goto end;
2617 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002618 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2619 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002620 goto end;
2621 }
2622
2623 rcu_read_lock();
2624 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2625 if (likely(ulp_ops)) {
2626 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2627 cp->completed_kcq + i, j);
2628 }
2629 rcu_read_unlock();
2630end:
2631 num_cqes -= j;
2632 i += j;
2633 j = 1;
2634 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002635 if (unlikely(comp))
2636 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002637}
2638
2639static u16 cnic_bnx2_next_idx(u16 idx)
2640{
2641 return idx + 1;
2642}
2643
2644static u16 cnic_bnx2_hw_idx(u16 idx)
2645{
2646 return idx;
2647}
2648
Michael Chan71034ba2009-10-10 13:46:59 +00002649static u16 cnic_bnx2x_next_idx(u16 idx)
2650{
2651 idx++;
2652 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2653 idx++;
2654
2655 return idx;
2656}
2657
2658static u16 cnic_bnx2x_hw_idx(u16 idx)
2659{
2660 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2661 idx++;
2662 return idx;
2663}
2664
Michael Chan644b9d42010-06-24 14:58:40 +00002665static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002666{
2667 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002668 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002669 struct kcqe *kcqe;
2670 int kcqe_cnt = 0, last_cnt = 0;
2671
Michael Chan644b9d42010-06-24 14:58:40 +00002672 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002673 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002674 hw_prod = *info->hw_prod_idx_ptr;
2675 hw_prod = cp->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002676
2677 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002678 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002679 cp->completed_kcq[kcqe_cnt++] = kcqe;
2680 i = cp->next_idx(i);
2681 ri = i & MAX_KCQ_IDX;
2682 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2683 last_cnt = kcqe_cnt;
2684 last = i;
2685 }
2686 }
2687
Michael Chan644b9d42010-06-24 14:58:40 +00002688 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002689 return last_cnt;
2690}
2691
Michael Chan48f753d2010-05-18 11:32:53 +00002692static int cnic_l2_completion(struct cnic_local *cp)
2693{
2694 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002695 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002696 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002697 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002698 u32 cmd;
2699 int comp = 0;
2700
2701 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2702 return 0;
2703
2704 hw_cons = *cp->rx_cons_ptr;
2705 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2706 hw_cons++;
2707
2708 sw_cons = cp->rx_cons;
2709 while (sw_cons != hw_cons) {
2710 u8 cqe_fp_flags;
2711
2712 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2713 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2714 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2715 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2716 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2717 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2718 cmd == RAMROD_CMD_ID_ETH_HALT)
2719 comp++;
2720 }
2721 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2722 }
2723 return comp;
2724}
2725
Michael Chan86b53602009-10-10 13:46:57 +00002726static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002727{
Michael Chan541a7812010-10-06 03:17:22 +00002728 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002729 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002730
Michael Chan541a7812010-10-06 03:17:22 +00002731 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002732 return;
2733
Michael Chan541a7812010-10-06 03:17:22 +00002734 rx_cons = *cp->rx_cons_ptr;
2735 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002736 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002737 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2738 comp = cnic_l2_completion(cp);
2739
Michael Chana4636962009-06-08 18:14:43 -07002740 cp->tx_cons = tx_cons;
2741 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002742
Michael Chancd801532010-10-13 14:06:49 +00002743 if (cp->udev)
2744 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002745 }
Michael Chan48f753d2010-05-18 11:32:53 +00002746 if (comp)
2747 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002748}
2749
Michael Chanb177a5d52010-06-24 14:58:41 +00002750static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002751{
Michael Chana4636962009-06-08 18:14:43 -07002752 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002753 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002754 int kcqe_cnt;
2755
Michael Chana4636962009-06-08 18:14:43 -07002756 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2757
Michael Chan644b9d42010-06-24 14:58:40 +00002758 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002759
2760 service_kcqes(dev, kcqe_cnt);
2761
2762 /* Tell compiler that status_blk fields can change. */
2763 barrier();
Michael Chan644b9d42010-06-24 14:58:40 +00002764 if (status_idx != *cp->kcq1.status_idx_ptr) {
Michael Chanb177a5d52010-06-24 14:58:41 +00002765 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002766 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002767 } else
2768 break;
2769 }
2770
Michael Chan644b9d42010-06-24 14:58:40 +00002771 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002772
Michael Chan86b53602009-10-10 13:46:57 +00002773 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002774
Michael Chana4636962009-06-08 18:14:43 -07002775 return status_idx;
2776}
2777
Michael Chanb177a5d52010-06-24 14:58:41 +00002778static int cnic_service_bnx2(void *data, void *status_blk)
2779{
2780 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002781
Michael Chaneaaa6e92010-12-23 08:38:30 +00002782 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2783 struct status_block *sblk = status_blk;
2784
2785 return sblk->status_idx;
2786 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002787
2788 return cnic_service_bnx2_queues(dev);
2789}
2790
Michael Chana4636962009-06-08 18:14:43 -07002791static void cnic_service_bnx2_msix(unsigned long data)
2792{
2793 struct cnic_dev *dev = (struct cnic_dev *) data;
2794 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002795
Michael Chanb177a5d52010-06-24 14:58:41 +00002796 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002797
Michael Chana4636962009-06-08 18:14:43 -07002798 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2799 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2800}
2801
Michael Chan66fee9e2010-06-24 14:58:38 +00002802static void cnic_doirq(struct cnic_dev *dev)
2803{
2804 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002805
2806 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002807 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2808
Michael Chan66fee9e2010-06-24 14:58:38 +00002809 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002810 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002811
2812 tasklet_schedule(&cp->cnic_irq_task);
2813 }
2814}
2815
Michael Chana4636962009-06-08 18:14:43 -07002816static irqreturn_t cnic_irq(int irq, void *dev_instance)
2817{
2818 struct cnic_dev *dev = dev_instance;
2819 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002820
2821 if (cp->ack_int)
2822 cp->ack_int(dev);
2823
Michael Chan66fee9e2010-06-24 14:58:38 +00002824 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002825
2826 return IRQ_HANDLED;
2827}
2828
Michael Chan71034ba2009-10-10 13:46:59 +00002829static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2830 u16 index, u8 op, u8 update)
2831{
2832 struct cnic_local *cp = dev->cnic_priv;
2833 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2834 COMMAND_REG_INT_ACK);
2835 struct igu_ack_register igu_ack;
2836
2837 igu_ack.status_block_index = index;
2838 igu_ack.sb_id_and_flags =
2839 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2840 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2841 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2842 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2843
2844 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2845}
2846
Michael Chanee87a822010-10-13 14:06:51 +00002847static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2848 u16 index, u8 op, u8 update)
2849{
2850 struct igu_regular cmd_data;
2851 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2852
2853 cmd_data.sb_id_and_flags =
2854 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2855 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2856 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2857 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2858
2859
2860 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2861}
2862
Michael Chan71034ba2009-10-10 13:46:59 +00002863static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2864{
2865 struct cnic_local *cp = dev->cnic_priv;
2866
Dmitry Kravkov523224a2010-10-06 03:23:26 +00002867 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00002868 IGU_INT_DISABLE, 0);
2869}
2870
Michael Chanee87a822010-10-13 14:06:51 +00002871static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2872{
2873 struct cnic_local *cp = dev->cnic_priv;
2874
2875 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2876 IGU_INT_DISABLE, 0);
2877}
2878
Michael Chanb177a5d52010-06-24 14:58:41 +00002879static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002880{
Michael Chanb177a5d52010-06-24 14:58:41 +00002881 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002882 int kcqe_cnt;
2883
Michael Chanb177a5d52010-06-24 14:58:41 +00002884 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002885
2886 service_kcqes(dev, kcqe_cnt);
2887
2888 /* Tell compiler that sblk fields can change. */
2889 barrier();
Michael Chanb177a5d52010-06-24 14:58:41 +00002890 if (last_status == *info->status_idx_ptr)
Michael Chan71034ba2009-10-10 13:46:59 +00002891 break;
2892
Michael Chanb177a5d52010-06-24 14:58:41 +00002893 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002894 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002895 return last_status;
2896}
2897
2898static void cnic_service_bnx2x_bh(unsigned long data)
2899{
2900 struct cnic_dev *dev = (struct cnic_dev *) data;
2901 struct cnic_local *cp = dev->cnic_priv;
2902 u32 status_idx;
2903
2904 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2905 return;
2906
2907 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002908
Michael Chan644b9d42010-06-24 14:58:40 +00002909 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00002910
2911 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
2912 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
2913
2914 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
2915 MAX_KCQ_IDX);
2916
Michael Chanee87a822010-10-13 14:06:51 +00002917 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2918 status_idx, IGU_INT_ENABLE, 1);
Michael Chane21ba412010-12-23 07:43:03 +00002919 } else {
Michael Chanee87a822010-10-13 14:06:51 +00002920 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2921 status_idx, IGU_INT_ENABLE, 1);
Michael Chane21ba412010-12-23 07:43:03 +00002922 }
Michael Chan71034ba2009-10-10 13:46:59 +00002923}
2924
2925static int cnic_service_bnx2x(void *data, void *status_blk)
2926{
2927 struct cnic_dev *dev = data;
2928 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002929
Michael Chan66fee9e2010-06-24 14:58:38 +00002930 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2931 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002932
Michael Chan66fee9e2010-06-24 14:58:38 +00002933 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002934
2935 return 0;
2936}
2937
Michael Chana4636962009-06-08 18:14:43 -07002938static void cnic_ulp_stop(struct cnic_dev *dev)
2939{
2940 struct cnic_local *cp = dev->cnic_priv;
2941 int if_type;
2942
Michael Chancd801532010-10-13 14:06:49 +00002943 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
Michael Chan6d7760a2009-07-27 11:25:58 -07002944
Michael Chana4636962009-06-08 18:14:43 -07002945 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2946 struct cnic_ulp_ops *ulp_ops;
2947
Michael Chan681dbd72009-08-14 15:49:46 +00002948 mutex_lock(&cnic_lock);
2949 ulp_ops = cp->ulp_ops[if_type];
2950 if (!ulp_ops) {
2951 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002952 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002953 }
2954 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2955 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002956
2957 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2958 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002959
2960 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002961 }
Michael Chana4636962009-06-08 18:14:43 -07002962}
2963
2964static void cnic_ulp_start(struct cnic_dev *dev)
2965{
2966 struct cnic_local *cp = dev->cnic_priv;
2967 int if_type;
2968
Michael Chana4636962009-06-08 18:14:43 -07002969 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2970 struct cnic_ulp_ops *ulp_ops;
2971
Michael Chan681dbd72009-08-14 15:49:46 +00002972 mutex_lock(&cnic_lock);
2973 ulp_ops = cp->ulp_ops[if_type];
2974 if (!ulp_ops || !ulp_ops->cnic_start) {
2975 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002976 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002977 }
2978 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2979 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002980
2981 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2982 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002983
2984 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002985 }
Michael Chana4636962009-06-08 18:14:43 -07002986}
2987
2988static int cnic_ctl(void *data, struct cnic_ctl_info *info)
2989{
2990 struct cnic_dev *dev = data;
2991
2992 switch (info->cmd) {
2993 case CNIC_CTL_STOP_CMD:
2994 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07002995
2996 cnic_ulp_stop(dev);
2997 cnic_stop_hw(dev);
2998
Michael Chana4636962009-06-08 18:14:43 -07002999 cnic_put(dev);
3000 break;
3001 case CNIC_CTL_START_CMD:
3002 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003003
3004 if (!cnic_start_hw(dev))
3005 cnic_ulp_start(dev);
3006
Michael Chana4636962009-06-08 18:14:43 -07003007 cnic_put(dev);
3008 break;
Michael Chan71034ba2009-10-10 13:46:59 +00003009 case CNIC_CTL_COMPLETION_CMD: {
3010 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
3011 u32 l5_cid;
3012 struct cnic_local *cp = dev->cnic_priv;
3013
3014 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3015 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3016
3017 ctx->wait_cond = 1;
3018 wake_up(&ctx->waitq);
3019 }
3020 break;
3021 }
Michael Chana4636962009-06-08 18:14:43 -07003022 default:
3023 return -EINVAL;
3024 }
3025 return 0;
3026}
3027
3028static void cnic_ulp_init(struct cnic_dev *dev)
3029{
3030 int i;
3031 struct cnic_local *cp = dev->cnic_priv;
3032
Michael Chana4636962009-06-08 18:14:43 -07003033 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3034 struct cnic_ulp_ops *ulp_ops;
3035
Michael Chan7fc1ece2009-08-14 15:49:47 +00003036 mutex_lock(&cnic_lock);
3037 ulp_ops = cnic_ulp_tbl[i];
3038 if (!ulp_ops || !ulp_ops->cnic_init) {
3039 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003040 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003041 }
3042 ulp_get(ulp_ops);
3043 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003044
3045 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3046 ulp_ops->cnic_init(dev);
3047
Michael Chan7fc1ece2009-08-14 15:49:47 +00003048 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003049 }
Michael Chana4636962009-06-08 18:14:43 -07003050}
3051
3052static void cnic_ulp_exit(struct cnic_dev *dev)
3053{
3054 int i;
3055 struct cnic_local *cp = dev->cnic_priv;
3056
Michael Chana4636962009-06-08 18:14:43 -07003057 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3058 struct cnic_ulp_ops *ulp_ops;
3059
Michael Chan7fc1ece2009-08-14 15:49:47 +00003060 mutex_lock(&cnic_lock);
3061 ulp_ops = cnic_ulp_tbl[i];
3062 if (!ulp_ops || !ulp_ops->cnic_exit) {
3063 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003064 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003065 }
3066 ulp_get(ulp_ops);
3067 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003068
3069 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3070 ulp_ops->cnic_exit(dev);
3071
Michael Chan7fc1ece2009-08-14 15:49:47 +00003072 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003073 }
Michael Chana4636962009-06-08 18:14:43 -07003074}
3075
3076static int cnic_cm_offload_pg(struct cnic_sock *csk)
3077{
3078 struct cnic_dev *dev = csk->dev;
3079 struct l4_kwq_offload_pg *l4kwqe;
3080 struct kwqe *wqes[1];
3081
3082 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3083 memset(l4kwqe, 0, sizeof(*l4kwqe));
3084 wqes[0] = (struct kwqe *) l4kwqe;
3085
3086 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3087 l4kwqe->flags =
3088 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3089 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3090
3091 l4kwqe->da0 = csk->ha[0];
3092 l4kwqe->da1 = csk->ha[1];
3093 l4kwqe->da2 = csk->ha[2];
3094 l4kwqe->da3 = csk->ha[3];
3095 l4kwqe->da4 = csk->ha[4];
3096 l4kwqe->da5 = csk->ha[5];
3097
3098 l4kwqe->sa0 = dev->mac_addr[0];
3099 l4kwqe->sa1 = dev->mac_addr[1];
3100 l4kwqe->sa2 = dev->mac_addr[2];
3101 l4kwqe->sa3 = dev->mac_addr[3];
3102 l4kwqe->sa4 = dev->mac_addr[4];
3103 l4kwqe->sa5 = dev->mac_addr[5];
3104
3105 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003106 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003107 l4kwqe->host_opaque = csk->l5_cid;
3108
3109 if (csk->vlan_id) {
3110 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3111 l4kwqe->vlan_tag = csk->vlan_id;
3112 l4kwqe->l2hdr_nbytes += 4;
3113 }
3114
3115 return dev->submit_kwqes(dev, wqes, 1);
3116}
3117
3118static int cnic_cm_update_pg(struct cnic_sock *csk)
3119{
3120 struct cnic_dev *dev = csk->dev;
3121 struct l4_kwq_update_pg *l4kwqe;
3122 struct kwqe *wqes[1];
3123
3124 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3125 memset(l4kwqe, 0, sizeof(*l4kwqe));
3126 wqes[0] = (struct kwqe *) l4kwqe;
3127
3128 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3129 l4kwqe->flags =
3130 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3131 l4kwqe->pg_cid = csk->pg_cid;
3132
3133 l4kwqe->da0 = csk->ha[0];
3134 l4kwqe->da1 = csk->ha[1];
3135 l4kwqe->da2 = csk->ha[2];
3136 l4kwqe->da3 = csk->ha[3];
3137 l4kwqe->da4 = csk->ha[4];
3138 l4kwqe->da5 = csk->ha[5];
3139
3140 l4kwqe->pg_host_opaque = csk->l5_cid;
3141 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3142
3143 return dev->submit_kwqes(dev, wqes, 1);
3144}
3145
3146static int cnic_cm_upload_pg(struct cnic_sock *csk)
3147{
3148 struct cnic_dev *dev = csk->dev;
3149 struct l4_kwq_upload *l4kwqe;
3150 struct kwqe *wqes[1];
3151
3152 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3153 memset(l4kwqe, 0, sizeof(*l4kwqe));
3154 wqes[0] = (struct kwqe *) l4kwqe;
3155
3156 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3157 l4kwqe->flags =
3158 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3159 l4kwqe->cid = csk->pg_cid;
3160
3161 return dev->submit_kwqes(dev, wqes, 1);
3162}
3163
3164static int cnic_cm_conn_req(struct cnic_sock *csk)
3165{
3166 struct cnic_dev *dev = csk->dev;
3167 struct l4_kwq_connect_req1 *l4kwqe1;
3168 struct l4_kwq_connect_req2 *l4kwqe2;
3169 struct l4_kwq_connect_req3 *l4kwqe3;
3170 struct kwqe *wqes[3];
3171 u8 tcp_flags = 0;
3172 int num_wqes = 2;
3173
3174 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3175 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3176 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3177 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3178 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3179 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3180
3181 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3182 l4kwqe3->flags =
3183 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3184 l4kwqe3->ka_timeout = csk->ka_timeout;
3185 l4kwqe3->ka_interval = csk->ka_interval;
3186 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3187 l4kwqe3->tos = csk->tos;
3188 l4kwqe3->ttl = csk->ttl;
3189 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3190 l4kwqe3->pmtu = csk->mtu;
3191 l4kwqe3->rcv_buf = csk->rcv_buf;
3192 l4kwqe3->snd_buf = csk->snd_buf;
3193 l4kwqe3->seed = csk->seed;
3194
3195 wqes[0] = (struct kwqe *) l4kwqe1;
3196 if (test_bit(SK_F_IPV6, &csk->flags)) {
3197 wqes[1] = (struct kwqe *) l4kwqe2;
3198 wqes[2] = (struct kwqe *) l4kwqe3;
3199 num_wqes = 3;
3200
3201 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3202 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3203 l4kwqe2->flags =
3204 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3205 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3206 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3207 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3208 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3209 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3210 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3211 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3212 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3213 sizeof(struct tcphdr);
3214 } else {
3215 wqes[1] = (struct kwqe *) l4kwqe3;
3216 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3217 sizeof(struct tcphdr);
3218 }
3219
3220 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3221 l4kwqe1->flags =
3222 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3223 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3224 l4kwqe1->cid = csk->cid;
3225 l4kwqe1->pg_cid = csk->pg_cid;
3226 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3227 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3228 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3229 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3230 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3231 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3232 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3233 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3234 if (csk->tcp_flags & SK_TCP_NAGLE)
3235 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3236 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3237 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3238 if (csk->tcp_flags & SK_TCP_SACK)
3239 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3240 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3241 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3242
3243 l4kwqe1->tcp_flags = tcp_flags;
3244
3245 return dev->submit_kwqes(dev, wqes, num_wqes);
3246}
3247
3248static int cnic_cm_close_req(struct cnic_sock *csk)
3249{
3250 struct cnic_dev *dev = csk->dev;
3251 struct l4_kwq_close_req *l4kwqe;
3252 struct kwqe *wqes[1];
3253
3254 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3255 memset(l4kwqe, 0, sizeof(*l4kwqe));
3256 wqes[0] = (struct kwqe *) l4kwqe;
3257
3258 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3259 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3260 l4kwqe->cid = csk->cid;
3261
3262 return dev->submit_kwqes(dev, wqes, 1);
3263}
3264
3265static int cnic_cm_abort_req(struct cnic_sock *csk)
3266{
3267 struct cnic_dev *dev = csk->dev;
3268 struct l4_kwq_reset_req *l4kwqe;
3269 struct kwqe *wqes[1];
3270
3271 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3272 memset(l4kwqe, 0, sizeof(*l4kwqe));
3273 wqes[0] = (struct kwqe *) l4kwqe;
3274
3275 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3276 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3277 l4kwqe->cid = csk->cid;
3278
3279 return dev->submit_kwqes(dev, wqes, 1);
3280}
3281
3282static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3283 u32 l5_cid, struct cnic_sock **csk, void *context)
3284{
3285 struct cnic_local *cp = dev->cnic_priv;
3286 struct cnic_sock *csk1;
3287
3288 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3289 return -EINVAL;
3290
Michael Chanfdf24082010-10-13 14:06:47 +00003291 if (cp->ctx_tbl) {
3292 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3293
3294 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3295 return -EAGAIN;
3296 }
3297
Michael Chana4636962009-06-08 18:14:43 -07003298 csk1 = &cp->csk_tbl[l5_cid];
3299 if (atomic_read(&csk1->ref_count))
3300 return -EAGAIN;
3301
3302 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3303 return -EBUSY;
3304
3305 csk1->dev = dev;
3306 csk1->cid = cid;
3307 csk1->l5_cid = l5_cid;
3308 csk1->ulp_type = ulp_type;
3309 csk1->context = context;
3310
3311 csk1->ka_timeout = DEF_KA_TIMEOUT;
3312 csk1->ka_interval = DEF_KA_INTERVAL;
3313 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3314 csk1->tos = DEF_TOS;
3315 csk1->ttl = DEF_TTL;
3316 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3317 csk1->rcv_buf = DEF_RCV_BUF;
3318 csk1->snd_buf = DEF_SND_BUF;
3319 csk1->seed = DEF_SEED;
3320
3321 *csk = csk1;
3322 return 0;
3323}
3324
3325static void cnic_cm_cleanup(struct cnic_sock *csk)
3326{
3327 if (csk->src_port) {
3328 struct cnic_dev *dev = csk->dev;
3329 struct cnic_local *cp = dev->cnic_priv;
3330
Michael Chan9b093362010-12-23 07:42:56 +00003331 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003332 csk->src_port = 0;
3333 }
3334}
3335
3336static void cnic_close_conn(struct cnic_sock *csk)
3337{
3338 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3339 cnic_cm_upload_pg(csk);
3340 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3341 }
3342 cnic_cm_cleanup(csk);
3343}
3344
3345static int cnic_cm_destroy(struct cnic_sock *csk)
3346{
3347 if (!cnic_in_use(csk))
3348 return -EINVAL;
3349
3350 csk_hold(csk);
3351 clear_bit(SK_F_INUSE, &csk->flags);
3352 smp_mb__after_clear_bit();
3353 while (atomic_read(&csk->ref_count) != 1)
3354 msleep(1);
3355 cnic_cm_cleanup(csk);
3356
3357 csk->flags = 0;
3358 csk_put(csk);
3359 return 0;
3360}
3361
3362static inline u16 cnic_get_vlan(struct net_device *dev,
3363 struct net_device **vlan_dev)
3364{
3365 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3366 *vlan_dev = vlan_dev_real_dev(dev);
3367 return vlan_dev_vlan_id(dev);
3368 }
3369 *vlan_dev = dev;
3370 return 0;
3371}
3372
3373static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3374 struct dst_entry **dst)
3375{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003376#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003377 struct flowi fl;
3378 int err;
3379 struct rtable *rt;
3380
3381 memset(&fl, 0, sizeof(fl));
3382 fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
3383
3384 err = ip_route_output_key(&init_net, &rt, &fl);
3385 if (!err)
Changli Gaod8d1f302010-06-10 23:31:35 -07003386 *dst = &rt->dst;
Michael Chana4636962009-06-08 18:14:43 -07003387 return err;
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003388#else
3389 return -ENETUNREACH;
3390#endif
Michael Chana4636962009-06-08 18:14:43 -07003391}
3392
3393static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3394 struct dst_entry **dst)
3395{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003396#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
Michael Chana4636962009-06-08 18:14:43 -07003397 struct flowi fl;
3398
3399 memset(&fl, 0, sizeof(fl));
3400 ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
3401 if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
3402 fl.oif = dst_addr->sin6_scope_id;
3403
3404 *dst = ip6_route_output(&init_net, NULL, &fl);
3405 if (*dst)
3406 return 0;
3407#endif
3408
3409 return -ENETUNREACH;
3410}
3411
3412static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3413 int ulp_type)
3414{
3415 struct cnic_dev *dev = NULL;
3416 struct dst_entry *dst;
3417 struct net_device *netdev = NULL;
3418 int err = -ENETUNREACH;
3419
3420 if (dst_addr->sin_family == AF_INET)
3421 err = cnic_get_v4_route(dst_addr, &dst);
3422 else if (dst_addr->sin_family == AF_INET6) {
3423 struct sockaddr_in6 *dst_addr6 =
3424 (struct sockaddr_in6 *) dst_addr;
3425
3426 err = cnic_get_v6_route(dst_addr6, &dst);
3427 } else
3428 return NULL;
3429
3430 if (err)
3431 return NULL;
3432
3433 if (!dst->dev)
3434 goto done;
3435
3436 cnic_get_vlan(dst->dev, &netdev);
3437
3438 dev = cnic_from_netdev(netdev);
3439
3440done:
3441 dst_release(dst);
3442 if (dev)
3443 cnic_put(dev);
3444 return dev;
3445}
3446
3447static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3448{
3449 struct cnic_dev *dev = csk->dev;
3450 struct cnic_local *cp = dev->cnic_priv;
3451
3452 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3453}
3454
3455static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3456{
3457 struct cnic_dev *dev = csk->dev;
3458 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003459 int is_v6, rc = 0;
3460 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003461 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003462 __be16 local_port;
3463 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003464
3465 if (saddr->local.v6.sin6_family == AF_INET6 &&
3466 saddr->remote.v6.sin6_family == AF_INET6)
3467 is_v6 = 1;
3468 else if (saddr->local.v4.sin_family == AF_INET &&
3469 saddr->remote.v4.sin_family == AF_INET)
3470 is_v6 = 0;
3471 else
3472 return -EINVAL;
3473
3474 clear_bit(SK_F_IPV6, &csk->flags);
3475
3476 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003477 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003478 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003479
3480 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3481 sizeof(struct in6_addr));
3482 csk->dst_port = saddr->remote.v6.sin6_port;
3483 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003484
3485 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003486 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003487
3488 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3489 csk->dst_port = saddr->remote.v4.sin_port;
3490 local_port = saddr->local.v4.sin_port;
3491 }
3492
Michael Chanc76284a2010-02-24 14:42:07 +00003493 csk->vlan_id = 0;
3494 csk->mtu = dev->netdev->mtu;
3495 if (dst && dst->dev) {
3496 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3497 if (realdev == dev->netdev) {
3498 csk->vlan_id = vlan;
3499 csk->mtu = dst_mtu(dst);
3500 }
3501 }
Michael Chana4636962009-06-08 18:14:43 -07003502
Michael Chan9b093362010-12-23 07:42:56 +00003503 port_id = be16_to_cpu(local_port);
3504 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3505 port_id < CNIC_LOCAL_PORT_MAX) {
3506 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3507 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003508 } else
Michael Chan9b093362010-12-23 07:42:56 +00003509 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003510
Michael Chan9b093362010-12-23 07:42:56 +00003511 if (!port_id) {
3512 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3513 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003514 rc = -ENOMEM;
3515 goto err_out;
3516 }
Michael Chan9b093362010-12-23 07:42:56 +00003517 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003518 }
3519 csk->src_port = local_port;
3520
Michael Chana4636962009-06-08 18:14:43 -07003521err_out:
3522 dst_release(dst);
3523 return rc;
3524}
3525
3526static void cnic_init_csk_state(struct cnic_sock *csk)
3527{
3528 csk->state = 0;
3529 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3530 clear_bit(SK_F_CLOSING, &csk->flags);
3531}
3532
3533static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3534{
3535 int err = 0;
3536
3537 if (!cnic_in_use(csk))
3538 return -EINVAL;
3539
3540 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3541 return -EINVAL;
3542
3543 cnic_init_csk_state(csk);
3544
3545 err = cnic_get_route(csk, saddr);
3546 if (err)
3547 goto err_out;
3548
3549 err = cnic_resolve_addr(csk, saddr);
3550 if (!err)
3551 return 0;
3552
3553err_out:
3554 clear_bit(SK_F_CONNECT_START, &csk->flags);
3555 return err;
3556}
3557
3558static int cnic_cm_abort(struct cnic_sock *csk)
3559{
3560 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003561 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003562
3563 if (!cnic_in_use(csk))
3564 return -EINVAL;
3565
3566 if (cnic_abort_prep(csk))
3567 return cnic_cm_abort_req(csk);
3568
3569 /* Getting here means that we haven't started connect, or
3570 * connect was not successful.
3571 */
3572
Michael Chana4636962009-06-08 18:14:43 -07003573 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003574 if (csk->state != opcode)
3575 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003576
3577 return 0;
3578}
3579
3580static int cnic_cm_close(struct cnic_sock *csk)
3581{
3582 if (!cnic_in_use(csk))
3583 return -EINVAL;
3584
3585 if (cnic_close_prep(csk)) {
3586 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3587 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003588 } else {
3589 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003590 }
3591 return 0;
3592}
3593
3594static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3595 u8 opcode)
3596{
3597 struct cnic_ulp_ops *ulp_ops;
3598 int ulp_type = csk->ulp_type;
3599
3600 rcu_read_lock();
3601 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3602 if (ulp_ops) {
3603 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3604 ulp_ops->cm_connect_complete(csk);
3605 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3606 ulp_ops->cm_close_complete(csk);
3607 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3608 ulp_ops->cm_remote_abort(csk);
3609 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3610 ulp_ops->cm_abort_complete(csk);
3611 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3612 ulp_ops->cm_remote_close(csk);
3613 }
3614 rcu_read_unlock();
3615}
3616
3617static int cnic_cm_set_pg(struct cnic_sock *csk)
3618{
3619 if (cnic_offld_prep(csk)) {
3620 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3621 cnic_cm_update_pg(csk);
3622 else
3623 cnic_cm_offload_pg(csk);
3624 }
3625 return 0;
3626}
3627
3628static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3629{
3630 struct cnic_local *cp = dev->cnic_priv;
3631 u32 l5_cid = kcqe->pg_host_opaque;
3632 u8 opcode = kcqe->op_code;
3633 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3634
3635 csk_hold(csk);
3636 if (!cnic_in_use(csk))
3637 goto done;
3638
3639 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3640 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3641 goto done;
3642 }
Eddie Waia9736c02010-02-24 14:42:04 +00003643 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3644 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3645 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3646 cnic_cm_upcall(cp, csk,
3647 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3648 goto done;
3649 }
3650
Michael Chana4636962009-06-08 18:14:43 -07003651 csk->pg_cid = kcqe->pg_cid;
3652 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3653 cnic_cm_conn_req(csk);
3654
3655done:
3656 csk_put(csk);
3657}
3658
Michael Chane1928c82010-12-23 07:43:04 +00003659static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3660{
3661 struct cnic_local *cp = dev->cnic_priv;
3662 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3663 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3664 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3665
3666 ctx->timestamp = jiffies;
3667 ctx->wait_cond = 1;
3668 wake_up(&ctx->waitq);
3669}
3670
Michael Chana4636962009-06-08 18:14:43 -07003671static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3672{
3673 struct cnic_local *cp = dev->cnic_priv;
3674 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3675 u8 opcode = l4kcqe->op_code;
3676 u32 l5_cid;
3677 struct cnic_sock *csk;
3678
Michael Chane1928c82010-12-23 07:43:04 +00003679 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3680 cnic_process_fcoe_term_conn(dev, kcqe);
3681 return;
3682 }
Michael Chana4636962009-06-08 18:14:43 -07003683 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3684 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3685 cnic_cm_process_offld_pg(dev, l4kcqe);
3686 return;
3687 }
3688
3689 l5_cid = l4kcqe->conn_id;
3690 if (opcode & 0x80)
3691 l5_cid = l4kcqe->cid;
3692 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3693 return;
3694
3695 csk = &cp->csk_tbl[l5_cid];
3696 csk_hold(csk);
3697
3698 if (!cnic_in_use(csk)) {
3699 csk_put(csk);
3700 return;
3701 }
3702
3703 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003704 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3705 if (l4kcqe->status != 0) {
3706 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3707 cnic_cm_upcall(cp, csk,
3708 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3709 }
3710 break;
Michael Chana4636962009-06-08 18:14:43 -07003711 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3712 if (l4kcqe->status == 0)
3713 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3714
3715 smp_mb__before_clear_bit();
3716 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3717 cnic_cm_upcall(cp, csk, opcode);
3718 break;
3719
3720 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003721 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3722 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003723 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3724 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003725 cp->close_conn(csk, opcode);
3726 break;
3727
3728 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
3729 cnic_cm_upcall(cp, csk, opcode);
3730 break;
3731 }
3732 csk_put(csk);
3733}
3734
3735static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3736{
3737 struct cnic_dev *dev = data;
3738 int i;
3739
3740 for (i = 0; i < num; i++)
3741 cnic_cm_process_kcqe(dev, kcqe[i]);
3742}
3743
3744static struct cnic_ulp_ops cm_ulp_ops = {
3745 .indicate_kcqes = cnic_cm_indicate_kcqe,
3746};
3747
3748static void cnic_cm_free_mem(struct cnic_dev *dev)
3749{
3750 struct cnic_local *cp = dev->cnic_priv;
3751
3752 kfree(cp->csk_tbl);
3753 cp->csk_tbl = NULL;
3754 cnic_free_id_tbl(&cp->csk_port_tbl);
3755}
3756
3757static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3758{
3759 struct cnic_local *cp = dev->cnic_priv;
3760
3761 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3762 GFP_KERNEL);
3763 if (!cp->csk_tbl)
3764 return -ENOMEM;
3765
3766 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
3767 CNIC_LOCAL_PORT_MIN)) {
3768 cnic_cm_free_mem(dev);
3769 return -ENOMEM;
3770 }
3771 return 0;
3772}
3773
3774static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3775{
Michael Chan943189f2010-06-15 08:57:02 +00003776 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3777 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3778 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3779 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003780 }
Michael Chan943189f2010-06-15 08:57:02 +00003781
3782 /* 1. If event opcode matches the expected event in csk->state
3783 * 2. If the expected event is CLOSE_COMP, we accept any event
Michael Chan7b34a462010-06-15 08:57:03 +00003784 * 3. If the expected event is 0, meaning the connection was never
3785 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003786 */
Michael Chan7b34a462010-06-15 08:57:03 +00003787 if (opcode == csk->state || csk->state == 0 ||
3788 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP) {
3789 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3790 if (csk->state == 0)
3791 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003792 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003793 }
Michael Chana4636962009-06-08 18:14:43 -07003794 }
3795 return 0;
3796}
3797
3798static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3799{
3800 struct cnic_dev *dev = csk->dev;
3801 struct cnic_local *cp = dev->cnic_priv;
3802
Michael Chana1e621b2010-06-15 08:57:01 +00003803 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3804 cnic_cm_upcall(cp, csk, opcode);
3805 return;
3806 }
3807
Michael Chana4636962009-06-08 18:14:43 -07003808 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003809 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003810 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003811 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003812}
3813
3814static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3815{
3816}
3817
3818static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3819{
3820 u32 seed;
3821
3822 get_random_bytes(&seed, 4);
3823 cnic_ctx_wr(dev, 45, 0, seed);
3824 return 0;
3825}
3826
Michael Chan71034ba2009-10-10 13:46:59 +00003827static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3828{
3829 struct cnic_dev *dev = csk->dev;
3830 struct cnic_local *cp = dev->cnic_priv;
3831 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3832 union l5cm_specific_data l5_data;
3833 u32 cmd = 0;
3834 int close_complete = 0;
3835
3836 switch (opcode) {
3837 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3838 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3839 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003840 if (cnic_ready_to_close(csk, opcode)) {
3841 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3842 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3843 else
3844 close_complete = 1;
3845 }
Michael Chan71034ba2009-10-10 13:46:59 +00003846 break;
3847 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3848 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3849 break;
3850 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3851 close_complete = 1;
3852 break;
3853 }
3854 if (cmd) {
3855 memset(&l5_data, 0, sizeof(l5_data));
3856
3857 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3858 &l5_data);
3859 } else if (close_complete) {
3860 ctx->timestamp = jiffies;
3861 cnic_close_conn(csk);
3862 cnic_cm_upcall(cp, csk, csk->state);
3863 }
3864}
3865
3866static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3867{
Michael Chanfdf24082010-10-13 14:06:47 +00003868 struct cnic_local *cp = dev->cnic_priv;
3869 int i;
3870
3871 if (!cp->ctx_tbl)
3872 return;
3873
3874 if (!netif_running(dev->netdev))
3875 return;
3876
3877 for (i = 0; i < cp->max_cid_space; i++) {
3878 struct cnic_context *ctx = &cp->ctx_tbl[i];
3879
3880 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3881 msleep(10);
3882
3883 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3884 netdev_warn(dev->netdev, "CID %x not deleted\n",
3885 ctx->cid);
3886 }
3887
3888 cancel_delayed_work(&cp->delete_task);
3889 flush_workqueue(cnic_wq);
3890
3891 if (atomic_read(&cp->iscsi_conn) != 0)
3892 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3893 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00003894}
3895
3896static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3897{
3898 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003899 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003900 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003901
3902 cnic_init_bnx2x_mac(dev);
3903 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3904
3905 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003906 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003907
3908 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003909 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003910 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003911 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00003912 DEF_MAX_DA_COUNT);
3913
3914 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003915 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003916 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003917 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003918 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003919 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003920 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003921 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003922
Michael Chan14203982010-10-06 03:16:06 +00003923 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003924 DEF_MAX_CWND);
3925 return 0;
3926}
3927
Michael Chanfdf24082010-10-13 14:06:47 +00003928static void cnic_delete_task(struct work_struct *work)
3929{
3930 struct cnic_local *cp;
3931 struct cnic_dev *dev;
3932 u32 i;
3933 int need_resched = 0;
3934
3935 cp = container_of(work, struct cnic_local, delete_task.work);
3936 dev = cp->dev;
3937
3938 for (i = 0; i < cp->max_cid_space; i++) {
3939 struct cnic_context *ctx = &cp->ctx_tbl[i];
3940
3941 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
3942 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3943 continue;
3944
3945 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
3946 need_resched = 1;
3947 continue;
3948 }
3949
3950 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3951 continue;
3952
3953 cnic_bnx2x_destroy_ramrod(dev, i);
3954
3955 cnic_free_bnx2x_conn_resc(dev, i);
3956 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
3957 atomic_dec(&cp->iscsi_conn);
3958
3959 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
3960 }
3961
3962 if (need_resched)
3963 queue_delayed_work(cnic_wq, &cp->delete_task,
3964 msecs_to_jiffies(10));
3965
3966}
3967
Michael Chana4636962009-06-08 18:14:43 -07003968static int cnic_cm_open(struct cnic_dev *dev)
3969{
3970 struct cnic_local *cp = dev->cnic_priv;
3971 int err;
3972
3973 err = cnic_cm_alloc_mem(dev);
3974 if (err)
3975 return err;
3976
3977 err = cp->start_cm(dev);
3978
3979 if (err)
3980 goto err_out;
3981
Michael Chanfdf24082010-10-13 14:06:47 +00003982 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
3983
Michael Chana4636962009-06-08 18:14:43 -07003984 dev->cm_create = cnic_cm_create;
3985 dev->cm_destroy = cnic_cm_destroy;
3986 dev->cm_connect = cnic_cm_connect;
3987 dev->cm_abort = cnic_cm_abort;
3988 dev->cm_close = cnic_cm_close;
3989 dev->cm_select_dev = cnic_cm_select_dev;
3990
3991 cp->ulp_handle[CNIC_ULP_L4] = dev;
3992 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
3993 return 0;
3994
3995err_out:
3996 cnic_cm_free_mem(dev);
3997 return err;
3998}
3999
4000static int cnic_cm_shutdown(struct cnic_dev *dev)
4001{
4002 struct cnic_local *cp = dev->cnic_priv;
4003 int i;
4004
4005 cp->stop_cm(dev);
4006
4007 if (!cp->csk_tbl)
4008 return 0;
4009
4010 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4011 struct cnic_sock *csk = &cp->csk_tbl[i];
4012
4013 clear_bit(SK_F_INUSE, &csk->flags);
4014 cnic_cm_cleanup(csk);
4015 }
4016 cnic_cm_free_mem(dev);
4017
4018 return 0;
4019}
4020
4021static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4022{
Michael Chana4636962009-06-08 18:14:43 -07004023 u32 cid_addr;
4024 int i;
4025
Michael Chana4636962009-06-08 18:14:43 -07004026 cid_addr = GET_CID_ADDR(cid);
4027
4028 for (i = 0; i < CTX_SIZE; i += 4)
4029 cnic_ctx_wr(dev, cid_addr, i, 0);
4030}
4031
4032static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4033{
4034 struct cnic_local *cp = dev->cnic_priv;
4035 int ret = 0, i;
4036 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4037
4038 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4039 return 0;
4040
4041 for (i = 0; i < cp->ctx_blks; i++) {
4042 int j;
4043 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4044 u32 val;
4045
4046 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4047
4048 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4049 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4050 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4051 (u64) cp->ctx_arr[i].mapping >> 32);
4052 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4053 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4054 for (j = 0; j < 10; j++) {
4055
4056 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4057 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4058 break;
4059 udelay(5);
4060 }
4061 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4062 ret = -EBUSY;
4063 break;
4064 }
4065 }
4066 return ret;
4067}
4068
4069static void cnic_free_irq(struct cnic_dev *dev)
4070{
4071 struct cnic_local *cp = dev->cnic_priv;
4072 struct cnic_eth_dev *ethdev = cp->ethdev;
4073
4074 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4075 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004076 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004077 free_irq(ethdev->irq_arr[0].vector, dev);
4078 }
4079}
4080
Michael Chan6e0dc642010-10-13 14:06:44 +00004081static int cnic_request_irq(struct cnic_dev *dev)
4082{
4083 struct cnic_local *cp = dev->cnic_priv;
4084 struct cnic_eth_dev *ethdev = cp->ethdev;
4085 int err;
4086
4087 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4088 if (err)
4089 tasklet_disable(&cp->cnic_irq_task);
4090
4091 return err;
4092}
4093
Michael Chana4636962009-06-08 18:14:43 -07004094static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4095{
4096 struct cnic_local *cp = dev->cnic_priv;
4097 struct cnic_eth_dev *ethdev = cp->ethdev;
4098
4099 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4100 int err, i = 0;
4101 int sblk_num = cp->status_blk_num;
4102 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4103 BNX2_HC_SB_CONFIG_1;
4104
4105 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4106
4107 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4108 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4109 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4110
Michael Chana4dde3a2010-02-24 14:42:08 +00004111 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004112 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004113 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004114 err = cnic_request_irq(dev);
4115 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004116 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004117
Michael Chana4dde3a2010-02-24 14:42:08 +00004118 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004119 i < 10) {
4120 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4121 1 << (11 + sblk_num));
4122 udelay(10);
4123 i++;
4124 barrier();
4125 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004126 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004127 cnic_free_irq(dev);
4128 goto failed;
4129 }
4130
4131 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004132 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004133 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4134 int i = 0;
4135
4136 while (sblk->status_completion_producer_index && i < 10) {
4137 CNIC_WR(dev, BNX2_HC_COMMAND,
4138 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4139 udelay(10);
4140 i++;
4141 barrier();
4142 }
4143 if (sblk->status_completion_producer_index)
4144 goto failed;
4145
4146 }
4147 return 0;
4148
4149failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004150 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004151 return -EBUSY;
4152}
4153
4154static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4155{
4156 struct cnic_local *cp = dev->cnic_priv;
4157 struct cnic_eth_dev *ethdev = cp->ethdev;
4158
4159 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4160 return;
4161
4162 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4163 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4164}
4165
4166static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4167{
4168 struct cnic_local *cp = dev->cnic_priv;
4169 struct cnic_eth_dev *ethdev = cp->ethdev;
4170
4171 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4172 return;
4173
4174 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4175 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4176 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4177 synchronize_irq(ethdev->irq_arr[0].vector);
4178}
4179
4180static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4181{
4182 struct cnic_local *cp = dev->cnic_priv;
4183 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004184 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004185 u32 cid_addr, tx_cid, sb_id;
4186 u32 val, offset0, offset1, offset2, offset3;
4187 int i;
4188 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004189 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004190 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004191
4192 sb_id = cp->status_blk_num;
4193 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004194 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4195 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004196 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004197
4198 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004199 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4200 (TX_TSS_CID << 7));
4201 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4202 }
4203 cp->tx_cons = *cp->tx_cons_ptr;
4204
4205 cid_addr = GET_CID_ADDR(tx_cid);
4206 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4207 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4208
4209 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4210 cnic_ctx_wr(dev, cid_addr2, i, 0);
4211
4212 offset0 = BNX2_L2CTX_TYPE_XI;
4213 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4214 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4215 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4216 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004217 cnic_init_context(dev, tx_cid);
4218 cnic_init_context(dev, tx_cid + 1);
4219
Michael Chana4636962009-06-08 18:14:43 -07004220 offset0 = BNX2_L2CTX_TYPE;
4221 offset1 = BNX2_L2CTX_CMD_TYPE;
4222 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4223 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4224 }
4225 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4226 cnic_ctx_wr(dev, cid_addr, offset0, val);
4227
4228 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4229 cnic_ctx_wr(dev, cid_addr, offset1, val);
4230
Michael Chancd801532010-10-13 14:06:49 +00004231 txbd = (struct tx_bd *) udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004232
Michael Chancd801532010-10-13 14:06:49 +00004233 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004234 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4235 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4236 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4237 }
Michael Chancd801532010-10-13 14:06:49 +00004238 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004239 cnic_ctx_wr(dev, cid_addr, offset2, val);
4240 txbd->tx_bd_haddr_hi = val;
4241
Michael Chancd801532010-10-13 14:06:49 +00004242 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004243 cnic_ctx_wr(dev, cid_addr, offset3, val);
4244 txbd->tx_bd_haddr_lo = val;
4245}
4246
4247static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4248{
4249 struct cnic_local *cp = dev->cnic_priv;
4250 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004251 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004252 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4253 int i;
4254 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004255 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004256 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004257
4258 sb_id = cp->status_blk_num;
4259 cnic_init_context(dev, 2);
4260 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4261 coal_reg = BNX2_HC_COMMAND;
4262 coal_val = CNIC_RD(dev, coal_reg);
4263 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004264 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004265
4266 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4267 coal_reg = BNX2_HC_COALESCE_NOW;
4268 coal_val = 1 << (11 + sb_id);
4269 }
4270 i = 0;
4271 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4272 CNIC_WR(dev, coal_reg, coal_val);
4273 udelay(10);
4274 i++;
4275 barrier();
4276 }
4277 cp->rx_cons = *cp->rx_cons_ptr;
4278
4279 cid_addr = GET_CID_ADDR(2);
4280 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4281 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4282 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4283
4284 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004285 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004286 else
Michael Chand0549382009-10-28 03:41:59 -07004287 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004288 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4289
Michael Chancd801532010-10-13 14:06:49 +00004290 rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
Michael Chana4636962009-06-08 18:14:43 -07004291 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4292 dma_addr_t buf_map;
4293 int n = (i % cp->l2_rx_ring_size) + 1;
4294
Michael Chancd801532010-10-13 14:06:49 +00004295 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004296 rxbd->rx_bd_len = cp->l2_single_buf_size;
4297 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4298 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4299 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4300 }
Michael Chancd801532010-10-13 14:06:49 +00004301 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004302 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4303 rxbd->rx_bd_haddr_hi = val;
4304
Michael Chancd801532010-10-13 14:06:49 +00004305 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004306 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4307 rxbd->rx_bd_haddr_lo = val;
4308
4309 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4310 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4311}
4312
4313static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4314{
4315 struct kwqe *wqes[1], l2kwqe;
4316
4317 memset(&l2kwqe, 0, sizeof(l2kwqe));
4318 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004319 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004320 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4321 KWQE_OPCODE_SHIFT) | 2;
4322 dev->submit_kwqes(dev, wqes, 1);
4323}
4324
4325static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4326{
4327 struct cnic_local *cp = dev->cnic_priv;
4328 u32 val;
4329
4330 val = cp->func << 2;
4331
4332 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4333
4334 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4335 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4336 dev->mac_addr[0] = (u8) (val >> 8);
4337 dev->mac_addr[1] = (u8) val;
4338
4339 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4340
4341 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4342 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4343 dev->mac_addr[2] = (u8) (val >> 24);
4344 dev->mac_addr[3] = (u8) (val >> 16);
4345 dev->mac_addr[4] = (u8) (val >> 8);
4346 dev->mac_addr[5] = (u8) val;
4347
4348 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4349
4350 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4351 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4352 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4353
4354 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4355 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4356 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4357}
4358
4359static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4360{
4361 struct cnic_local *cp = dev->cnic_priv;
4362 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004363 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004364 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004365 int err;
4366
4367 cnic_set_bnx2_mac(dev);
4368
4369 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4370 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4371 if (BCM_PAGE_BITS > 12)
4372 val |= (12 - 8) << 4;
4373 else
4374 val |= (BCM_PAGE_BITS - 8) << 4;
4375
4376 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4377
4378 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4379 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4380 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4381
4382 err = cnic_setup_5709_context(dev, 1);
4383 if (err)
4384 return err;
4385
4386 cnic_init_context(dev, KWQ_CID);
4387 cnic_init_context(dev, KCQ_CID);
4388
Michael Chane6c28892010-06-24 14:58:39 +00004389 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004390 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4391
4392 cp->max_kwq_idx = MAX_KWQ_IDX;
4393 cp->kwq_prod_idx = 0;
4394 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004395 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004396
4397 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4398 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4399 else
4400 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4401
4402 /* Initialize the kernel work queue context. */
4403 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4404 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004405 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004406
4407 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004408 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004409
4410 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004411 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004412
4413 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004414 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004415
4416 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004417 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004418
Michael Chane6c28892010-06-24 14:58:39 +00004419 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4420 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004421
Michael Chane6c28892010-06-24 14:58:39 +00004422 cp->kcq1.sw_prod_idx = 0;
4423 cp->kcq1.hw_prod_idx_ptr =
4424 (u16 *) &sblk->status_completion_producer_index;
4425
4426 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004427
4428 /* Initialize the kernel complete queue context. */
4429 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4430 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004431 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004432
4433 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004434 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004435
4436 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004437 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004438
Michael Chane6c28892010-06-24 14:58:39 +00004439 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4440 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004441
Michael Chane6c28892010-06-24 14:58:39 +00004442 val = (u32) cp->kcq1.dma.pgtbl_map;
4443 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004444
4445 cp->int_num = 0;
4446 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004447 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004448 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004449 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004450
Michael Chane6c28892010-06-24 14:58:39 +00004451 cp->kcq1.hw_prod_idx_ptr =
4452 (u16 *) &msblk->status_completion_producer_index;
4453 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004454 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004455 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004456 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4457 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004458 }
4459
4460 /* Enable Commnad Scheduler notification when we write to the
4461 * host producer index of the kernel contexts. */
4462 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4463
4464 /* Enable Command Scheduler notification when we write to either
4465 * the Send Queue or Receive Queue producer indexes of the kernel
4466 * bypass contexts. */
4467 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4468 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4469
4470 /* Notify COM when the driver post an application buffer. */
4471 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4472
4473 /* Set the CP and COM doorbells. These two processors polls the
4474 * doorbell for a non zero value before running. This must be done
4475 * after setting up the kernel queue contexts. */
4476 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4477 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4478
4479 cnic_init_bnx2_tx_ring(dev);
4480 cnic_init_bnx2_rx_ring(dev);
4481
4482 err = cnic_init_bnx2_irq(dev);
4483 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004484 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004485 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4486 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4487 return err;
4488 }
4489
4490 return 0;
4491}
4492
Michael Chan71034ba2009-10-10 13:46:59 +00004493static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4494{
4495 struct cnic_local *cp = dev->cnic_priv;
4496 struct cnic_eth_dev *ethdev = cp->ethdev;
4497 u32 start_offset = ethdev->ctx_tbl_offset;
4498 int i;
4499
4500 for (i = 0; i < cp->ctx_blks; i++) {
4501 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4502 dma_addr_t map = ctx->mapping;
4503
4504 if (cp->ctx_align) {
4505 unsigned long mask = cp->ctx_align - 1;
4506
4507 map = (map + mask) & ~mask;
4508 }
4509
4510 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4511 }
4512}
4513
4514static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4515{
4516 struct cnic_local *cp = dev->cnic_priv;
4517 struct cnic_eth_dev *ethdev = cp->ethdev;
4518 int err = 0;
4519
Joe Perches164165d2009-11-19 09:30:10 +00004520 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004521 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004522 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4523 err = cnic_request_irq(dev);
4524
Michael Chan71034ba2009-10-10 13:46:59 +00004525 return err;
4526}
4527
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004528static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4529 u16 sb_id, u8 sb_index,
4530 u8 disable)
4531{
4532
4533 u32 addr = BAR_CSTRORM_INTMEM +
4534 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4535 offsetof(struct hc_status_block_data_e1x, index_data) +
4536 sizeof(struct hc_index_data)*sb_index +
4537 offsetof(struct hc_index_data, flags);
4538 u16 flags = CNIC_RD16(dev, addr);
4539 /* clear and set */
4540 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4541 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4542 HC_INDEX_DATA_HC_ENABLED);
4543 CNIC_WR16(dev, addr, flags);
4544}
4545
Michael Chan71034ba2009-10-10 13:46:59 +00004546static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4547{
4548 struct cnic_local *cp = dev->cnic_priv;
4549 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004550
4551 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004552 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4553 offsetof(struct hc_status_block_data_e1x, index_data) +
4554 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
4555 offsetof(struct hc_index_data, timeout), 64 / 12);
4556 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004557}
4558
4559static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4560{
4561}
4562
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004563static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4564 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004565{
4566 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004567 struct cnic_uio_dev *udev = cp->udev;
4568 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4569 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004570 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004571 int port = CNIC_PORT(cp);
4572 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004573 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004574 u32 val;
4575
4576 memset(txbd, 0, BCM_PAGE_SIZE);
4577
Michael Chancd801532010-10-13 14:06:49 +00004578 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004579 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4580 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4581 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4582
4583 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4584 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4585 reg_bd->addr_hi = start_bd->addr_hi;
4586 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4587 start_bd->nbytes = cpu_to_le16(0x10);
4588 start_bd->nbd = cpu_to_le16(3);
4589 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4590 start_bd->general_data = (UNICAST_ADDRESS <<
4591 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4592 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4593
4594 }
Michael Chan71034ba2009-10-10 13:46:59 +00004595
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004596 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004597 txbd->next_bd.addr_hi = cpu_to_le32(val);
4598
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004599 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004600
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004601 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004602 txbd->next_bd.addr_lo = cpu_to_le32(val);
4603
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004604 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004605
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004606 /* Other ramrod params */
4607 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4608 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004609
4610 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004611 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004612 val = BAR_XSTRORM_INTMEM +
4613 XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4614 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
4615 CNIC_WR(dev, val + i * 4, 0);
4616 }
Michael Chan71034ba2009-10-10 13:46:59 +00004617
4618 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004619 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004620}
4621
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004622static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4623 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004624{
4625 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004626 struct cnic_uio_dev *udev = cp->udev;
4627 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004628 BCM_PAGE_SIZE);
4629 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004630 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004631 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004632 int i;
4633 int port = CNIC_PORT(cp);
Michael Chan5159fdc2010-12-23 07:42:59 +00004634 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004635 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004636 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004637 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004638
4639 /* General data */
4640 data->general.client_id = cli;
4641 data->general.statistics_en_flg = 1;
4642 data->general.statistics_counter_id = cli;
4643 data->general.activate_flg = 1;
4644 data->general.sp_client_id = cli;
Michael Chan71034ba2009-10-10 13:46:59 +00004645
4646 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4647 dma_addr_t buf_map;
4648 int n = (i % cp->l2_rx_ring_size) + 1;
4649
Michael Chancd801532010-10-13 14:06:49 +00004650 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004651 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4652 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4653 }
Michael Chan71034ba2009-10-10 13:46:59 +00004654
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004655 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004656 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004657 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004658
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004659 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004660 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004661 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004662
4663 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004664 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004665 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004666 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004667
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004668 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004669 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004670 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004671
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004672 /* Other ramrod params */
4673 data->rx.client_qzone_id = cl_qzone_id;
4674 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4675 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004676
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004677 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
4678 data->rx.bd_buff_size = cpu_to_le16(cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004679
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004680 data->rx.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4681 data->rx.outer_vlan_removal_enable_flg = 1;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004682
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004683 /* reset tstorm and ustorm per client statistics */
4684 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004685 val = BAR_TSTRORM_INTMEM +
4686 TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4687 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4688 CNIC_WR(dev, val + i * 4, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004689
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004690 val = BAR_USTRORM_INTMEM +
4691 USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4692 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4693 CNIC_WR(dev, val + i * 4, 0);
4694 }
Michael Chan71034ba2009-10-10 13:46:59 +00004695
4696 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004697 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004698 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004699}
4700
Michael Chan4aacb7a2010-12-23 07:43:01 +00004701static int cnic_read_bnx2x_iscsi_mac(struct cnic_dev *dev, u32 upper_addr,
4702 u32 lower_addr)
4703{
4704 u32 val;
4705 u8 mac[6];
4706
4707 val = CNIC_RD(dev, upper_addr);
4708
4709 mac[0] = (u8) (val >> 8);
4710 mac[1] = (u8) val;
4711
4712 val = CNIC_RD(dev, lower_addr);
4713
4714 mac[2] = (u8) (val >> 24);
4715 mac[3] = (u8) (val >> 16);
4716 mac[4] = (u8) (val >> 8);
4717 mac[5] = (u8) val;
4718
4719 if (is_valid_ether_addr(mac)) {
4720 memcpy(dev->mac_addr, mac, 6);
4721 return 0;
4722 } else {
4723 return -EINVAL;
4724 }
4725}
4726
Michael Chan71034ba2009-10-10 13:46:59 +00004727static void cnic_get_bnx2x_iscsi_info(struct cnic_dev *dev)
4728{
4729 struct cnic_local *cp = dev->cnic_priv;
Michael Chan4aacb7a2010-12-23 07:43:01 +00004730 u32 base, base2, addr, addr1, val;
Michael Chan71034ba2009-10-10 13:46:59 +00004731 int port = CNIC_PORT(cp);
4732
4733 dev->max_iscsi_conn = 0;
4734 base = CNIC_RD(dev, MISC_REG_SHARED_MEM_ADDR);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004735 if (base == 0)
Michael Chan71034ba2009-10-10 13:46:59 +00004736 return;
4737
Michael Chanee87a822010-10-13 14:06:51 +00004738 base2 = CNIC_RD(dev, (CNIC_PATH(cp) ? MISC_REG_GENERIC_CR_1 :
4739 MISC_REG_GENERIC_CR_0));
Michael Chandd2e4db2009-12-02 15:15:37 +00004740 addr = BNX2X_SHMEM_ADDR(base,
Michael Chan71034ba2009-10-10 13:46:59 +00004741 dev_info.port_hw_config[port].iscsi_mac_upper);
4742
Michael Chan4aacb7a2010-12-23 07:43:01 +00004743 addr1 = BNX2X_SHMEM_ADDR(base,
Michael Chan71034ba2009-10-10 13:46:59 +00004744 dev_info.port_hw_config[port].iscsi_mac_lower);
4745
Michael Chan4aacb7a2010-12-23 07:43:01 +00004746 cnic_read_bnx2x_iscsi_mac(dev, addr, addr1);
Michael Chan71034ba2009-10-10 13:46:59 +00004747
4748 addr = BNX2X_SHMEM_ADDR(base, validity_map[port]);
4749 val = CNIC_RD(dev, addr);
4750
4751 if (!(val & SHR_MEM_VALIDITY_LIC_NO_KEY_IN_EFFECT)) {
4752 u16 val16;
4753
4754 addr = BNX2X_SHMEM_ADDR(base,
4755 drv_lic_key[port].max_iscsi_init_conn);
4756 val16 = CNIC_RD16(dev, addr);
4757
4758 if (val16)
4759 val16 ^= 0x1e1e;
4760 dev->max_iscsi_conn = val16;
4761 }
Michael Chane1928c82010-12-23 07:43:04 +00004762
4763 if (BNX2X_CHIP_IS_E2(cp->chip_id))
4764 dev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
4765
Michael Chanee87a822010-10-13 14:06:51 +00004766 if (BNX2X_CHIP_IS_E1H(cp->chip_id) || BNX2X_CHIP_IS_E2(cp->chip_id)) {
Michael Chan71034ba2009-10-10 13:46:59 +00004767 int func = CNIC_FUNC(cp);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004768 u32 mf_cfg_addr;
Michael Chan71034ba2009-10-10 13:46:59 +00004769
Michael Chanee87a822010-10-13 14:06:51 +00004770 if (BNX2X_SHMEM2_HAS(base2, mf_cfg_addr))
4771 mf_cfg_addr = CNIC_RD(dev, BNX2X_SHMEM2_ADDR(base2,
4772 mf_cfg_addr));
4773 else
4774 mf_cfg_addr = base + BNX2X_SHMEM_MF_BLK_OFFSET;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004775
Michael Chan4aacb7a2010-12-23 07:43:01 +00004776 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4777 /* Must determine if the MF is SD vs SI mode */
4778 addr = BNX2X_SHMEM_ADDR(base,
4779 dev_info.shared_feature_config.config);
4780 val = CNIC_RD(dev, addr);
4781 if ((val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK) ==
4782 SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT) {
4783 int rc;
4784
4785 /* MULTI_FUNCTION_SI mode */
4786 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4787 func_ext_config[func].func_cfg);
4788 val = CNIC_RD(dev, addr);
4789 if (!(val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD))
4790 dev->max_iscsi_conn = 0;
4791
Michael Chane1928c82010-12-23 07:43:04 +00004792 if (!(val & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD))
4793 dev->max_fcoe_conn = 0;
4794
Michael Chan4aacb7a2010-12-23 07:43:01 +00004795 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4796 func_ext_config[func].
4797 iscsi_mac_addr_upper);
4798 addr1 = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4799 func_ext_config[func].
4800 iscsi_mac_addr_lower);
4801 rc = cnic_read_bnx2x_iscsi_mac(dev, addr,
4802 addr1);
4803 if (rc && func > 1)
4804 dev->max_iscsi_conn = 0;
4805
4806 return;
4807 }
4808 }
4809
4810 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4811 func_mf_config[func].e1hov_tag);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004812
Michael Chan71034ba2009-10-10 13:46:59 +00004813 val = CNIC_RD(dev, addr);
4814 val &= FUNC_MF_CFG_E1HOV_TAG_MASK;
4815 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
Michael Chan57045c92011-01-03 15:21:45 +00004816 dev->max_fcoe_conn = 0;
4817 dev->max_iscsi_conn = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00004818 }
4819 }
Michael Chan4aacb7a2010-12-23 07:43:01 +00004820 if (!is_valid_ether_addr(dev->mac_addr))
4821 dev->max_iscsi_conn = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00004822}
4823
Michael Chane21ba412010-12-23 07:43:03 +00004824static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4825{
4826 struct cnic_local *cp = dev->cnic_priv;
4827 u32 pfid = cp->pfid;
4828
4829 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4830 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4831 cp->kcq1.sw_prod_idx = 0;
4832
4833 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4834 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4835
4836 cp->kcq1.hw_prod_idx_ptr =
4837 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4838 cp->kcq1.status_idx_ptr =
4839 &sb->sb.running_index[SM_RX_ID];
4840 } else {
4841 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4842
4843 cp->kcq1.hw_prod_idx_ptr =
4844 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4845 cp->kcq1.status_idx_ptr =
4846 &sb->sb.running_index[SM_RX_ID];
4847 }
4848
4849 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4850 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4851
4852 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4853 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4854 cp->kcq2.sw_prod_idx = 0;
4855 cp->kcq2.hw_prod_idx_ptr =
4856 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4857 cp->kcq2.status_idx_ptr =
4858 &sb->sb.running_index[SM_RX_ID];
4859 }
4860}
4861
Michael Chan71034ba2009-10-10 13:46:59 +00004862static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4863{
4864 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004865 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chan71034ba2009-10-10 13:46:59 +00004866 int func = CNIC_FUNC(cp), ret, i;
Michael Chan14203982010-10-06 03:16:06 +00004867 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004868
Michael Chanee87a822010-10-13 14:06:51 +00004869 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4870 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4871
4872 if (!(val & 1))
4873 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4874 else
4875 val = (val >> 1) & 1;
4876
4877 if (val)
4878 cp->pfid = func >> 1;
4879 else
4880 cp->pfid = func & 0x6;
4881 } else {
4882 cp->pfid = func;
4883 }
Michael Chan14203982010-10-06 03:16:06 +00004884 pfid = cp->pfid;
4885
Michael Chan71034ba2009-10-10 13:46:59 +00004886 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Michael Chan520efdf2010-06-24 14:58:37 +00004887 cp->iscsi_start_cid);
Michael Chan71034ba2009-10-10 13:46:59 +00004888
4889 if (ret)
4890 return -ENOMEM;
4891
Michael Chane1928c82010-12-23 07:43:04 +00004892 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4893 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl,
4894 BNX2X_FCOE_NUM_CONNECTIONS,
4895 cp->fcoe_start_cid);
4896
4897 if (ret)
4898 return -ENOMEM;
4899 }
4900
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004901 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
4902
Michael Chane21ba412010-12-23 07:43:03 +00004903 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004904
4905 cnic_get_bnx2x_iscsi_info(dev);
4906
4907 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00004908 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00004909 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004910 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004911 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004912 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004913 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004914 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004915 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004916 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004917 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004918 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004919 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004920 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004921 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004922 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004923 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004924 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004925 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004926 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00004927 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004928 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004929 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00004930
4931 for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4932 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004933 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
Michael Chan71034ba2009-10-10 13:46:59 +00004934 cp->conn_buf_info.pgtbl[2 * i]);
4935 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004936 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004937 cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4938 }
4939
4940 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004941 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004942 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4943 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004944 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004945 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4946
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004947 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4948 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
4949
Michael Chan71034ba2009-10-10 13:46:59 +00004950 cnic_setup_bnx2x_context(dev);
4951
Michael Chan71034ba2009-10-10 13:46:59 +00004952 ret = cnic_init_bnx2x_irq(dev);
4953 if (ret)
4954 return ret;
4955
Michael Chan71034ba2009-10-10 13:46:59 +00004956 return 0;
4957}
4958
Michael Chan86b53602009-10-10 13:46:57 +00004959static void cnic_init_rings(struct cnic_dev *dev)
4960{
Michael Chan541a7812010-10-06 03:17:22 +00004961 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004962 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00004963
4964 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4965 return;
4966
Michael Chan86b53602009-10-10 13:46:57 +00004967 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4968 cnic_init_bnx2_tx_ring(dev);
4969 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00004970 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00004971 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00004972 u32 cli = cp->ethdev->iscsi_l2_client_id;
4973 u32 cid = cp->ethdev->iscsi_l2_cid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004974 u32 cl_qzone_id, type;
4975 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00004976 union l5cm_specific_data l5_data;
4977 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chanc7596b72009-12-02 15:15:35 +00004978 u32 off, i;
Michael Chan71034ba2009-10-10 13:46:59 +00004979
4980 rx_prods.bd_prod = 0;
4981 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4982 barrier();
4983
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004984 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4985
Michael Chanc7596b72009-12-02 15:15:35 +00004986 off = BAR_USTRORM_INTMEM +
Michael Chanee87a822010-10-13 14:06:51 +00004987 (BNX2X_CHIP_IS_E2(cp->chip_id) ?
4988 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
4989 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00004990
4991 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00004992 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00004993
Michael Chan48f753d2010-05-18 11:32:53 +00004994 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4995
Michael Chancd801532010-10-13 14:06:49 +00004996 data = udev->l2_buf;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004997
4998 memset(data, 0, sizeof(*data));
4999
5000 cnic_init_bnx2x_tx_ring(dev, data);
5001 cnic_init_bnx2x_rx_ring(dev, data);
5002
Michael Chancd801532010-10-13 14:06:49 +00005003 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
5004 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005005
5006 type = (ETH_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
5007 & SPE_HDR_CONN_TYPE;
5008 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
5009 SPE_HDR_FUNCTION_ID);
Michael Chan71034ba2009-10-10 13:46:59 +00005010
Michael Chan541a7812010-10-06 03:17:22 +00005011 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
5012
Michael Chan71034ba2009-10-10 13:46:59 +00005013 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan5159fdc2010-12-23 07:42:59 +00005014 cid, type, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005015
Michael Chan48f753d2010-05-18 11:32:53 +00005016 i = 0;
5017 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5018 ++i < 10)
5019 msleep(1);
5020
5021 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5022 netdev_err(dev->netdev,
5023 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005024 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00005025 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chan86b53602009-10-10 13:46:57 +00005026 }
5027}
5028
5029static void cnic_shutdown_rings(struct cnic_dev *dev)
5030{
Michael Chan541a7812010-10-06 03:17:22 +00005031 struct cnic_local *cp = dev->cnic_priv;
5032
5033 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5034 return;
5035
Michael Chan86b53602009-10-10 13:46:57 +00005036 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5037 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005038 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
5039 struct cnic_local *cp = dev->cnic_priv;
Michael Chan5159fdc2010-12-23 07:42:59 +00005040 u32 cli = cp->ethdev->iscsi_l2_client_id;
5041 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00005042 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00005043 int i;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005044 u32 type;
Michael Chan71034ba2009-10-10 13:46:59 +00005045
Michael Chan5159fdc2010-12-23 07:42:59 +00005046 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00005047
Michael Chan48f753d2010-05-18 11:32:53 +00005048 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5049
Michael Chan8b065b62009-12-02 15:15:36 +00005050 l5_data.phy_address.lo = cli;
5051 l5_data.phy_address.hi = 0;
5052 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00005053 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00005054 i = 0;
5055 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5056 ++i < 10)
5057 msleep(1);
5058
5059 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5060 netdev_err(dev->netdev,
5061 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005062 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005063
5064 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005065 type = (NONE_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
5066 & SPE_HDR_CONN_TYPE;
5067 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
5068 SPE_HDR_FUNCTION_ID);
5069 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan5159fdc2010-12-23 07:42:59 +00005070 cid, type, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005071 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005072 }
Michael Chan541a7812010-10-06 03:17:22 +00005073 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan86b53602009-10-10 13:46:57 +00005074}
5075
Michael Chana3059b12009-08-14 15:49:44 +00005076static int cnic_register_netdev(struct cnic_dev *dev)
5077{
5078 struct cnic_local *cp = dev->cnic_priv;
5079 struct cnic_eth_dev *ethdev = cp->ethdev;
5080 int err;
5081
5082 if (!ethdev)
5083 return -ENODEV;
5084
5085 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5086 return 0;
5087
5088 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5089 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005090 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005091
5092 return err;
5093}
5094
5095static void cnic_unregister_netdev(struct cnic_dev *dev)
5096{
5097 struct cnic_local *cp = dev->cnic_priv;
5098 struct cnic_eth_dev *ethdev = cp->ethdev;
5099
5100 if (!ethdev)
5101 return;
5102
5103 ethdev->drv_unregister_cnic(dev->netdev);
5104}
5105
Michael Chana4636962009-06-08 18:14:43 -07005106static int cnic_start_hw(struct cnic_dev *dev)
5107{
5108 struct cnic_local *cp = dev->cnic_priv;
5109 struct cnic_eth_dev *ethdev = cp->ethdev;
5110 int err;
5111
5112 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5113 return -EALREADY;
5114
Michael Chana4636962009-06-08 18:14:43 -07005115 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005116 pci_dev_get(dev->pcidev);
5117 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005118 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005119 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5120
5121 err = cp->alloc_resc(dev);
5122 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005123 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005124 goto err1;
5125 }
5126
5127 err = cp->start_hw(dev);
5128 if (err)
5129 goto err1;
5130
5131 err = cnic_cm_open(dev);
5132 if (err)
5133 goto err1;
5134
5135 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5136
5137 cp->enable_int(dev);
5138
5139 return 0;
5140
5141err1:
Michael Chana4636962009-06-08 18:14:43 -07005142 cp->free_resc(dev);
5143 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005144 return err;
5145}
5146
5147static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5148{
Michael Chana4636962009-06-08 18:14:43 -07005149 cnic_disable_bnx2_int_sync(dev);
5150
5151 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5152 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5153
5154 cnic_init_context(dev, KWQ_CID);
5155 cnic_init_context(dev, KCQ_CID);
5156
5157 cnic_setup_5709_context(dev, 0);
5158 cnic_free_irq(dev);
5159
Michael Chana4636962009-06-08 18:14:43 -07005160 cnic_free_resc(dev);
5161}
5162
Michael Chan71034ba2009-10-10 13:46:59 +00005163
5164static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5165{
5166 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005167
5168 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005169 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005170 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005171 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005172 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005173 cnic_free_resc(dev);
5174}
5175
Michael Chana4636962009-06-08 18:14:43 -07005176static void cnic_stop_hw(struct cnic_dev *dev)
5177{
5178 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5179 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005180 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005181
Michael Chan48f753d2010-05-18 11:32:53 +00005182 /* Need to wait for the ring shutdown event to complete
5183 * before clearing the CNIC_UP flag.
5184 */
Michael Chancd801532010-10-13 14:06:49 +00005185 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005186 msleep(100);
5187 i++;
5188 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005189 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005190 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
5191 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
5192 synchronize_rcu();
5193 cnic_cm_shutdown(dev);
5194 cp->stop_hw(dev);
5195 pci_dev_put(dev->pcidev);
5196 }
5197}
5198
5199static void cnic_free_dev(struct cnic_dev *dev)
5200{
5201 int i = 0;
5202
5203 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5204 msleep(100);
5205 i++;
5206 }
5207 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005208 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005209
Joe Perchesddf79b22010-02-17 15:01:54 +00005210 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005211 dev_put(dev->netdev);
5212 kfree(dev);
5213}
5214
5215static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5216 struct pci_dev *pdev)
5217{
5218 struct cnic_dev *cdev;
5219 struct cnic_local *cp;
5220 int alloc_size;
5221
5222 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5223
5224 cdev = kzalloc(alloc_size , GFP_KERNEL);
5225 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005226 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005227 return NULL;
5228 }
5229
5230 cdev->netdev = dev;
5231 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5232 cdev->register_device = cnic_register_device;
5233 cdev->unregister_device = cnic_unregister_device;
5234 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5235
5236 cp = cdev->cnic_priv;
5237 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005238 cp->l2_single_buf_size = 0x400;
5239 cp->l2_rx_ring_size = 3;
5240
5241 spin_lock_init(&cp->cnic_ulp_lock);
5242
Joe Perchesddf79b22010-02-17 15:01:54 +00005243 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005244
5245 return cdev;
5246}
5247
5248static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5249{
5250 struct pci_dev *pdev;
5251 struct cnic_dev *cdev;
5252 struct cnic_local *cp;
5253 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005254 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005255
Michael Chane2ee3612009-06-13 17:43:02 -07005256 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005257 if (probe) {
5258 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005259 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005260 }
5261 if (!ethdev)
5262 return NULL;
5263
5264 pdev = ethdev->pdev;
5265 if (!pdev)
5266 return NULL;
5267
5268 dev_hold(dev);
5269 pci_dev_get(pdev);
5270 if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5271 pdev->device == PCI_DEVICE_ID_NX2_5709S) {
5272 u8 rev;
5273
5274 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
5275 if (rev < 0x10) {
5276 pci_dev_put(pdev);
5277 goto cnic_err;
5278 }
5279 }
5280 pci_dev_put(pdev);
5281
5282 cdev = cnic_alloc_dev(dev, pdev);
5283 if (cdev == NULL)
5284 goto cnic_err;
5285
5286 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5287 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5288
5289 cp = cdev->cnic_priv;
5290 cp->ethdev = ethdev;
5291 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005292 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005293
5294 cp->cnic_ops = &cnic_bnx2_ops;
5295 cp->start_hw = cnic_start_bnx2_hw;
5296 cp->stop_hw = cnic_stop_bnx2_hw;
5297 cp->setup_pgtbl = cnic_setup_page_tbl;
5298 cp->alloc_resc = cnic_alloc_bnx2_resc;
5299 cp->free_resc = cnic_free_resc;
5300 cp->start_cm = cnic_cm_init_bnx2_hw;
5301 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5302 cp->enable_int = cnic_enable_bnx2_int;
5303 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5304 cp->close_conn = cnic_close_bnx2_conn;
5305 cp->next_idx = cnic_bnx2_next_idx;
5306 cp->hw_idx = cnic_bnx2_hw_idx;
5307 return cdev;
5308
5309cnic_err:
5310 dev_put(dev);
5311 return NULL;
5312}
5313
Michael Chan71034ba2009-10-10 13:46:59 +00005314static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5315{
5316 struct pci_dev *pdev;
5317 struct cnic_dev *cdev;
5318 struct cnic_local *cp;
5319 struct cnic_eth_dev *ethdev = NULL;
5320 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5321
5322 probe = symbol_get(bnx2x_cnic_probe);
5323 if (probe) {
5324 ethdev = (*probe)(dev);
5325 symbol_put(bnx2x_cnic_probe);
5326 }
5327 if (!ethdev)
5328 return NULL;
5329
5330 pdev = ethdev->pdev;
5331 if (!pdev)
5332 return NULL;
5333
5334 dev_hold(dev);
5335 cdev = cnic_alloc_dev(dev, pdev);
5336 if (cdev == NULL) {
5337 dev_put(dev);
5338 return NULL;
5339 }
5340
5341 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5342 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5343
5344 cp = cdev->cnic_priv;
5345 cp->ethdev = ethdev;
5346 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005347 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005348
5349 cp->cnic_ops = &cnic_bnx2x_ops;
5350 cp->start_hw = cnic_start_bnx2x_hw;
5351 cp->stop_hw = cnic_stop_bnx2x_hw;
5352 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5353 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5354 cp->free_resc = cnic_free_resc;
5355 cp->start_cm = cnic_cm_init_bnx2x_hw;
5356 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5357 cp->enable_int = cnic_enable_bnx2x_int;
5358 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Michael Chanee87a822010-10-13 14:06:51 +00005359 if (BNX2X_CHIP_IS_E2(cp->chip_id))
5360 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5361 else
5362 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005363 cp->close_conn = cnic_close_bnx2x_conn;
5364 cp->next_idx = cnic_bnx2x_next_idx;
5365 cp->hw_idx = cnic_bnx2x_hw_idx;
5366 return cdev;
5367}
5368
Michael Chana4636962009-06-08 18:14:43 -07005369static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5370{
5371 struct ethtool_drvinfo drvinfo;
5372 struct cnic_dev *cdev = NULL;
5373
5374 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5375 memset(&drvinfo, 0, sizeof(drvinfo));
5376 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5377
5378 if (!strcmp(drvinfo.driver, "bnx2"))
5379 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005380 if (!strcmp(drvinfo.driver, "bnx2x"))
5381 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005382 if (cdev) {
5383 write_lock(&cnic_dev_lock);
5384 list_add(&cdev->list, &cnic_dev_list);
5385 write_unlock(&cnic_dev_lock);
5386 }
5387 }
5388 return cdev;
5389}
5390
5391/**
5392 * netdev event handler
5393 */
5394static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5395 void *ptr)
5396{
5397 struct net_device *netdev = ptr;
5398 struct cnic_dev *dev;
5399 int if_type;
5400 int new_dev = 0;
5401
5402 dev = cnic_from_netdev(netdev);
5403
5404 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
5405 /* Check for the hot-plug device */
5406 dev = is_cnic_dev(netdev);
5407 if (dev) {
5408 new_dev = 1;
5409 cnic_hold(dev);
5410 }
5411 }
5412 if (dev) {
5413 struct cnic_local *cp = dev->cnic_priv;
5414
5415 if (new_dev)
5416 cnic_ulp_init(dev);
5417 else if (event == NETDEV_UNREGISTER)
5418 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005419
5420 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00005421 if (cnic_register_netdev(dev) != 0) {
5422 cnic_put(dev);
5423 goto done;
5424 }
Michael Chana4636962009-06-08 18:14:43 -07005425 if (!cnic_start_hw(dev))
5426 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005427 }
5428
5429 rcu_read_lock();
5430 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5431 struct cnic_ulp_ops *ulp_ops;
5432 void *ctx;
5433
5434 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5435 if (!ulp_ops || !ulp_ops->indicate_netevent)
5436 continue;
5437
5438 ctx = cp->ulp_handle[if_type];
5439
5440 ulp_ops->indicate_netevent(ctx, event);
5441 }
5442 rcu_read_unlock();
5443
5444 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005445 cnic_ulp_stop(dev);
5446 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005447 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005448 } else if (event == NETDEV_UNREGISTER) {
5449 write_lock(&cnic_dev_lock);
5450 list_del_init(&dev->list);
5451 write_unlock(&cnic_dev_lock);
5452
5453 cnic_put(dev);
5454 cnic_free_dev(dev);
5455 goto done;
5456 }
5457 cnic_put(dev);
5458 }
5459done:
5460 return NOTIFY_DONE;
5461}
5462
5463static struct notifier_block cnic_netdev_notifier = {
5464 .notifier_call = cnic_netdev_event
5465};
5466
5467static void cnic_release(void)
5468{
5469 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005470 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005471
5472 while (!list_empty(&cnic_dev_list)) {
5473 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5474 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5475 cnic_ulp_stop(dev);
5476 cnic_stop_hw(dev);
5477 }
5478
5479 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005480 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005481 list_del_init(&dev->list);
5482 cnic_free_dev(dev);
5483 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005484 while (!list_empty(&cnic_udev_list)) {
5485 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5486 list);
5487 cnic_free_uio(udev);
5488 }
Michael Chana4636962009-06-08 18:14:43 -07005489}
5490
5491static int __init cnic_init(void)
5492{
5493 int rc = 0;
5494
Joe Perchesddf79b22010-02-17 15:01:54 +00005495 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005496
5497 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5498 if (rc) {
5499 cnic_release();
5500 return rc;
5501 }
5502
Michael Chanfdf24082010-10-13 14:06:47 +00005503 cnic_wq = create_singlethread_workqueue("cnic_wq");
5504 if (!cnic_wq) {
5505 cnic_release();
5506 unregister_netdevice_notifier(&cnic_netdev_notifier);
5507 return -ENOMEM;
5508 }
5509
Michael Chana4636962009-06-08 18:14:43 -07005510 return 0;
5511}
5512
5513static void __exit cnic_exit(void)
5514{
5515 unregister_netdevice_notifier(&cnic_netdev_notifier);
5516 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005517 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005518}
5519
5520module_init(cnic_init);
5521module_exit(cnic_exit);