blob: bf4a804e4787b792ff8cd62dd96838bb93c2d8f6 [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
325static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
326 char *buf, u16 len)
327{
328 int rc = -EINVAL;
329
330 switch (msg_type) {
331 case ISCSI_UEVENT_PATH_UPDATE: {
332 struct cnic_local *cp;
333 u32 l5_cid;
334 struct cnic_sock *csk;
335 struct iscsi_path *path_resp;
336
337 if (len < sizeof(*path_resp))
338 break;
339
340 path_resp = (struct iscsi_path *) buf;
341 cp = dev->cnic_priv;
342 l5_cid = (u32) path_resp->handle;
343 if (l5_cid >= MAX_CM_SK_TBL_SZ)
344 break;
345
Michael Chand02a5e62010-02-24 14:42:06 +0000346 rcu_read_lock();
347 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
348 rc = -ENODEV;
349 rcu_read_unlock();
350 break;
351 }
Michael Chana4636962009-06-08 18:14:43 -0700352 csk = &cp->csk_tbl[l5_cid];
353 csk_hold(csk);
354 if (cnic_in_use(csk)) {
355 memcpy(csk->ha, path_resp->mac_addr, 6);
356 if (test_bit(SK_F_IPV6, &csk->flags))
357 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
358 sizeof(struct in6_addr));
359 else
360 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
361 sizeof(struct in_addr));
362 if (is_valid_ether_addr(csk->ha))
363 cnic_cm_set_pg(csk);
364 }
365 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000366 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700367 rc = 0;
368 }
369 }
370
371 return rc;
372}
373
374static int cnic_offld_prep(struct cnic_sock *csk)
375{
376 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
377 return 0;
378
379 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
380 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
381 return 0;
382 }
383
384 return 1;
385}
386
387static int cnic_close_prep(struct cnic_sock *csk)
388{
389 clear_bit(SK_F_CONNECT_START, &csk->flags);
390 smp_mb__after_clear_bit();
391
392 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
393 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
394 msleep(1);
395
396 return 1;
397 }
398 return 0;
399}
400
401static int cnic_abort_prep(struct cnic_sock *csk)
402{
403 clear_bit(SK_F_CONNECT_START, &csk->flags);
404 smp_mb__after_clear_bit();
405
406 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
407 msleep(1);
408
409 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
410 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
411 return 1;
412 }
413
414 return 0;
415}
416
Michael Chan6d7760a2009-07-27 11:25:58 -0700417static void cnic_uio_stop(void)
418{
419 struct cnic_dev *dev;
420
421 read_lock(&cnic_dev_lock);
422 list_for_each_entry(dev, &cnic_dev_list, list) {
423 struct cnic_local *cp = dev->cnic_priv;
424
Michael Chancd801532010-10-13 14:06:49 +0000425 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
Michael Chan6d7760a2009-07-27 11:25:58 -0700426 }
427 read_unlock(&cnic_dev_lock);
428}
429
Michael Chana4636962009-06-08 18:14:43 -0700430int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
431{
432 struct cnic_dev *dev;
433
roel kluin0d37f362009-11-02 06:53:44 +0000434 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000435 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700436 return -EINVAL;
437 }
438 mutex_lock(&cnic_lock);
439 if (cnic_ulp_tbl[ulp_type]) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000440 pr_err("%s: Type %d has already been registered\n",
441 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700442 mutex_unlock(&cnic_lock);
443 return -EBUSY;
444 }
445
446 read_lock(&cnic_dev_lock);
447 list_for_each_entry(dev, &cnic_dev_list, list) {
448 struct cnic_local *cp = dev->cnic_priv;
449
450 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
451 }
452 read_unlock(&cnic_dev_lock);
453
Michael Chan7fc1ece2009-08-14 15:49:47 +0000454 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700455 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
456 mutex_unlock(&cnic_lock);
457
458 /* Prevent race conditions with netdev_event */
459 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700460 list_for_each_entry(dev, &cnic_dev_list, list) {
461 struct cnic_local *cp = dev->cnic_priv;
462
463 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
464 ulp_ops->cnic_init(dev);
465 }
Michael Chana4636962009-06-08 18:14:43 -0700466 rtnl_unlock();
467
468 return 0;
469}
470
471int cnic_unregister_driver(int ulp_type)
472{
473 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000474 struct cnic_ulp_ops *ulp_ops;
475 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700476
roel kluin0d37f362009-11-02 06:53:44 +0000477 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000478 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700479 return -EINVAL;
480 }
481 mutex_lock(&cnic_lock);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000482 ulp_ops = cnic_ulp_tbl[ulp_type];
483 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000484 pr_err("%s: Type %d has not been registered\n",
485 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700486 goto out_unlock;
487 }
488 read_lock(&cnic_dev_lock);
489 list_for_each_entry(dev, &cnic_dev_list, list) {
490 struct cnic_local *cp = dev->cnic_priv;
491
492 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000493 pr_err("%s: Type %d still has devices registered\n",
494 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700495 read_unlock(&cnic_dev_lock);
496 goto out_unlock;
497 }
498 }
499 read_unlock(&cnic_dev_lock);
500
Michael Chan6d7760a2009-07-27 11:25:58 -0700501 if (ulp_type == CNIC_ULP_ISCSI)
502 cnic_uio_stop();
503
Michael Chana4636962009-06-08 18:14:43 -0700504 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
505
506 mutex_unlock(&cnic_lock);
507 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000508 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
509 msleep(100);
510 i++;
511 }
512
513 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000514 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700515 return 0;
516
517out_unlock:
518 mutex_unlock(&cnic_lock);
519 return -EINVAL;
520}
521
522static int cnic_start_hw(struct cnic_dev *);
523static void cnic_stop_hw(struct cnic_dev *);
524
525static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
526 void *ulp_ctx)
527{
528 struct cnic_local *cp = dev->cnic_priv;
529 struct cnic_ulp_ops *ulp_ops;
530
roel kluin0d37f362009-11-02 06:53:44 +0000531 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000532 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700533 return -EINVAL;
534 }
535 mutex_lock(&cnic_lock);
536 if (cnic_ulp_tbl[ulp_type] == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000537 pr_err("%s: Driver with type %d has not been registered\n",
538 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700539 mutex_unlock(&cnic_lock);
540 return -EAGAIN;
541 }
542 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000543 pr_err("%s: Type %d has already been registered to this device\n",
544 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700545 mutex_unlock(&cnic_lock);
546 return -EBUSY;
547 }
548
549 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
550 cp->ulp_handle[ulp_type] = ulp_ctx;
551 ulp_ops = cnic_ulp_tbl[ulp_type];
552 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
553 cnic_hold(dev);
554
555 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
556 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
557 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
558
559 mutex_unlock(&cnic_lock);
560
561 return 0;
562
563}
564EXPORT_SYMBOL(cnic_register_driver);
565
566static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
567{
568 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000569 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700570
roel kluin0d37f362009-11-02 06:53:44 +0000571 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000572 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700573 return -EINVAL;
574 }
575 mutex_lock(&cnic_lock);
576 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
577 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
578 cnic_put(dev);
579 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000580 pr_err("%s: device not registered to this ulp type %d\n",
581 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700582 mutex_unlock(&cnic_lock);
583 return -EINVAL;
584 }
585 mutex_unlock(&cnic_lock);
586
587 synchronize_rcu();
588
Michael Chan681dbd72009-08-14 15:49:46 +0000589 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
590 i < 20) {
591 msleep(100);
592 i++;
593 }
594 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000595 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000596
Michael Chana4636962009-06-08 18:14:43 -0700597 return 0;
598}
599EXPORT_SYMBOL(cnic_unregister_driver);
600
601static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
602{
603 id_tbl->start = start_id;
604 id_tbl->max = size;
605 id_tbl->next = 0;
606 spin_lock_init(&id_tbl->lock);
607 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
608 if (!id_tbl->table)
609 return -ENOMEM;
610
611 return 0;
612}
613
614static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
615{
616 kfree(id_tbl->table);
617 id_tbl->table = NULL;
618}
619
620static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
621{
622 int ret = -1;
623
624 id -= id_tbl->start;
625 if (id >= id_tbl->max)
626 return ret;
627
628 spin_lock(&id_tbl->lock);
629 if (!test_bit(id, id_tbl->table)) {
630 set_bit(id, id_tbl->table);
631 ret = 0;
632 }
633 spin_unlock(&id_tbl->lock);
634 return ret;
635}
636
637/* Returns -1 if not successful */
638static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
639{
640 u32 id;
641
642 spin_lock(&id_tbl->lock);
643 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
644 if (id >= id_tbl->max) {
645 id = -1;
646 if (id_tbl->next != 0) {
647 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
648 if (id >= id_tbl->next)
649 id = -1;
650 }
651 }
652
653 if (id < id_tbl->max) {
654 set_bit(id, id_tbl->table);
655 id_tbl->next = (id + 1) & (id_tbl->max - 1);
656 id += id_tbl->start;
657 }
658
659 spin_unlock(&id_tbl->lock);
660
661 return id;
662}
663
664static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
665{
666 if (id == -1)
667 return;
668
669 id -= id_tbl->start;
670 if (id >= id_tbl->max)
671 return;
672
673 clear_bit(id, id_tbl->table);
674}
675
676static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
677{
678 int i;
679
680 if (!dma->pg_arr)
681 return;
682
683 for (i = 0; i < dma->num_pages; i++) {
684 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000685 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
686 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700687 dma->pg_arr[i] = NULL;
688 }
689 }
690 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000691 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
692 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700693 dma->pgtbl = NULL;
694 }
695 kfree(dma->pg_arr);
696 dma->pg_arr = NULL;
697 dma->num_pages = 0;
698}
699
700static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
701{
702 int i;
703 u32 *page_table = dma->pgtbl;
704
705 for (i = 0; i < dma->num_pages; i++) {
706 /* Each entry needs to be in big endian format. */
707 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
708 page_table++;
709 *page_table = (u32) dma->pg_map_arr[i];
710 page_table++;
711 }
712}
713
Michael Chan71034ba2009-10-10 13:46:59 +0000714static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
715{
716 int i;
717 u32 *page_table = dma->pgtbl;
718
719 for (i = 0; i < dma->num_pages; i++) {
720 /* Each entry needs to be in little endian format. */
721 *page_table = dma->pg_map_arr[i] & 0xffffffff;
722 page_table++;
723 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
724 page_table++;
725 }
726}
727
Michael Chana4636962009-06-08 18:14:43 -0700728static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
729 int pages, int use_pg_tbl)
730{
731 int i, size;
732 struct cnic_local *cp = dev->cnic_priv;
733
734 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
735 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
736 if (dma->pg_arr == NULL)
737 return -ENOMEM;
738
739 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
740 dma->num_pages = pages;
741
742 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000743 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
744 BCM_PAGE_SIZE,
745 &dma->pg_map_arr[i],
746 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700747 if (dma->pg_arr[i] == NULL)
748 goto error;
749 }
750 if (!use_pg_tbl)
751 return 0;
752
753 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
754 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000755 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
756 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700757 if (dma->pgtbl == NULL)
758 goto error;
759
760 cp->setup_pgtbl(dev, dma);
761
762 return 0;
763
764error:
765 cnic_free_dma(dev, dma);
766 return -ENOMEM;
767}
768
Michael Chan86b53602009-10-10 13:46:57 +0000769static void cnic_free_context(struct cnic_dev *dev)
770{
771 struct cnic_local *cp = dev->cnic_priv;
772 int i;
773
774 for (i = 0; i < cp->ctx_blks; i++) {
775 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000776 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
777 cp->ctx_arr[i].ctx,
778 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000779 cp->ctx_arr[i].ctx = NULL;
780 }
781 }
782}
783
Michael Chancd801532010-10-13 14:06:49 +0000784static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700785{
Michael Chancd801532010-10-13 14:06:49 +0000786 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700787
Michael Chancd801532010-10-13 14:06:49 +0000788 if (udev->l2_buf) {
789 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
790 udev->l2_buf, udev->l2_buf_map);
791 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700792 }
793
Michael Chancd801532010-10-13 14:06:49 +0000794 if (udev->l2_ring) {
795 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
796 udev->l2_ring, udev->l2_ring_map);
797 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700798 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000799
800 pci_dev_put(udev->pdev);
801 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000802}
803
Michael Chancd801532010-10-13 14:06:49 +0000804static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000805{
Michael Chancd801532010-10-13 14:06:49 +0000806 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000807 return;
808
Michael Chana3ceeeb2010-10-13 14:06:50 +0000809 write_lock(&cnic_dev_lock);
810 list_del_init(&udev->list);
811 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000812 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000813}
814
815static void cnic_free_resc(struct cnic_dev *dev)
816{
817 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000818 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000819
Michael Chancd801532010-10-13 14:06:49 +0000820 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000821 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000822 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000823 }
Michael Chana4636962009-06-08 18:14:43 -0700824
Michael Chan86b53602009-10-10 13:46:57 +0000825 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700826 kfree(cp->ctx_arr);
827 cp->ctx_arr = NULL;
828 cp->ctx_blks = 0;
829
830 cnic_free_dma(dev, &cp->gbl_buf_info);
831 cnic_free_dma(dev, &cp->conn_buf_info);
832 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000833 cnic_free_dma(dev, &cp->kwq_16_data_info);
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
840 cnic_free_id_tbl(&cp->cid_tbl);
841}
842
843static int cnic_alloc_context(struct cnic_dev *dev)
844{
845 struct cnic_local *cp = dev->cnic_priv;
846
847 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
848 int i, k, arr_size;
849
850 cp->ctx_blk_size = BCM_PAGE_SIZE;
851 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
852 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
853 sizeof(struct cnic_ctx);
854 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
855 if (cp->ctx_arr == NULL)
856 return -ENOMEM;
857
858 k = 0;
859 for (i = 0; i < 2; i++) {
860 u32 j, reg, off, lo, hi;
861
862 if (i == 0)
863 off = BNX2_PG_CTX_MAP;
864 else
865 off = BNX2_ISCSI_CTX_MAP;
866
867 reg = cnic_reg_rd_ind(dev, off);
868 lo = reg >> 16;
869 hi = reg & 0xffff;
870 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
871 cp->ctx_arr[k].cid = j;
872 }
873
874 cp->ctx_blks = k;
875 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
876 cp->ctx_blks = 0;
877 return -ENOMEM;
878 }
879
880 for (i = 0; i < cp->ctx_blks; i++) {
881 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000882 dma_alloc_coherent(&dev->pcidev->dev,
883 BCM_PAGE_SIZE,
884 &cp->ctx_arr[i].mapping,
885 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700886 if (cp->ctx_arr[i].ctx == NULL)
887 return -ENOMEM;
888 }
889 }
890 return 0;
891}
892
Michael Chane6c28892010-06-24 14:58:39 +0000893static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
894{
895 int err, i, is_bnx2 = 0;
896 struct kcqe **kcq;
897
898 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
899 is_bnx2 = 1;
900
901 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
902 if (err)
903 return err;
904
905 kcq = (struct kcqe **) info->dma.pg_arr;
906 info->kcq = kcq;
907
908 if (is_bnx2)
909 return 0;
910
911 for (i = 0; i < KCQ_PAGE_CNT; i++) {
912 struct bnx2x_bd_chain_next *next =
913 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
914 int j = i + 1;
915
916 if (j >= KCQ_PAGE_CNT)
917 j = 0;
918 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
919 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
920 }
921 return 0;
922}
923
Michael Chancd801532010-10-13 14:06:49 +0000924static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000925{
926 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000927 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000928
Michael Chana3ceeeb2010-10-13 14:06:50 +0000929 read_lock(&cnic_dev_lock);
930 list_for_each_entry(udev, &cnic_udev_list, list) {
931 if (udev->pdev == dev->pcidev) {
932 udev->dev = dev;
933 cp->udev = udev;
934 read_unlock(&cnic_dev_lock);
935 return 0;
936 }
937 }
938 read_unlock(&cnic_dev_lock);
939
Michael Chancd801532010-10-13 14:06:49 +0000940 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
941 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +0000942 return -ENOMEM;
943
Michael Chancd801532010-10-13 14:06:49 +0000944 udev->uio_dev = -1;
945
946 udev->dev = dev;
947 udev->pdev = dev->pcidev;
948 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
949 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
950 &udev->l2_ring_map,
951 GFP_KERNEL | __GFP_COMP);
952 if (!udev->l2_ring)
Michael Chanec0248e2009-08-26 09:49:22 +0000953 return -ENOMEM;
954
Michael Chancd801532010-10-13 14:06:49 +0000955 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
956 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
957 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
958 &udev->l2_buf_map,
959 GFP_KERNEL | __GFP_COMP);
960 if (!udev->l2_buf)
961 return -ENOMEM;
962
Michael Chana3ceeeb2010-10-13 14:06:50 +0000963 write_lock(&cnic_dev_lock);
964 list_add(&udev->list, &cnic_udev_list);
965 write_unlock(&cnic_dev_lock);
966
967 pci_dev_get(udev->pdev);
968
Michael Chancd801532010-10-13 14:06:49 +0000969 cp->udev = udev;
970
Michael Chanec0248e2009-08-26 09:49:22 +0000971 return 0;
972}
973
Michael Chancd801532010-10-13 14:06:49 +0000974static int cnic_init_uio(struct cnic_dev *dev)
975{
Michael Chan5e9b2db2009-08-26 09:49:23 +0000976 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000977 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000978 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +0000979 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000980
Michael Chancd801532010-10-13 14:06:49 +0000981 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +0000982 return -ENOMEM;
983
Michael Chancd801532010-10-13 14:06:49 +0000984 uinfo = &udev->cnic_uinfo;
985
Michael Chan5e9b2db2009-08-26 09:49:23 +0000986 uinfo->mem[0].addr = dev->netdev->base_addr;
987 uinfo->mem[0].internal_addr = dev->regview;
988 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
989 uinfo->mem[0].memtype = UIO_MEM_PHYS;
990
Michael Chan5e9b2db2009-08-26 09:49:23 +0000991 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +0000992 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +0000993 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000994 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
995 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
996 else
997 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
998
999 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001000 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1001 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1002 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001003 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001004
1005 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001006 }
1007
1008 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1009
Michael Chancd801532010-10-13 14:06:49 +00001010 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1011 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001012 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1013
Michael Chancd801532010-10-13 14:06:49 +00001014 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1015 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001016 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1017
1018 uinfo->version = CNIC_MODULE_VERSION;
1019 uinfo->irq = UIO_IRQ_CUSTOM;
1020
1021 uinfo->open = cnic_uio_open;
1022 uinfo->release = cnic_uio_close;
1023
Michael Chana3ceeeb2010-10-13 14:06:50 +00001024 if (udev->uio_dev == -1) {
1025 if (!uinfo->priv) {
1026 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001027
Michael Chana3ceeeb2010-10-13 14:06:50 +00001028 ret = uio_register_device(&udev->pdev->dev, uinfo);
1029 }
1030 } else {
1031 cnic_init_rings(dev);
1032 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001033
Michael Chancd801532010-10-13 14:06:49 +00001034 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001035}
1036
Michael Chana4636962009-06-08 18:14:43 -07001037static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1038{
1039 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001040 int ret;
1041
1042 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1043 if (ret)
1044 goto error;
1045 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1046
Michael Chane6c28892010-06-24 14:58:39 +00001047 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chana4636962009-06-08 18:14:43 -07001048 if (ret)
1049 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001050
1051 ret = cnic_alloc_context(dev);
1052 if (ret)
1053 goto error;
1054
Michael Chancd801532010-10-13 14:06:49 +00001055 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001056 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001057 goto error;
1058
Michael Chancd801532010-10-13 14:06:49 +00001059 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001060 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001061 goto error;
1062
Michael Chana4636962009-06-08 18:14:43 -07001063 return 0;
1064
1065error:
1066 cnic_free_resc(dev);
1067 return ret;
1068}
1069
Michael Chan71034ba2009-10-10 13:46:59 +00001070static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1071{
1072 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001073 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001074 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001075
Michael Chan520efdf2010-06-24 14:58:37 +00001076 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001077 blks = total_mem / ctx_blk_size;
1078 if (total_mem % ctx_blk_size)
1079 blks++;
1080
1081 if (blks > cp->ethdev->ctx_tbl_len)
1082 return -ENOMEM;
1083
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001084 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001085 if (cp->ctx_arr == NULL)
1086 return -ENOMEM;
1087
1088 cp->ctx_blks = blks;
1089 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001090 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001091 cp->ctx_align = 0;
1092 else
1093 cp->ctx_align = ctx_blk_size;
1094
1095 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1096
1097 for (i = 0; i < blks; i++) {
1098 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001099 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1100 &cp->ctx_arr[i].mapping,
1101 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001102 if (cp->ctx_arr[i].ctx == NULL)
1103 return -ENOMEM;
1104
1105 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1106 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1107 cnic_free_context(dev);
1108 cp->ctx_blk_size += cp->ctx_align;
1109 i = -1;
1110 continue;
1111 }
1112 }
1113 }
1114 return 0;
1115}
1116
1117static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1118{
1119 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001120 struct cnic_eth_dev *ethdev = cp->ethdev;
1121 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001122 int i, j, n, ret, pages;
1123 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1124
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001125 cp->iro_arr = ethdev->iro_arr;
1126
Michael Chan520efdf2010-06-24 14:58:37 +00001127 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
1128 cp->iscsi_start_cid = start_cid;
1129 if (start_cid < BNX2X_ISCSI_START_CID) {
1130 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1131
1132 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
1133 cp->max_cid_space += delta;
1134 }
1135
Michael Chan71034ba2009-10-10 13:46:59 +00001136 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1137 GFP_KERNEL);
1138 if (!cp->iscsi_tbl)
1139 goto error;
1140
1141 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001142 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001143 if (!cp->ctx_tbl)
1144 goto error;
1145
1146 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1147 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1148 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1149 }
1150
Michael Chan520efdf2010-06-24 14:58:37 +00001151 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001152 PAGE_SIZE;
1153
1154 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1155 if (ret)
1156 return -ENOMEM;
1157
1158 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001159 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001160 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1161
1162 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1163 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1164 off;
1165
1166 if ((i % n) == (n - 1))
1167 j++;
1168 }
1169
Michael Chane6c28892010-06-24 14:58:39 +00001170 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00001171 if (ret)
1172 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001173
1174 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1175 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1176 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1177 if (ret)
1178 goto error;
1179
1180 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1181 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1182 if (ret)
1183 goto error;
1184
1185 ret = cnic_alloc_bnx2x_context(dev);
1186 if (ret)
1187 goto error;
1188
Michael Chan71034ba2009-10-10 13:46:59 +00001189 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1190
1191 cp->l2_rx_ring_size = 15;
1192
Michael Chancd801532010-10-13 14:06:49 +00001193 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001194 if (ret)
1195 goto error;
1196
Michael Chancd801532010-10-13 14:06:49 +00001197 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001198 if (ret)
1199 goto error;
1200
1201 return 0;
1202
1203error:
1204 cnic_free_resc(dev);
1205 return -ENOMEM;
1206}
1207
Michael Chana4636962009-06-08 18:14:43 -07001208static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1209{
1210 return cp->max_kwq_idx -
1211 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1212}
1213
1214static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1215 u32 num_wqes)
1216{
1217 struct cnic_local *cp = dev->cnic_priv;
1218 struct kwqe *prod_qe;
1219 u16 prod, sw_prod, i;
1220
1221 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1222 return -EAGAIN; /* bnx2 is down */
1223
1224 spin_lock_bh(&cp->cnic_ulp_lock);
1225 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001226 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001227 spin_unlock_bh(&cp->cnic_ulp_lock);
1228 return -EAGAIN;
1229 }
1230
Michael Chan1f1332a2010-05-18 11:32:52 +00001231 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001232
1233 prod = cp->kwq_prod_idx;
1234 sw_prod = prod & MAX_KWQ_IDX;
1235 for (i = 0; i < num_wqes; i++) {
1236 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1237 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1238 prod++;
1239 sw_prod = prod & MAX_KWQ_IDX;
1240 }
1241 cp->kwq_prod_idx = prod;
1242
1243 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1244
1245 spin_unlock_bh(&cp->cnic_ulp_lock);
1246 return 0;
1247}
1248
Michael Chan71034ba2009-10-10 13:46:59 +00001249static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1250 union l5cm_specific_data *l5_data)
1251{
1252 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1253 dma_addr_t map;
1254
1255 map = ctx->kwqe_data_mapping;
1256 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1257 l5_data->phy_address.hi = (u64) map >> 32;
1258 return ctx->kwqe_data;
1259}
1260
1261static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1262 u32 type, union l5cm_specific_data *l5_data)
1263{
1264 struct cnic_local *cp = dev->cnic_priv;
1265 struct l5cm_spe kwqe;
1266 struct kwqe_16 *kwq[1];
1267 int ret;
1268
1269 kwqe.hdr.conn_and_cmd_data =
1270 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001271 BNX2X_HW_CID(cp, cid)));
Michael Chan71034ba2009-10-10 13:46:59 +00001272 kwqe.hdr.type = cpu_to_le16(type);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001273 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001274 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1275 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1276
1277 kwq[0] = (struct kwqe_16 *) &kwqe;
1278
1279 spin_lock_bh(&cp->cnic_ulp_lock);
1280 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1281 spin_unlock_bh(&cp->cnic_ulp_lock);
1282
1283 if (ret == 1)
1284 return 0;
1285
1286 return -EBUSY;
1287}
1288
1289static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1290 struct kcqe *cqes[], u32 num_cqes)
1291{
1292 struct cnic_local *cp = dev->cnic_priv;
1293 struct cnic_ulp_ops *ulp_ops;
1294
1295 rcu_read_lock();
1296 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1297 if (likely(ulp_ops)) {
1298 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1299 cqes, num_cqes);
1300 }
1301 rcu_read_unlock();
1302}
1303
1304static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1305{
1306 struct cnic_local *cp = dev->cnic_priv;
1307 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001308 int hq_bds, pages;
1309 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001310
1311 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1312 cp->num_ccells = req1->num_ccells_per_conn;
1313 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1314 cp->num_iscsi_tasks;
1315 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1316 BNX2X_ISCSI_R2TQE_SIZE;
1317 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1318 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1319 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1320 cp->num_cqs = req1->num_cqs;
1321
1322 if (!dev->max_iscsi_conn)
1323 return 0;
1324
1325 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001326 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001327 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001328 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001329 PAGE_SIZE);
1330 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001331 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001332 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001333 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001334 req1->num_tasks_per_conn);
1335
1336 /* init Ustorm RAM */
1337 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001338 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001339 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001340 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001341 PAGE_SIZE);
1342 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001343 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001344 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001345 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001346 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001347 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001348 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001349 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001350 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001351 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001352 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1353
1354 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001355 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001356 PAGE_SIZE);
1357 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001358 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001359 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001360 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001361 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001362 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001363 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001364 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001365 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001366 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001367 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1368
1369 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001370 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001371 PAGE_SIZE);
1372 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001373 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001374 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001375 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001376 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001377 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001378 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001379 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001380 hq_bds);
1381
1382 return 0;
1383}
1384
1385static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1386{
1387 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1388 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001389 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001390 struct iscsi_kcqe kcqe;
1391 struct kcqe *cqes[1];
1392
1393 memset(&kcqe, 0, sizeof(kcqe));
1394 if (!dev->max_iscsi_conn) {
1395 kcqe.completion_status =
1396 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1397 goto done;
1398 }
1399
1400 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001401 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001402 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001403 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001404 req2->error_bit_map[1]);
1405
1406 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001407 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001408 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001409 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001410 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001411 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001412 req2->error_bit_map[1]);
1413
1414 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001415 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001416
1417 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1418
1419done:
1420 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1421 cqes[0] = (struct kcqe *) &kcqe;
1422 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1423
1424 return 0;
1425}
1426
1427static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1428{
1429 struct cnic_local *cp = dev->cnic_priv;
1430 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1431
1432 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1433 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1434
1435 cnic_free_dma(dev, &iscsi->hq_info);
1436 cnic_free_dma(dev, &iscsi->r2tq_info);
1437 cnic_free_dma(dev, &iscsi->task_array_info);
1438 }
1439 cnic_free_id(&cp->cid_tbl, ctx->cid);
1440 ctx->cid = 0;
1441}
1442
1443static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1444{
1445 u32 cid;
1446 int ret, pages;
1447 struct cnic_local *cp = dev->cnic_priv;
1448 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1449 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1450
1451 cid = cnic_alloc_new_id(&cp->cid_tbl);
1452 if (cid == -1) {
1453 ret = -ENOMEM;
1454 goto error;
1455 }
1456
1457 ctx->cid = cid;
1458 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1459
1460 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1461 if (ret)
1462 goto error;
1463
1464 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1465 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1466 if (ret)
1467 goto error;
1468
1469 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1470 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1471 if (ret)
1472 goto error;
1473
1474 return 0;
1475
1476error:
1477 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1478 return ret;
1479}
1480
1481static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1482 struct regpair *ctx_addr)
1483{
1484 struct cnic_local *cp = dev->cnic_priv;
1485 struct cnic_eth_dev *ethdev = cp->ethdev;
1486 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1487 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1488 unsigned long align_off = 0;
1489 dma_addr_t ctx_map;
1490 void *ctx;
1491
1492 if (cp->ctx_align) {
1493 unsigned long mask = cp->ctx_align - 1;
1494
1495 if (cp->ctx_arr[blk].mapping & mask)
1496 align_off = cp->ctx_align -
1497 (cp->ctx_arr[blk].mapping & mask);
1498 }
1499 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1500 (off * BNX2X_CONTEXT_MEM_SIZE);
1501 ctx = cp->ctx_arr[blk].ctx + align_off +
1502 (off * BNX2X_CONTEXT_MEM_SIZE);
1503 if (init)
1504 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1505
1506 ctx_addr->lo = ctx_map & 0xffffffff;
1507 ctx_addr->hi = (u64) ctx_map >> 32;
1508 return ctx;
1509}
1510
1511static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1512 u32 num)
1513{
1514 struct cnic_local *cp = dev->cnic_priv;
1515 struct iscsi_kwqe_conn_offload1 *req1 =
1516 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1517 struct iscsi_kwqe_conn_offload2 *req2 =
1518 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1519 struct iscsi_kwqe_conn_offload3 *req3;
1520 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1521 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1522 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001523 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001524 struct iscsi_context *ictx;
1525 struct regpair context_addr;
1526 int i, j, n = 2, n_max;
1527
1528 ctx->ctx_flags = 0;
1529 if (!req2->num_additional_wqes)
1530 return -EINVAL;
1531
1532 n_max = req2->num_additional_wqes + 2;
1533
1534 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1535 if (ictx == NULL)
1536 return -ENOMEM;
1537
1538 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1539
1540 ictx->xstorm_ag_context.hq_prod = 1;
1541
1542 ictx->xstorm_st_context.iscsi.first_burst_length =
1543 ISCSI_DEF_FIRST_BURST_LEN;
1544 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1545 ISCSI_DEF_MAX_RECV_SEG_LEN;
1546 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1547 req1->sq_page_table_addr_lo;
1548 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1549 req1->sq_page_table_addr_hi;
1550 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1551 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1552 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1553 iscsi->hq_info.pgtbl_map & 0xffffffff;
1554 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1555 (u64) iscsi->hq_info.pgtbl_map >> 32;
1556 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1557 iscsi->hq_info.pgtbl[0];
1558 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1559 iscsi->hq_info.pgtbl[1];
1560 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1561 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1562 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1563 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1564 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1565 iscsi->r2tq_info.pgtbl[0];
1566 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1567 iscsi->r2tq_info.pgtbl[1];
1568 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1569 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1570 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1571 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1572 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1573 BNX2X_ISCSI_PBL_NOT_CACHED;
1574 ictx->xstorm_st_context.iscsi.flags.flags |=
1575 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1576 ictx->xstorm_st_context.iscsi.flags.flags |=
1577 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1578
1579 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1580 /* TSTORM requires the base address of RQ DB & not PTE */
1581 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1582 req2->rq_page_table_addr_lo & PAGE_MASK;
1583 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1584 req2->rq_page_table_addr_hi;
1585 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1586 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1587 ictx->tstorm_st_context.tcp.flags2 |=
1588 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001589 ictx->tstorm_st_context.tcp.ooo_support_mode =
1590 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001591
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001592 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001593
1594 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001595 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001596 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001597 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001598 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1599 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1600 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1601 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1602 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1603 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1604 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1605 iscsi->r2tq_info.pgtbl[0];
1606 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1607 iscsi->r2tq_info.pgtbl[1];
1608 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1609 req1->cq_page_table_addr_lo;
1610 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1611 req1->cq_page_table_addr_hi;
1612 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1613 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1614 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1615 ictx->ustorm_st_context.task_pbe_cache_index =
1616 BNX2X_ISCSI_PBL_NOT_CACHED;
1617 ictx->ustorm_st_context.task_pdu_cache_index =
1618 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1619
1620 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1621 if (j == 3) {
1622 if (n >= n_max)
1623 break;
1624 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1625 j = 0;
1626 }
1627 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1628 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1629 req3->qp_first_pte[j].hi;
1630 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1631 req3->qp_first_pte[j].lo;
1632 }
1633
1634 ictx->ustorm_st_context.task_pbl_base.lo =
1635 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1636 ictx->ustorm_st_context.task_pbl_base.hi =
1637 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1638 ictx->ustorm_st_context.tce_phy_addr.lo =
1639 iscsi->task_array_info.pgtbl[0];
1640 ictx->ustorm_st_context.tce_phy_addr.hi =
1641 iscsi->task_array_info.pgtbl[1];
1642 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1643 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1644 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1645 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1646 ISCSI_DEF_MAX_BURST_LEN;
1647 ictx->ustorm_st_context.negotiated_rx |=
1648 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1649 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1650
1651 ictx->cstorm_st_context.hq_pbl_base.lo =
1652 iscsi->hq_info.pgtbl_map & 0xffffffff;
1653 ictx->cstorm_st_context.hq_pbl_base.hi =
1654 (u64) iscsi->hq_info.pgtbl_map >> 32;
1655 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1656 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1657 ictx->cstorm_st_context.task_pbl_base.lo =
1658 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1659 ictx->cstorm_st_context.task_pbl_base.hi =
1660 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1661 /* CSTORM and USTORM initialization is different, CSTORM requires
1662 * CQ DB base & not PTE addr */
1663 ictx->cstorm_st_context.cq_db_base.lo =
1664 req1->cq_page_table_addr_lo & PAGE_MASK;
1665 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1666 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1667 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1668 for (i = 0; i < cp->num_cqs; i++) {
1669 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1670 ISCSI_INITIAL_SN;
1671 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1672 ISCSI_INITIAL_SN;
1673 }
1674
1675 ictx->xstorm_ag_context.cdu_reserved =
1676 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1677 ISCSI_CONNECTION_TYPE);
1678 ictx->ustorm_ag_context.cdu_usage =
1679 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1680 ISCSI_CONNECTION_TYPE);
1681 return 0;
1682
1683}
1684
1685static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1686 u32 num, int *work)
1687{
1688 struct iscsi_kwqe_conn_offload1 *req1;
1689 struct iscsi_kwqe_conn_offload2 *req2;
1690 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001691 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001692 struct iscsi_kcqe kcqe;
1693 struct kcqe *cqes[1];
1694 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001695 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001696
1697 if (num < 2) {
1698 *work = num;
1699 return -EINVAL;
1700 }
1701
1702 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1703 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1704 if ((num - 2) < req2->num_additional_wqes) {
1705 *work = num;
1706 return -EINVAL;
1707 }
Joe Perches779bb412010-11-14 17:04:37 +00001708 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001709
1710 l5_cid = req1->iscsi_conn_id;
1711 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1712 return -EINVAL;
1713
1714 memset(&kcqe, 0, sizeof(kcqe));
1715 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1716 kcqe.iscsi_conn_id = l5_cid;
1717 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1718
Michael Chanfdf24082010-10-13 14:06:47 +00001719 ctx = &cp->ctx_tbl[l5_cid];
1720 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1721 kcqe.completion_status =
1722 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1723 goto done;
1724 }
1725
Michael Chan71034ba2009-10-10 13:46:59 +00001726 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1727 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001728 goto done;
1729 }
1730 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1731 if (ret) {
1732 atomic_dec(&cp->iscsi_conn);
1733 ret = 0;
1734 goto done;
1735 }
1736 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1737 if (ret < 0) {
1738 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1739 atomic_dec(&cp->iscsi_conn);
1740 goto done;
1741 }
1742
1743 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001744 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001745
1746done:
1747 cqes[0] = (struct kcqe *) &kcqe;
1748 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1749 return ret;
1750}
1751
1752
1753static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1754{
1755 struct cnic_local *cp = dev->cnic_priv;
1756 struct iscsi_kwqe_conn_update *req =
1757 (struct iscsi_kwqe_conn_update *) kwqe;
1758 void *data;
1759 union l5cm_specific_data l5_data;
1760 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1761 int ret;
1762
1763 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1764 return -EINVAL;
1765
1766 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1767 if (!data)
1768 return -ENOMEM;
1769
1770 memcpy(data, kwqe, sizeof(struct kwqe));
1771
1772 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1773 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1774 return ret;
1775}
1776
Michael Chana2c9e762010-10-13 14:06:46 +00001777static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001778{
1779 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001780 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001781 union l5cm_specific_data l5_data;
1782 int ret;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001783 u32 hw_cid, type;
Michael Chan71034ba2009-10-10 13:46:59 +00001784
Michael Chan71034ba2009-10-10 13:46:59 +00001785 init_waitqueue_head(&ctx->waitq);
1786 ctx->wait_cond = 0;
1787 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001788 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
1789 type = (NONE_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
1790 & SPE_HDR_CONN_TYPE;
1791 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1792 SPE_HDR_FUNCTION_ID);
1793
1794 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
1795 hw_cid, type, &l5_data);
1796
Michael Chan71034ba2009-10-10 13:46:59 +00001797 if (ret == 0)
1798 wait_event(ctx->waitq, ctx->wait_cond);
1799
Michael Chana2c9e762010-10-13 14:06:46 +00001800 return ret;
1801}
1802
1803static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1804{
1805 struct cnic_local *cp = dev->cnic_priv;
1806 struct iscsi_kwqe_conn_destroy *req =
1807 (struct iscsi_kwqe_conn_destroy *) kwqe;
1808 u32 l5_cid = req->reserved0;
1809 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1810 int ret = 0;
1811 struct iscsi_kcqe kcqe;
1812 struct kcqe *cqes[1];
1813
1814 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1815 goto skip_cfc_delete;
1816
Michael Chanfdf24082010-10-13 14:06:47 +00001817 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1818 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1819
1820 if (delta > (2 * HZ))
1821 delta = 0;
1822
1823 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1824 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1825 goto destroy_reply;
1826 }
Michael Chana2c9e762010-10-13 14:06:46 +00001827
1828 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1829
Michael Chan71034ba2009-10-10 13:46:59 +00001830skip_cfc_delete:
1831 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1832
1833 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00001834 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001835
Michael Chanfdf24082010-10-13 14:06:47 +00001836destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001837 memset(&kcqe, 0, sizeof(kcqe));
1838 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1839 kcqe.iscsi_conn_id = l5_cid;
1840 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1841 kcqe.iscsi_conn_context_id = req->context_id;
1842
1843 cqes[0] = (struct kcqe *) &kcqe;
1844 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1845
1846 return ret;
1847}
1848
1849static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1850 struct l4_kwq_connect_req1 *kwqe1,
1851 struct l4_kwq_connect_req3 *kwqe3,
1852 struct l5cm_active_conn_buffer *conn_buf)
1853{
1854 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1855 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1856 &conn_buf->xstorm_conn_buffer;
1857 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1858 &conn_buf->tstorm_conn_buffer;
1859 struct regpair context_addr;
1860 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1861 struct in6_addr src_ip, dst_ip;
1862 int i;
1863 u32 *addrp;
1864
1865 addrp = (u32 *) &conn_addr->local_ip_addr;
1866 for (i = 0; i < 4; i++, addrp++)
1867 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1868
1869 addrp = (u32 *) &conn_addr->remote_ip_addr;
1870 for (i = 0; i < 4; i++, addrp++)
1871 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1872
1873 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1874
1875 xstorm_buf->context_addr.hi = context_addr.hi;
1876 xstorm_buf->context_addr.lo = context_addr.lo;
1877 xstorm_buf->mss = 0xffff;
1878 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1879 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1880 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1881 xstorm_buf->pseudo_header_checksum =
1882 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1883
1884 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1885 tstorm_buf->params |=
1886 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1887 if (kwqe3->ka_timeout) {
1888 tstorm_buf->ka_enable = 1;
1889 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1890 tstorm_buf->ka_interval = kwqe3->ka_interval;
1891 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1892 }
1893 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1894 tstorm_buf->snd_buf = kwqe3->snd_buf;
1895 tstorm_buf->max_rt_time = 0xffffffff;
1896}
1897
1898static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1899{
1900 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001901 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001902 u8 *mac = dev->mac_addr;
1903
1904 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001905 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001906 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001907 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001908 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001909 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001910 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001911 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001912 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001913 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001914 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001915 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001916
1917 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001918 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001919 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001920 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001921 mac[4]);
1922 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001923 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001924 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001925 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001926 mac[2]);
1927 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001928 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00001929 mac[1]);
1930 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001931 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00001932 mac[0]);
1933}
1934
1935static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1936{
1937 struct cnic_local *cp = dev->cnic_priv;
1938 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1939 u16 tstorm_flags = 0;
1940
1941 if (tcp_ts) {
1942 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1943 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1944 }
1945
1946 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001947 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001948
1949 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001950 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001951}
1952
1953static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
1954 u32 num, int *work)
1955{
1956 struct cnic_local *cp = dev->cnic_priv;
1957 struct l4_kwq_connect_req1 *kwqe1 =
1958 (struct l4_kwq_connect_req1 *) wqes[0];
1959 struct l4_kwq_connect_req3 *kwqe3;
1960 struct l5cm_active_conn_buffer *conn_buf;
1961 struct l5cm_conn_addr_params *conn_addr;
1962 union l5cm_specific_data l5_data;
1963 u32 l5_cid = kwqe1->pg_cid;
1964 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1965 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1966 int ret;
1967
1968 if (num < 2) {
1969 *work = num;
1970 return -EINVAL;
1971 }
1972
1973 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
1974 *work = 3;
1975 else
1976 *work = 2;
1977
1978 if (num < *work) {
1979 *work = num;
1980 return -EINVAL;
1981 }
1982
1983 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00001984 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00001985 return -ENOMEM;
1986 }
1987 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1988 if (!conn_buf)
1989 return -ENOMEM;
1990
1991 memset(conn_buf, 0, sizeof(*conn_buf));
1992
1993 conn_addr = &conn_buf->conn_addr_buf;
1994 conn_addr->remote_addr_0 = csk->ha[0];
1995 conn_addr->remote_addr_1 = csk->ha[1];
1996 conn_addr->remote_addr_2 = csk->ha[2];
1997 conn_addr->remote_addr_3 = csk->ha[3];
1998 conn_addr->remote_addr_4 = csk->ha[4];
1999 conn_addr->remote_addr_5 = csk->ha[5];
2000
2001 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2002 struct l4_kwq_connect_req2 *kwqe2 =
2003 (struct l4_kwq_connect_req2 *) wqes[1];
2004
2005 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2006 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2007 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2008
2009 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2010 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2011 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2012 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2013 }
2014 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2015
2016 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2017 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2018 conn_addr->local_tcp_port = kwqe1->src_port;
2019 conn_addr->remote_tcp_port = kwqe1->dst_port;
2020
2021 conn_addr->pmtu = kwqe3->pmtu;
2022 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2023
2024 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002025 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002026
2027 cnic_bnx2x_set_tcp_timestamp(dev,
2028 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2029
2030 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2031 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2032 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002033 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002034
2035 return ret;
2036}
2037
2038static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2039{
2040 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2041 union l5cm_specific_data l5_data;
2042 int ret;
2043
2044 memset(&l5_data, 0, sizeof(l5_data));
2045 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2046 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2047 return ret;
2048}
2049
2050static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2051{
2052 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2053 union l5cm_specific_data l5_data;
2054 int ret;
2055
2056 memset(&l5_data, 0, sizeof(l5_data));
2057 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2058 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2059 return ret;
2060}
2061static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2062{
2063 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2064 struct l4_kcq kcqe;
2065 struct kcqe *cqes[1];
2066
2067 memset(&kcqe, 0, sizeof(kcqe));
2068 kcqe.pg_host_opaque = req->host_opaque;
2069 kcqe.pg_cid = req->host_opaque;
2070 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2071 cqes[0] = (struct kcqe *) &kcqe;
2072 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2073 return 0;
2074}
2075
2076static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2077{
2078 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2079 struct l4_kcq kcqe;
2080 struct kcqe *cqes[1];
2081
2082 memset(&kcqe, 0, sizeof(kcqe));
2083 kcqe.pg_host_opaque = req->pg_host_opaque;
2084 kcqe.pg_cid = req->pg_cid;
2085 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2086 cqes[0] = (struct kcqe *) &kcqe;
2087 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2088 return 0;
2089}
2090
2091static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2092 u32 num_wqes)
2093{
2094 int i, work, ret;
2095 u32 opcode;
2096 struct kwqe *kwqe;
2097
2098 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2099 return -EAGAIN; /* bnx2 is down */
2100
2101 for (i = 0; i < num_wqes; ) {
2102 kwqe = wqes[i];
2103 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2104 work = 1;
2105
2106 switch (opcode) {
2107 case ISCSI_KWQE_OPCODE_INIT1:
2108 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2109 break;
2110 case ISCSI_KWQE_OPCODE_INIT2:
2111 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2112 break;
2113 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2114 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2115 num_wqes - i, &work);
2116 break;
2117 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2118 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2119 break;
2120 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2121 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2122 break;
2123 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2124 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2125 &work);
2126 break;
2127 case L4_KWQE_OPCODE_VALUE_CLOSE:
2128 ret = cnic_bnx2x_close(dev, kwqe);
2129 break;
2130 case L4_KWQE_OPCODE_VALUE_RESET:
2131 ret = cnic_bnx2x_reset(dev, kwqe);
2132 break;
2133 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2134 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2135 break;
2136 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2137 ret = cnic_bnx2x_update_pg(dev, kwqe);
2138 break;
2139 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2140 ret = 0;
2141 break;
2142 default:
2143 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002144 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2145 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002146 break;
2147 }
2148 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002149 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2150 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002151 i += work;
2152 }
2153 return 0;
2154}
2155
Michael Chana4636962009-06-08 18:14:43 -07002156static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2157{
2158 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002159 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002160
2161 i = 0;
2162 j = 1;
2163 while (num_cqes) {
2164 struct cnic_ulp_ops *ulp_ops;
2165 int ulp_type;
2166 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
2167 u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
2168
2169 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002170 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002171
2172 while (j < num_cqes) {
2173 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2174
2175 if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
2176 break;
2177
2178 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002179 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002180 j++;
2181 }
2182
2183 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2184 ulp_type = CNIC_ULP_RDMA;
2185 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2186 ulp_type = CNIC_ULP_ISCSI;
2187 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2188 ulp_type = CNIC_ULP_L4;
2189 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2190 goto end;
2191 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002192 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2193 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002194 goto end;
2195 }
2196
2197 rcu_read_lock();
2198 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2199 if (likely(ulp_ops)) {
2200 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2201 cp->completed_kcq + i, j);
2202 }
2203 rcu_read_unlock();
2204end:
2205 num_cqes -= j;
2206 i += j;
2207 j = 1;
2208 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002209 if (unlikely(comp))
2210 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002211}
2212
2213static u16 cnic_bnx2_next_idx(u16 idx)
2214{
2215 return idx + 1;
2216}
2217
2218static u16 cnic_bnx2_hw_idx(u16 idx)
2219{
2220 return idx;
2221}
2222
Michael Chan71034ba2009-10-10 13:46:59 +00002223static u16 cnic_bnx2x_next_idx(u16 idx)
2224{
2225 idx++;
2226 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2227 idx++;
2228
2229 return idx;
2230}
2231
2232static u16 cnic_bnx2x_hw_idx(u16 idx)
2233{
2234 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2235 idx++;
2236 return idx;
2237}
2238
Michael Chan644b9d42010-06-24 14:58:40 +00002239static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002240{
2241 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002242 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002243 struct kcqe *kcqe;
2244 int kcqe_cnt = 0, last_cnt = 0;
2245
Michael Chan644b9d42010-06-24 14:58:40 +00002246 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002247 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002248 hw_prod = *info->hw_prod_idx_ptr;
2249 hw_prod = cp->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002250
2251 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002252 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002253 cp->completed_kcq[kcqe_cnt++] = kcqe;
2254 i = cp->next_idx(i);
2255 ri = i & MAX_KCQ_IDX;
2256 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2257 last_cnt = kcqe_cnt;
2258 last = i;
2259 }
2260 }
2261
Michael Chan644b9d42010-06-24 14:58:40 +00002262 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002263 return last_cnt;
2264}
2265
Michael Chan48f753d2010-05-18 11:32:53 +00002266static int cnic_l2_completion(struct cnic_local *cp)
2267{
2268 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002269 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002270 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002271 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002272 u32 cmd;
2273 int comp = 0;
2274
2275 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2276 return 0;
2277
2278 hw_cons = *cp->rx_cons_ptr;
2279 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2280 hw_cons++;
2281
2282 sw_cons = cp->rx_cons;
2283 while (sw_cons != hw_cons) {
2284 u8 cqe_fp_flags;
2285
2286 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2287 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2288 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2289 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2290 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2291 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2292 cmd == RAMROD_CMD_ID_ETH_HALT)
2293 comp++;
2294 }
2295 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2296 }
2297 return comp;
2298}
2299
Michael Chan86b53602009-10-10 13:46:57 +00002300static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002301{
Michael Chan541a7812010-10-06 03:17:22 +00002302 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002303 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002304
Michael Chan541a7812010-10-06 03:17:22 +00002305 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002306 return;
2307
Michael Chan541a7812010-10-06 03:17:22 +00002308 rx_cons = *cp->rx_cons_ptr;
2309 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002310 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002311 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2312 comp = cnic_l2_completion(cp);
2313
Michael Chana4636962009-06-08 18:14:43 -07002314 cp->tx_cons = tx_cons;
2315 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002316
Michael Chancd801532010-10-13 14:06:49 +00002317 if (cp->udev)
2318 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002319 }
Michael Chan48f753d2010-05-18 11:32:53 +00002320 if (comp)
2321 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002322}
2323
Michael Chanb177a5d52010-06-24 14:58:41 +00002324static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002325{
Michael Chana4636962009-06-08 18:14:43 -07002326 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002327 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002328 int kcqe_cnt;
2329
Michael Chana4636962009-06-08 18:14:43 -07002330 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2331
Michael Chan644b9d42010-06-24 14:58:40 +00002332 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002333
2334 service_kcqes(dev, kcqe_cnt);
2335
2336 /* Tell compiler that status_blk fields can change. */
2337 barrier();
Michael Chan644b9d42010-06-24 14:58:40 +00002338 if (status_idx != *cp->kcq1.status_idx_ptr) {
Michael Chanb177a5d52010-06-24 14:58:41 +00002339 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002340 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002341 } else
2342 break;
2343 }
2344
Michael Chan644b9d42010-06-24 14:58:40 +00002345 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002346
Michael Chan86b53602009-10-10 13:46:57 +00002347 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002348
Michael Chana4636962009-06-08 18:14:43 -07002349 return status_idx;
2350}
2351
Michael Chanb177a5d52010-06-24 14:58:41 +00002352static int cnic_service_bnx2(void *data, void *status_blk)
2353{
2354 struct cnic_dev *dev = data;
2355 struct cnic_local *cp = dev->cnic_priv;
2356 u32 status_idx = *cp->kcq1.status_idx_ptr;
2357
2358 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2359 return status_idx;
2360
2361 return cnic_service_bnx2_queues(dev);
2362}
2363
Michael Chana4636962009-06-08 18:14:43 -07002364static void cnic_service_bnx2_msix(unsigned long data)
2365{
2366 struct cnic_dev *dev = (struct cnic_dev *) data;
2367 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002368
Michael Chanb177a5d52010-06-24 14:58:41 +00002369 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002370
Michael Chana4636962009-06-08 18:14:43 -07002371 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2372 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2373}
2374
Michael Chan66fee9e2010-06-24 14:58:38 +00002375static void cnic_doirq(struct cnic_dev *dev)
2376{
2377 struct cnic_local *cp = dev->cnic_priv;
Michael Chane6c28892010-06-24 14:58:39 +00002378 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
Michael Chan66fee9e2010-06-24 14:58:38 +00002379
2380 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2381 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002382 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002383
2384 tasklet_schedule(&cp->cnic_irq_task);
2385 }
2386}
2387
Michael Chana4636962009-06-08 18:14:43 -07002388static irqreturn_t cnic_irq(int irq, void *dev_instance)
2389{
2390 struct cnic_dev *dev = dev_instance;
2391 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002392
2393 if (cp->ack_int)
2394 cp->ack_int(dev);
2395
Michael Chan66fee9e2010-06-24 14:58:38 +00002396 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002397
2398 return IRQ_HANDLED;
2399}
2400
Michael Chan71034ba2009-10-10 13:46:59 +00002401static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2402 u16 index, u8 op, u8 update)
2403{
2404 struct cnic_local *cp = dev->cnic_priv;
2405 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2406 COMMAND_REG_INT_ACK);
2407 struct igu_ack_register igu_ack;
2408
2409 igu_ack.status_block_index = index;
2410 igu_ack.sb_id_and_flags =
2411 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2412 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2413 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2414 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2415
2416 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2417}
2418
Michael Chanee87a822010-10-13 14:06:51 +00002419static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2420 u16 index, u8 op, u8 update)
2421{
2422 struct igu_regular cmd_data;
2423 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2424
2425 cmd_data.sb_id_and_flags =
2426 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2427 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2428 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2429 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2430
2431
2432 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2433}
2434
Michael Chan71034ba2009-10-10 13:46:59 +00002435static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2436{
2437 struct cnic_local *cp = dev->cnic_priv;
2438
Dmitry Kravkov523224a2010-10-06 03:23:26 +00002439 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00002440 IGU_INT_DISABLE, 0);
2441}
2442
Michael Chanee87a822010-10-13 14:06:51 +00002443static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2444{
2445 struct cnic_local *cp = dev->cnic_priv;
2446
2447 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2448 IGU_INT_DISABLE, 0);
2449}
2450
Michael Chanb177a5d52010-06-24 14:58:41 +00002451static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002452{
Michael Chanb177a5d52010-06-24 14:58:41 +00002453 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002454 int kcqe_cnt;
2455
Michael Chanb177a5d52010-06-24 14:58:41 +00002456 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002457
2458 service_kcqes(dev, kcqe_cnt);
2459
2460 /* Tell compiler that sblk fields can change. */
2461 barrier();
Michael Chanb177a5d52010-06-24 14:58:41 +00002462 if (last_status == *info->status_idx_ptr)
Michael Chan71034ba2009-10-10 13:46:59 +00002463 break;
2464
Michael Chanb177a5d52010-06-24 14:58:41 +00002465 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002466 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002467 return last_status;
2468}
2469
2470static void cnic_service_bnx2x_bh(unsigned long data)
2471{
2472 struct cnic_dev *dev = (struct cnic_dev *) data;
2473 struct cnic_local *cp = dev->cnic_priv;
2474 u32 status_idx;
2475
2476 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2477 return;
2478
2479 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002480
Michael Chan644b9d42010-06-24 14:58:40 +00002481 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chanee87a822010-10-13 14:06:51 +00002482 if (BNX2X_CHIP_IS_E2(cp->chip_id))
2483 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2484 status_idx, IGU_INT_ENABLE, 1);
2485 else
2486 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2487 status_idx, IGU_INT_ENABLE, 1);
Michael Chan71034ba2009-10-10 13:46:59 +00002488}
2489
2490static int cnic_service_bnx2x(void *data, void *status_blk)
2491{
2492 struct cnic_dev *dev = data;
2493 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002494
Michael Chan66fee9e2010-06-24 14:58:38 +00002495 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2496 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002497
Michael Chan66fee9e2010-06-24 14:58:38 +00002498 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002499
2500 return 0;
2501}
2502
Michael Chana4636962009-06-08 18:14:43 -07002503static void cnic_ulp_stop(struct cnic_dev *dev)
2504{
2505 struct cnic_local *cp = dev->cnic_priv;
2506 int if_type;
2507
Michael Chancd801532010-10-13 14:06:49 +00002508 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
Michael Chan6d7760a2009-07-27 11:25:58 -07002509
Michael Chana4636962009-06-08 18:14:43 -07002510 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2511 struct cnic_ulp_ops *ulp_ops;
2512
Michael Chan681dbd72009-08-14 15:49:46 +00002513 mutex_lock(&cnic_lock);
2514 ulp_ops = cp->ulp_ops[if_type];
2515 if (!ulp_ops) {
2516 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002517 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002518 }
2519 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2520 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002521
2522 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2523 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002524
2525 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002526 }
Michael Chana4636962009-06-08 18:14:43 -07002527}
2528
2529static void cnic_ulp_start(struct cnic_dev *dev)
2530{
2531 struct cnic_local *cp = dev->cnic_priv;
2532 int if_type;
2533
Michael Chana4636962009-06-08 18:14:43 -07002534 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2535 struct cnic_ulp_ops *ulp_ops;
2536
Michael Chan681dbd72009-08-14 15:49:46 +00002537 mutex_lock(&cnic_lock);
2538 ulp_ops = cp->ulp_ops[if_type];
2539 if (!ulp_ops || !ulp_ops->cnic_start) {
2540 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002541 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002542 }
2543 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2544 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002545
2546 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2547 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002548
2549 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002550 }
Michael Chana4636962009-06-08 18:14:43 -07002551}
2552
2553static int cnic_ctl(void *data, struct cnic_ctl_info *info)
2554{
2555 struct cnic_dev *dev = data;
2556
2557 switch (info->cmd) {
2558 case CNIC_CTL_STOP_CMD:
2559 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07002560
2561 cnic_ulp_stop(dev);
2562 cnic_stop_hw(dev);
2563
Michael Chana4636962009-06-08 18:14:43 -07002564 cnic_put(dev);
2565 break;
2566 case CNIC_CTL_START_CMD:
2567 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07002568
2569 if (!cnic_start_hw(dev))
2570 cnic_ulp_start(dev);
2571
Michael Chana4636962009-06-08 18:14:43 -07002572 cnic_put(dev);
2573 break;
Michael Chan71034ba2009-10-10 13:46:59 +00002574 case CNIC_CTL_COMPLETION_CMD: {
2575 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
2576 u32 l5_cid;
2577 struct cnic_local *cp = dev->cnic_priv;
2578
2579 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
2580 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2581
2582 ctx->wait_cond = 1;
2583 wake_up(&ctx->waitq);
2584 }
2585 break;
2586 }
Michael Chana4636962009-06-08 18:14:43 -07002587 default:
2588 return -EINVAL;
2589 }
2590 return 0;
2591}
2592
2593static void cnic_ulp_init(struct cnic_dev *dev)
2594{
2595 int i;
2596 struct cnic_local *cp = dev->cnic_priv;
2597
Michael Chana4636962009-06-08 18:14:43 -07002598 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
2599 struct cnic_ulp_ops *ulp_ops;
2600
Michael Chan7fc1ece2009-08-14 15:49:47 +00002601 mutex_lock(&cnic_lock);
2602 ulp_ops = cnic_ulp_tbl[i];
2603 if (!ulp_ops || !ulp_ops->cnic_init) {
2604 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002605 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00002606 }
2607 ulp_get(ulp_ops);
2608 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002609
2610 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
2611 ulp_ops->cnic_init(dev);
2612
Michael Chan7fc1ece2009-08-14 15:49:47 +00002613 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07002614 }
Michael Chana4636962009-06-08 18:14:43 -07002615}
2616
2617static void cnic_ulp_exit(struct cnic_dev *dev)
2618{
2619 int i;
2620 struct cnic_local *cp = dev->cnic_priv;
2621
Michael Chana4636962009-06-08 18:14:43 -07002622 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
2623 struct cnic_ulp_ops *ulp_ops;
2624
Michael Chan7fc1ece2009-08-14 15:49:47 +00002625 mutex_lock(&cnic_lock);
2626 ulp_ops = cnic_ulp_tbl[i];
2627 if (!ulp_ops || !ulp_ops->cnic_exit) {
2628 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002629 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00002630 }
2631 ulp_get(ulp_ops);
2632 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002633
2634 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
2635 ulp_ops->cnic_exit(dev);
2636
Michael Chan7fc1ece2009-08-14 15:49:47 +00002637 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07002638 }
Michael Chana4636962009-06-08 18:14:43 -07002639}
2640
2641static int cnic_cm_offload_pg(struct cnic_sock *csk)
2642{
2643 struct cnic_dev *dev = csk->dev;
2644 struct l4_kwq_offload_pg *l4kwqe;
2645 struct kwqe *wqes[1];
2646
2647 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
2648 memset(l4kwqe, 0, sizeof(*l4kwqe));
2649 wqes[0] = (struct kwqe *) l4kwqe;
2650
2651 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
2652 l4kwqe->flags =
2653 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
2654 l4kwqe->l2hdr_nbytes = ETH_HLEN;
2655
2656 l4kwqe->da0 = csk->ha[0];
2657 l4kwqe->da1 = csk->ha[1];
2658 l4kwqe->da2 = csk->ha[2];
2659 l4kwqe->da3 = csk->ha[3];
2660 l4kwqe->da4 = csk->ha[4];
2661 l4kwqe->da5 = csk->ha[5];
2662
2663 l4kwqe->sa0 = dev->mac_addr[0];
2664 l4kwqe->sa1 = dev->mac_addr[1];
2665 l4kwqe->sa2 = dev->mac_addr[2];
2666 l4kwqe->sa3 = dev->mac_addr[3];
2667 l4kwqe->sa4 = dev->mac_addr[4];
2668 l4kwqe->sa5 = dev->mac_addr[5];
2669
2670 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00002671 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07002672 l4kwqe->host_opaque = csk->l5_cid;
2673
2674 if (csk->vlan_id) {
2675 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
2676 l4kwqe->vlan_tag = csk->vlan_id;
2677 l4kwqe->l2hdr_nbytes += 4;
2678 }
2679
2680 return dev->submit_kwqes(dev, wqes, 1);
2681}
2682
2683static int cnic_cm_update_pg(struct cnic_sock *csk)
2684{
2685 struct cnic_dev *dev = csk->dev;
2686 struct l4_kwq_update_pg *l4kwqe;
2687 struct kwqe *wqes[1];
2688
2689 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
2690 memset(l4kwqe, 0, sizeof(*l4kwqe));
2691 wqes[0] = (struct kwqe *) l4kwqe;
2692
2693 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
2694 l4kwqe->flags =
2695 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
2696 l4kwqe->pg_cid = csk->pg_cid;
2697
2698 l4kwqe->da0 = csk->ha[0];
2699 l4kwqe->da1 = csk->ha[1];
2700 l4kwqe->da2 = csk->ha[2];
2701 l4kwqe->da3 = csk->ha[3];
2702 l4kwqe->da4 = csk->ha[4];
2703 l4kwqe->da5 = csk->ha[5];
2704
2705 l4kwqe->pg_host_opaque = csk->l5_cid;
2706 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
2707
2708 return dev->submit_kwqes(dev, wqes, 1);
2709}
2710
2711static int cnic_cm_upload_pg(struct cnic_sock *csk)
2712{
2713 struct cnic_dev *dev = csk->dev;
2714 struct l4_kwq_upload *l4kwqe;
2715 struct kwqe *wqes[1];
2716
2717 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
2718 memset(l4kwqe, 0, sizeof(*l4kwqe));
2719 wqes[0] = (struct kwqe *) l4kwqe;
2720
2721 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
2722 l4kwqe->flags =
2723 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
2724 l4kwqe->cid = csk->pg_cid;
2725
2726 return dev->submit_kwqes(dev, wqes, 1);
2727}
2728
2729static int cnic_cm_conn_req(struct cnic_sock *csk)
2730{
2731 struct cnic_dev *dev = csk->dev;
2732 struct l4_kwq_connect_req1 *l4kwqe1;
2733 struct l4_kwq_connect_req2 *l4kwqe2;
2734 struct l4_kwq_connect_req3 *l4kwqe3;
2735 struct kwqe *wqes[3];
2736 u8 tcp_flags = 0;
2737 int num_wqes = 2;
2738
2739 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
2740 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
2741 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
2742 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
2743 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
2744 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
2745
2746 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
2747 l4kwqe3->flags =
2748 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
2749 l4kwqe3->ka_timeout = csk->ka_timeout;
2750 l4kwqe3->ka_interval = csk->ka_interval;
2751 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
2752 l4kwqe3->tos = csk->tos;
2753 l4kwqe3->ttl = csk->ttl;
2754 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
2755 l4kwqe3->pmtu = csk->mtu;
2756 l4kwqe3->rcv_buf = csk->rcv_buf;
2757 l4kwqe3->snd_buf = csk->snd_buf;
2758 l4kwqe3->seed = csk->seed;
2759
2760 wqes[0] = (struct kwqe *) l4kwqe1;
2761 if (test_bit(SK_F_IPV6, &csk->flags)) {
2762 wqes[1] = (struct kwqe *) l4kwqe2;
2763 wqes[2] = (struct kwqe *) l4kwqe3;
2764 num_wqes = 3;
2765
2766 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
2767 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
2768 l4kwqe2->flags =
2769 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
2770 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
2771 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
2772 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
2773 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
2774 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
2775 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
2776 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
2777 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
2778 sizeof(struct tcphdr);
2779 } else {
2780 wqes[1] = (struct kwqe *) l4kwqe3;
2781 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
2782 sizeof(struct tcphdr);
2783 }
2784
2785 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
2786 l4kwqe1->flags =
2787 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
2788 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
2789 l4kwqe1->cid = csk->cid;
2790 l4kwqe1->pg_cid = csk->pg_cid;
2791 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
2792 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
2793 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
2794 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
2795 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
2796 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
2797 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
2798 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
2799 if (csk->tcp_flags & SK_TCP_NAGLE)
2800 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
2801 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
2802 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
2803 if (csk->tcp_flags & SK_TCP_SACK)
2804 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
2805 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
2806 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
2807
2808 l4kwqe1->tcp_flags = tcp_flags;
2809
2810 return dev->submit_kwqes(dev, wqes, num_wqes);
2811}
2812
2813static int cnic_cm_close_req(struct cnic_sock *csk)
2814{
2815 struct cnic_dev *dev = csk->dev;
2816 struct l4_kwq_close_req *l4kwqe;
2817 struct kwqe *wqes[1];
2818
2819 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
2820 memset(l4kwqe, 0, sizeof(*l4kwqe));
2821 wqes[0] = (struct kwqe *) l4kwqe;
2822
2823 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
2824 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
2825 l4kwqe->cid = csk->cid;
2826
2827 return dev->submit_kwqes(dev, wqes, 1);
2828}
2829
2830static int cnic_cm_abort_req(struct cnic_sock *csk)
2831{
2832 struct cnic_dev *dev = csk->dev;
2833 struct l4_kwq_reset_req *l4kwqe;
2834 struct kwqe *wqes[1];
2835
2836 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
2837 memset(l4kwqe, 0, sizeof(*l4kwqe));
2838 wqes[0] = (struct kwqe *) l4kwqe;
2839
2840 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
2841 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
2842 l4kwqe->cid = csk->cid;
2843
2844 return dev->submit_kwqes(dev, wqes, 1);
2845}
2846
2847static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
2848 u32 l5_cid, struct cnic_sock **csk, void *context)
2849{
2850 struct cnic_local *cp = dev->cnic_priv;
2851 struct cnic_sock *csk1;
2852
2853 if (l5_cid >= MAX_CM_SK_TBL_SZ)
2854 return -EINVAL;
2855
Michael Chanfdf24082010-10-13 14:06:47 +00002856 if (cp->ctx_tbl) {
2857 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2858
2859 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2860 return -EAGAIN;
2861 }
2862
Michael Chana4636962009-06-08 18:14:43 -07002863 csk1 = &cp->csk_tbl[l5_cid];
2864 if (atomic_read(&csk1->ref_count))
2865 return -EAGAIN;
2866
2867 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
2868 return -EBUSY;
2869
2870 csk1->dev = dev;
2871 csk1->cid = cid;
2872 csk1->l5_cid = l5_cid;
2873 csk1->ulp_type = ulp_type;
2874 csk1->context = context;
2875
2876 csk1->ka_timeout = DEF_KA_TIMEOUT;
2877 csk1->ka_interval = DEF_KA_INTERVAL;
2878 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
2879 csk1->tos = DEF_TOS;
2880 csk1->ttl = DEF_TTL;
2881 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
2882 csk1->rcv_buf = DEF_RCV_BUF;
2883 csk1->snd_buf = DEF_SND_BUF;
2884 csk1->seed = DEF_SEED;
2885
2886 *csk = csk1;
2887 return 0;
2888}
2889
2890static void cnic_cm_cleanup(struct cnic_sock *csk)
2891{
2892 if (csk->src_port) {
2893 struct cnic_dev *dev = csk->dev;
2894 struct cnic_local *cp = dev->cnic_priv;
2895
Michael Chan9b093362010-12-23 07:42:56 +00002896 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07002897 csk->src_port = 0;
2898 }
2899}
2900
2901static void cnic_close_conn(struct cnic_sock *csk)
2902{
2903 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
2904 cnic_cm_upload_pg(csk);
2905 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
2906 }
2907 cnic_cm_cleanup(csk);
2908}
2909
2910static int cnic_cm_destroy(struct cnic_sock *csk)
2911{
2912 if (!cnic_in_use(csk))
2913 return -EINVAL;
2914
2915 csk_hold(csk);
2916 clear_bit(SK_F_INUSE, &csk->flags);
2917 smp_mb__after_clear_bit();
2918 while (atomic_read(&csk->ref_count) != 1)
2919 msleep(1);
2920 cnic_cm_cleanup(csk);
2921
2922 csk->flags = 0;
2923 csk_put(csk);
2924 return 0;
2925}
2926
2927static inline u16 cnic_get_vlan(struct net_device *dev,
2928 struct net_device **vlan_dev)
2929{
2930 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2931 *vlan_dev = vlan_dev_real_dev(dev);
2932 return vlan_dev_vlan_id(dev);
2933 }
2934 *vlan_dev = dev;
2935 return 0;
2936}
2937
2938static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
2939 struct dst_entry **dst)
2940{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07002941#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07002942 struct flowi fl;
2943 int err;
2944 struct rtable *rt;
2945
2946 memset(&fl, 0, sizeof(fl));
2947 fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
2948
2949 err = ip_route_output_key(&init_net, &rt, &fl);
2950 if (!err)
Changli Gaod8d1f302010-06-10 23:31:35 -07002951 *dst = &rt->dst;
Michael Chana4636962009-06-08 18:14:43 -07002952 return err;
Randy Dunlapfaea56c2009-06-12 11:43:48 -07002953#else
2954 return -ENETUNREACH;
2955#endif
Michael Chana4636962009-06-08 18:14:43 -07002956}
2957
2958static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
2959 struct dst_entry **dst)
2960{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07002961#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
Michael Chana4636962009-06-08 18:14:43 -07002962 struct flowi fl;
2963
2964 memset(&fl, 0, sizeof(fl));
2965 ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
2966 if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
2967 fl.oif = dst_addr->sin6_scope_id;
2968
2969 *dst = ip6_route_output(&init_net, NULL, &fl);
2970 if (*dst)
2971 return 0;
2972#endif
2973
2974 return -ENETUNREACH;
2975}
2976
2977static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
2978 int ulp_type)
2979{
2980 struct cnic_dev *dev = NULL;
2981 struct dst_entry *dst;
2982 struct net_device *netdev = NULL;
2983 int err = -ENETUNREACH;
2984
2985 if (dst_addr->sin_family == AF_INET)
2986 err = cnic_get_v4_route(dst_addr, &dst);
2987 else if (dst_addr->sin_family == AF_INET6) {
2988 struct sockaddr_in6 *dst_addr6 =
2989 (struct sockaddr_in6 *) dst_addr;
2990
2991 err = cnic_get_v6_route(dst_addr6, &dst);
2992 } else
2993 return NULL;
2994
2995 if (err)
2996 return NULL;
2997
2998 if (!dst->dev)
2999 goto done;
3000
3001 cnic_get_vlan(dst->dev, &netdev);
3002
3003 dev = cnic_from_netdev(netdev);
3004
3005done:
3006 dst_release(dst);
3007 if (dev)
3008 cnic_put(dev);
3009 return dev;
3010}
3011
3012static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3013{
3014 struct cnic_dev *dev = csk->dev;
3015 struct cnic_local *cp = dev->cnic_priv;
3016
3017 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3018}
3019
3020static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3021{
3022 struct cnic_dev *dev = csk->dev;
3023 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003024 int is_v6, rc = 0;
3025 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003026 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003027 __be16 local_port;
3028 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003029
3030 if (saddr->local.v6.sin6_family == AF_INET6 &&
3031 saddr->remote.v6.sin6_family == AF_INET6)
3032 is_v6 = 1;
3033 else if (saddr->local.v4.sin_family == AF_INET &&
3034 saddr->remote.v4.sin_family == AF_INET)
3035 is_v6 = 0;
3036 else
3037 return -EINVAL;
3038
3039 clear_bit(SK_F_IPV6, &csk->flags);
3040
3041 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003042 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003043 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003044
3045 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3046 sizeof(struct in6_addr));
3047 csk->dst_port = saddr->remote.v6.sin6_port;
3048 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003049
3050 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003051 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003052
3053 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3054 csk->dst_port = saddr->remote.v4.sin_port;
3055 local_port = saddr->local.v4.sin_port;
3056 }
3057
Michael Chanc76284a2010-02-24 14:42:07 +00003058 csk->vlan_id = 0;
3059 csk->mtu = dev->netdev->mtu;
3060 if (dst && dst->dev) {
3061 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3062 if (realdev == dev->netdev) {
3063 csk->vlan_id = vlan;
3064 csk->mtu = dst_mtu(dst);
3065 }
3066 }
Michael Chana4636962009-06-08 18:14:43 -07003067
Michael Chan9b093362010-12-23 07:42:56 +00003068 port_id = be16_to_cpu(local_port);
3069 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3070 port_id < CNIC_LOCAL_PORT_MAX) {
3071 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3072 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003073 } else
Michael Chan9b093362010-12-23 07:42:56 +00003074 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003075
Michael Chan9b093362010-12-23 07:42:56 +00003076 if (!port_id) {
3077 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3078 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003079 rc = -ENOMEM;
3080 goto err_out;
3081 }
Michael Chan9b093362010-12-23 07:42:56 +00003082 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003083 }
3084 csk->src_port = local_port;
3085
Michael Chana4636962009-06-08 18:14:43 -07003086err_out:
3087 dst_release(dst);
3088 return rc;
3089}
3090
3091static void cnic_init_csk_state(struct cnic_sock *csk)
3092{
3093 csk->state = 0;
3094 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3095 clear_bit(SK_F_CLOSING, &csk->flags);
3096}
3097
3098static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3099{
3100 int err = 0;
3101
3102 if (!cnic_in_use(csk))
3103 return -EINVAL;
3104
3105 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3106 return -EINVAL;
3107
3108 cnic_init_csk_state(csk);
3109
3110 err = cnic_get_route(csk, saddr);
3111 if (err)
3112 goto err_out;
3113
3114 err = cnic_resolve_addr(csk, saddr);
3115 if (!err)
3116 return 0;
3117
3118err_out:
3119 clear_bit(SK_F_CONNECT_START, &csk->flags);
3120 return err;
3121}
3122
3123static int cnic_cm_abort(struct cnic_sock *csk)
3124{
3125 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003126 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003127
3128 if (!cnic_in_use(csk))
3129 return -EINVAL;
3130
3131 if (cnic_abort_prep(csk))
3132 return cnic_cm_abort_req(csk);
3133
3134 /* Getting here means that we haven't started connect, or
3135 * connect was not successful.
3136 */
3137
Michael Chana4636962009-06-08 18:14:43 -07003138 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003139 if (csk->state != opcode)
3140 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003141
3142 return 0;
3143}
3144
3145static int cnic_cm_close(struct cnic_sock *csk)
3146{
3147 if (!cnic_in_use(csk))
3148 return -EINVAL;
3149
3150 if (cnic_close_prep(csk)) {
3151 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3152 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003153 } else {
3154 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003155 }
3156 return 0;
3157}
3158
3159static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3160 u8 opcode)
3161{
3162 struct cnic_ulp_ops *ulp_ops;
3163 int ulp_type = csk->ulp_type;
3164
3165 rcu_read_lock();
3166 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3167 if (ulp_ops) {
3168 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3169 ulp_ops->cm_connect_complete(csk);
3170 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3171 ulp_ops->cm_close_complete(csk);
3172 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3173 ulp_ops->cm_remote_abort(csk);
3174 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3175 ulp_ops->cm_abort_complete(csk);
3176 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3177 ulp_ops->cm_remote_close(csk);
3178 }
3179 rcu_read_unlock();
3180}
3181
3182static int cnic_cm_set_pg(struct cnic_sock *csk)
3183{
3184 if (cnic_offld_prep(csk)) {
3185 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3186 cnic_cm_update_pg(csk);
3187 else
3188 cnic_cm_offload_pg(csk);
3189 }
3190 return 0;
3191}
3192
3193static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3194{
3195 struct cnic_local *cp = dev->cnic_priv;
3196 u32 l5_cid = kcqe->pg_host_opaque;
3197 u8 opcode = kcqe->op_code;
3198 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3199
3200 csk_hold(csk);
3201 if (!cnic_in_use(csk))
3202 goto done;
3203
3204 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3205 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3206 goto done;
3207 }
Eddie Waia9736c02010-02-24 14:42:04 +00003208 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3209 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3210 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3211 cnic_cm_upcall(cp, csk,
3212 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3213 goto done;
3214 }
3215
Michael Chana4636962009-06-08 18:14:43 -07003216 csk->pg_cid = kcqe->pg_cid;
3217 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3218 cnic_cm_conn_req(csk);
3219
3220done:
3221 csk_put(csk);
3222}
3223
3224static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3225{
3226 struct cnic_local *cp = dev->cnic_priv;
3227 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3228 u8 opcode = l4kcqe->op_code;
3229 u32 l5_cid;
3230 struct cnic_sock *csk;
3231
3232 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3233 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3234 cnic_cm_process_offld_pg(dev, l4kcqe);
3235 return;
3236 }
3237
3238 l5_cid = l4kcqe->conn_id;
3239 if (opcode & 0x80)
3240 l5_cid = l4kcqe->cid;
3241 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3242 return;
3243
3244 csk = &cp->csk_tbl[l5_cid];
3245 csk_hold(csk);
3246
3247 if (!cnic_in_use(csk)) {
3248 csk_put(csk);
3249 return;
3250 }
3251
3252 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003253 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3254 if (l4kcqe->status != 0) {
3255 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3256 cnic_cm_upcall(cp, csk,
3257 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3258 }
3259 break;
Michael Chana4636962009-06-08 18:14:43 -07003260 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3261 if (l4kcqe->status == 0)
3262 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3263
3264 smp_mb__before_clear_bit();
3265 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3266 cnic_cm_upcall(cp, csk, opcode);
3267 break;
3268
3269 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003270 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3271 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003272 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3273 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003274 cp->close_conn(csk, opcode);
3275 break;
3276
3277 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
3278 cnic_cm_upcall(cp, csk, opcode);
3279 break;
3280 }
3281 csk_put(csk);
3282}
3283
3284static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3285{
3286 struct cnic_dev *dev = data;
3287 int i;
3288
3289 for (i = 0; i < num; i++)
3290 cnic_cm_process_kcqe(dev, kcqe[i]);
3291}
3292
3293static struct cnic_ulp_ops cm_ulp_ops = {
3294 .indicate_kcqes = cnic_cm_indicate_kcqe,
3295};
3296
3297static void cnic_cm_free_mem(struct cnic_dev *dev)
3298{
3299 struct cnic_local *cp = dev->cnic_priv;
3300
3301 kfree(cp->csk_tbl);
3302 cp->csk_tbl = NULL;
3303 cnic_free_id_tbl(&cp->csk_port_tbl);
3304}
3305
3306static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3307{
3308 struct cnic_local *cp = dev->cnic_priv;
3309
3310 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3311 GFP_KERNEL);
3312 if (!cp->csk_tbl)
3313 return -ENOMEM;
3314
3315 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
3316 CNIC_LOCAL_PORT_MIN)) {
3317 cnic_cm_free_mem(dev);
3318 return -ENOMEM;
3319 }
3320 return 0;
3321}
3322
3323static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3324{
Michael Chan943189f2010-06-15 08:57:02 +00003325 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3326 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3327 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3328 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003329 }
Michael Chan943189f2010-06-15 08:57:02 +00003330
3331 /* 1. If event opcode matches the expected event in csk->state
3332 * 2. If the expected event is CLOSE_COMP, we accept any event
Michael Chan7b34a462010-06-15 08:57:03 +00003333 * 3. If the expected event is 0, meaning the connection was never
3334 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003335 */
Michael Chan7b34a462010-06-15 08:57:03 +00003336 if (opcode == csk->state || csk->state == 0 ||
3337 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP) {
3338 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3339 if (csk->state == 0)
3340 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003341 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003342 }
Michael Chana4636962009-06-08 18:14:43 -07003343 }
3344 return 0;
3345}
3346
3347static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3348{
3349 struct cnic_dev *dev = csk->dev;
3350 struct cnic_local *cp = dev->cnic_priv;
3351
Michael Chana1e621b2010-06-15 08:57:01 +00003352 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3353 cnic_cm_upcall(cp, csk, opcode);
3354 return;
3355 }
3356
Michael Chana4636962009-06-08 18:14:43 -07003357 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003358 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003359 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003360 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003361}
3362
3363static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3364{
3365}
3366
3367static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3368{
3369 u32 seed;
3370
3371 get_random_bytes(&seed, 4);
3372 cnic_ctx_wr(dev, 45, 0, seed);
3373 return 0;
3374}
3375
Michael Chan71034ba2009-10-10 13:46:59 +00003376static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3377{
3378 struct cnic_dev *dev = csk->dev;
3379 struct cnic_local *cp = dev->cnic_priv;
3380 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3381 union l5cm_specific_data l5_data;
3382 u32 cmd = 0;
3383 int close_complete = 0;
3384
3385 switch (opcode) {
3386 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3387 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3388 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003389 if (cnic_ready_to_close(csk, opcode)) {
3390 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3391 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3392 else
3393 close_complete = 1;
3394 }
Michael Chan71034ba2009-10-10 13:46:59 +00003395 break;
3396 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3397 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3398 break;
3399 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3400 close_complete = 1;
3401 break;
3402 }
3403 if (cmd) {
3404 memset(&l5_data, 0, sizeof(l5_data));
3405
3406 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3407 &l5_data);
3408 } else if (close_complete) {
3409 ctx->timestamp = jiffies;
3410 cnic_close_conn(csk);
3411 cnic_cm_upcall(cp, csk, csk->state);
3412 }
3413}
3414
3415static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3416{
Michael Chanfdf24082010-10-13 14:06:47 +00003417 struct cnic_local *cp = dev->cnic_priv;
3418 int i;
3419
3420 if (!cp->ctx_tbl)
3421 return;
3422
3423 if (!netif_running(dev->netdev))
3424 return;
3425
3426 for (i = 0; i < cp->max_cid_space; i++) {
3427 struct cnic_context *ctx = &cp->ctx_tbl[i];
3428
3429 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3430 msleep(10);
3431
3432 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3433 netdev_warn(dev->netdev, "CID %x not deleted\n",
3434 ctx->cid);
3435 }
3436
3437 cancel_delayed_work(&cp->delete_task);
3438 flush_workqueue(cnic_wq);
3439
3440 if (atomic_read(&cp->iscsi_conn) != 0)
3441 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3442 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00003443}
3444
3445static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3446{
3447 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003448 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003449 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003450
3451 cnic_init_bnx2x_mac(dev);
3452 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3453
3454 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003455 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003456
3457 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003458 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003459 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003460 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00003461 DEF_MAX_DA_COUNT);
3462
3463 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003464 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003465 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003466 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003467 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003468 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003469 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003470 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003471
Michael Chan14203982010-10-06 03:16:06 +00003472 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003473 DEF_MAX_CWND);
3474 return 0;
3475}
3476
Michael Chanfdf24082010-10-13 14:06:47 +00003477static void cnic_delete_task(struct work_struct *work)
3478{
3479 struct cnic_local *cp;
3480 struct cnic_dev *dev;
3481 u32 i;
3482 int need_resched = 0;
3483
3484 cp = container_of(work, struct cnic_local, delete_task.work);
3485 dev = cp->dev;
3486
3487 for (i = 0; i < cp->max_cid_space; i++) {
3488 struct cnic_context *ctx = &cp->ctx_tbl[i];
3489
3490 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
3491 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3492 continue;
3493
3494 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
3495 need_resched = 1;
3496 continue;
3497 }
3498
3499 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3500 continue;
3501
3502 cnic_bnx2x_destroy_ramrod(dev, i);
3503
3504 cnic_free_bnx2x_conn_resc(dev, i);
3505 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
3506 atomic_dec(&cp->iscsi_conn);
3507
3508 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
3509 }
3510
3511 if (need_resched)
3512 queue_delayed_work(cnic_wq, &cp->delete_task,
3513 msecs_to_jiffies(10));
3514
3515}
3516
Michael Chana4636962009-06-08 18:14:43 -07003517static int cnic_cm_open(struct cnic_dev *dev)
3518{
3519 struct cnic_local *cp = dev->cnic_priv;
3520 int err;
3521
3522 err = cnic_cm_alloc_mem(dev);
3523 if (err)
3524 return err;
3525
3526 err = cp->start_cm(dev);
3527
3528 if (err)
3529 goto err_out;
3530
Michael Chanfdf24082010-10-13 14:06:47 +00003531 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
3532
Michael Chana4636962009-06-08 18:14:43 -07003533 dev->cm_create = cnic_cm_create;
3534 dev->cm_destroy = cnic_cm_destroy;
3535 dev->cm_connect = cnic_cm_connect;
3536 dev->cm_abort = cnic_cm_abort;
3537 dev->cm_close = cnic_cm_close;
3538 dev->cm_select_dev = cnic_cm_select_dev;
3539
3540 cp->ulp_handle[CNIC_ULP_L4] = dev;
3541 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
3542 return 0;
3543
3544err_out:
3545 cnic_cm_free_mem(dev);
3546 return err;
3547}
3548
3549static int cnic_cm_shutdown(struct cnic_dev *dev)
3550{
3551 struct cnic_local *cp = dev->cnic_priv;
3552 int i;
3553
3554 cp->stop_cm(dev);
3555
3556 if (!cp->csk_tbl)
3557 return 0;
3558
3559 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
3560 struct cnic_sock *csk = &cp->csk_tbl[i];
3561
3562 clear_bit(SK_F_INUSE, &csk->flags);
3563 cnic_cm_cleanup(csk);
3564 }
3565 cnic_cm_free_mem(dev);
3566
3567 return 0;
3568}
3569
3570static void cnic_init_context(struct cnic_dev *dev, u32 cid)
3571{
Michael Chana4636962009-06-08 18:14:43 -07003572 u32 cid_addr;
3573 int i;
3574
Michael Chana4636962009-06-08 18:14:43 -07003575 cid_addr = GET_CID_ADDR(cid);
3576
3577 for (i = 0; i < CTX_SIZE; i += 4)
3578 cnic_ctx_wr(dev, cid_addr, i, 0);
3579}
3580
3581static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
3582{
3583 struct cnic_local *cp = dev->cnic_priv;
3584 int ret = 0, i;
3585 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
3586
3587 if (CHIP_NUM(cp) != CHIP_NUM_5709)
3588 return 0;
3589
3590 for (i = 0; i < cp->ctx_blks; i++) {
3591 int j;
3592 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
3593 u32 val;
3594
3595 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
3596
3597 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
3598 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
3599 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
3600 (u64) cp->ctx_arr[i].mapping >> 32);
3601 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
3602 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
3603 for (j = 0; j < 10; j++) {
3604
3605 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
3606 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
3607 break;
3608 udelay(5);
3609 }
3610 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
3611 ret = -EBUSY;
3612 break;
3613 }
3614 }
3615 return ret;
3616}
3617
3618static void cnic_free_irq(struct cnic_dev *dev)
3619{
3620 struct cnic_local *cp = dev->cnic_priv;
3621 struct cnic_eth_dev *ethdev = cp->ethdev;
3622
3623 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3624 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00003625 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07003626 free_irq(ethdev->irq_arr[0].vector, dev);
3627 }
3628}
3629
Michael Chan6e0dc642010-10-13 14:06:44 +00003630static int cnic_request_irq(struct cnic_dev *dev)
3631{
3632 struct cnic_local *cp = dev->cnic_priv;
3633 struct cnic_eth_dev *ethdev = cp->ethdev;
3634 int err;
3635
3636 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
3637 if (err)
3638 tasklet_disable(&cp->cnic_irq_task);
3639
3640 return err;
3641}
3642
Michael Chana4636962009-06-08 18:14:43 -07003643static int cnic_init_bnx2_irq(struct cnic_dev *dev)
3644{
3645 struct cnic_local *cp = dev->cnic_priv;
3646 struct cnic_eth_dev *ethdev = cp->ethdev;
3647
3648 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3649 int err, i = 0;
3650 int sblk_num = cp->status_blk_num;
3651 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
3652 BNX2_HC_SB_CONFIG_1;
3653
3654 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
3655
3656 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
3657 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
3658 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
3659
Michael Chana4dde3a2010-02-24 14:42:08 +00003660 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00003661 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07003662 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00003663 err = cnic_request_irq(dev);
3664 if (err)
Michael Chana4636962009-06-08 18:14:43 -07003665 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00003666
Michael Chana4dde3a2010-02-24 14:42:08 +00003667 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07003668 i < 10) {
3669 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
3670 1 << (11 + sblk_num));
3671 udelay(10);
3672 i++;
3673 barrier();
3674 }
Michael Chana4dde3a2010-02-24 14:42:08 +00003675 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07003676 cnic_free_irq(dev);
3677 goto failed;
3678 }
3679
3680 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00003681 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07003682 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
3683 int i = 0;
3684
3685 while (sblk->status_completion_producer_index && i < 10) {
3686 CNIC_WR(dev, BNX2_HC_COMMAND,
3687 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
3688 udelay(10);
3689 i++;
3690 barrier();
3691 }
3692 if (sblk->status_completion_producer_index)
3693 goto failed;
3694
3695 }
3696 return 0;
3697
3698failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00003699 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07003700 return -EBUSY;
3701}
3702
3703static void cnic_enable_bnx2_int(struct cnic_dev *dev)
3704{
3705 struct cnic_local *cp = dev->cnic_priv;
3706 struct cnic_eth_dev *ethdev = cp->ethdev;
3707
3708 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3709 return;
3710
3711 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
3712 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
3713}
3714
3715static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
3716{
3717 struct cnic_local *cp = dev->cnic_priv;
3718 struct cnic_eth_dev *ethdev = cp->ethdev;
3719
3720 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3721 return;
3722
3723 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
3724 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
3725 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
3726 synchronize_irq(ethdev->irq_arr[0].vector);
3727}
3728
3729static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
3730{
3731 struct cnic_local *cp = dev->cnic_priv;
3732 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00003733 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07003734 u32 cid_addr, tx_cid, sb_id;
3735 u32 val, offset0, offset1, offset2, offset3;
3736 int i;
3737 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00003738 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00003739 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07003740
3741 sb_id = cp->status_blk_num;
3742 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07003743 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
3744 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00003745 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07003746
3747 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07003748 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
3749 (TX_TSS_CID << 7));
3750 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
3751 }
3752 cp->tx_cons = *cp->tx_cons_ptr;
3753
3754 cid_addr = GET_CID_ADDR(tx_cid);
3755 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
3756 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
3757
3758 for (i = 0; i < PHY_CTX_SIZE; i += 4)
3759 cnic_ctx_wr(dev, cid_addr2, i, 0);
3760
3761 offset0 = BNX2_L2CTX_TYPE_XI;
3762 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
3763 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
3764 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
3765 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07003766 cnic_init_context(dev, tx_cid);
3767 cnic_init_context(dev, tx_cid + 1);
3768
Michael Chana4636962009-06-08 18:14:43 -07003769 offset0 = BNX2_L2CTX_TYPE;
3770 offset1 = BNX2_L2CTX_CMD_TYPE;
3771 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
3772 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
3773 }
3774 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
3775 cnic_ctx_wr(dev, cid_addr, offset0, val);
3776
3777 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
3778 cnic_ctx_wr(dev, cid_addr, offset1, val);
3779
Michael Chancd801532010-10-13 14:06:49 +00003780 txbd = (struct tx_bd *) udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07003781
Michael Chancd801532010-10-13 14:06:49 +00003782 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07003783 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
3784 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
3785 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
3786 }
Michael Chancd801532010-10-13 14:06:49 +00003787 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07003788 cnic_ctx_wr(dev, cid_addr, offset2, val);
3789 txbd->tx_bd_haddr_hi = val;
3790
Michael Chancd801532010-10-13 14:06:49 +00003791 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07003792 cnic_ctx_wr(dev, cid_addr, offset3, val);
3793 txbd->tx_bd_haddr_lo = val;
3794}
3795
3796static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
3797{
3798 struct cnic_local *cp = dev->cnic_priv;
3799 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00003800 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07003801 u32 cid_addr, sb_id, val, coal_reg, coal_val;
3802 int i;
3803 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00003804 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00003805 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07003806
3807 sb_id = cp->status_blk_num;
3808 cnic_init_context(dev, 2);
3809 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
3810 coal_reg = BNX2_HC_COMMAND;
3811 coal_val = CNIC_RD(dev, coal_reg);
3812 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00003813 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07003814
3815 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
3816 coal_reg = BNX2_HC_COALESCE_NOW;
3817 coal_val = 1 << (11 + sb_id);
3818 }
3819 i = 0;
3820 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
3821 CNIC_WR(dev, coal_reg, coal_val);
3822 udelay(10);
3823 i++;
3824 barrier();
3825 }
3826 cp->rx_cons = *cp->rx_cons_ptr;
3827
3828 cid_addr = GET_CID_ADDR(2);
3829 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
3830 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
3831 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
3832
3833 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07003834 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07003835 else
Michael Chand0549382009-10-28 03:41:59 -07003836 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07003837 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
3838
Michael Chancd801532010-10-13 14:06:49 +00003839 rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
Michael Chana4636962009-06-08 18:14:43 -07003840 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
3841 dma_addr_t buf_map;
3842 int n = (i % cp->l2_rx_ring_size) + 1;
3843
Michael Chancd801532010-10-13 14:06:49 +00003844 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07003845 rxbd->rx_bd_len = cp->l2_single_buf_size;
3846 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
3847 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
3848 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
3849 }
Michael Chancd801532010-10-13 14:06:49 +00003850 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07003851 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
3852 rxbd->rx_bd_haddr_hi = val;
3853
Michael Chancd801532010-10-13 14:06:49 +00003854 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07003855 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
3856 rxbd->rx_bd_haddr_lo = val;
3857
3858 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
3859 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
3860}
3861
3862static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
3863{
3864 struct kwqe *wqes[1], l2kwqe;
3865
3866 memset(&l2kwqe, 0, sizeof(l2kwqe));
3867 wqes[0] = &l2kwqe;
3868 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
3869 (L2_KWQE_OPCODE_VALUE_FLUSH <<
3870 KWQE_OPCODE_SHIFT) | 2;
3871 dev->submit_kwqes(dev, wqes, 1);
3872}
3873
3874static void cnic_set_bnx2_mac(struct cnic_dev *dev)
3875{
3876 struct cnic_local *cp = dev->cnic_priv;
3877 u32 val;
3878
3879 val = cp->func << 2;
3880
3881 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
3882
3883 val = cnic_reg_rd_ind(dev, cp->shmem_base +
3884 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
3885 dev->mac_addr[0] = (u8) (val >> 8);
3886 dev->mac_addr[1] = (u8) val;
3887
3888 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
3889
3890 val = cnic_reg_rd_ind(dev, cp->shmem_base +
3891 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
3892 dev->mac_addr[2] = (u8) (val >> 24);
3893 dev->mac_addr[3] = (u8) (val >> 16);
3894 dev->mac_addr[4] = (u8) (val >> 8);
3895 dev->mac_addr[5] = (u8) val;
3896
3897 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
3898
3899 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
3900 if (CHIP_NUM(cp) != CHIP_NUM_5709)
3901 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
3902
3903 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
3904 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
3905 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
3906}
3907
3908static int cnic_start_bnx2_hw(struct cnic_dev *dev)
3909{
3910 struct cnic_local *cp = dev->cnic_priv;
3911 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00003912 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00003913 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07003914 int err;
3915
3916 cnic_set_bnx2_mac(dev);
3917
3918 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
3919 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
3920 if (BCM_PAGE_BITS > 12)
3921 val |= (12 - 8) << 4;
3922 else
3923 val |= (BCM_PAGE_BITS - 8) << 4;
3924
3925 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
3926
3927 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
3928 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
3929 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
3930
3931 err = cnic_setup_5709_context(dev, 1);
3932 if (err)
3933 return err;
3934
3935 cnic_init_context(dev, KWQ_CID);
3936 cnic_init_context(dev, KCQ_CID);
3937
Michael Chane6c28892010-06-24 14:58:39 +00003938 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07003939 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
3940
3941 cp->max_kwq_idx = MAX_KWQ_IDX;
3942 cp->kwq_prod_idx = 0;
3943 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00003944 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07003945
3946 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
3947 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
3948 else
3949 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
3950
3951 /* Initialize the kernel work queue context. */
3952 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
3953 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00003954 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07003955
3956 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00003957 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07003958
3959 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00003960 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07003961
3962 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00003963 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07003964
3965 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00003966 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07003967
Michael Chane6c28892010-06-24 14:58:39 +00003968 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
3969 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07003970
Michael Chane6c28892010-06-24 14:58:39 +00003971 cp->kcq1.sw_prod_idx = 0;
3972 cp->kcq1.hw_prod_idx_ptr =
3973 (u16 *) &sblk->status_completion_producer_index;
3974
3975 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07003976
3977 /* Initialize the kernel complete queue context. */
3978 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
3979 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00003980 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07003981
3982 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00003983 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07003984
3985 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00003986 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07003987
Michael Chane6c28892010-06-24 14:58:39 +00003988 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
3989 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07003990
Michael Chane6c28892010-06-24 14:58:39 +00003991 val = (u32) cp->kcq1.dma.pgtbl_map;
3992 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07003993
3994 cp->int_num = 0;
3995 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00003996 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07003997 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07003998 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07003999
Michael Chane6c28892010-06-24 14:58:39 +00004000 cp->kcq1.hw_prod_idx_ptr =
4001 (u16 *) &msblk->status_completion_producer_index;
4002 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004003 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004004 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004005 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4006 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004007 }
4008
4009 /* Enable Commnad Scheduler notification when we write to the
4010 * host producer index of the kernel contexts. */
4011 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4012
4013 /* Enable Command Scheduler notification when we write to either
4014 * the Send Queue or Receive Queue producer indexes of the kernel
4015 * bypass contexts. */
4016 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4017 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4018
4019 /* Notify COM when the driver post an application buffer. */
4020 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4021
4022 /* Set the CP and COM doorbells. These two processors polls the
4023 * doorbell for a non zero value before running. This must be done
4024 * after setting up the kernel queue contexts. */
4025 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4026 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4027
4028 cnic_init_bnx2_tx_ring(dev);
4029 cnic_init_bnx2_rx_ring(dev);
4030
4031 err = cnic_init_bnx2_irq(dev);
4032 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004033 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004034 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4035 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4036 return err;
4037 }
4038
4039 return 0;
4040}
4041
Michael Chan71034ba2009-10-10 13:46:59 +00004042static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4043{
4044 struct cnic_local *cp = dev->cnic_priv;
4045 struct cnic_eth_dev *ethdev = cp->ethdev;
4046 u32 start_offset = ethdev->ctx_tbl_offset;
4047 int i;
4048
4049 for (i = 0; i < cp->ctx_blks; i++) {
4050 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4051 dma_addr_t map = ctx->mapping;
4052
4053 if (cp->ctx_align) {
4054 unsigned long mask = cp->ctx_align - 1;
4055
4056 map = (map + mask) & ~mask;
4057 }
4058
4059 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4060 }
4061}
4062
4063static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4064{
4065 struct cnic_local *cp = dev->cnic_priv;
4066 struct cnic_eth_dev *ethdev = cp->ethdev;
4067 int err = 0;
4068
Joe Perches164165d2009-11-19 09:30:10 +00004069 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004070 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004071 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4072 err = cnic_request_irq(dev);
4073
Michael Chan71034ba2009-10-10 13:46:59 +00004074 return err;
4075}
4076
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004077static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4078 u16 sb_id, u8 sb_index,
4079 u8 disable)
4080{
4081
4082 u32 addr = BAR_CSTRORM_INTMEM +
4083 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4084 offsetof(struct hc_status_block_data_e1x, index_data) +
4085 sizeof(struct hc_index_data)*sb_index +
4086 offsetof(struct hc_index_data, flags);
4087 u16 flags = CNIC_RD16(dev, addr);
4088 /* clear and set */
4089 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4090 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4091 HC_INDEX_DATA_HC_ENABLED);
4092 CNIC_WR16(dev, addr, flags);
4093}
4094
Michael Chan71034ba2009-10-10 13:46:59 +00004095static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4096{
4097 struct cnic_local *cp = dev->cnic_priv;
4098 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004099
4100 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004101 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4102 offsetof(struct hc_status_block_data_e1x, index_data) +
4103 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
4104 offsetof(struct hc_index_data, timeout), 64 / 12);
4105 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004106}
4107
4108static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4109{
4110}
4111
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004112static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4113 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004114{
4115 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004116 struct cnic_uio_dev *udev = cp->udev;
4117 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4118 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004119 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004120 int port = CNIC_PORT(cp);
4121 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004122 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004123 u32 val;
4124
4125 memset(txbd, 0, BCM_PAGE_SIZE);
4126
Michael Chancd801532010-10-13 14:06:49 +00004127 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004128 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4129 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4130 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4131
4132 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4133 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4134 reg_bd->addr_hi = start_bd->addr_hi;
4135 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4136 start_bd->nbytes = cpu_to_le16(0x10);
4137 start_bd->nbd = cpu_to_le16(3);
4138 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4139 start_bd->general_data = (UNICAST_ADDRESS <<
4140 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4141 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4142
4143 }
Michael Chan71034ba2009-10-10 13:46:59 +00004144
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004145 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004146 txbd->next_bd.addr_hi = cpu_to_le32(val);
4147
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004148 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004149
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004150 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004151 txbd->next_bd.addr_lo = cpu_to_le32(val);
4152
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004153 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004154
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004155 /* Other ramrod params */
4156 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4157 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004158
4159 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004160 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004161 val = BAR_XSTRORM_INTMEM +
4162 XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4163 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
4164 CNIC_WR(dev, val + i * 4, 0);
4165 }
Michael Chan71034ba2009-10-10 13:46:59 +00004166
4167 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004168 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004169}
4170
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004171static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4172 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004173{
4174 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004175 struct cnic_uio_dev *udev = cp->udev;
4176 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004177 BCM_PAGE_SIZE);
4178 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004179 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004180 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004181 int i;
4182 int port = CNIC_PORT(cp);
Michael Chan5159fdc2010-12-23 07:42:59 +00004183 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004184 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004185 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004186 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004187
4188 /* General data */
4189 data->general.client_id = cli;
4190 data->general.statistics_en_flg = 1;
4191 data->general.statistics_counter_id = cli;
4192 data->general.activate_flg = 1;
4193 data->general.sp_client_id = cli;
Michael Chan71034ba2009-10-10 13:46:59 +00004194
4195 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4196 dma_addr_t buf_map;
4197 int n = (i % cp->l2_rx_ring_size) + 1;
4198
Michael Chancd801532010-10-13 14:06:49 +00004199 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004200 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4201 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4202 }
Michael Chan71034ba2009-10-10 13:46:59 +00004203
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004204 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004205 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004206 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004207
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004208 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004209 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004210 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004211
4212 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004213 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004214 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004215 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004216
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004217 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004218 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004219 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004220
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004221 /* Other ramrod params */
4222 data->rx.client_qzone_id = cl_qzone_id;
4223 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4224 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004225
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004226 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
4227 data->rx.bd_buff_size = cpu_to_le16(cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004228
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004229 data->rx.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4230 data->rx.outer_vlan_removal_enable_flg = 1;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004231
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004232 /* reset tstorm and ustorm per client statistics */
4233 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004234 val = BAR_TSTRORM_INTMEM +
4235 TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4236 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4237 CNIC_WR(dev, val + i * 4, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004238
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004239 val = BAR_USTRORM_INTMEM +
4240 USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4241 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4242 CNIC_WR(dev, val + i * 4, 0);
4243 }
Michael Chan71034ba2009-10-10 13:46:59 +00004244
4245 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004246 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004247 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004248}
4249
Michael Chan4aacb7a2010-12-23 07:43:01 +00004250static int cnic_read_bnx2x_iscsi_mac(struct cnic_dev *dev, u32 upper_addr,
4251 u32 lower_addr)
4252{
4253 u32 val;
4254 u8 mac[6];
4255
4256 val = CNIC_RD(dev, upper_addr);
4257
4258 mac[0] = (u8) (val >> 8);
4259 mac[1] = (u8) val;
4260
4261 val = CNIC_RD(dev, lower_addr);
4262
4263 mac[2] = (u8) (val >> 24);
4264 mac[3] = (u8) (val >> 16);
4265 mac[4] = (u8) (val >> 8);
4266 mac[5] = (u8) val;
4267
4268 if (is_valid_ether_addr(mac)) {
4269 memcpy(dev->mac_addr, mac, 6);
4270 return 0;
4271 } else {
4272 return -EINVAL;
4273 }
4274}
4275
Michael Chan71034ba2009-10-10 13:46:59 +00004276static void cnic_get_bnx2x_iscsi_info(struct cnic_dev *dev)
4277{
4278 struct cnic_local *cp = dev->cnic_priv;
Michael Chan4aacb7a2010-12-23 07:43:01 +00004279 u32 base, base2, addr, addr1, val;
Michael Chan71034ba2009-10-10 13:46:59 +00004280 int port = CNIC_PORT(cp);
4281
4282 dev->max_iscsi_conn = 0;
4283 base = CNIC_RD(dev, MISC_REG_SHARED_MEM_ADDR);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004284 if (base == 0)
Michael Chan71034ba2009-10-10 13:46:59 +00004285 return;
4286
Michael Chanee87a822010-10-13 14:06:51 +00004287 base2 = CNIC_RD(dev, (CNIC_PATH(cp) ? MISC_REG_GENERIC_CR_1 :
4288 MISC_REG_GENERIC_CR_0));
Michael Chandd2e4db2009-12-02 15:15:37 +00004289 addr = BNX2X_SHMEM_ADDR(base,
Michael Chan71034ba2009-10-10 13:46:59 +00004290 dev_info.port_hw_config[port].iscsi_mac_upper);
4291
Michael Chan4aacb7a2010-12-23 07:43:01 +00004292 addr1 = BNX2X_SHMEM_ADDR(base,
Michael Chan71034ba2009-10-10 13:46:59 +00004293 dev_info.port_hw_config[port].iscsi_mac_lower);
4294
Michael Chan4aacb7a2010-12-23 07:43:01 +00004295 cnic_read_bnx2x_iscsi_mac(dev, addr, addr1);
Michael Chan71034ba2009-10-10 13:46:59 +00004296
4297 addr = BNX2X_SHMEM_ADDR(base, validity_map[port]);
4298 val = CNIC_RD(dev, addr);
4299
4300 if (!(val & SHR_MEM_VALIDITY_LIC_NO_KEY_IN_EFFECT)) {
4301 u16 val16;
4302
4303 addr = BNX2X_SHMEM_ADDR(base,
4304 drv_lic_key[port].max_iscsi_init_conn);
4305 val16 = CNIC_RD16(dev, addr);
4306
4307 if (val16)
4308 val16 ^= 0x1e1e;
4309 dev->max_iscsi_conn = val16;
4310 }
Michael Chanee87a822010-10-13 14:06:51 +00004311 if (BNX2X_CHIP_IS_E1H(cp->chip_id) || BNX2X_CHIP_IS_E2(cp->chip_id)) {
Michael Chan71034ba2009-10-10 13:46:59 +00004312 int func = CNIC_FUNC(cp);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004313 u32 mf_cfg_addr;
Michael Chan71034ba2009-10-10 13:46:59 +00004314
Michael Chanee87a822010-10-13 14:06:51 +00004315 if (BNX2X_SHMEM2_HAS(base2, mf_cfg_addr))
4316 mf_cfg_addr = CNIC_RD(dev, BNX2X_SHMEM2_ADDR(base2,
4317 mf_cfg_addr));
4318 else
4319 mf_cfg_addr = base + BNX2X_SHMEM_MF_BLK_OFFSET;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004320
Michael Chan4aacb7a2010-12-23 07:43:01 +00004321 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4322 /* Must determine if the MF is SD vs SI mode */
4323 addr = BNX2X_SHMEM_ADDR(base,
4324 dev_info.shared_feature_config.config);
4325 val = CNIC_RD(dev, addr);
4326 if ((val & SHARED_FEAT_CFG_FORCE_SF_MODE_MASK) ==
4327 SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT) {
4328 int rc;
4329
4330 /* MULTI_FUNCTION_SI mode */
4331 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4332 func_ext_config[func].func_cfg);
4333 val = CNIC_RD(dev, addr);
4334 if (!(val & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD))
4335 dev->max_iscsi_conn = 0;
4336
4337 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4338 func_ext_config[func].
4339 iscsi_mac_addr_upper);
4340 addr1 = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4341 func_ext_config[func].
4342 iscsi_mac_addr_lower);
4343 rc = cnic_read_bnx2x_iscsi_mac(dev, addr,
4344 addr1);
4345 if (rc && func > 1)
4346 dev->max_iscsi_conn = 0;
4347
4348 return;
4349 }
4350 }
4351
4352 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4353 func_mf_config[func].e1hov_tag);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004354
Michael Chan71034ba2009-10-10 13:46:59 +00004355 val = CNIC_RD(dev, addr);
4356 val &= FUNC_MF_CFG_E1HOV_TAG_MASK;
4357 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
Michael Chan4aacb7a2010-12-23 07:43:01 +00004358 addr = BNX2X_MF_CFG_ADDR(mf_cfg_addr,
4359 func_mf_config[func].config);
Michael Chan71034ba2009-10-10 13:46:59 +00004360 val = CNIC_RD(dev, addr);
4361 val &= FUNC_MF_CFG_PROTOCOL_MASK;
4362 if (val != FUNC_MF_CFG_PROTOCOL_ISCSI)
4363 dev->max_iscsi_conn = 0;
4364 }
4365 }
Michael Chan4aacb7a2010-12-23 07:43:01 +00004366 if (!is_valid_ether_addr(dev->mac_addr))
4367 dev->max_iscsi_conn = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00004368}
4369
4370static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4371{
4372 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004373 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chan71034ba2009-10-10 13:46:59 +00004374 int func = CNIC_FUNC(cp), ret, i;
Michael Chan14203982010-10-06 03:16:06 +00004375 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004376
Michael Chanee87a822010-10-13 14:06:51 +00004377 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4378 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4379
4380 if (!(val & 1))
4381 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4382 else
4383 val = (val >> 1) & 1;
4384
4385 if (val)
4386 cp->pfid = func >> 1;
4387 else
4388 cp->pfid = func & 0x6;
4389 } else {
4390 cp->pfid = func;
4391 }
Michael Chan14203982010-10-06 03:16:06 +00004392 pfid = cp->pfid;
4393
Michael Chan71034ba2009-10-10 13:46:59 +00004394 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Michael Chan520efdf2010-06-24 14:58:37 +00004395 cp->iscsi_start_cid);
Michael Chan71034ba2009-10-10 13:46:59 +00004396
4397 if (ret)
4398 return -ENOMEM;
4399
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004400 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
4401
Michael Chane6c28892010-06-24 14:58:39 +00004402 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004403 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
Michael Chane6c28892010-06-24 14:58:39 +00004404 cp->kcq1.sw_prod_idx = 0;
4405
Michael Chanee87a822010-10-13 14:06:51 +00004406 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4407 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4408
4409 cp->kcq1.hw_prod_idx_ptr =
4410 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4411 cp->kcq1.status_idx_ptr =
4412 &sb->sb.running_index[SM_RX_ID];
4413 } else {
4414 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4415
4416 cp->kcq1.hw_prod_idx_ptr =
4417 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4418 cp->kcq1.status_idx_ptr =
4419 &sb->sb.running_index[SM_RX_ID];
4420 }
Michael Chan71034ba2009-10-10 13:46:59 +00004421
4422 cnic_get_bnx2x_iscsi_info(dev);
4423
4424 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00004425 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00004426 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004427 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004428 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004429 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004430 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004431 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004432 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004433 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004434 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004435 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004436 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004437 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004438 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004439 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004440 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004441 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004442 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004443 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00004444 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004445 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004446 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00004447
4448 for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4449 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004450 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
Michael Chan71034ba2009-10-10 13:46:59 +00004451 cp->conn_buf_info.pgtbl[2 * i]);
4452 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004453 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004454 cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4455 }
4456
4457 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004458 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004459 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4460 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004461 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004462 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4463
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004464 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4465 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
4466
Michael Chan71034ba2009-10-10 13:46:59 +00004467 cnic_setup_bnx2x_context(dev);
4468
Michael Chan71034ba2009-10-10 13:46:59 +00004469 ret = cnic_init_bnx2x_irq(dev);
4470 if (ret)
4471 return ret;
4472
Michael Chan71034ba2009-10-10 13:46:59 +00004473 return 0;
4474}
4475
Michael Chan86b53602009-10-10 13:46:57 +00004476static void cnic_init_rings(struct cnic_dev *dev)
4477{
Michael Chan541a7812010-10-06 03:17:22 +00004478 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004479 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00004480
4481 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4482 return;
4483
Michael Chan86b53602009-10-10 13:46:57 +00004484 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4485 cnic_init_bnx2_tx_ring(dev);
4486 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00004487 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00004488 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00004489 u32 cli = cp->ethdev->iscsi_l2_client_id;
4490 u32 cid = cp->ethdev->iscsi_l2_cid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004491 u32 cl_qzone_id, type;
4492 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00004493 union l5cm_specific_data l5_data;
4494 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chanc7596b72009-12-02 15:15:35 +00004495 u32 off, i;
Michael Chan71034ba2009-10-10 13:46:59 +00004496
4497 rx_prods.bd_prod = 0;
4498 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4499 barrier();
4500
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004501 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4502
Michael Chanc7596b72009-12-02 15:15:35 +00004503 off = BAR_USTRORM_INTMEM +
Michael Chanee87a822010-10-13 14:06:51 +00004504 (BNX2X_CHIP_IS_E2(cp->chip_id) ?
4505 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
4506 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00004507
4508 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00004509 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00004510
Michael Chan48f753d2010-05-18 11:32:53 +00004511 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4512
Michael Chancd801532010-10-13 14:06:49 +00004513 data = udev->l2_buf;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004514
4515 memset(data, 0, sizeof(*data));
4516
4517 cnic_init_bnx2x_tx_ring(dev, data);
4518 cnic_init_bnx2x_rx_ring(dev, data);
4519
Michael Chancd801532010-10-13 14:06:49 +00004520 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
4521 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004522
4523 type = (ETH_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
4524 & SPE_HDR_CONN_TYPE;
4525 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
4526 SPE_HDR_FUNCTION_ID);
Michael Chan71034ba2009-10-10 13:46:59 +00004527
Michael Chan541a7812010-10-06 03:17:22 +00004528 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4529
Michael Chan71034ba2009-10-10 13:46:59 +00004530 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan5159fdc2010-12-23 07:42:59 +00004531 cid, type, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004532
Michael Chan48f753d2010-05-18 11:32:53 +00004533 i = 0;
4534 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4535 ++i < 10)
4536 msleep(1);
4537
4538 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4539 netdev_err(dev->netdev,
4540 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004541 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00004542 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chan86b53602009-10-10 13:46:57 +00004543 }
4544}
4545
4546static void cnic_shutdown_rings(struct cnic_dev *dev)
4547{
Michael Chan541a7812010-10-06 03:17:22 +00004548 struct cnic_local *cp = dev->cnic_priv;
4549
4550 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4551 return;
4552
Michael Chan86b53602009-10-10 13:46:57 +00004553 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4554 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004555 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4556 struct cnic_local *cp = dev->cnic_priv;
Michael Chan5159fdc2010-12-23 07:42:59 +00004557 u32 cli = cp->ethdev->iscsi_l2_client_id;
4558 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00004559 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00004560 int i;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004561 u32 type;
Michael Chan71034ba2009-10-10 13:46:59 +00004562
Michael Chan5159fdc2010-12-23 07:42:59 +00004563 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00004564
Michael Chan48f753d2010-05-18 11:32:53 +00004565 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4566
Michael Chan8b065b62009-12-02 15:15:36 +00004567 l5_data.phy_address.lo = cli;
4568 l5_data.phy_address.hi = 0;
4569 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00004570 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00004571 i = 0;
4572 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4573 ++i < 10)
4574 msleep(1);
4575
4576 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4577 netdev_err(dev->netdev,
4578 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004579 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00004580
4581 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004582 type = (NONE_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
4583 & SPE_HDR_CONN_TYPE;
4584 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
4585 SPE_HDR_FUNCTION_ID);
4586 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan5159fdc2010-12-23 07:42:59 +00004587 cid, type, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00004588 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00004589 }
Michael Chan541a7812010-10-06 03:17:22 +00004590 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan86b53602009-10-10 13:46:57 +00004591}
4592
Michael Chana3059b12009-08-14 15:49:44 +00004593static int cnic_register_netdev(struct cnic_dev *dev)
4594{
4595 struct cnic_local *cp = dev->cnic_priv;
4596 struct cnic_eth_dev *ethdev = cp->ethdev;
4597 int err;
4598
4599 if (!ethdev)
4600 return -ENODEV;
4601
4602 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
4603 return 0;
4604
4605 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
4606 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00004607 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00004608
4609 return err;
4610}
4611
4612static void cnic_unregister_netdev(struct cnic_dev *dev)
4613{
4614 struct cnic_local *cp = dev->cnic_priv;
4615 struct cnic_eth_dev *ethdev = cp->ethdev;
4616
4617 if (!ethdev)
4618 return;
4619
4620 ethdev->drv_unregister_cnic(dev->netdev);
4621}
4622
Michael Chana4636962009-06-08 18:14:43 -07004623static int cnic_start_hw(struct cnic_dev *dev)
4624{
4625 struct cnic_local *cp = dev->cnic_priv;
4626 struct cnic_eth_dev *ethdev = cp->ethdev;
4627 int err;
4628
4629 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
4630 return -EALREADY;
4631
Michael Chana4636962009-06-08 18:14:43 -07004632 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07004633 pci_dev_get(dev->pcidev);
4634 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00004635 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07004636 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
4637
4638 err = cp->alloc_resc(dev);
4639 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004640 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07004641 goto err1;
4642 }
4643
4644 err = cp->start_hw(dev);
4645 if (err)
4646 goto err1;
4647
4648 err = cnic_cm_open(dev);
4649 if (err)
4650 goto err1;
4651
4652 set_bit(CNIC_F_CNIC_UP, &dev->flags);
4653
4654 cp->enable_int(dev);
4655
4656 return 0;
4657
4658err1:
Michael Chana4636962009-06-08 18:14:43 -07004659 cp->free_resc(dev);
4660 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07004661 return err;
4662}
4663
4664static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
4665{
Michael Chana4636962009-06-08 18:14:43 -07004666 cnic_disable_bnx2_int_sync(dev);
4667
4668 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4669 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4670
4671 cnic_init_context(dev, KWQ_CID);
4672 cnic_init_context(dev, KCQ_CID);
4673
4674 cnic_setup_5709_context(dev, 0);
4675 cnic_free_irq(dev);
4676
Michael Chana4636962009-06-08 18:14:43 -07004677 cnic_free_resc(dev);
4678}
4679
Michael Chan71034ba2009-10-10 13:46:59 +00004680
4681static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
4682{
4683 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00004684
4685 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004686 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00004687 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004688 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00004689 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004690 cnic_free_resc(dev);
4691}
4692
Michael Chana4636962009-06-08 18:14:43 -07004693static void cnic_stop_hw(struct cnic_dev *dev)
4694{
4695 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
4696 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00004697 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07004698
Michael Chan48f753d2010-05-18 11:32:53 +00004699 /* Need to wait for the ring shutdown event to complete
4700 * before clearing the CNIC_UP flag.
4701 */
Michael Chancd801532010-10-13 14:06:49 +00004702 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00004703 msleep(100);
4704 i++;
4705 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00004706 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07004707 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
4708 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
4709 synchronize_rcu();
4710 cnic_cm_shutdown(dev);
4711 cp->stop_hw(dev);
4712 pci_dev_put(dev->pcidev);
4713 }
4714}
4715
4716static void cnic_free_dev(struct cnic_dev *dev)
4717{
4718 int i = 0;
4719
4720 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
4721 msleep(100);
4722 i++;
4723 }
4724 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00004725 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07004726
Joe Perchesddf79b22010-02-17 15:01:54 +00004727 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07004728 dev_put(dev->netdev);
4729 kfree(dev);
4730}
4731
4732static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
4733 struct pci_dev *pdev)
4734{
4735 struct cnic_dev *cdev;
4736 struct cnic_local *cp;
4737 int alloc_size;
4738
4739 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
4740
4741 cdev = kzalloc(alloc_size , GFP_KERNEL);
4742 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004743 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07004744 return NULL;
4745 }
4746
4747 cdev->netdev = dev;
4748 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
4749 cdev->register_device = cnic_register_device;
4750 cdev->unregister_device = cnic_unregister_device;
4751 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
4752
4753 cp = cdev->cnic_priv;
4754 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07004755 cp->l2_single_buf_size = 0x400;
4756 cp->l2_rx_ring_size = 3;
4757
4758 spin_lock_init(&cp->cnic_ulp_lock);
4759
Joe Perchesddf79b22010-02-17 15:01:54 +00004760 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07004761
4762 return cdev;
4763}
4764
4765static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
4766{
4767 struct pci_dev *pdev;
4768 struct cnic_dev *cdev;
4769 struct cnic_local *cp;
4770 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07004771 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07004772
Michael Chane2ee3612009-06-13 17:43:02 -07004773 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07004774 if (probe) {
4775 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00004776 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07004777 }
4778 if (!ethdev)
4779 return NULL;
4780
4781 pdev = ethdev->pdev;
4782 if (!pdev)
4783 return NULL;
4784
4785 dev_hold(dev);
4786 pci_dev_get(pdev);
4787 if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
4788 pdev->device == PCI_DEVICE_ID_NX2_5709S) {
4789 u8 rev;
4790
4791 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
4792 if (rev < 0x10) {
4793 pci_dev_put(pdev);
4794 goto cnic_err;
4795 }
4796 }
4797 pci_dev_put(pdev);
4798
4799 cdev = cnic_alloc_dev(dev, pdev);
4800 if (cdev == NULL)
4801 goto cnic_err;
4802
4803 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
4804 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
4805
4806 cp = cdev->cnic_priv;
4807 cp->ethdev = ethdev;
4808 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00004809 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07004810
4811 cp->cnic_ops = &cnic_bnx2_ops;
4812 cp->start_hw = cnic_start_bnx2_hw;
4813 cp->stop_hw = cnic_stop_bnx2_hw;
4814 cp->setup_pgtbl = cnic_setup_page_tbl;
4815 cp->alloc_resc = cnic_alloc_bnx2_resc;
4816 cp->free_resc = cnic_free_resc;
4817 cp->start_cm = cnic_cm_init_bnx2_hw;
4818 cp->stop_cm = cnic_cm_stop_bnx2_hw;
4819 cp->enable_int = cnic_enable_bnx2_int;
4820 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
4821 cp->close_conn = cnic_close_bnx2_conn;
4822 cp->next_idx = cnic_bnx2_next_idx;
4823 cp->hw_idx = cnic_bnx2_hw_idx;
4824 return cdev;
4825
4826cnic_err:
4827 dev_put(dev);
4828 return NULL;
4829}
4830
Michael Chan71034ba2009-10-10 13:46:59 +00004831static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
4832{
4833 struct pci_dev *pdev;
4834 struct cnic_dev *cdev;
4835 struct cnic_local *cp;
4836 struct cnic_eth_dev *ethdev = NULL;
4837 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
4838
4839 probe = symbol_get(bnx2x_cnic_probe);
4840 if (probe) {
4841 ethdev = (*probe)(dev);
4842 symbol_put(bnx2x_cnic_probe);
4843 }
4844 if (!ethdev)
4845 return NULL;
4846
4847 pdev = ethdev->pdev;
4848 if (!pdev)
4849 return NULL;
4850
4851 dev_hold(dev);
4852 cdev = cnic_alloc_dev(dev, pdev);
4853 if (cdev == NULL) {
4854 dev_put(dev);
4855 return NULL;
4856 }
4857
4858 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
4859 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
4860
4861 cp = cdev->cnic_priv;
4862 cp->ethdev = ethdev;
4863 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00004864 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004865
4866 cp->cnic_ops = &cnic_bnx2x_ops;
4867 cp->start_hw = cnic_start_bnx2x_hw;
4868 cp->stop_hw = cnic_stop_bnx2x_hw;
4869 cp->setup_pgtbl = cnic_setup_page_tbl_le;
4870 cp->alloc_resc = cnic_alloc_bnx2x_resc;
4871 cp->free_resc = cnic_free_resc;
4872 cp->start_cm = cnic_cm_init_bnx2x_hw;
4873 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
4874 cp->enable_int = cnic_enable_bnx2x_int;
4875 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Michael Chanee87a822010-10-13 14:06:51 +00004876 if (BNX2X_CHIP_IS_E2(cp->chip_id))
4877 cp->ack_int = cnic_ack_bnx2x_e2_msix;
4878 else
4879 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00004880 cp->close_conn = cnic_close_bnx2x_conn;
4881 cp->next_idx = cnic_bnx2x_next_idx;
4882 cp->hw_idx = cnic_bnx2x_hw_idx;
4883 return cdev;
4884}
4885
Michael Chana4636962009-06-08 18:14:43 -07004886static struct cnic_dev *is_cnic_dev(struct net_device *dev)
4887{
4888 struct ethtool_drvinfo drvinfo;
4889 struct cnic_dev *cdev = NULL;
4890
4891 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
4892 memset(&drvinfo, 0, sizeof(drvinfo));
4893 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
4894
4895 if (!strcmp(drvinfo.driver, "bnx2"))
4896 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004897 if (!strcmp(drvinfo.driver, "bnx2x"))
4898 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07004899 if (cdev) {
4900 write_lock(&cnic_dev_lock);
4901 list_add(&cdev->list, &cnic_dev_list);
4902 write_unlock(&cnic_dev_lock);
4903 }
4904 }
4905 return cdev;
4906}
4907
4908/**
4909 * netdev event handler
4910 */
4911static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
4912 void *ptr)
4913{
4914 struct net_device *netdev = ptr;
4915 struct cnic_dev *dev;
4916 int if_type;
4917 int new_dev = 0;
4918
4919 dev = cnic_from_netdev(netdev);
4920
4921 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
4922 /* Check for the hot-plug device */
4923 dev = is_cnic_dev(netdev);
4924 if (dev) {
4925 new_dev = 1;
4926 cnic_hold(dev);
4927 }
4928 }
4929 if (dev) {
4930 struct cnic_local *cp = dev->cnic_priv;
4931
4932 if (new_dev)
4933 cnic_ulp_init(dev);
4934 else if (event == NETDEV_UNREGISTER)
4935 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07004936
4937 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00004938 if (cnic_register_netdev(dev) != 0) {
4939 cnic_put(dev);
4940 goto done;
4941 }
Michael Chana4636962009-06-08 18:14:43 -07004942 if (!cnic_start_hw(dev))
4943 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07004944 }
4945
4946 rcu_read_lock();
4947 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
4948 struct cnic_ulp_ops *ulp_ops;
4949 void *ctx;
4950
4951 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
4952 if (!ulp_ops || !ulp_ops->indicate_netevent)
4953 continue;
4954
4955 ctx = cp->ulp_handle[if_type];
4956
4957 ulp_ops->indicate_netevent(ctx, event);
4958 }
4959 rcu_read_unlock();
4960
4961 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07004962 cnic_ulp_stop(dev);
4963 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00004964 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07004965 } else if (event == NETDEV_UNREGISTER) {
4966 write_lock(&cnic_dev_lock);
4967 list_del_init(&dev->list);
4968 write_unlock(&cnic_dev_lock);
4969
4970 cnic_put(dev);
4971 cnic_free_dev(dev);
4972 goto done;
4973 }
4974 cnic_put(dev);
4975 }
4976done:
4977 return NOTIFY_DONE;
4978}
4979
4980static struct notifier_block cnic_netdev_notifier = {
4981 .notifier_call = cnic_netdev_event
4982};
4983
4984static void cnic_release(void)
4985{
4986 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00004987 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07004988
4989 while (!list_empty(&cnic_dev_list)) {
4990 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
4991 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
4992 cnic_ulp_stop(dev);
4993 cnic_stop_hw(dev);
4994 }
4995
4996 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00004997 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07004998 list_del_init(&dev->list);
4999 cnic_free_dev(dev);
5000 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005001 while (!list_empty(&cnic_udev_list)) {
5002 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5003 list);
5004 cnic_free_uio(udev);
5005 }
Michael Chana4636962009-06-08 18:14:43 -07005006}
5007
5008static int __init cnic_init(void)
5009{
5010 int rc = 0;
5011
Joe Perchesddf79b22010-02-17 15:01:54 +00005012 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005013
5014 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5015 if (rc) {
5016 cnic_release();
5017 return rc;
5018 }
5019
Michael Chanfdf24082010-10-13 14:06:47 +00005020 cnic_wq = create_singlethread_workqueue("cnic_wq");
5021 if (!cnic_wq) {
5022 cnic_release();
5023 unregister_netdevice_notifier(&cnic_netdev_notifier);
5024 return -ENOMEM;
5025 }
5026
Michael Chana4636962009-06-08 18:14:43 -07005027 return 0;
5028}
5029
5030static void __exit cnic_exit(void)
5031{
5032 unregister_netdevice_notifier(&cnic_netdev_notifier);
5033 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005034 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005035}
5036
5037module_init(cnic_init);
5038module_exit(cnic_exit);