blob: 25f08880ae0f3bd17a55273f52b4e3eba0ac32c9 [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
Eric Dumazet13707f92011-01-26 19:28:23 +000068static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
69
70/* helper function, assuming cnic_lock is held */
71static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
72{
73 return rcu_dereference_protected(cnic_ulp_tbl[type],
74 lockdep_is_held(&cnic_lock));
75}
Michael Chana4636962009-06-08 18:14:43 -070076
77static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000078static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070079static int cnic_ctl(void *, struct cnic_ctl_info *);
80
81static struct cnic_ops cnic_bnx2_ops = {
82 .cnic_owner = THIS_MODULE,
83 .cnic_handler = cnic_service_bnx2,
84 .cnic_ctl = cnic_ctl,
85};
86
Michael Chan71034ba2009-10-10 13:46:59 +000087static struct cnic_ops cnic_bnx2x_ops = {
88 .cnic_owner = THIS_MODULE,
89 .cnic_handler = cnic_service_bnx2x,
90 .cnic_ctl = cnic_ctl,
91};
92
Michael Chanfdf24082010-10-13 14:06:47 +000093static struct workqueue_struct *cnic_wq;
94
Michael Chan86b53602009-10-10 13:46:57 +000095static void cnic_shutdown_rings(struct cnic_dev *);
96static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070097static int cnic_cm_set_pg(struct cnic_sock *);
98
99static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
100{
Michael Chancd801532010-10-13 14:06:49 +0000101 struct cnic_uio_dev *udev = uinfo->priv;
102 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -0700103
104 if (!capable(CAP_NET_ADMIN))
105 return -EPERM;
106
Michael Chancd801532010-10-13 14:06:49 +0000107 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700108 return -EBUSY;
109
Michael Chan86b53602009-10-10 13:46:57 +0000110 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000111 dev = udev->dev;
112
Michael Chana3ceeeb2010-10-13 14:06:50 +0000113 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000114 rtnl_unlock();
115 return -ENODEV;
116 }
117
Michael Chancd801532010-10-13 14:06:49 +0000118 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700119
Michael Chana3ceeeb2010-10-13 14:06:50 +0000120 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000121 cnic_init_rings(dev);
122 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700123
124 return 0;
125}
126
127static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
128{
Michael Chancd801532010-10-13 14:06:49 +0000129 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000130
Michael Chancd801532010-10-13 14:06:49 +0000131 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700132 return 0;
133}
134
135static inline void cnic_hold(struct cnic_dev *dev)
136{
137 atomic_inc(&dev->ref_count);
138}
139
140static inline void cnic_put(struct cnic_dev *dev)
141{
142 atomic_dec(&dev->ref_count);
143}
144
145static inline void csk_hold(struct cnic_sock *csk)
146{
147 atomic_inc(&csk->ref_count);
148}
149
150static inline void csk_put(struct cnic_sock *csk)
151{
152 atomic_dec(&csk->ref_count);
153}
154
155static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
156{
157 struct cnic_dev *cdev;
158
159 read_lock(&cnic_dev_lock);
160 list_for_each_entry(cdev, &cnic_dev_list, list) {
161 if (netdev == cdev->netdev) {
162 cnic_hold(cdev);
163 read_unlock(&cnic_dev_lock);
164 return cdev;
165 }
166 }
167 read_unlock(&cnic_dev_lock);
168 return NULL;
169}
170
Michael Chan7fc1ece2009-08-14 15:49:47 +0000171static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
172{
173 atomic_inc(&ulp_ops->ref_count);
174}
175
176static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
177{
178 atomic_dec(&ulp_ops->ref_count);
179}
180
Michael Chana4636962009-06-08 18:14:43 -0700181static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
182{
183 struct cnic_local *cp = dev->cnic_priv;
184 struct cnic_eth_dev *ethdev = cp->ethdev;
185 struct drv_ctl_info info;
186 struct drv_ctl_io *io = &info.data.io;
187
188 info.cmd = DRV_CTL_CTX_WR_CMD;
189 io->cid_addr = cid_addr;
190 io->offset = off;
191 io->data = val;
192 ethdev->drv_ctl(dev->netdev, &info);
193}
194
Michael Chan71034ba2009-10-10 13:46:59 +0000195static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
196{
197 struct cnic_local *cp = dev->cnic_priv;
198 struct cnic_eth_dev *ethdev = cp->ethdev;
199 struct drv_ctl_info info;
200 struct drv_ctl_io *io = &info.data.io;
201
202 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
203 io->offset = off;
204 io->dma_addr = addr;
205 ethdev->drv_ctl(dev->netdev, &info);
206}
207
208static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
209{
210 struct cnic_local *cp = dev->cnic_priv;
211 struct cnic_eth_dev *ethdev = cp->ethdev;
212 struct drv_ctl_info info;
213 struct drv_ctl_l2_ring *ring = &info.data.ring;
214
215 if (start)
216 info.cmd = DRV_CTL_START_L2_CMD;
217 else
218 info.cmd = DRV_CTL_STOP_L2_CMD;
219
220 ring->cid = cid;
221 ring->client_id = cl_id;
222 ethdev->drv_ctl(dev->netdev, &info);
223}
224
Michael Chana4636962009-06-08 18:14:43 -0700225static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
226{
227 struct cnic_local *cp = dev->cnic_priv;
228 struct cnic_eth_dev *ethdev = cp->ethdev;
229 struct drv_ctl_info info;
230 struct drv_ctl_io *io = &info.data.io;
231
232 info.cmd = DRV_CTL_IO_WR_CMD;
233 io->offset = off;
234 io->data = val;
235 ethdev->drv_ctl(dev->netdev, &info);
236}
237
238static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
239{
240 struct cnic_local *cp = dev->cnic_priv;
241 struct cnic_eth_dev *ethdev = cp->ethdev;
242 struct drv_ctl_info info;
243 struct drv_ctl_io *io = &info.data.io;
244
245 info.cmd = DRV_CTL_IO_RD_CMD;
246 io->offset = off;
247 ethdev->drv_ctl(dev->netdev, &info);
248 return io->data;
249}
250
251static int cnic_in_use(struct cnic_sock *csk)
252{
253 return test_bit(SK_F_INUSE, &csk->flags);
254}
255
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000256static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700257{
258 struct cnic_local *cp = dev->cnic_priv;
259 struct cnic_eth_dev *ethdev = cp->ethdev;
260 struct drv_ctl_info info;
261
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000262 info.cmd = cmd;
263 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700264 ethdev->drv_ctl(dev->netdev, &info);
265}
266
Michael Chan71034ba2009-10-10 13:46:59 +0000267static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
268{
269 u32 i;
270
Michael Chan520efdf2010-06-24 14:58:37 +0000271 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000272 if (cp->ctx_tbl[i].cid == cid) {
273 *l5_cid = i;
274 return 0;
275 }
276 }
277 return -EINVAL;
278}
279
Michael Chana4636962009-06-08 18:14:43 -0700280static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
281 struct cnic_sock *csk)
282{
283 struct iscsi_path path_req;
284 char *buf = NULL;
285 u16 len = 0;
286 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
287 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000288 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000289 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700290
Michael Chancd801532010-10-13 14:06:49 +0000291 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700292 return -ENODEV;
293
294 if (csk) {
295 len = sizeof(path_req);
296 buf = (char *) &path_req;
297 memset(&path_req, 0, len);
298
299 msg_type = ISCSI_KEVENT_PATH_REQ;
300 path_req.handle = (u64) csk->l5_cid;
301 if (test_bit(SK_F_IPV6, &csk->flags)) {
302 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
303 sizeof(struct in6_addr));
304 path_req.ip_addr_len = 16;
305 } else {
306 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
307 sizeof(struct in_addr));
308 path_req.ip_addr_len = 4;
309 }
310 path_req.vlan_id = csk->vlan_id;
311 path_req.pmtu = csk->mtu;
312 }
313
Michael Chan939b82e2010-12-23 07:42:58 +0000314 while (retry < 3) {
315 rc = 0;
316 rcu_read_lock();
317 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
318 if (ulp_ops)
319 rc = ulp_ops->iscsi_nl_send_msg(
320 cp->ulp_handle[CNIC_ULP_ISCSI],
321 msg_type, buf, len);
322 rcu_read_unlock();
323 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
324 break;
325
326 msleep(100);
327 retry++;
328 }
Michael Chana4636962009-06-08 18:14:43 -0700329 return 0;
330}
331
Eddie Wai42ecbb82010-12-23 07:43:02 +0000332static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
333
Michael Chana4636962009-06-08 18:14:43 -0700334static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
335 char *buf, u16 len)
336{
337 int rc = -EINVAL;
338
339 switch (msg_type) {
340 case ISCSI_UEVENT_PATH_UPDATE: {
341 struct cnic_local *cp;
342 u32 l5_cid;
343 struct cnic_sock *csk;
344 struct iscsi_path *path_resp;
345
346 if (len < sizeof(*path_resp))
347 break;
348
349 path_resp = (struct iscsi_path *) buf;
350 cp = dev->cnic_priv;
351 l5_cid = (u32) path_resp->handle;
352 if (l5_cid >= MAX_CM_SK_TBL_SZ)
353 break;
354
Michael Chand02a5e62010-02-24 14:42:06 +0000355 rcu_read_lock();
356 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
357 rc = -ENODEV;
358 rcu_read_unlock();
359 break;
360 }
Michael Chana4636962009-06-08 18:14:43 -0700361 csk = &cp->csk_tbl[l5_cid];
362 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000363 if (cnic_in_use(csk) &&
364 test_bit(SK_F_CONNECT_START, &csk->flags)) {
365
Michael Chana4636962009-06-08 18:14:43 -0700366 memcpy(csk->ha, path_resp->mac_addr, 6);
367 if (test_bit(SK_F_IPV6, &csk->flags))
368 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
369 sizeof(struct in6_addr));
370 else
371 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
372 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000373
374 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700375 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000376 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
377 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
378
379 cnic_cm_upcall(cp, csk,
380 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
381 clear_bit(SK_F_CONNECT_START, &csk->flags);
382 }
Michael Chana4636962009-06-08 18:14:43 -0700383 }
384 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000385 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700386 rc = 0;
387 }
388 }
389
390 return rc;
391}
392
393static int cnic_offld_prep(struct cnic_sock *csk)
394{
395 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
396 return 0;
397
398 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
399 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
400 return 0;
401 }
402
403 return 1;
404}
405
406static int cnic_close_prep(struct cnic_sock *csk)
407{
408 clear_bit(SK_F_CONNECT_START, &csk->flags);
409 smp_mb__after_clear_bit();
410
411 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
412 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
413 msleep(1);
414
415 return 1;
416 }
417 return 0;
418}
419
420static int cnic_abort_prep(struct cnic_sock *csk)
421{
422 clear_bit(SK_F_CONNECT_START, &csk->flags);
423 smp_mb__after_clear_bit();
424
425 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
426 msleep(1);
427
428 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
429 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
430 return 1;
431 }
432
433 return 0;
434}
435
436int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
437{
438 struct cnic_dev *dev;
439
roel kluin0d37f362009-11-02 06:53:44 +0000440 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000441 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700442 return -EINVAL;
443 }
444 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000445 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000446 pr_err("%s: Type %d has already been registered\n",
447 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700448 mutex_unlock(&cnic_lock);
449 return -EBUSY;
450 }
451
452 read_lock(&cnic_dev_lock);
453 list_for_each_entry(dev, &cnic_dev_list, list) {
454 struct cnic_local *cp = dev->cnic_priv;
455
456 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
457 }
458 read_unlock(&cnic_dev_lock);
459
Michael Chan7fc1ece2009-08-14 15:49:47 +0000460 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700461 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
462 mutex_unlock(&cnic_lock);
463
464 /* Prevent race conditions with netdev_event */
465 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700466 list_for_each_entry(dev, &cnic_dev_list, list) {
467 struct cnic_local *cp = dev->cnic_priv;
468
469 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
470 ulp_ops->cnic_init(dev);
471 }
Michael Chana4636962009-06-08 18:14:43 -0700472 rtnl_unlock();
473
474 return 0;
475}
476
477int cnic_unregister_driver(int ulp_type)
478{
479 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000480 struct cnic_ulp_ops *ulp_ops;
481 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700482
roel kluin0d37f362009-11-02 06:53:44 +0000483 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000484 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700485 return -EINVAL;
486 }
487 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000488 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000489 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000490 pr_err("%s: Type %d has not been registered\n",
491 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700492 goto out_unlock;
493 }
494 read_lock(&cnic_dev_lock);
495 list_for_each_entry(dev, &cnic_dev_list, list) {
496 struct cnic_local *cp = dev->cnic_priv;
497
498 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000499 pr_err("%s: Type %d still has devices registered\n",
500 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700501 read_unlock(&cnic_dev_lock);
502 goto out_unlock;
503 }
504 }
505 read_unlock(&cnic_dev_lock);
506
507 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
508
509 mutex_unlock(&cnic_lock);
510 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000511 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
512 msleep(100);
513 i++;
514 }
515
516 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000517 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700518 return 0;
519
520out_unlock:
521 mutex_unlock(&cnic_lock);
522 return -EINVAL;
523}
524
525static int cnic_start_hw(struct cnic_dev *);
526static void cnic_stop_hw(struct cnic_dev *);
527
528static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
529 void *ulp_ctx)
530{
531 struct cnic_local *cp = dev->cnic_priv;
532 struct cnic_ulp_ops *ulp_ops;
533
roel kluin0d37f362009-11-02 06:53:44 +0000534 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000535 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700536 return -EINVAL;
537 }
538 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000539 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000540 pr_err("%s: Driver with type %d has not been registered\n",
541 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700542 mutex_unlock(&cnic_lock);
543 return -EAGAIN;
544 }
545 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000546 pr_err("%s: Type %d has already been registered to this device\n",
547 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700548 mutex_unlock(&cnic_lock);
549 return -EBUSY;
550 }
551
552 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
553 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000554 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700555 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
556 cnic_hold(dev);
557
558 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
559 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
560 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
561
562 mutex_unlock(&cnic_lock);
563
564 return 0;
565
566}
567EXPORT_SYMBOL(cnic_register_driver);
568
569static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
570{
571 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000572 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700573
roel kluin0d37f362009-11-02 06:53:44 +0000574 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000575 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700576 return -EINVAL;
577 }
578 mutex_lock(&cnic_lock);
579 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
580 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
581 cnic_put(dev);
582 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000583 pr_err("%s: device not registered to this ulp type %d\n",
584 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700585 mutex_unlock(&cnic_lock);
586 return -EINVAL;
587 }
588 mutex_unlock(&cnic_lock);
589
Michael Chan42bb8d52011-01-03 15:21:46 +0000590 if (ulp_type == CNIC_ULP_ISCSI)
591 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
592
Michael Chana4636962009-06-08 18:14:43 -0700593 synchronize_rcu();
594
Michael Chan681dbd72009-08-14 15:49:46 +0000595 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
596 i < 20) {
597 msleep(100);
598 i++;
599 }
600 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000601 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000602
Michael Chana4636962009-06-08 18:14:43 -0700603 return 0;
604}
605EXPORT_SYMBOL(cnic_unregister_driver);
606
607static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
608{
609 id_tbl->start = start_id;
610 id_tbl->max = size;
611 id_tbl->next = 0;
612 spin_lock_init(&id_tbl->lock);
613 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
614 if (!id_tbl->table)
615 return -ENOMEM;
616
617 return 0;
618}
619
620static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
621{
622 kfree(id_tbl->table);
623 id_tbl->table = NULL;
624}
625
626static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
627{
628 int ret = -1;
629
630 id -= id_tbl->start;
631 if (id >= id_tbl->max)
632 return ret;
633
634 spin_lock(&id_tbl->lock);
635 if (!test_bit(id, id_tbl->table)) {
636 set_bit(id, id_tbl->table);
637 ret = 0;
638 }
639 spin_unlock(&id_tbl->lock);
640 return ret;
641}
642
643/* Returns -1 if not successful */
644static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
645{
646 u32 id;
647
648 spin_lock(&id_tbl->lock);
649 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
650 if (id >= id_tbl->max) {
651 id = -1;
652 if (id_tbl->next != 0) {
653 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
654 if (id >= id_tbl->next)
655 id = -1;
656 }
657 }
658
659 if (id < id_tbl->max) {
660 set_bit(id, id_tbl->table);
661 id_tbl->next = (id + 1) & (id_tbl->max - 1);
662 id += id_tbl->start;
663 }
664
665 spin_unlock(&id_tbl->lock);
666
667 return id;
668}
669
670static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
671{
672 if (id == -1)
673 return;
674
675 id -= id_tbl->start;
676 if (id >= id_tbl->max)
677 return;
678
679 clear_bit(id, id_tbl->table);
680}
681
682static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
683{
684 int i;
685
686 if (!dma->pg_arr)
687 return;
688
689 for (i = 0; i < dma->num_pages; i++) {
690 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000691 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
692 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700693 dma->pg_arr[i] = NULL;
694 }
695 }
696 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000697 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
698 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700699 dma->pgtbl = NULL;
700 }
701 kfree(dma->pg_arr);
702 dma->pg_arr = NULL;
703 dma->num_pages = 0;
704}
705
706static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
707{
708 int i;
Michael Chan51388262011-01-25 22:14:50 +0000709 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700710
711 for (i = 0; i < dma->num_pages; i++) {
712 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000713 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700714 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000715 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700716 page_table++;
717 }
718}
719
Michael Chan71034ba2009-10-10 13:46:59 +0000720static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
721{
722 int i;
Michael Chan51388262011-01-25 22:14:50 +0000723 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000724
725 for (i = 0; i < dma->num_pages; i++) {
726 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000727 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000728 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000729 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000730 page_table++;
731 }
732}
733
Michael Chana4636962009-06-08 18:14:43 -0700734static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
735 int pages, int use_pg_tbl)
736{
737 int i, size;
738 struct cnic_local *cp = dev->cnic_priv;
739
740 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
741 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
742 if (dma->pg_arr == NULL)
743 return -ENOMEM;
744
745 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
746 dma->num_pages = pages;
747
748 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000749 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
750 BCM_PAGE_SIZE,
751 &dma->pg_map_arr[i],
752 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700753 if (dma->pg_arr[i] == NULL)
754 goto error;
755 }
756 if (!use_pg_tbl)
757 return 0;
758
759 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
760 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000761 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
762 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700763 if (dma->pgtbl == NULL)
764 goto error;
765
766 cp->setup_pgtbl(dev, dma);
767
768 return 0;
769
770error:
771 cnic_free_dma(dev, dma);
772 return -ENOMEM;
773}
774
Michael Chan86b53602009-10-10 13:46:57 +0000775static void cnic_free_context(struct cnic_dev *dev)
776{
777 struct cnic_local *cp = dev->cnic_priv;
778 int i;
779
780 for (i = 0; i < cp->ctx_blks; i++) {
781 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000782 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
783 cp->ctx_arr[i].ctx,
784 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000785 cp->ctx_arr[i].ctx = NULL;
786 }
787 }
788}
789
Michael Chancd801532010-10-13 14:06:49 +0000790static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700791{
Michael Chancd801532010-10-13 14:06:49 +0000792 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700793
Michael Chancd801532010-10-13 14:06:49 +0000794 if (udev->l2_buf) {
795 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
796 udev->l2_buf, udev->l2_buf_map);
797 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700798 }
799
Michael Chancd801532010-10-13 14:06:49 +0000800 if (udev->l2_ring) {
801 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
802 udev->l2_ring, udev->l2_ring_map);
803 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700804 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000805
806 pci_dev_put(udev->pdev);
807 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000808}
809
Michael Chancd801532010-10-13 14:06:49 +0000810static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000811{
Michael Chancd801532010-10-13 14:06:49 +0000812 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000813 return;
814
Michael Chana3ceeeb2010-10-13 14:06:50 +0000815 write_lock(&cnic_dev_lock);
816 list_del_init(&udev->list);
817 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000818 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000819}
820
821static void cnic_free_resc(struct cnic_dev *dev)
822{
823 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000824 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000825
Michael Chancd801532010-10-13 14:06:49 +0000826 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000827 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000828 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000829 }
Michael Chana4636962009-06-08 18:14:43 -0700830
Michael Chan86b53602009-10-10 13:46:57 +0000831 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700832 kfree(cp->ctx_arr);
833 cp->ctx_arr = NULL;
834 cp->ctx_blks = 0;
835
836 cnic_free_dma(dev, &cp->gbl_buf_info);
837 cnic_free_dma(dev, &cp->conn_buf_info);
838 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000839 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000840 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000841 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700842 kfree(cp->iscsi_tbl);
843 cp->iscsi_tbl = NULL;
844 kfree(cp->ctx_tbl);
845 cp->ctx_tbl = NULL;
846
Michael Chane1928c82010-12-23 07:43:04 +0000847 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700848 cnic_free_id_tbl(&cp->cid_tbl);
849}
850
851static int cnic_alloc_context(struct cnic_dev *dev)
852{
853 struct cnic_local *cp = dev->cnic_priv;
854
855 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
856 int i, k, arr_size;
857
858 cp->ctx_blk_size = BCM_PAGE_SIZE;
859 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
860 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
861 sizeof(struct cnic_ctx);
862 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
863 if (cp->ctx_arr == NULL)
864 return -ENOMEM;
865
866 k = 0;
867 for (i = 0; i < 2; i++) {
868 u32 j, reg, off, lo, hi;
869
870 if (i == 0)
871 off = BNX2_PG_CTX_MAP;
872 else
873 off = BNX2_ISCSI_CTX_MAP;
874
875 reg = cnic_reg_rd_ind(dev, off);
876 lo = reg >> 16;
877 hi = reg & 0xffff;
878 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
879 cp->ctx_arr[k].cid = j;
880 }
881
882 cp->ctx_blks = k;
883 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
884 cp->ctx_blks = 0;
885 return -ENOMEM;
886 }
887
888 for (i = 0; i < cp->ctx_blks; i++) {
889 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000890 dma_alloc_coherent(&dev->pcidev->dev,
891 BCM_PAGE_SIZE,
892 &cp->ctx_arr[i].mapping,
893 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700894 if (cp->ctx_arr[i].ctx == NULL)
895 return -ENOMEM;
896 }
897 }
898 return 0;
899}
900
Michael Chane6c28892010-06-24 14:58:39 +0000901static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
902{
903 int err, i, is_bnx2 = 0;
904 struct kcqe **kcq;
905
906 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
907 is_bnx2 = 1;
908
909 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
910 if (err)
911 return err;
912
913 kcq = (struct kcqe **) info->dma.pg_arr;
914 info->kcq = kcq;
915
916 if (is_bnx2)
917 return 0;
918
919 for (i = 0; i < KCQ_PAGE_CNT; i++) {
920 struct bnx2x_bd_chain_next *next =
921 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
922 int j = i + 1;
923
924 if (j >= KCQ_PAGE_CNT)
925 j = 0;
926 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
927 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
928 }
929 return 0;
930}
931
Michael Chancd801532010-10-13 14:06:49 +0000932static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000933{
934 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000935 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000936
Michael Chana3ceeeb2010-10-13 14:06:50 +0000937 read_lock(&cnic_dev_lock);
938 list_for_each_entry(udev, &cnic_udev_list, list) {
939 if (udev->pdev == dev->pcidev) {
940 udev->dev = dev;
941 cp->udev = udev;
942 read_unlock(&cnic_dev_lock);
943 return 0;
944 }
945 }
946 read_unlock(&cnic_dev_lock);
947
Michael Chancd801532010-10-13 14:06:49 +0000948 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
949 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +0000950 return -ENOMEM;
951
Michael Chancd801532010-10-13 14:06:49 +0000952 udev->uio_dev = -1;
953
954 udev->dev = dev;
955 udev->pdev = dev->pcidev;
956 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
957 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
958 &udev->l2_ring_map,
959 GFP_KERNEL | __GFP_COMP);
960 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800961 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000962
Michael Chancd801532010-10-13 14:06:49 +0000963 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
964 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
965 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
966 &udev->l2_buf_map,
967 GFP_KERNEL | __GFP_COMP);
968 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800969 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +0000970
Michael Chana3ceeeb2010-10-13 14:06:50 +0000971 write_lock(&cnic_dev_lock);
972 list_add(&udev->list, &cnic_udev_list);
973 write_unlock(&cnic_dev_lock);
974
975 pci_dev_get(udev->pdev);
976
Michael Chancd801532010-10-13 14:06:49 +0000977 cp->udev = udev;
978
Michael Chanec0248e2009-08-26 09:49:22 +0000979 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800980 err_dma:
981 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
982 udev->l2_ring, udev->l2_ring_map);
983 err_udev:
984 kfree(udev);
985 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +0000986}
987
Michael Chancd801532010-10-13 14:06:49 +0000988static int cnic_init_uio(struct cnic_dev *dev)
989{
Michael Chan5e9b2db2009-08-26 09:49:23 +0000990 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000991 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000992 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +0000993 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000994
Michael Chancd801532010-10-13 14:06:49 +0000995 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +0000996 return -ENOMEM;
997
Michael Chancd801532010-10-13 14:06:49 +0000998 uinfo = &udev->cnic_uinfo;
999
Michael Chan5e9b2db2009-08-26 09:49:23 +00001000 uinfo->mem[0].addr = dev->netdev->base_addr;
1001 uinfo->mem[0].internal_addr = dev->regview;
1002 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
1003 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1004
Michael Chan5e9b2db2009-08-26 09:49:23 +00001005 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001006 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001007 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001008 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1009 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1010 else
1011 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1012
1013 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001014 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1015 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1016 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001017 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001018
1019 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001020 }
1021
1022 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1023
Michael Chancd801532010-10-13 14:06:49 +00001024 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1025 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001026 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1027
Michael Chancd801532010-10-13 14:06:49 +00001028 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1029 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001030 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1031
1032 uinfo->version = CNIC_MODULE_VERSION;
1033 uinfo->irq = UIO_IRQ_CUSTOM;
1034
1035 uinfo->open = cnic_uio_open;
1036 uinfo->release = cnic_uio_close;
1037
Michael Chana3ceeeb2010-10-13 14:06:50 +00001038 if (udev->uio_dev == -1) {
1039 if (!uinfo->priv) {
1040 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001041
Michael Chana3ceeeb2010-10-13 14:06:50 +00001042 ret = uio_register_device(&udev->pdev->dev, uinfo);
1043 }
1044 } else {
1045 cnic_init_rings(dev);
1046 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001047
Michael Chancd801532010-10-13 14:06:49 +00001048 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001049}
1050
Michael Chana4636962009-06-08 18:14:43 -07001051static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1052{
1053 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001054 int ret;
1055
1056 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1057 if (ret)
1058 goto error;
1059 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1060
Michael Chane6c28892010-06-24 14:58:39 +00001061 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chana4636962009-06-08 18:14:43 -07001062 if (ret)
1063 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001064
1065 ret = cnic_alloc_context(dev);
1066 if (ret)
1067 goto error;
1068
Michael Chancd801532010-10-13 14:06:49 +00001069 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001070 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001071 goto error;
1072
Michael Chancd801532010-10-13 14:06:49 +00001073 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001074 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001075 goto error;
1076
Michael Chana4636962009-06-08 18:14:43 -07001077 return 0;
1078
1079error:
1080 cnic_free_resc(dev);
1081 return ret;
1082}
1083
Michael Chan71034ba2009-10-10 13:46:59 +00001084static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1085{
1086 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001087 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001088 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001089
Michael Chan520efdf2010-06-24 14:58:37 +00001090 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001091 blks = total_mem / ctx_blk_size;
1092 if (total_mem % ctx_blk_size)
1093 blks++;
1094
1095 if (blks > cp->ethdev->ctx_tbl_len)
1096 return -ENOMEM;
1097
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001098 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001099 if (cp->ctx_arr == NULL)
1100 return -ENOMEM;
1101
1102 cp->ctx_blks = blks;
1103 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001104 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001105 cp->ctx_align = 0;
1106 else
1107 cp->ctx_align = ctx_blk_size;
1108
1109 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1110
1111 for (i = 0; i < blks; i++) {
1112 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001113 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1114 &cp->ctx_arr[i].mapping,
1115 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001116 if (cp->ctx_arr[i].ctx == NULL)
1117 return -ENOMEM;
1118
1119 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1120 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1121 cnic_free_context(dev);
1122 cp->ctx_blk_size += cp->ctx_align;
1123 i = -1;
1124 continue;
1125 }
1126 }
1127 }
1128 return 0;
1129}
1130
1131static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1132{
1133 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001134 struct cnic_eth_dev *ethdev = cp->ethdev;
1135 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001136 int i, j, n, ret, pages;
1137 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1138
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001139 cp->iro_arr = ethdev->iro_arr;
1140
Michael Chane1928c82010-12-23 07:43:04 +00001141 cp->max_cid_space = MAX_ISCSI_TBL_SZ + BNX2X_FCOE_NUM_CONNECTIONS;
Michael Chan520efdf2010-06-24 14:58:37 +00001142 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001143 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1144
1145 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1146 cp->max_cid_space += BNX2X_FCOE_NUM_CONNECTIONS;
1147 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1148 if (!cp->fcoe_init_cid)
1149 cp->fcoe_init_cid = 0x10;
1150 }
1151
Michael Chan520efdf2010-06-24 14:58:37 +00001152 if (start_cid < BNX2X_ISCSI_START_CID) {
1153 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1154
1155 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
Michael Chane1928c82010-12-23 07:43:04 +00001156 cp->fcoe_start_cid += delta;
Michael Chan520efdf2010-06-24 14:58:37 +00001157 cp->max_cid_space += delta;
1158 }
1159
Michael Chan71034ba2009-10-10 13:46:59 +00001160 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1161 GFP_KERNEL);
1162 if (!cp->iscsi_tbl)
1163 goto error;
1164
1165 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001166 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001167 if (!cp->ctx_tbl)
1168 goto error;
1169
1170 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1171 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1172 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1173 }
1174
Michael Chane1928c82010-12-23 07:43:04 +00001175 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1176 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1177
Michael Chan520efdf2010-06-24 14:58:37 +00001178 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001179 PAGE_SIZE;
1180
1181 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1182 if (ret)
1183 return -ENOMEM;
1184
1185 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001186 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001187 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1188
1189 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1190 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1191 off;
1192
1193 if ((i % n) == (n - 1))
1194 j++;
1195 }
1196
Michael Chane6c28892010-06-24 14:58:39 +00001197 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00001198 if (ret)
1199 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001200
Michael Chane21ba412010-12-23 07:43:03 +00001201 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1202 ret = cnic_alloc_kcq(dev, &cp->kcq2);
1203 if (ret)
1204 goto error;
1205 }
1206
Michael Chan71034ba2009-10-10 13:46:59 +00001207 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1208 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1209 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1210 if (ret)
1211 goto error;
1212
1213 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1214 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1215 if (ret)
1216 goto error;
1217
1218 ret = cnic_alloc_bnx2x_context(dev);
1219 if (ret)
1220 goto error;
1221
Michael Chan71034ba2009-10-10 13:46:59 +00001222 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1223
1224 cp->l2_rx_ring_size = 15;
1225
Michael Chancd801532010-10-13 14:06:49 +00001226 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001227 if (ret)
1228 goto error;
1229
Michael Chancd801532010-10-13 14:06:49 +00001230 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001231 if (ret)
1232 goto error;
1233
1234 return 0;
1235
1236error:
1237 cnic_free_resc(dev);
1238 return -ENOMEM;
1239}
1240
Michael Chana4636962009-06-08 18:14:43 -07001241static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1242{
1243 return cp->max_kwq_idx -
1244 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1245}
1246
1247static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1248 u32 num_wqes)
1249{
1250 struct cnic_local *cp = dev->cnic_priv;
1251 struct kwqe *prod_qe;
1252 u16 prod, sw_prod, i;
1253
1254 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1255 return -EAGAIN; /* bnx2 is down */
1256
1257 spin_lock_bh(&cp->cnic_ulp_lock);
1258 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001259 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001260 spin_unlock_bh(&cp->cnic_ulp_lock);
1261 return -EAGAIN;
1262 }
1263
Michael Chan1f1332a2010-05-18 11:32:52 +00001264 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001265
1266 prod = cp->kwq_prod_idx;
1267 sw_prod = prod & MAX_KWQ_IDX;
1268 for (i = 0; i < num_wqes; i++) {
1269 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1270 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1271 prod++;
1272 sw_prod = prod & MAX_KWQ_IDX;
1273 }
1274 cp->kwq_prod_idx = prod;
1275
1276 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1277
1278 spin_unlock_bh(&cp->cnic_ulp_lock);
1279 return 0;
1280}
1281
Michael Chan71034ba2009-10-10 13:46:59 +00001282static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1283 union l5cm_specific_data *l5_data)
1284{
1285 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1286 dma_addr_t map;
1287
1288 map = ctx->kwqe_data_mapping;
1289 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1290 l5_data->phy_address.hi = (u64) map >> 32;
1291 return ctx->kwqe_data;
1292}
1293
1294static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1295 u32 type, union l5cm_specific_data *l5_data)
1296{
1297 struct cnic_local *cp = dev->cnic_priv;
1298 struct l5cm_spe kwqe;
1299 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001300 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001301 int ret;
1302
1303 kwqe.hdr.conn_and_cmd_data =
1304 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001305 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001306
1307 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1308 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1309 SPE_HDR_FUNCTION_ID;
1310
1311 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001312 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001313 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1314 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1315
1316 kwq[0] = (struct kwqe_16 *) &kwqe;
1317
1318 spin_lock_bh(&cp->cnic_ulp_lock);
1319 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1320 spin_unlock_bh(&cp->cnic_ulp_lock);
1321
1322 if (ret == 1)
1323 return 0;
1324
1325 return -EBUSY;
1326}
1327
1328static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1329 struct kcqe *cqes[], u32 num_cqes)
1330{
1331 struct cnic_local *cp = dev->cnic_priv;
1332 struct cnic_ulp_ops *ulp_ops;
1333
1334 rcu_read_lock();
1335 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1336 if (likely(ulp_ops)) {
1337 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1338 cqes, num_cqes);
1339 }
1340 rcu_read_unlock();
1341}
1342
1343static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1344{
1345 struct cnic_local *cp = dev->cnic_priv;
1346 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001347 int hq_bds, pages;
1348 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001349
1350 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1351 cp->num_ccells = req1->num_ccells_per_conn;
1352 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1353 cp->num_iscsi_tasks;
1354 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1355 BNX2X_ISCSI_R2TQE_SIZE;
1356 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1357 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1358 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1359 cp->num_cqs = req1->num_cqs;
1360
1361 if (!dev->max_iscsi_conn)
1362 return 0;
1363
1364 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001365 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001366 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001367 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001368 PAGE_SIZE);
1369 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001370 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001371 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001372 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001373 req1->num_tasks_per_conn);
1374
1375 /* init Ustorm RAM */
1376 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001377 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001378 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001379 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001380 PAGE_SIZE);
1381 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001382 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001383 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001384 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001385 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001386 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001387 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001388 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001389 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001390 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001391 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1392
1393 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001394 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001395 PAGE_SIZE);
1396 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001397 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001398 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001399 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001400 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001401 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001402 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001403 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001404 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001405 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001406 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1407
1408 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001409 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001410 PAGE_SIZE);
1411 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001412 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001413 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001414 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001415 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001416 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001417 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001418 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001419 hq_bds);
1420
1421 return 0;
1422}
1423
1424static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1425{
1426 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1427 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001428 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001429 struct iscsi_kcqe kcqe;
1430 struct kcqe *cqes[1];
1431
1432 memset(&kcqe, 0, sizeof(kcqe));
1433 if (!dev->max_iscsi_conn) {
1434 kcqe.completion_status =
1435 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1436 goto done;
1437 }
1438
1439 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001440 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001441 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001442 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001443 req2->error_bit_map[1]);
1444
1445 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001446 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001447 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001448 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001449 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001450 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001451 req2->error_bit_map[1]);
1452
1453 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001454 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001455
1456 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1457
1458done:
1459 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1460 cqes[0] = (struct kcqe *) &kcqe;
1461 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1462
1463 return 0;
1464}
1465
1466static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1467{
1468 struct cnic_local *cp = dev->cnic_priv;
1469 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1470
1471 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1472 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1473
1474 cnic_free_dma(dev, &iscsi->hq_info);
1475 cnic_free_dma(dev, &iscsi->r2tq_info);
1476 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001477 cnic_free_id(&cp->cid_tbl, ctx->cid);
1478 } else {
1479 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001480 }
Michael Chane1928c82010-12-23 07:43:04 +00001481
Michael Chan71034ba2009-10-10 13:46:59 +00001482 ctx->cid = 0;
1483}
1484
1485static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1486{
1487 u32 cid;
1488 int ret, pages;
1489 struct cnic_local *cp = dev->cnic_priv;
1490 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1491 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1492
Michael Chane1928c82010-12-23 07:43:04 +00001493 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1494 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1495 if (cid == -1) {
1496 ret = -ENOMEM;
1497 goto error;
1498 }
1499 ctx->cid = cid;
1500 return 0;
1501 }
1502
Michael Chan71034ba2009-10-10 13:46:59 +00001503 cid = cnic_alloc_new_id(&cp->cid_tbl);
1504 if (cid == -1) {
1505 ret = -ENOMEM;
1506 goto error;
1507 }
1508
1509 ctx->cid = cid;
1510 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1511
1512 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1513 if (ret)
1514 goto error;
1515
1516 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1517 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1518 if (ret)
1519 goto error;
1520
1521 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1522 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1523 if (ret)
1524 goto error;
1525
1526 return 0;
1527
1528error:
1529 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1530 return ret;
1531}
1532
1533static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1534 struct regpair *ctx_addr)
1535{
1536 struct cnic_local *cp = dev->cnic_priv;
1537 struct cnic_eth_dev *ethdev = cp->ethdev;
1538 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1539 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1540 unsigned long align_off = 0;
1541 dma_addr_t ctx_map;
1542 void *ctx;
1543
1544 if (cp->ctx_align) {
1545 unsigned long mask = cp->ctx_align - 1;
1546
1547 if (cp->ctx_arr[blk].mapping & mask)
1548 align_off = cp->ctx_align -
1549 (cp->ctx_arr[blk].mapping & mask);
1550 }
1551 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1552 (off * BNX2X_CONTEXT_MEM_SIZE);
1553 ctx = cp->ctx_arr[blk].ctx + align_off +
1554 (off * BNX2X_CONTEXT_MEM_SIZE);
1555 if (init)
1556 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1557
1558 ctx_addr->lo = ctx_map & 0xffffffff;
1559 ctx_addr->hi = (u64) ctx_map >> 32;
1560 return ctx;
1561}
1562
1563static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1564 u32 num)
1565{
1566 struct cnic_local *cp = dev->cnic_priv;
1567 struct iscsi_kwqe_conn_offload1 *req1 =
1568 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1569 struct iscsi_kwqe_conn_offload2 *req2 =
1570 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1571 struct iscsi_kwqe_conn_offload3 *req3;
1572 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1573 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1574 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001575 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001576 struct iscsi_context *ictx;
1577 struct regpair context_addr;
1578 int i, j, n = 2, n_max;
1579
1580 ctx->ctx_flags = 0;
1581 if (!req2->num_additional_wqes)
1582 return -EINVAL;
1583
1584 n_max = req2->num_additional_wqes + 2;
1585
1586 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1587 if (ictx == NULL)
1588 return -ENOMEM;
1589
1590 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1591
1592 ictx->xstorm_ag_context.hq_prod = 1;
1593
1594 ictx->xstorm_st_context.iscsi.first_burst_length =
1595 ISCSI_DEF_FIRST_BURST_LEN;
1596 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1597 ISCSI_DEF_MAX_RECV_SEG_LEN;
1598 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1599 req1->sq_page_table_addr_lo;
1600 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1601 req1->sq_page_table_addr_hi;
1602 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1603 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1604 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1605 iscsi->hq_info.pgtbl_map & 0xffffffff;
1606 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1607 (u64) iscsi->hq_info.pgtbl_map >> 32;
1608 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1609 iscsi->hq_info.pgtbl[0];
1610 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1611 iscsi->hq_info.pgtbl[1];
1612 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1613 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1614 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1615 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1616 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1617 iscsi->r2tq_info.pgtbl[0];
1618 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1619 iscsi->r2tq_info.pgtbl[1];
1620 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1621 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1622 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1623 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1624 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1625 BNX2X_ISCSI_PBL_NOT_CACHED;
1626 ictx->xstorm_st_context.iscsi.flags.flags |=
1627 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1628 ictx->xstorm_st_context.iscsi.flags.flags |=
1629 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1630
1631 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1632 /* TSTORM requires the base address of RQ DB & not PTE */
1633 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1634 req2->rq_page_table_addr_lo & PAGE_MASK;
1635 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1636 req2->rq_page_table_addr_hi;
1637 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1638 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1639 ictx->tstorm_st_context.tcp.flags2 |=
1640 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001641 ictx->tstorm_st_context.tcp.ooo_support_mode =
1642 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001643
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001644 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001645
1646 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001647 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001648 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001649 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001650 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1651 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1652 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1653 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1654 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1655 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1656 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1657 iscsi->r2tq_info.pgtbl[0];
1658 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1659 iscsi->r2tq_info.pgtbl[1];
1660 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1661 req1->cq_page_table_addr_lo;
1662 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1663 req1->cq_page_table_addr_hi;
1664 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1665 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1666 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1667 ictx->ustorm_st_context.task_pbe_cache_index =
1668 BNX2X_ISCSI_PBL_NOT_CACHED;
1669 ictx->ustorm_st_context.task_pdu_cache_index =
1670 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1671
1672 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1673 if (j == 3) {
1674 if (n >= n_max)
1675 break;
1676 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1677 j = 0;
1678 }
1679 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1680 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1681 req3->qp_first_pte[j].hi;
1682 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1683 req3->qp_first_pte[j].lo;
1684 }
1685
1686 ictx->ustorm_st_context.task_pbl_base.lo =
1687 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1688 ictx->ustorm_st_context.task_pbl_base.hi =
1689 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1690 ictx->ustorm_st_context.tce_phy_addr.lo =
1691 iscsi->task_array_info.pgtbl[0];
1692 ictx->ustorm_st_context.tce_phy_addr.hi =
1693 iscsi->task_array_info.pgtbl[1];
1694 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1695 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1696 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1697 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1698 ISCSI_DEF_MAX_BURST_LEN;
1699 ictx->ustorm_st_context.negotiated_rx |=
1700 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1701 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1702
1703 ictx->cstorm_st_context.hq_pbl_base.lo =
1704 iscsi->hq_info.pgtbl_map & 0xffffffff;
1705 ictx->cstorm_st_context.hq_pbl_base.hi =
1706 (u64) iscsi->hq_info.pgtbl_map >> 32;
1707 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1708 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1709 ictx->cstorm_st_context.task_pbl_base.lo =
1710 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1711 ictx->cstorm_st_context.task_pbl_base.hi =
1712 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1713 /* CSTORM and USTORM initialization is different, CSTORM requires
1714 * CQ DB base & not PTE addr */
1715 ictx->cstorm_st_context.cq_db_base.lo =
1716 req1->cq_page_table_addr_lo & PAGE_MASK;
1717 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1718 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1719 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1720 for (i = 0; i < cp->num_cqs; i++) {
1721 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1722 ISCSI_INITIAL_SN;
1723 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1724 ISCSI_INITIAL_SN;
1725 }
1726
1727 ictx->xstorm_ag_context.cdu_reserved =
1728 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1729 ISCSI_CONNECTION_TYPE);
1730 ictx->ustorm_ag_context.cdu_usage =
1731 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1732 ISCSI_CONNECTION_TYPE);
1733 return 0;
1734
1735}
1736
1737static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1738 u32 num, int *work)
1739{
1740 struct iscsi_kwqe_conn_offload1 *req1;
1741 struct iscsi_kwqe_conn_offload2 *req2;
1742 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001743 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001744 struct iscsi_kcqe kcqe;
1745 struct kcqe *cqes[1];
1746 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001747 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001748
1749 if (num < 2) {
1750 *work = num;
1751 return -EINVAL;
1752 }
1753
1754 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1755 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1756 if ((num - 2) < req2->num_additional_wqes) {
1757 *work = num;
1758 return -EINVAL;
1759 }
Joe Perches779bb412010-11-14 17:04:37 +00001760 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001761
1762 l5_cid = req1->iscsi_conn_id;
1763 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1764 return -EINVAL;
1765
1766 memset(&kcqe, 0, sizeof(kcqe));
1767 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1768 kcqe.iscsi_conn_id = l5_cid;
1769 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1770
Michael Chanfdf24082010-10-13 14:06:47 +00001771 ctx = &cp->ctx_tbl[l5_cid];
1772 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1773 kcqe.completion_status =
1774 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1775 goto done;
1776 }
1777
Michael Chan71034ba2009-10-10 13:46:59 +00001778 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1779 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001780 goto done;
1781 }
1782 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1783 if (ret) {
1784 atomic_dec(&cp->iscsi_conn);
1785 ret = 0;
1786 goto done;
1787 }
1788 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1789 if (ret < 0) {
1790 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1791 atomic_dec(&cp->iscsi_conn);
1792 goto done;
1793 }
1794
1795 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001796 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001797
1798done:
1799 cqes[0] = (struct kcqe *) &kcqe;
1800 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1801 return ret;
1802}
1803
1804
1805static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1806{
1807 struct cnic_local *cp = dev->cnic_priv;
1808 struct iscsi_kwqe_conn_update *req =
1809 (struct iscsi_kwqe_conn_update *) kwqe;
1810 void *data;
1811 union l5cm_specific_data l5_data;
1812 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1813 int ret;
1814
1815 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1816 return -EINVAL;
1817
1818 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1819 if (!data)
1820 return -ENOMEM;
1821
1822 memcpy(data, kwqe, sizeof(struct kwqe));
1823
1824 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1825 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1826 return ret;
1827}
1828
Michael Chana2c9e762010-10-13 14:06:46 +00001829static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001830{
1831 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001832 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001833 union l5cm_specific_data l5_data;
1834 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001835 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001836
Michael Chan71034ba2009-10-10 13:46:59 +00001837 init_waitqueue_head(&ctx->waitq);
1838 ctx->wait_cond = 0;
1839 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001840 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001841
1842 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001843 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001844
Michael Chan71034ba2009-10-10 13:46:59 +00001845 if (ret == 0)
1846 wait_event(ctx->waitq, ctx->wait_cond);
1847
Michael Chana2c9e762010-10-13 14:06:46 +00001848 return ret;
1849}
1850
1851static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1852{
1853 struct cnic_local *cp = dev->cnic_priv;
1854 struct iscsi_kwqe_conn_destroy *req =
1855 (struct iscsi_kwqe_conn_destroy *) kwqe;
1856 u32 l5_cid = req->reserved0;
1857 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1858 int ret = 0;
1859 struct iscsi_kcqe kcqe;
1860 struct kcqe *cqes[1];
1861
1862 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1863 goto skip_cfc_delete;
1864
Michael Chanfdf24082010-10-13 14:06:47 +00001865 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1866 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1867
1868 if (delta > (2 * HZ))
1869 delta = 0;
1870
1871 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1872 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1873 goto destroy_reply;
1874 }
Michael Chana2c9e762010-10-13 14:06:46 +00001875
1876 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1877
Michael Chan71034ba2009-10-10 13:46:59 +00001878skip_cfc_delete:
1879 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1880
1881 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00001882 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001883
Michael Chanfdf24082010-10-13 14:06:47 +00001884destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001885 memset(&kcqe, 0, sizeof(kcqe));
1886 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1887 kcqe.iscsi_conn_id = l5_cid;
1888 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1889 kcqe.iscsi_conn_context_id = req->context_id;
1890
1891 cqes[0] = (struct kcqe *) &kcqe;
1892 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1893
1894 return ret;
1895}
1896
1897static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1898 struct l4_kwq_connect_req1 *kwqe1,
1899 struct l4_kwq_connect_req3 *kwqe3,
1900 struct l5cm_active_conn_buffer *conn_buf)
1901{
1902 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1903 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1904 &conn_buf->xstorm_conn_buffer;
1905 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1906 &conn_buf->tstorm_conn_buffer;
1907 struct regpair context_addr;
1908 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1909 struct in6_addr src_ip, dst_ip;
1910 int i;
1911 u32 *addrp;
1912
1913 addrp = (u32 *) &conn_addr->local_ip_addr;
1914 for (i = 0; i < 4; i++, addrp++)
1915 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1916
1917 addrp = (u32 *) &conn_addr->remote_ip_addr;
1918 for (i = 0; i < 4; i++, addrp++)
1919 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1920
1921 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1922
1923 xstorm_buf->context_addr.hi = context_addr.hi;
1924 xstorm_buf->context_addr.lo = context_addr.lo;
1925 xstorm_buf->mss = 0xffff;
1926 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1927 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1928 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1929 xstorm_buf->pseudo_header_checksum =
1930 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1931
1932 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1933 tstorm_buf->params |=
1934 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1935 if (kwqe3->ka_timeout) {
1936 tstorm_buf->ka_enable = 1;
1937 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1938 tstorm_buf->ka_interval = kwqe3->ka_interval;
1939 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1940 }
1941 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1942 tstorm_buf->snd_buf = kwqe3->snd_buf;
1943 tstorm_buf->max_rt_time = 0xffffffff;
1944}
1945
1946static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1947{
1948 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001949 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001950 u8 *mac = dev->mac_addr;
1951
1952 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001953 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001954 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001955 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001956 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001957 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001958 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001959 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001960 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001961 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001962 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001963 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001964
1965 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001966 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001967 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001968 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001969 mac[4]);
1970 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001971 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001972 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001973 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001974 mac[2]);
1975 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001976 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00001977 mac[1]);
1978 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001979 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00001980 mac[0]);
1981}
1982
1983static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1984{
1985 struct cnic_local *cp = dev->cnic_priv;
1986 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1987 u16 tstorm_flags = 0;
1988
1989 if (tcp_ts) {
1990 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1991 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1992 }
1993
1994 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001995 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001996
1997 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001998 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001999}
2000
2001static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2002 u32 num, int *work)
2003{
2004 struct cnic_local *cp = dev->cnic_priv;
2005 struct l4_kwq_connect_req1 *kwqe1 =
2006 (struct l4_kwq_connect_req1 *) wqes[0];
2007 struct l4_kwq_connect_req3 *kwqe3;
2008 struct l5cm_active_conn_buffer *conn_buf;
2009 struct l5cm_conn_addr_params *conn_addr;
2010 union l5cm_specific_data l5_data;
2011 u32 l5_cid = kwqe1->pg_cid;
2012 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2013 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2014 int ret;
2015
2016 if (num < 2) {
2017 *work = num;
2018 return -EINVAL;
2019 }
2020
2021 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2022 *work = 3;
2023 else
2024 *work = 2;
2025
2026 if (num < *work) {
2027 *work = num;
2028 return -EINVAL;
2029 }
2030
2031 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002032 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002033 return -ENOMEM;
2034 }
2035 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2036 if (!conn_buf)
2037 return -ENOMEM;
2038
2039 memset(conn_buf, 0, sizeof(*conn_buf));
2040
2041 conn_addr = &conn_buf->conn_addr_buf;
2042 conn_addr->remote_addr_0 = csk->ha[0];
2043 conn_addr->remote_addr_1 = csk->ha[1];
2044 conn_addr->remote_addr_2 = csk->ha[2];
2045 conn_addr->remote_addr_3 = csk->ha[3];
2046 conn_addr->remote_addr_4 = csk->ha[4];
2047 conn_addr->remote_addr_5 = csk->ha[5];
2048
2049 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2050 struct l4_kwq_connect_req2 *kwqe2 =
2051 (struct l4_kwq_connect_req2 *) wqes[1];
2052
2053 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2054 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2055 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2056
2057 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2058 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2059 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2060 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2061 }
2062 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2063
2064 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2065 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2066 conn_addr->local_tcp_port = kwqe1->src_port;
2067 conn_addr->remote_tcp_port = kwqe1->dst_port;
2068
2069 conn_addr->pmtu = kwqe3->pmtu;
2070 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2071
2072 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002073 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002074
2075 cnic_bnx2x_set_tcp_timestamp(dev,
2076 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2077
2078 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2079 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2080 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002081 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002082
2083 return ret;
2084}
2085
2086static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2087{
2088 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2089 union l5cm_specific_data l5_data;
2090 int ret;
2091
2092 memset(&l5_data, 0, sizeof(l5_data));
2093 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2094 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2095 return ret;
2096}
2097
2098static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2099{
2100 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2101 union l5cm_specific_data l5_data;
2102 int ret;
2103
2104 memset(&l5_data, 0, sizeof(l5_data));
2105 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2106 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2107 return ret;
2108}
2109static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2110{
2111 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2112 struct l4_kcq kcqe;
2113 struct kcqe *cqes[1];
2114
2115 memset(&kcqe, 0, sizeof(kcqe));
2116 kcqe.pg_host_opaque = req->host_opaque;
2117 kcqe.pg_cid = req->host_opaque;
2118 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2119 cqes[0] = (struct kcqe *) &kcqe;
2120 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2121 return 0;
2122}
2123
2124static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2125{
2126 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2127 struct l4_kcq kcqe;
2128 struct kcqe *cqes[1];
2129
2130 memset(&kcqe, 0, sizeof(kcqe));
2131 kcqe.pg_host_opaque = req->pg_host_opaque;
2132 kcqe.pg_cid = req->pg_cid;
2133 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2134 cqes[0] = (struct kcqe *) &kcqe;
2135 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2136 return 0;
2137}
2138
Michael Chane1928c82010-12-23 07:43:04 +00002139static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2140{
2141 struct fcoe_kwqe_stat *req;
2142 struct fcoe_stat_ramrod_params *fcoe_stat;
2143 union l5cm_specific_data l5_data;
2144 struct cnic_local *cp = dev->cnic_priv;
2145 int ret;
2146 u32 cid;
2147
2148 req = (struct fcoe_kwqe_stat *) kwqe;
2149 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2150
2151 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2152 if (!fcoe_stat)
2153 return -ENOMEM;
2154
2155 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2156 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2157
2158 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT, cid,
2159 FCOE_CONNECTION_TYPE, &l5_data);
2160 return ret;
2161}
2162
2163static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2164 u32 num, int *work)
2165{
2166 int ret;
2167 struct cnic_local *cp = dev->cnic_priv;
2168 u32 cid;
2169 struct fcoe_init_ramrod_params *fcoe_init;
2170 struct fcoe_kwqe_init1 *req1;
2171 struct fcoe_kwqe_init2 *req2;
2172 struct fcoe_kwqe_init3 *req3;
2173 union l5cm_specific_data l5_data;
2174
2175 if (num < 3) {
2176 *work = num;
2177 return -EINVAL;
2178 }
2179 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2180 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2181 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2182 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2183 *work = 1;
2184 return -EINVAL;
2185 }
2186 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2187 *work = 2;
2188 return -EINVAL;
2189 }
2190
2191 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2192 netdev_err(dev->netdev, "fcoe_init size too big\n");
2193 return -ENOMEM;
2194 }
2195 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2196 if (!fcoe_init)
2197 return -ENOMEM;
2198
2199 memset(fcoe_init, 0, sizeof(*fcoe_init));
2200 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2201 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2202 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
2203 fcoe_init->eq_addr.lo = cp->kcq2.dma.pg_map_arr[0] & 0xffffffff;
2204 fcoe_init->eq_addr.hi = (u64) cp->kcq2.dma.pg_map_arr[0] >> 32;
2205 fcoe_init->eq_next_page_addr.lo =
2206 cp->kcq2.dma.pg_map_arr[1] & 0xffffffff;
2207 fcoe_init->eq_next_page_addr.hi =
2208 (u64) cp->kcq2.dma.pg_map_arr[1] >> 32;
2209
2210 fcoe_init->sb_num = cp->status_blk_num;
2211 fcoe_init->eq_prod = MAX_KCQ_IDX;
2212 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2213 cp->kcq2.sw_prod_idx = 0;
2214
2215 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Michael Chane1928c82010-12-23 07:43:04 +00002216 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT, cid,
2217 FCOE_CONNECTION_TYPE, &l5_data);
2218 *work = 3;
2219 return ret;
2220}
2221
2222static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2223 u32 num, int *work)
2224{
2225 int ret = 0;
2226 u32 cid = -1, l5_cid;
2227 struct cnic_local *cp = dev->cnic_priv;
2228 struct fcoe_kwqe_conn_offload1 *req1;
2229 struct fcoe_kwqe_conn_offload2 *req2;
2230 struct fcoe_kwqe_conn_offload3 *req3;
2231 struct fcoe_kwqe_conn_offload4 *req4;
2232 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2233 struct cnic_context *ctx;
2234 struct fcoe_context *fctx;
2235 struct regpair ctx_addr;
2236 union l5cm_specific_data l5_data;
2237 struct fcoe_kcqe kcqe;
2238 struct kcqe *cqes[1];
2239
2240 if (num < 4) {
2241 *work = num;
2242 return -EINVAL;
2243 }
2244 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2245 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2246 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2247 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2248
2249 *work = 4;
2250
2251 l5_cid = req1->fcoe_conn_id;
2252 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2253 goto err_reply;
2254
2255 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2256
2257 ctx = &cp->ctx_tbl[l5_cid];
2258 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2259 goto err_reply;
2260
2261 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2262 if (ret) {
2263 ret = 0;
2264 goto err_reply;
2265 }
2266 cid = ctx->cid;
2267
2268 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2269 if (fctx) {
2270 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2271 u32 val;
2272
2273 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2274 FCOE_CONNECTION_TYPE);
2275 fctx->xstorm_ag_context.cdu_reserved = val;
2276 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2277 FCOE_CONNECTION_TYPE);
2278 fctx->ustorm_ag_context.cdu_usage = val;
2279 }
2280 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2281 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2282 goto err_reply;
2283 }
2284 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2285 if (!fcoe_offload)
2286 goto err_reply;
2287
2288 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2289 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2290 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2291 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2292 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2293
2294 cid = BNX2X_HW_CID(cp, cid);
2295 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2296 FCOE_CONNECTION_TYPE, &l5_data);
2297 if (!ret)
2298 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2299
2300 return ret;
2301
2302err_reply:
2303 if (cid != -1)
2304 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2305
2306 memset(&kcqe, 0, sizeof(kcqe));
2307 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2308 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2309 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2310
2311 cqes[0] = (struct kcqe *) &kcqe;
2312 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2313 return ret;
2314}
2315
2316static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2317{
2318 struct fcoe_kwqe_conn_enable_disable *req;
2319 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2320 union l5cm_specific_data l5_data;
2321 int ret;
2322 u32 cid, l5_cid;
2323 struct cnic_local *cp = dev->cnic_priv;
2324
2325 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2326 cid = req->context_id;
2327 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2328
2329 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2330 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2331 return -ENOMEM;
2332 }
2333 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2334 if (!fcoe_enable)
2335 return -ENOMEM;
2336
2337 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2338 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2339 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2340 FCOE_CONNECTION_TYPE, &l5_data);
2341 return ret;
2342}
2343
2344static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2345{
2346 struct fcoe_kwqe_conn_enable_disable *req;
2347 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2348 union l5cm_specific_data l5_data;
2349 int ret;
2350 u32 cid, l5_cid;
2351 struct cnic_local *cp = dev->cnic_priv;
2352
2353 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2354 cid = req->context_id;
2355 l5_cid = req->conn_id;
2356 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2357 return -EINVAL;
2358
2359 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2360
2361 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2362 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2363 return -ENOMEM;
2364 }
2365 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2366 if (!fcoe_disable)
2367 return -ENOMEM;
2368
2369 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2370 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2371 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2372 FCOE_CONNECTION_TYPE, &l5_data);
2373 return ret;
2374}
2375
2376static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2377{
2378 struct fcoe_kwqe_conn_destroy *req;
2379 union l5cm_specific_data l5_data;
2380 int ret;
2381 u32 cid, l5_cid;
2382 struct cnic_local *cp = dev->cnic_priv;
2383 struct cnic_context *ctx;
2384 struct fcoe_kcqe kcqe;
2385 struct kcqe *cqes[1];
2386
2387 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2388 cid = req->context_id;
2389 l5_cid = req->conn_id;
2390 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2391 return -EINVAL;
2392
2393 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2394
2395 ctx = &cp->ctx_tbl[l5_cid];
2396
2397 init_waitqueue_head(&ctx->waitq);
2398 ctx->wait_cond = 0;
2399
2400 memset(&l5_data, 0, sizeof(l5_data));
2401 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2402 FCOE_CONNECTION_TYPE, &l5_data);
2403 if (ret == 0) {
2404 wait_event(ctx->waitq, ctx->wait_cond);
2405 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2406 queue_delayed_work(cnic_wq, &cp->delete_task,
2407 msecs_to_jiffies(2000));
2408 }
2409
2410 memset(&kcqe, 0, sizeof(kcqe));
2411 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2412 kcqe.fcoe_conn_id = req->conn_id;
2413 kcqe.fcoe_conn_context_id = cid;
2414
2415 cqes[0] = (struct kcqe *) &kcqe;
2416 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2417 return ret;
2418}
2419
2420static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2421{
2422 struct fcoe_kwqe_destroy *req;
2423 union l5cm_specific_data l5_data;
2424 struct cnic_local *cp = dev->cnic_priv;
2425 int ret;
2426 u32 cid;
2427
2428 req = (struct fcoe_kwqe_destroy *) kwqe;
2429 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2430
2431 memset(&l5_data, 0, sizeof(l5_data));
2432 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY, cid,
2433 FCOE_CONNECTION_TYPE, &l5_data);
2434 return ret;
2435}
2436
2437static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2438 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002439{
2440 int i, work, ret;
2441 u32 opcode;
2442 struct kwqe *kwqe;
2443
2444 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2445 return -EAGAIN; /* bnx2 is down */
2446
2447 for (i = 0; i < num_wqes; ) {
2448 kwqe = wqes[i];
2449 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2450 work = 1;
2451
2452 switch (opcode) {
2453 case ISCSI_KWQE_OPCODE_INIT1:
2454 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2455 break;
2456 case ISCSI_KWQE_OPCODE_INIT2:
2457 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2458 break;
2459 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2460 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2461 num_wqes - i, &work);
2462 break;
2463 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2464 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2465 break;
2466 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2467 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2468 break;
2469 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2470 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2471 &work);
2472 break;
2473 case L4_KWQE_OPCODE_VALUE_CLOSE:
2474 ret = cnic_bnx2x_close(dev, kwqe);
2475 break;
2476 case L4_KWQE_OPCODE_VALUE_RESET:
2477 ret = cnic_bnx2x_reset(dev, kwqe);
2478 break;
2479 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2480 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2481 break;
2482 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2483 ret = cnic_bnx2x_update_pg(dev, kwqe);
2484 break;
2485 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2486 ret = 0;
2487 break;
2488 default:
2489 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002490 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2491 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002492 break;
2493 }
2494 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002495 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2496 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002497 i += work;
2498 }
2499 return 0;
2500}
2501
Michael Chane1928c82010-12-23 07:43:04 +00002502static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2503 struct kwqe *wqes[], u32 num_wqes)
2504{
2505 struct cnic_local *cp = dev->cnic_priv;
2506 int i, work, ret;
2507 u32 opcode;
2508 struct kwqe *kwqe;
2509
2510 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2511 return -EAGAIN; /* bnx2 is down */
2512
2513 if (BNX2X_CHIP_NUM(cp->chip_id) == BNX2X_CHIP_NUM_57710)
2514 return -EINVAL;
2515
2516 for (i = 0; i < num_wqes; ) {
2517 kwqe = wqes[i];
2518 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2519 work = 1;
2520
2521 switch (opcode) {
2522 case FCOE_KWQE_OPCODE_INIT1:
2523 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2524 num_wqes - i, &work);
2525 break;
2526 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2527 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2528 num_wqes - i, &work);
2529 break;
2530 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2531 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2532 break;
2533 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2534 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2535 break;
2536 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2537 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2538 break;
2539 case FCOE_KWQE_OPCODE_DESTROY:
2540 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2541 break;
2542 case FCOE_KWQE_OPCODE_STAT:
2543 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2544 break;
2545 default:
2546 ret = 0;
2547 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2548 opcode);
2549 break;
2550 }
2551 if (ret < 0)
2552 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2553 opcode);
2554 i += work;
2555 }
2556 return 0;
2557}
2558
2559static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2560 u32 num_wqes)
2561{
2562 int ret = -EINVAL;
2563 u32 layer_code;
2564
2565 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2566 return -EAGAIN; /* bnx2x is down */
2567
2568 if (!num_wqes)
2569 return 0;
2570
2571 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2572 switch (layer_code) {
2573 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2574 case KWQE_FLAGS_LAYER_MASK_L4:
2575 case KWQE_FLAGS_LAYER_MASK_L2:
2576 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2577 break;
2578
2579 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2580 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2581 break;
2582 }
2583 return ret;
2584}
2585
2586static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2587{
2588 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2589 return KCQE_FLAGS_LAYER_MASK_L4;
2590
2591 return opflag & KCQE_FLAGS_LAYER_MASK;
2592}
2593
Michael Chana4636962009-06-08 18:14:43 -07002594static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2595{
2596 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002597 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002598
2599 i = 0;
2600 j = 1;
2601 while (num_cqes) {
2602 struct cnic_ulp_ops *ulp_ops;
2603 int ulp_type;
2604 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002605 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002606
2607 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002608 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002609
2610 while (j < num_cqes) {
2611 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2612
Michael Chane1928c82010-12-23 07:43:04 +00002613 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002614 break;
2615
2616 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002617 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002618 j++;
2619 }
2620
2621 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2622 ulp_type = CNIC_ULP_RDMA;
2623 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2624 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002625 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2626 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002627 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2628 ulp_type = CNIC_ULP_L4;
2629 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2630 goto end;
2631 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002632 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2633 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002634 goto end;
2635 }
2636
2637 rcu_read_lock();
2638 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2639 if (likely(ulp_ops)) {
2640 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2641 cp->completed_kcq + i, j);
2642 }
2643 rcu_read_unlock();
2644end:
2645 num_cqes -= j;
2646 i += j;
2647 j = 1;
2648 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002649 if (unlikely(comp))
2650 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002651}
2652
2653static u16 cnic_bnx2_next_idx(u16 idx)
2654{
2655 return idx + 1;
2656}
2657
2658static u16 cnic_bnx2_hw_idx(u16 idx)
2659{
2660 return idx;
2661}
2662
Michael Chan71034ba2009-10-10 13:46:59 +00002663static u16 cnic_bnx2x_next_idx(u16 idx)
2664{
2665 idx++;
2666 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2667 idx++;
2668
2669 return idx;
2670}
2671
2672static u16 cnic_bnx2x_hw_idx(u16 idx)
2673{
2674 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2675 idx++;
2676 return idx;
2677}
2678
Michael Chan644b9d42010-06-24 14:58:40 +00002679static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002680{
2681 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002682 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002683 struct kcqe *kcqe;
2684 int kcqe_cnt = 0, last_cnt = 0;
2685
Michael Chan644b9d42010-06-24 14:58:40 +00002686 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002687 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002688 hw_prod = *info->hw_prod_idx_ptr;
2689 hw_prod = cp->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002690
2691 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002692 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002693 cp->completed_kcq[kcqe_cnt++] = kcqe;
2694 i = cp->next_idx(i);
2695 ri = i & MAX_KCQ_IDX;
2696 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2697 last_cnt = kcqe_cnt;
2698 last = i;
2699 }
2700 }
2701
Michael Chan644b9d42010-06-24 14:58:40 +00002702 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002703 return last_cnt;
2704}
2705
Michael Chan48f753d2010-05-18 11:32:53 +00002706static int cnic_l2_completion(struct cnic_local *cp)
2707{
2708 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002709 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002710 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002711 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002712 u32 cmd;
2713 int comp = 0;
2714
2715 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2716 return 0;
2717
2718 hw_cons = *cp->rx_cons_ptr;
2719 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2720 hw_cons++;
2721
2722 sw_cons = cp->rx_cons;
2723 while (sw_cons != hw_cons) {
2724 u8 cqe_fp_flags;
2725
2726 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2727 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2728 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2729 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2730 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2731 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2732 cmd == RAMROD_CMD_ID_ETH_HALT)
2733 comp++;
2734 }
2735 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2736 }
2737 return comp;
2738}
2739
Michael Chan86b53602009-10-10 13:46:57 +00002740static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002741{
Michael Chan541a7812010-10-06 03:17:22 +00002742 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002743 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002744
Michael Chan541a7812010-10-06 03:17:22 +00002745 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002746 return;
2747
Michael Chan541a7812010-10-06 03:17:22 +00002748 rx_cons = *cp->rx_cons_ptr;
2749 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002750 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002751 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2752 comp = cnic_l2_completion(cp);
2753
Michael Chana4636962009-06-08 18:14:43 -07002754 cp->tx_cons = tx_cons;
2755 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002756
Michael Chancd801532010-10-13 14:06:49 +00002757 if (cp->udev)
2758 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002759 }
Michael Chan48f753d2010-05-18 11:32:53 +00002760 if (comp)
2761 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002762}
2763
Michael Chanb177a5d52010-06-24 14:58:41 +00002764static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002765{
Michael Chana4636962009-06-08 18:14:43 -07002766 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002767 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002768 int kcqe_cnt;
2769
Michael Chana4636962009-06-08 18:14:43 -07002770 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2771
Michael Chan644b9d42010-06-24 14:58:40 +00002772 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002773
2774 service_kcqes(dev, kcqe_cnt);
2775
2776 /* Tell compiler that status_blk fields can change. */
2777 barrier();
Michael Chan644b9d42010-06-24 14:58:40 +00002778 if (status_idx != *cp->kcq1.status_idx_ptr) {
Michael Chanb177a5d52010-06-24 14:58:41 +00002779 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002780 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002781 } else
2782 break;
2783 }
2784
Michael Chan644b9d42010-06-24 14:58:40 +00002785 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002786
Michael Chan86b53602009-10-10 13:46:57 +00002787 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002788
Michael Chana4636962009-06-08 18:14:43 -07002789 return status_idx;
2790}
2791
Michael Chanb177a5d52010-06-24 14:58:41 +00002792static int cnic_service_bnx2(void *data, void *status_blk)
2793{
2794 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002795
Michael Chaneaaa6e92010-12-23 08:38:30 +00002796 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2797 struct status_block *sblk = status_blk;
2798
2799 return sblk->status_idx;
2800 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002801
2802 return cnic_service_bnx2_queues(dev);
2803}
2804
Michael Chana4636962009-06-08 18:14:43 -07002805static void cnic_service_bnx2_msix(unsigned long data)
2806{
2807 struct cnic_dev *dev = (struct cnic_dev *) data;
2808 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002809
Michael Chanb177a5d52010-06-24 14:58:41 +00002810 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002811
Michael Chana4636962009-06-08 18:14:43 -07002812 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2813 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2814}
2815
Michael Chan66fee9e2010-06-24 14:58:38 +00002816static void cnic_doirq(struct cnic_dev *dev)
2817{
2818 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002819
2820 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002821 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2822
Michael Chan66fee9e2010-06-24 14:58:38 +00002823 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002824 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002825
2826 tasklet_schedule(&cp->cnic_irq_task);
2827 }
2828}
2829
Michael Chana4636962009-06-08 18:14:43 -07002830static irqreturn_t cnic_irq(int irq, void *dev_instance)
2831{
2832 struct cnic_dev *dev = dev_instance;
2833 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002834
2835 if (cp->ack_int)
2836 cp->ack_int(dev);
2837
Michael Chan66fee9e2010-06-24 14:58:38 +00002838 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002839
2840 return IRQ_HANDLED;
2841}
2842
Michael Chan71034ba2009-10-10 13:46:59 +00002843static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2844 u16 index, u8 op, u8 update)
2845{
2846 struct cnic_local *cp = dev->cnic_priv;
2847 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2848 COMMAND_REG_INT_ACK);
2849 struct igu_ack_register igu_ack;
2850
2851 igu_ack.status_block_index = index;
2852 igu_ack.sb_id_and_flags =
2853 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2854 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2855 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2856 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2857
2858 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2859}
2860
Michael Chanee87a822010-10-13 14:06:51 +00002861static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2862 u16 index, u8 op, u8 update)
2863{
2864 struct igu_regular cmd_data;
2865 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2866
2867 cmd_data.sb_id_and_flags =
2868 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2869 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2870 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2871 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2872
2873
2874 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2875}
2876
Michael Chan71034ba2009-10-10 13:46:59 +00002877static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2878{
2879 struct cnic_local *cp = dev->cnic_priv;
2880
Dmitry Kravkov523224a2010-10-06 03:23:26 +00002881 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00002882 IGU_INT_DISABLE, 0);
2883}
2884
Michael Chanee87a822010-10-13 14:06:51 +00002885static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2886{
2887 struct cnic_local *cp = dev->cnic_priv;
2888
2889 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2890 IGU_INT_DISABLE, 0);
2891}
2892
Michael Chanb177a5d52010-06-24 14:58:41 +00002893static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002894{
Michael Chanb177a5d52010-06-24 14:58:41 +00002895 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002896 int kcqe_cnt;
2897
Michael Chanb177a5d52010-06-24 14:58:41 +00002898 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002899
2900 service_kcqes(dev, kcqe_cnt);
2901
2902 /* Tell compiler that sblk fields can change. */
2903 barrier();
Michael Chanb177a5d52010-06-24 14:58:41 +00002904 if (last_status == *info->status_idx_ptr)
Michael Chan71034ba2009-10-10 13:46:59 +00002905 break;
2906
Michael Chanb177a5d52010-06-24 14:58:41 +00002907 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002908 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002909 return last_status;
2910}
2911
2912static void cnic_service_bnx2x_bh(unsigned long data)
2913{
2914 struct cnic_dev *dev = (struct cnic_dev *) data;
2915 struct cnic_local *cp = dev->cnic_priv;
2916 u32 status_idx;
2917
2918 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2919 return;
2920
2921 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002922
Michael Chan644b9d42010-06-24 14:58:40 +00002923 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00002924
2925 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
2926 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
2927
2928 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
2929 MAX_KCQ_IDX);
2930
Michael Chanee87a822010-10-13 14:06:51 +00002931 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2932 status_idx, IGU_INT_ENABLE, 1);
Michael Chane21ba412010-12-23 07:43:03 +00002933 } else {
Michael Chanee87a822010-10-13 14:06:51 +00002934 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2935 status_idx, IGU_INT_ENABLE, 1);
Michael Chane21ba412010-12-23 07:43:03 +00002936 }
Michael Chan71034ba2009-10-10 13:46:59 +00002937}
2938
2939static int cnic_service_bnx2x(void *data, void *status_blk)
2940{
2941 struct cnic_dev *dev = data;
2942 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002943
Michael Chan66fee9e2010-06-24 14:58:38 +00002944 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2945 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002946
Michael Chan66fee9e2010-06-24 14:58:38 +00002947 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002948
2949 return 0;
2950}
2951
Michael Chana4636962009-06-08 18:14:43 -07002952static void cnic_ulp_stop(struct cnic_dev *dev)
2953{
2954 struct cnic_local *cp = dev->cnic_priv;
2955 int if_type;
2956
Michael Chancd801532010-10-13 14:06:49 +00002957 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
Michael Chan6d7760a2009-07-27 11:25:58 -07002958
Michael Chana4636962009-06-08 18:14:43 -07002959 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2960 struct cnic_ulp_ops *ulp_ops;
2961
Michael Chan681dbd72009-08-14 15:49:46 +00002962 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00002963 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
2964 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00002965 if (!ulp_ops) {
2966 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002967 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002968 }
2969 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2970 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002971
2972 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2973 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002974
2975 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002976 }
Michael Chana4636962009-06-08 18:14:43 -07002977}
2978
2979static void cnic_ulp_start(struct cnic_dev *dev)
2980{
2981 struct cnic_local *cp = dev->cnic_priv;
2982 int if_type;
2983
Michael Chana4636962009-06-08 18:14:43 -07002984 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2985 struct cnic_ulp_ops *ulp_ops;
2986
Michael Chan681dbd72009-08-14 15:49:46 +00002987 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00002988 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
2989 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00002990 if (!ulp_ops || !ulp_ops->cnic_start) {
2991 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002992 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002993 }
2994 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2995 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002996
2997 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2998 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002999
3000 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003001 }
Michael Chana4636962009-06-08 18:14:43 -07003002}
3003
3004static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3005{
3006 struct cnic_dev *dev = data;
3007
3008 switch (info->cmd) {
3009 case CNIC_CTL_STOP_CMD:
3010 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003011
3012 cnic_ulp_stop(dev);
3013 cnic_stop_hw(dev);
3014
Michael Chana4636962009-06-08 18:14:43 -07003015 cnic_put(dev);
3016 break;
3017 case CNIC_CTL_START_CMD:
3018 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003019
3020 if (!cnic_start_hw(dev))
3021 cnic_ulp_start(dev);
3022
Michael Chana4636962009-06-08 18:14:43 -07003023 cnic_put(dev);
3024 break;
Michael Chan71034ba2009-10-10 13:46:59 +00003025 case CNIC_CTL_COMPLETION_CMD: {
3026 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
3027 u32 l5_cid;
3028 struct cnic_local *cp = dev->cnic_priv;
3029
3030 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3031 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3032
3033 ctx->wait_cond = 1;
3034 wake_up(&ctx->waitq);
3035 }
3036 break;
3037 }
Michael Chana4636962009-06-08 18:14:43 -07003038 default:
3039 return -EINVAL;
3040 }
3041 return 0;
3042}
3043
3044static void cnic_ulp_init(struct cnic_dev *dev)
3045{
3046 int i;
3047 struct cnic_local *cp = dev->cnic_priv;
3048
Michael Chana4636962009-06-08 18:14:43 -07003049 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3050 struct cnic_ulp_ops *ulp_ops;
3051
Michael Chan7fc1ece2009-08-14 15:49:47 +00003052 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003053 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003054 if (!ulp_ops || !ulp_ops->cnic_init) {
3055 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003056 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003057 }
3058 ulp_get(ulp_ops);
3059 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003060
3061 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3062 ulp_ops->cnic_init(dev);
3063
Michael Chan7fc1ece2009-08-14 15:49:47 +00003064 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003065 }
Michael Chana4636962009-06-08 18:14:43 -07003066}
3067
3068static void cnic_ulp_exit(struct cnic_dev *dev)
3069{
3070 int i;
3071 struct cnic_local *cp = dev->cnic_priv;
3072
Michael Chana4636962009-06-08 18:14:43 -07003073 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3074 struct cnic_ulp_ops *ulp_ops;
3075
Michael Chan7fc1ece2009-08-14 15:49:47 +00003076 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003077 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003078 if (!ulp_ops || !ulp_ops->cnic_exit) {
3079 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003080 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003081 }
3082 ulp_get(ulp_ops);
3083 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003084
3085 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3086 ulp_ops->cnic_exit(dev);
3087
Michael Chan7fc1ece2009-08-14 15:49:47 +00003088 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003089 }
Michael Chana4636962009-06-08 18:14:43 -07003090}
3091
3092static int cnic_cm_offload_pg(struct cnic_sock *csk)
3093{
3094 struct cnic_dev *dev = csk->dev;
3095 struct l4_kwq_offload_pg *l4kwqe;
3096 struct kwqe *wqes[1];
3097
3098 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3099 memset(l4kwqe, 0, sizeof(*l4kwqe));
3100 wqes[0] = (struct kwqe *) l4kwqe;
3101
3102 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3103 l4kwqe->flags =
3104 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3105 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3106
3107 l4kwqe->da0 = csk->ha[0];
3108 l4kwqe->da1 = csk->ha[1];
3109 l4kwqe->da2 = csk->ha[2];
3110 l4kwqe->da3 = csk->ha[3];
3111 l4kwqe->da4 = csk->ha[4];
3112 l4kwqe->da5 = csk->ha[5];
3113
3114 l4kwqe->sa0 = dev->mac_addr[0];
3115 l4kwqe->sa1 = dev->mac_addr[1];
3116 l4kwqe->sa2 = dev->mac_addr[2];
3117 l4kwqe->sa3 = dev->mac_addr[3];
3118 l4kwqe->sa4 = dev->mac_addr[4];
3119 l4kwqe->sa5 = dev->mac_addr[5];
3120
3121 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003122 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003123 l4kwqe->host_opaque = csk->l5_cid;
3124
3125 if (csk->vlan_id) {
3126 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3127 l4kwqe->vlan_tag = csk->vlan_id;
3128 l4kwqe->l2hdr_nbytes += 4;
3129 }
3130
3131 return dev->submit_kwqes(dev, wqes, 1);
3132}
3133
3134static int cnic_cm_update_pg(struct cnic_sock *csk)
3135{
3136 struct cnic_dev *dev = csk->dev;
3137 struct l4_kwq_update_pg *l4kwqe;
3138 struct kwqe *wqes[1];
3139
3140 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3141 memset(l4kwqe, 0, sizeof(*l4kwqe));
3142 wqes[0] = (struct kwqe *) l4kwqe;
3143
3144 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3145 l4kwqe->flags =
3146 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3147 l4kwqe->pg_cid = csk->pg_cid;
3148
3149 l4kwqe->da0 = csk->ha[0];
3150 l4kwqe->da1 = csk->ha[1];
3151 l4kwqe->da2 = csk->ha[2];
3152 l4kwqe->da3 = csk->ha[3];
3153 l4kwqe->da4 = csk->ha[4];
3154 l4kwqe->da5 = csk->ha[5];
3155
3156 l4kwqe->pg_host_opaque = csk->l5_cid;
3157 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3158
3159 return dev->submit_kwqes(dev, wqes, 1);
3160}
3161
3162static int cnic_cm_upload_pg(struct cnic_sock *csk)
3163{
3164 struct cnic_dev *dev = csk->dev;
3165 struct l4_kwq_upload *l4kwqe;
3166 struct kwqe *wqes[1];
3167
3168 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3169 memset(l4kwqe, 0, sizeof(*l4kwqe));
3170 wqes[0] = (struct kwqe *) l4kwqe;
3171
3172 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3173 l4kwqe->flags =
3174 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3175 l4kwqe->cid = csk->pg_cid;
3176
3177 return dev->submit_kwqes(dev, wqes, 1);
3178}
3179
3180static int cnic_cm_conn_req(struct cnic_sock *csk)
3181{
3182 struct cnic_dev *dev = csk->dev;
3183 struct l4_kwq_connect_req1 *l4kwqe1;
3184 struct l4_kwq_connect_req2 *l4kwqe2;
3185 struct l4_kwq_connect_req3 *l4kwqe3;
3186 struct kwqe *wqes[3];
3187 u8 tcp_flags = 0;
3188 int num_wqes = 2;
3189
3190 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3191 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3192 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3193 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3194 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3195 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3196
3197 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3198 l4kwqe3->flags =
3199 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3200 l4kwqe3->ka_timeout = csk->ka_timeout;
3201 l4kwqe3->ka_interval = csk->ka_interval;
3202 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3203 l4kwqe3->tos = csk->tos;
3204 l4kwqe3->ttl = csk->ttl;
3205 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3206 l4kwqe3->pmtu = csk->mtu;
3207 l4kwqe3->rcv_buf = csk->rcv_buf;
3208 l4kwqe3->snd_buf = csk->snd_buf;
3209 l4kwqe3->seed = csk->seed;
3210
3211 wqes[0] = (struct kwqe *) l4kwqe1;
3212 if (test_bit(SK_F_IPV6, &csk->flags)) {
3213 wqes[1] = (struct kwqe *) l4kwqe2;
3214 wqes[2] = (struct kwqe *) l4kwqe3;
3215 num_wqes = 3;
3216
3217 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3218 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3219 l4kwqe2->flags =
3220 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3221 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3222 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3223 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3224 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3225 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3226 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3227 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3228 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3229 sizeof(struct tcphdr);
3230 } else {
3231 wqes[1] = (struct kwqe *) l4kwqe3;
3232 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3233 sizeof(struct tcphdr);
3234 }
3235
3236 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3237 l4kwqe1->flags =
3238 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3239 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3240 l4kwqe1->cid = csk->cid;
3241 l4kwqe1->pg_cid = csk->pg_cid;
3242 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3243 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3244 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3245 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3246 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3247 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3248 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3249 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3250 if (csk->tcp_flags & SK_TCP_NAGLE)
3251 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3252 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3253 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3254 if (csk->tcp_flags & SK_TCP_SACK)
3255 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3256 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3257 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3258
3259 l4kwqe1->tcp_flags = tcp_flags;
3260
3261 return dev->submit_kwqes(dev, wqes, num_wqes);
3262}
3263
3264static int cnic_cm_close_req(struct cnic_sock *csk)
3265{
3266 struct cnic_dev *dev = csk->dev;
3267 struct l4_kwq_close_req *l4kwqe;
3268 struct kwqe *wqes[1];
3269
3270 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3271 memset(l4kwqe, 0, sizeof(*l4kwqe));
3272 wqes[0] = (struct kwqe *) l4kwqe;
3273
3274 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3275 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3276 l4kwqe->cid = csk->cid;
3277
3278 return dev->submit_kwqes(dev, wqes, 1);
3279}
3280
3281static int cnic_cm_abort_req(struct cnic_sock *csk)
3282{
3283 struct cnic_dev *dev = csk->dev;
3284 struct l4_kwq_reset_req *l4kwqe;
3285 struct kwqe *wqes[1];
3286
3287 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3288 memset(l4kwqe, 0, sizeof(*l4kwqe));
3289 wqes[0] = (struct kwqe *) l4kwqe;
3290
3291 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3292 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3293 l4kwqe->cid = csk->cid;
3294
3295 return dev->submit_kwqes(dev, wqes, 1);
3296}
3297
3298static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3299 u32 l5_cid, struct cnic_sock **csk, void *context)
3300{
3301 struct cnic_local *cp = dev->cnic_priv;
3302 struct cnic_sock *csk1;
3303
3304 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3305 return -EINVAL;
3306
Michael Chanfdf24082010-10-13 14:06:47 +00003307 if (cp->ctx_tbl) {
3308 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3309
3310 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3311 return -EAGAIN;
3312 }
3313
Michael Chana4636962009-06-08 18:14:43 -07003314 csk1 = &cp->csk_tbl[l5_cid];
3315 if (atomic_read(&csk1->ref_count))
3316 return -EAGAIN;
3317
3318 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3319 return -EBUSY;
3320
3321 csk1->dev = dev;
3322 csk1->cid = cid;
3323 csk1->l5_cid = l5_cid;
3324 csk1->ulp_type = ulp_type;
3325 csk1->context = context;
3326
3327 csk1->ka_timeout = DEF_KA_TIMEOUT;
3328 csk1->ka_interval = DEF_KA_INTERVAL;
3329 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3330 csk1->tos = DEF_TOS;
3331 csk1->ttl = DEF_TTL;
3332 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3333 csk1->rcv_buf = DEF_RCV_BUF;
3334 csk1->snd_buf = DEF_SND_BUF;
3335 csk1->seed = DEF_SEED;
3336
3337 *csk = csk1;
3338 return 0;
3339}
3340
3341static void cnic_cm_cleanup(struct cnic_sock *csk)
3342{
3343 if (csk->src_port) {
3344 struct cnic_dev *dev = csk->dev;
3345 struct cnic_local *cp = dev->cnic_priv;
3346
Michael Chan9b093362010-12-23 07:42:56 +00003347 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003348 csk->src_port = 0;
3349 }
3350}
3351
3352static void cnic_close_conn(struct cnic_sock *csk)
3353{
3354 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3355 cnic_cm_upload_pg(csk);
3356 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3357 }
3358 cnic_cm_cleanup(csk);
3359}
3360
3361static int cnic_cm_destroy(struct cnic_sock *csk)
3362{
3363 if (!cnic_in_use(csk))
3364 return -EINVAL;
3365
3366 csk_hold(csk);
3367 clear_bit(SK_F_INUSE, &csk->flags);
3368 smp_mb__after_clear_bit();
3369 while (atomic_read(&csk->ref_count) != 1)
3370 msleep(1);
3371 cnic_cm_cleanup(csk);
3372
3373 csk->flags = 0;
3374 csk_put(csk);
3375 return 0;
3376}
3377
3378static inline u16 cnic_get_vlan(struct net_device *dev,
3379 struct net_device **vlan_dev)
3380{
3381 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3382 *vlan_dev = vlan_dev_real_dev(dev);
3383 return vlan_dev_vlan_id(dev);
3384 }
3385 *vlan_dev = dev;
3386 return 0;
3387}
3388
3389static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3390 struct dst_entry **dst)
3391{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003392#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003393 struct flowi fl;
3394 int err;
3395 struct rtable *rt;
3396
3397 memset(&fl, 0, sizeof(fl));
3398 fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
3399
David S. Millerb23dd4f2011-03-02 14:31:35 -08003400 rt = ip_route_output_key(&init_net, &fl);
3401 err = 0;
3402 if (!IS_ERR(rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07003403 *dst = &rt->dst;
David S. Millerb23dd4f2011-03-02 14:31:35 -08003404 else
3405 err = PTR_ERR(rt);
Michael Chana4636962009-06-08 18:14:43 -07003406 return err;
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003407#else
3408 return -ENETUNREACH;
3409#endif
Michael Chana4636962009-06-08 18:14:43 -07003410}
3411
3412static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3413 struct dst_entry **dst)
3414{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003415#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
Michael Chana4636962009-06-08 18:14:43 -07003416 struct flowi fl;
3417
3418 memset(&fl, 0, sizeof(fl));
3419 ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
3420 if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
3421 fl.oif = dst_addr->sin6_scope_id;
3422
3423 *dst = ip6_route_output(&init_net, NULL, &fl);
3424 if (*dst)
3425 return 0;
3426#endif
3427
3428 return -ENETUNREACH;
3429}
3430
3431static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3432 int ulp_type)
3433{
3434 struct cnic_dev *dev = NULL;
3435 struct dst_entry *dst;
3436 struct net_device *netdev = NULL;
3437 int err = -ENETUNREACH;
3438
3439 if (dst_addr->sin_family == AF_INET)
3440 err = cnic_get_v4_route(dst_addr, &dst);
3441 else if (dst_addr->sin_family == AF_INET6) {
3442 struct sockaddr_in6 *dst_addr6 =
3443 (struct sockaddr_in6 *) dst_addr;
3444
3445 err = cnic_get_v6_route(dst_addr6, &dst);
3446 } else
3447 return NULL;
3448
3449 if (err)
3450 return NULL;
3451
3452 if (!dst->dev)
3453 goto done;
3454
3455 cnic_get_vlan(dst->dev, &netdev);
3456
3457 dev = cnic_from_netdev(netdev);
3458
3459done:
3460 dst_release(dst);
3461 if (dev)
3462 cnic_put(dev);
3463 return dev;
3464}
3465
3466static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3467{
3468 struct cnic_dev *dev = csk->dev;
3469 struct cnic_local *cp = dev->cnic_priv;
3470
3471 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3472}
3473
3474static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3475{
3476 struct cnic_dev *dev = csk->dev;
3477 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003478 int is_v6, rc = 0;
3479 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003480 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003481 __be16 local_port;
3482 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003483
3484 if (saddr->local.v6.sin6_family == AF_INET6 &&
3485 saddr->remote.v6.sin6_family == AF_INET6)
3486 is_v6 = 1;
3487 else if (saddr->local.v4.sin_family == AF_INET &&
3488 saddr->remote.v4.sin_family == AF_INET)
3489 is_v6 = 0;
3490 else
3491 return -EINVAL;
3492
3493 clear_bit(SK_F_IPV6, &csk->flags);
3494
3495 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003496 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003497 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003498
3499 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3500 sizeof(struct in6_addr));
3501 csk->dst_port = saddr->remote.v6.sin6_port;
3502 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003503
3504 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003505 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003506
3507 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3508 csk->dst_port = saddr->remote.v4.sin_port;
3509 local_port = saddr->local.v4.sin_port;
3510 }
3511
Michael Chanc76284a2010-02-24 14:42:07 +00003512 csk->vlan_id = 0;
3513 csk->mtu = dev->netdev->mtu;
3514 if (dst && dst->dev) {
3515 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3516 if (realdev == dev->netdev) {
3517 csk->vlan_id = vlan;
3518 csk->mtu = dst_mtu(dst);
3519 }
3520 }
Michael Chana4636962009-06-08 18:14:43 -07003521
Michael Chan9b093362010-12-23 07:42:56 +00003522 port_id = be16_to_cpu(local_port);
3523 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3524 port_id < CNIC_LOCAL_PORT_MAX) {
3525 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3526 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003527 } else
Michael Chan9b093362010-12-23 07:42:56 +00003528 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003529
Michael Chan9b093362010-12-23 07:42:56 +00003530 if (!port_id) {
3531 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3532 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003533 rc = -ENOMEM;
3534 goto err_out;
3535 }
Michael Chan9b093362010-12-23 07:42:56 +00003536 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003537 }
3538 csk->src_port = local_port;
3539
Michael Chana4636962009-06-08 18:14:43 -07003540err_out:
3541 dst_release(dst);
3542 return rc;
3543}
3544
3545static void cnic_init_csk_state(struct cnic_sock *csk)
3546{
3547 csk->state = 0;
3548 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3549 clear_bit(SK_F_CLOSING, &csk->flags);
3550}
3551
3552static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3553{
3554 int err = 0;
3555
3556 if (!cnic_in_use(csk))
3557 return -EINVAL;
3558
3559 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3560 return -EINVAL;
3561
3562 cnic_init_csk_state(csk);
3563
3564 err = cnic_get_route(csk, saddr);
3565 if (err)
3566 goto err_out;
3567
3568 err = cnic_resolve_addr(csk, saddr);
3569 if (!err)
3570 return 0;
3571
3572err_out:
3573 clear_bit(SK_F_CONNECT_START, &csk->flags);
3574 return err;
3575}
3576
3577static int cnic_cm_abort(struct cnic_sock *csk)
3578{
3579 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003580 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003581
3582 if (!cnic_in_use(csk))
3583 return -EINVAL;
3584
3585 if (cnic_abort_prep(csk))
3586 return cnic_cm_abort_req(csk);
3587
3588 /* Getting here means that we haven't started connect, or
3589 * connect was not successful.
3590 */
3591
Michael Chana4636962009-06-08 18:14:43 -07003592 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003593 if (csk->state != opcode)
3594 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003595
3596 return 0;
3597}
3598
3599static int cnic_cm_close(struct cnic_sock *csk)
3600{
3601 if (!cnic_in_use(csk))
3602 return -EINVAL;
3603
3604 if (cnic_close_prep(csk)) {
3605 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3606 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003607 } else {
3608 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003609 }
3610 return 0;
3611}
3612
3613static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3614 u8 opcode)
3615{
3616 struct cnic_ulp_ops *ulp_ops;
3617 int ulp_type = csk->ulp_type;
3618
3619 rcu_read_lock();
3620 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3621 if (ulp_ops) {
3622 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3623 ulp_ops->cm_connect_complete(csk);
3624 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3625 ulp_ops->cm_close_complete(csk);
3626 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3627 ulp_ops->cm_remote_abort(csk);
3628 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3629 ulp_ops->cm_abort_complete(csk);
3630 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3631 ulp_ops->cm_remote_close(csk);
3632 }
3633 rcu_read_unlock();
3634}
3635
3636static int cnic_cm_set_pg(struct cnic_sock *csk)
3637{
3638 if (cnic_offld_prep(csk)) {
3639 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3640 cnic_cm_update_pg(csk);
3641 else
3642 cnic_cm_offload_pg(csk);
3643 }
3644 return 0;
3645}
3646
3647static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3648{
3649 struct cnic_local *cp = dev->cnic_priv;
3650 u32 l5_cid = kcqe->pg_host_opaque;
3651 u8 opcode = kcqe->op_code;
3652 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3653
3654 csk_hold(csk);
3655 if (!cnic_in_use(csk))
3656 goto done;
3657
3658 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3659 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3660 goto done;
3661 }
Eddie Waia9736c02010-02-24 14:42:04 +00003662 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3663 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3664 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3665 cnic_cm_upcall(cp, csk,
3666 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3667 goto done;
3668 }
3669
Michael Chana4636962009-06-08 18:14:43 -07003670 csk->pg_cid = kcqe->pg_cid;
3671 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3672 cnic_cm_conn_req(csk);
3673
3674done:
3675 csk_put(csk);
3676}
3677
Michael Chane1928c82010-12-23 07:43:04 +00003678static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3679{
3680 struct cnic_local *cp = dev->cnic_priv;
3681 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3682 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3683 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3684
3685 ctx->timestamp = jiffies;
3686 ctx->wait_cond = 1;
3687 wake_up(&ctx->waitq);
3688}
3689
Michael Chana4636962009-06-08 18:14:43 -07003690static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3691{
3692 struct cnic_local *cp = dev->cnic_priv;
3693 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3694 u8 opcode = l4kcqe->op_code;
3695 u32 l5_cid;
3696 struct cnic_sock *csk;
3697
Michael Chane1928c82010-12-23 07:43:04 +00003698 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3699 cnic_process_fcoe_term_conn(dev, kcqe);
3700 return;
3701 }
Michael Chana4636962009-06-08 18:14:43 -07003702 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3703 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3704 cnic_cm_process_offld_pg(dev, l4kcqe);
3705 return;
3706 }
3707
3708 l5_cid = l4kcqe->conn_id;
3709 if (opcode & 0x80)
3710 l5_cid = l4kcqe->cid;
3711 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3712 return;
3713
3714 csk = &cp->csk_tbl[l5_cid];
3715 csk_hold(csk);
3716
3717 if (!cnic_in_use(csk)) {
3718 csk_put(csk);
3719 return;
3720 }
3721
3722 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003723 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3724 if (l4kcqe->status != 0) {
3725 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3726 cnic_cm_upcall(cp, csk,
3727 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3728 }
3729 break;
Michael Chana4636962009-06-08 18:14:43 -07003730 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3731 if (l4kcqe->status == 0)
3732 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3733
3734 smp_mb__before_clear_bit();
3735 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3736 cnic_cm_upcall(cp, csk, opcode);
3737 break;
3738
3739 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003740 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3741 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003742 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3743 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003744 cp->close_conn(csk, opcode);
3745 break;
3746
3747 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
3748 cnic_cm_upcall(cp, csk, opcode);
3749 break;
3750 }
3751 csk_put(csk);
3752}
3753
3754static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3755{
3756 struct cnic_dev *dev = data;
3757 int i;
3758
3759 for (i = 0; i < num; i++)
3760 cnic_cm_process_kcqe(dev, kcqe[i]);
3761}
3762
3763static struct cnic_ulp_ops cm_ulp_ops = {
3764 .indicate_kcqes = cnic_cm_indicate_kcqe,
3765};
3766
3767static void cnic_cm_free_mem(struct cnic_dev *dev)
3768{
3769 struct cnic_local *cp = dev->cnic_priv;
3770
3771 kfree(cp->csk_tbl);
3772 cp->csk_tbl = NULL;
3773 cnic_free_id_tbl(&cp->csk_port_tbl);
3774}
3775
3776static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3777{
3778 struct cnic_local *cp = dev->cnic_priv;
3779
3780 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3781 GFP_KERNEL);
3782 if (!cp->csk_tbl)
3783 return -ENOMEM;
3784
3785 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
3786 CNIC_LOCAL_PORT_MIN)) {
3787 cnic_cm_free_mem(dev);
3788 return -ENOMEM;
3789 }
3790 return 0;
3791}
3792
3793static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3794{
Michael Chan943189f2010-06-15 08:57:02 +00003795 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3796 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3797 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3798 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003799 }
Michael Chan943189f2010-06-15 08:57:02 +00003800
3801 /* 1. If event opcode matches the expected event in csk->state
3802 * 2. If the expected event is CLOSE_COMP, we accept any event
Michael Chan7b34a462010-06-15 08:57:03 +00003803 * 3. If the expected event is 0, meaning the connection was never
3804 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003805 */
Michael Chan7b34a462010-06-15 08:57:03 +00003806 if (opcode == csk->state || csk->state == 0 ||
3807 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP) {
3808 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3809 if (csk->state == 0)
3810 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003811 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003812 }
Michael Chana4636962009-06-08 18:14:43 -07003813 }
3814 return 0;
3815}
3816
3817static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3818{
3819 struct cnic_dev *dev = csk->dev;
3820 struct cnic_local *cp = dev->cnic_priv;
3821
Michael Chana1e621b2010-06-15 08:57:01 +00003822 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3823 cnic_cm_upcall(cp, csk, opcode);
3824 return;
3825 }
3826
Michael Chana4636962009-06-08 18:14:43 -07003827 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003828 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003829 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003830 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003831}
3832
3833static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3834{
3835}
3836
3837static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3838{
3839 u32 seed;
3840
3841 get_random_bytes(&seed, 4);
3842 cnic_ctx_wr(dev, 45, 0, seed);
3843 return 0;
3844}
3845
Michael Chan71034ba2009-10-10 13:46:59 +00003846static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3847{
3848 struct cnic_dev *dev = csk->dev;
3849 struct cnic_local *cp = dev->cnic_priv;
3850 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3851 union l5cm_specific_data l5_data;
3852 u32 cmd = 0;
3853 int close_complete = 0;
3854
3855 switch (opcode) {
3856 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3857 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3858 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003859 if (cnic_ready_to_close(csk, opcode)) {
3860 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3861 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3862 else
3863 close_complete = 1;
3864 }
Michael Chan71034ba2009-10-10 13:46:59 +00003865 break;
3866 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3867 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3868 break;
3869 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3870 close_complete = 1;
3871 break;
3872 }
3873 if (cmd) {
3874 memset(&l5_data, 0, sizeof(l5_data));
3875
3876 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3877 &l5_data);
3878 } else if (close_complete) {
3879 ctx->timestamp = jiffies;
3880 cnic_close_conn(csk);
3881 cnic_cm_upcall(cp, csk, csk->state);
3882 }
3883}
3884
3885static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3886{
Michael Chanfdf24082010-10-13 14:06:47 +00003887 struct cnic_local *cp = dev->cnic_priv;
3888 int i;
3889
3890 if (!cp->ctx_tbl)
3891 return;
3892
3893 if (!netif_running(dev->netdev))
3894 return;
3895
3896 for (i = 0; i < cp->max_cid_space; i++) {
3897 struct cnic_context *ctx = &cp->ctx_tbl[i];
3898
3899 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3900 msleep(10);
3901
3902 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3903 netdev_warn(dev->netdev, "CID %x not deleted\n",
3904 ctx->cid);
3905 }
3906
3907 cancel_delayed_work(&cp->delete_task);
3908 flush_workqueue(cnic_wq);
3909
3910 if (atomic_read(&cp->iscsi_conn) != 0)
3911 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3912 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00003913}
3914
3915static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3916{
3917 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003918 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003919 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003920
3921 cnic_init_bnx2x_mac(dev);
3922 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3923
3924 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003925 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003926
3927 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003928 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003929 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003930 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00003931 DEF_MAX_DA_COUNT);
3932
3933 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003934 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003935 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003936 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003937 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003938 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003939 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003940 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003941
Michael Chan14203982010-10-06 03:16:06 +00003942 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003943 DEF_MAX_CWND);
3944 return 0;
3945}
3946
Michael Chanfdf24082010-10-13 14:06:47 +00003947static void cnic_delete_task(struct work_struct *work)
3948{
3949 struct cnic_local *cp;
3950 struct cnic_dev *dev;
3951 u32 i;
3952 int need_resched = 0;
3953
3954 cp = container_of(work, struct cnic_local, delete_task.work);
3955 dev = cp->dev;
3956
3957 for (i = 0; i < cp->max_cid_space; i++) {
3958 struct cnic_context *ctx = &cp->ctx_tbl[i];
3959
3960 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
3961 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3962 continue;
3963
3964 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
3965 need_resched = 1;
3966 continue;
3967 }
3968
3969 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3970 continue;
3971
3972 cnic_bnx2x_destroy_ramrod(dev, i);
3973
3974 cnic_free_bnx2x_conn_resc(dev, i);
3975 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
3976 atomic_dec(&cp->iscsi_conn);
3977
3978 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
3979 }
3980
3981 if (need_resched)
3982 queue_delayed_work(cnic_wq, &cp->delete_task,
3983 msecs_to_jiffies(10));
3984
3985}
3986
Michael Chana4636962009-06-08 18:14:43 -07003987static int cnic_cm_open(struct cnic_dev *dev)
3988{
3989 struct cnic_local *cp = dev->cnic_priv;
3990 int err;
3991
3992 err = cnic_cm_alloc_mem(dev);
3993 if (err)
3994 return err;
3995
3996 err = cp->start_cm(dev);
3997
3998 if (err)
3999 goto err_out;
4000
Michael Chanfdf24082010-10-13 14:06:47 +00004001 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4002
Michael Chana4636962009-06-08 18:14:43 -07004003 dev->cm_create = cnic_cm_create;
4004 dev->cm_destroy = cnic_cm_destroy;
4005 dev->cm_connect = cnic_cm_connect;
4006 dev->cm_abort = cnic_cm_abort;
4007 dev->cm_close = cnic_cm_close;
4008 dev->cm_select_dev = cnic_cm_select_dev;
4009
4010 cp->ulp_handle[CNIC_ULP_L4] = dev;
4011 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4012 return 0;
4013
4014err_out:
4015 cnic_cm_free_mem(dev);
4016 return err;
4017}
4018
4019static int cnic_cm_shutdown(struct cnic_dev *dev)
4020{
4021 struct cnic_local *cp = dev->cnic_priv;
4022 int i;
4023
4024 cp->stop_cm(dev);
4025
4026 if (!cp->csk_tbl)
4027 return 0;
4028
4029 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4030 struct cnic_sock *csk = &cp->csk_tbl[i];
4031
4032 clear_bit(SK_F_INUSE, &csk->flags);
4033 cnic_cm_cleanup(csk);
4034 }
4035 cnic_cm_free_mem(dev);
4036
4037 return 0;
4038}
4039
4040static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4041{
Michael Chana4636962009-06-08 18:14:43 -07004042 u32 cid_addr;
4043 int i;
4044
Michael Chana4636962009-06-08 18:14:43 -07004045 cid_addr = GET_CID_ADDR(cid);
4046
4047 for (i = 0; i < CTX_SIZE; i += 4)
4048 cnic_ctx_wr(dev, cid_addr, i, 0);
4049}
4050
4051static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4052{
4053 struct cnic_local *cp = dev->cnic_priv;
4054 int ret = 0, i;
4055 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4056
4057 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4058 return 0;
4059
4060 for (i = 0; i < cp->ctx_blks; i++) {
4061 int j;
4062 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4063 u32 val;
4064
4065 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4066
4067 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4068 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4069 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4070 (u64) cp->ctx_arr[i].mapping >> 32);
4071 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4072 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4073 for (j = 0; j < 10; j++) {
4074
4075 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4076 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4077 break;
4078 udelay(5);
4079 }
4080 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4081 ret = -EBUSY;
4082 break;
4083 }
4084 }
4085 return ret;
4086}
4087
4088static void cnic_free_irq(struct cnic_dev *dev)
4089{
4090 struct cnic_local *cp = dev->cnic_priv;
4091 struct cnic_eth_dev *ethdev = cp->ethdev;
4092
4093 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4094 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004095 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004096 free_irq(ethdev->irq_arr[0].vector, dev);
4097 }
4098}
4099
Michael Chan6e0dc642010-10-13 14:06:44 +00004100static int cnic_request_irq(struct cnic_dev *dev)
4101{
4102 struct cnic_local *cp = dev->cnic_priv;
4103 struct cnic_eth_dev *ethdev = cp->ethdev;
4104 int err;
4105
4106 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4107 if (err)
4108 tasklet_disable(&cp->cnic_irq_task);
4109
4110 return err;
4111}
4112
Michael Chana4636962009-06-08 18:14:43 -07004113static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4114{
4115 struct cnic_local *cp = dev->cnic_priv;
4116 struct cnic_eth_dev *ethdev = cp->ethdev;
4117
4118 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4119 int err, i = 0;
4120 int sblk_num = cp->status_blk_num;
4121 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4122 BNX2_HC_SB_CONFIG_1;
4123
4124 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4125
4126 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4127 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4128 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4129
Michael Chana4dde3a2010-02-24 14:42:08 +00004130 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004131 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004132 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004133 err = cnic_request_irq(dev);
4134 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004135 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004136
Michael Chana4dde3a2010-02-24 14:42:08 +00004137 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004138 i < 10) {
4139 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4140 1 << (11 + sblk_num));
4141 udelay(10);
4142 i++;
4143 barrier();
4144 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004145 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004146 cnic_free_irq(dev);
4147 goto failed;
4148 }
4149
4150 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004151 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004152 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4153 int i = 0;
4154
4155 while (sblk->status_completion_producer_index && i < 10) {
4156 CNIC_WR(dev, BNX2_HC_COMMAND,
4157 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4158 udelay(10);
4159 i++;
4160 barrier();
4161 }
4162 if (sblk->status_completion_producer_index)
4163 goto failed;
4164
4165 }
4166 return 0;
4167
4168failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004169 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004170 return -EBUSY;
4171}
4172
4173static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4174{
4175 struct cnic_local *cp = dev->cnic_priv;
4176 struct cnic_eth_dev *ethdev = cp->ethdev;
4177
4178 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4179 return;
4180
4181 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4182 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4183}
4184
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00004185static void cnic_get_bnx2_iscsi_info(struct cnic_dev *dev)
4186{
4187 u32 max_conn;
4188
4189 max_conn = cnic_reg_rd_ind(dev, BNX2_FW_MAX_ISCSI_CONN);
4190 dev->max_iscsi_conn = max_conn;
4191}
4192
Michael Chana4636962009-06-08 18:14:43 -07004193static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4194{
4195 struct cnic_local *cp = dev->cnic_priv;
4196 struct cnic_eth_dev *ethdev = cp->ethdev;
4197
4198 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4199 return;
4200
4201 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4202 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4203 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4204 synchronize_irq(ethdev->irq_arr[0].vector);
4205}
4206
4207static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4208{
4209 struct cnic_local *cp = dev->cnic_priv;
4210 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004211 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004212 u32 cid_addr, tx_cid, sb_id;
4213 u32 val, offset0, offset1, offset2, offset3;
4214 int i;
4215 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004216 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004217 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004218
4219 sb_id = cp->status_blk_num;
4220 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004221 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4222 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004223 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004224
4225 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004226 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4227 (TX_TSS_CID << 7));
4228 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4229 }
4230 cp->tx_cons = *cp->tx_cons_ptr;
4231
4232 cid_addr = GET_CID_ADDR(tx_cid);
4233 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4234 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4235
4236 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4237 cnic_ctx_wr(dev, cid_addr2, i, 0);
4238
4239 offset0 = BNX2_L2CTX_TYPE_XI;
4240 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4241 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4242 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4243 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004244 cnic_init_context(dev, tx_cid);
4245 cnic_init_context(dev, tx_cid + 1);
4246
Michael Chana4636962009-06-08 18:14:43 -07004247 offset0 = BNX2_L2CTX_TYPE;
4248 offset1 = BNX2_L2CTX_CMD_TYPE;
4249 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4250 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4251 }
4252 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4253 cnic_ctx_wr(dev, cid_addr, offset0, val);
4254
4255 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4256 cnic_ctx_wr(dev, cid_addr, offset1, val);
4257
Michael Chancd801532010-10-13 14:06:49 +00004258 txbd = (struct tx_bd *) udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004259
Michael Chancd801532010-10-13 14:06:49 +00004260 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004261 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4262 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4263 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4264 }
Michael Chancd801532010-10-13 14:06:49 +00004265 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004266 cnic_ctx_wr(dev, cid_addr, offset2, val);
4267 txbd->tx_bd_haddr_hi = val;
4268
Michael Chancd801532010-10-13 14:06:49 +00004269 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004270 cnic_ctx_wr(dev, cid_addr, offset3, val);
4271 txbd->tx_bd_haddr_lo = val;
4272}
4273
4274static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4275{
4276 struct cnic_local *cp = dev->cnic_priv;
4277 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004278 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004279 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4280 int i;
4281 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004282 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004283 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004284
4285 sb_id = cp->status_blk_num;
4286 cnic_init_context(dev, 2);
4287 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4288 coal_reg = BNX2_HC_COMMAND;
4289 coal_val = CNIC_RD(dev, coal_reg);
4290 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004291 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004292
4293 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4294 coal_reg = BNX2_HC_COALESCE_NOW;
4295 coal_val = 1 << (11 + sb_id);
4296 }
4297 i = 0;
4298 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4299 CNIC_WR(dev, coal_reg, coal_val);
4300 udelay(10);
4301 i++;
4302 barrier();
4303 }
4304 cp->rx_cons = *cp->rx_cons_ptr;
4305
4306 cid_addr = GET_CID_ADDR(2);
4307 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4308 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4309 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4310
4311 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004312 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004313 else
Michael Chand0549382009-10-28 03:41:59 -07004314 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004315 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4316
Michael Chancd801532010-10-13 14:06:49 +00004317 rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
Michael Chana4636962009-06-08 18:14:43 -07004318 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4319 dma_addr_t buf_map;
4320 int n = (i % cp->l2_rx_ring_size) + 1;
4321
Michael Chancd801532010-10-13 14:06:49 +00004322 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004323 rxbd->rx_bd_len = cp->l2_single_buf_size;
4324 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4325 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4326 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4327 }
Michael Chancd801532010-10-13 14:06:49 +00004328 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004329 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4330 rxbd->rx_bd_haddr_hi = val;
4331
Michael Chancd801532010-10-13 14:06:49 +00004332 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004333 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4334 rxbd->rx_bd_haddr_lo = val;
4335
4336 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4337 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4338}
4339
4340static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4341{
4342 struct kwqe *wqes[1], l2kwqe;
4343
4344 memset(&l2kwqe, 0, sizeof(l2kwqe));
4345 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004346 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004347 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4348 KWQE_OPCODE_SHIFT) | 2;
4349 dev->submit_kwqes(dev, wqes, 1);
4350}
4351
4352static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4353{
4354 struct cnic_local *cp = dev->cnic_priv;
4355 u32 val;
4356
4357 val = cp->func << 2;
4358
4359 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4360
4361 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4362 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4363 dev->mac_addr[0] = (u8) (val >> 8);
4364 dev->mac_addr[1] = (u8) val;
4365
4366 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4367
4368 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4369 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4370 dev->mac_addr[2] = (u8) (val >> 24);
4371 dev->mac_addr[3] = (u8) (val >> 16);
4372 dev->mac_addr[4] = (u8) (val >> 8);
4373 dev->mac_addr[5] = (u8) val;
4374
4375 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4376
4377 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4378 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4379 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4380
4381 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4382 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4383 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4384}
4385
4386static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4387{
4388 struct cnic_local *cp = dev->cnic_priv;
4389 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004390 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004391 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004392 int err;
4393
4394 cnic_set_bnx2_mac(dev);
4395
4396 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4397 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4398 if (BCM_PAGE_BITS > 12)
4399 val |= (12 - 8) << 4;
4400 else
4401 val |= (BCM_PAGE_BITS - 8) << 4;
4402
4403 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4404
4405 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4406 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4407 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4408
4409 err = cnic_setup_5709_context(dev, 1);
4410 if (err)
4411 return err;
4412
4413 cnic_init_context(dev, KWQ_CID);
4414 cnic_init_context(dev, KCQ_CID);
4415
Michael Chane6c28892010-06-24 14:58:39 +00004416 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004417 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4418
4419 cp->max_kwq_idx = MAX_KWQ_IDX;
4420 cp->kwq_prod_idx = 0;
4421 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004422 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004423
4424 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4425 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4426 else
4427 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4428
4429 /* Initialize the kernel work queue context. */
4430 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4431 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004432 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004433
4434 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004435 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004436
4437 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004438 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004439
4440 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004441 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004442
4443 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004444 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004445
Michael Chane6c28892010-06-24 14:58:39 +00004446 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4447 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004448
Michael Chane6c28892010-06-24 14:58:39 +00004449 cp->kcq1.sw_prod_idx = 0;
4450 cp->kcq1.hw_prod_idx_ptr =
4451 (u16 *) &sblk->status_completion_producer_index;
4452
4453 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004454
4455 /* Initialize the kernel complete queue context. */
4456 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4457 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004458 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004459
4460 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004461 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004462
4463 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004464 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004465
Michael Chane6c28892010-06-24 14:58:39 +00004466 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4467 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004468
Michael Chane6c28892010-06-24 14:58:39 +00004469 val = (u32) cp->kcq1.dma.pgtbl_map;
4470 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004471
4472 cp->int_num = 0;
4473 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004474 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004475 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004476 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004477
Michael Chane6c28892010-06-24 14:58:39 +00004478 cp->kcq1.hw_prod_idx_ptr =
4479 (u16 *) &msblk->status_completion_producer_index;
4480 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004481 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004482 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004483 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4484 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004485 }
4486
4487 /* Enable Commnad Scheduler notification when we write to the
4488 * host producer index of the kernel contexts. */
4489 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4490
4491 /* Enable Command Scheduler notification when we write to either
4492 * the Send Queue or Receive Queue producer indexes of the kernel
4493 * bypass contexts. */
4494 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4495 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4496
4497 /* Notify COM when the driver post an application buffer. */
4498 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4499
4500 /* Set the CP and COM doorbells. These two processors polls the
4501 * doorbell for a non zero value before running. This must be done
4502 * after setting up the kernel queue contexts. */
4503 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4504 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4505
4506 cnic_init_bnx2_tx_ring(dev);
4507 cnic_init_bnx2_rx_ring(dev);
4508
4509 err = cnic_init_bnx2_irq(dev);
4510 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004511 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004512 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4513 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4514 return err;
4515 }
4516
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00004517 cnic_get_bnx2_iscsi_info(dev);
4518
Michael Chana4636962009-06-08 18:14:43 -07004519 return 0;
4520}
4521
Michael Chan71034ba2009-10-10 13:46:59 +00004522static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4523{
4524 struct cnic_local *cp = dev->cnic_priv;
4525 struct cnic_eth_dev *ethdev = cp->ethdev;
4526 u32 start_offset = ethdev->ctx_tbl_offset;
4527 int i;
4528
4529 for (i = 0; i < cp->ctx_blks; i++) {
4530 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4531 dma_addr_t map = ctx->mapping;
4532
4533 if (cp->ctx_align) {
4534 unsigned long mask = cp->ctx_align - 1;
4535
4536 map = (map + mask) & ~mask;
4537 }
4538
4539 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4540 }
4541}
4542
4543static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4544{
4545 struct cnic_local *cp = dev->cnic_priv;
4546 struct cnic_eth_dev *ethdev = cp->ethdev;
4547 int err = 0;
4548
Joe Perches164165d2009-11-19 09:30:10 +00004549 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004550 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004551 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4552 err = cnic_request_irq(dev);
4553
Michael Chan71034ba2009-10-10 13:46:59 +00004554 return err;
4555}
4556
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004557static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4558 u16 sb_id, u8 sb_index,
4559 u8 disable)
4560{
4561
4562 u32 addr = BAR_CSTRORM_INTMEM +
4563 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4564 offsetof(struct hc_status_block_data_e1x, index_data) +
4565 sizeof(struct hc_index_data)*sb_index +
4566 offsetof(struct hc_index_data, flags);
4567 u16 flags = CNIC_RD16(dev, addr);
4568 /* clear and set */
4569 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4570 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4571 HC_INDEX_DATA_HC_ENABLED);
4572 CNIC_WR16(dev, addr, flags);
4573}
4574
Michael Chan71034ba2009-10-10 13:46:59 +00004575static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4576{
4577 struct cnic_local *cp = dev->cnic_priv;
4578 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004579
4580 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004581 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4582 offsetof(struct hc_status_block_data_e1x, index_data) +
4583 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
4584 offsetof(struct hc_index_data, timeout), 64 / 12);
4585 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004586}
4587
4588static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4589{
4590}
4591
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004592static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4593 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004594{
4595 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004596 struct cnic_uio_dev *udev = cp->udev;
4597 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4598 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004599 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004600 int port = CNIC_PORT(cp);
4601 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004602 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004603 u32 val;
4604
4605 memset(txbd, 0, BCM_PAGE_SIZE);
4606
Michael Chancd801532010-10-13 14:06:49 +00004607 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004608 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4609 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4610 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4611
4612 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4613 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4614 reg_bd->addr_hi = start_bd->addr_hi;
4615 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4616 start_bd->nbytes = cpu_to_le16(0x10);
4617 start_bd->nbd = cpu_to_le16(3);
4618 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4619 start_bd->general_data = (UNICAST_ADDRESS <<
4620 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4621 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4622
4623 }
Michael Chan71034ba2009-10-10 13:46:59 +00004624
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004625 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004626 txbd->next_bd.addr_hi = cpu_to_le32(val);
4627
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004628 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004629
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004630 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004631 txbd->next_bd.addr_lo = cpu_to_le32(val);
4632
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004633 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004634
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004635 /* Other ramrod params */
4636 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4637 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004638
4639 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004640 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004641 val = BAR_XSTRORM_INTMEM +
4642 XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4643 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
4644 CNIC_WR(dev, val + i * 4, 0);
4645 }
Michael Chan71034ba2009-10-10 13:46:59 +00004646
4647 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004648 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004649}
4650
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004651static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4652 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004653{
4654 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004655 struct cnic_uio_dev *udev = cp->udev;
4656 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004657 BCM_PAGE_SIZE);
4658 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004659 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004660 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004661 int i;
4662 int port = CNIC_PORT(cp);
Michael Chan5159fdc2010-12-23 07:42:59 +00004663 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004664 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004665 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004666 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004667
4668 /* General data */
4669 data->general.client_id = cli;
4670 data->general.statistics_en_flg = 1;
4671 data->general.statistics_counter_id = cli;
4672 data->general.activate_flg = 1;
4673 data->general.sp_client_id = cli;
Michael Chan71034ba2009-10-10 13:46:59 +00004674
4675 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4676 dma_addr_t buf_map;
4677 int n = (i % cp->l2_rx_ring_size) + 1;
4678
Michael Chancd801532010-10-13 14:06:49 +00004679 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004680 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4681 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4682 }
Michael Chan71034ba2009-10-10 13:46:59 +00004683
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004684 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004685 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004686 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004687
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004688 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004689 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004690 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004691
4692 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004693 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004694 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004695 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004696
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004697 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004698 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004699 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004700
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004701 /* Other ramrod params */
4702 data->rx.client_qzone_id = cl_qzone_id;
4703 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4704 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004705
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004706 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
4707 data->rx.bd_buff_size = cpu_to_le16(cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004708
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004709 data->rx.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4710 data->rx.outer_vlan_removal_enable_flg = 1;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004711
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004712 /* reset tstorm and ustorm per client statistics */
4713 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004714 val = BAR_TSTRORM_INTMEM +
4715 TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4716 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4717 CNIC_WR(dev, val + i * 4, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004718
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004719 val = BAR_USTRORM_INTMEM +
4720 USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4721 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4722 CNIC_WR(dev, val + i * 4, 0);
4723 }
Michael Chan71034ba2009-10-10 13:46:59 +00004724
4725 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004726 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004727 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004728}
4729
Michael Chane21ba412010-12-23 07:43:03 +00004730static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4731{
4732 struct cnic_local *cp = dev->cnic_priv;
4733 u32 pfid = cp->pfid;
4734
4735 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4736 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4737 cp->kcq1.sw_prod_idx = 0;
4738
4739 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4740 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4741
4742 cp->kcq1.hw_prod_idx_ptr =
4743 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4744 cp->kcq1.status_idx_ptr =
4745 &sb->sb.running_index[SM_RX_ID];
4746 } else {
4747 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4748
4749 cp->kcq1.hw_prod_idx_ptr =
4750 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4751 cp->kcq1.status_idx_ptr =
4752 &sb->sb.running_index[SM_RX_ID];
4753 }
4754
4755 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4756 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4757
4758 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4759 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4760 cp->kcq2.sw_prod_idx = 0;
4761 cp->kcq2.hw_prod_idx_ptr =
4762 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4763 cp->kcq2.status_idx_ptr =
4764 &sb->sb.running_index[SM_RX_ID];
4765 }
4766}
4767
Michael Chan71034ba2009-10-10 13:46:59 +00004768static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4769{
4770 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004771 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chan71034ba2009-10-10 13:46:59 +00004772 int func = CNIC_FUNC(cp), ret, i;
Michael Chan14203982010-10-06 03:16:06 +00004773 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004774
Michael Chanee87a822010-10-13 14:06:51 +00004775 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4776 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4777
4778 if (!(val & 1))
4779 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4780 else
4781 val = (val >> 1) & 1;
4782
4783 if (val)
4784 cp->pfid = func >> 1;
4785 else
4786 cp->pfid = func & 0x6;
4787 } else {
4788 cp->pfid = func;
4789 }
Michael Chan14203982010-10-06 03:16:06 +00004790 pfid = cp->pfid;
4791
Michael Chan71034ba2009-10-10 13:46:59 +00004792 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Michael Chan520efdf2010-06-24 14:58:37 +00004793 cp->iscsi_start_cid);
Michael Chan71034ba2009-10-10 13:46:59 +00004794
4795 if (ret)
4796 return -ENOMEM;
4797
Michael Chane1928c82010-12-23 07:43:04 +00004798 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4799 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl,
4800 BNX2X_FCOE_NUM_CONNECTIONS,
4801 cp->fcoe_start_cid);
4802
4803 if (ret)
4804 return -ENOMEM;
4805 }
4806
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004807 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
4808
Michael Chane21ba412010-12-23 07:43:03 +00004809 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004810
Michael Chan71034ba2009-10-10 13:46:59 +00004811 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00004812 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00004813 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004814 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004815 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004816 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004817 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004818 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004819 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004820 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004821 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004822 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004823 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004824 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004825 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004826 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004827 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004828 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004829 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004830 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00004831 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004832 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004833 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00004834
4835 for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4836 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004837 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
Michael Chan71034ba2009-10-10 13:46:59 +00004838 cp->conn_buf_info.pgtbl[2 * i]);
4839 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004840 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004841 cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4842 }
4843
4844 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004845 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004846 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4847 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004848 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004849 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4850
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004851 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4852 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
4853
Michael Chan71034ba2009-10-10 13:46:59 +00004854 cnic_setup_bnx2x_context(dev);
4855
Michael Chan71034ba2009-10-10 13:46:59 +00004856 ret = cnic_init_bnx2x_irq(dev);
4857 if (ret)
4858 return ret;
4859
Michael Chan71034ba2009-10-10 13:46:59 +00004860 return 0;
4861}
4862
Michael Chan86b53602009-10-10 13:46:57 +00004863static void cnic_init_rings(struct cnic_dev *dev)
4864{
Michael Chan541a7812010-10-06 03:17:22 +00004865 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004866 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00004867
4868 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4869 return;
4870
Michael Chan86b53602009-10-10 13:46:57 +00004871 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4872 cnic_init_bnx2_tx_ring(dev);
4873 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00004874 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00004875 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00004876 u32 cli = cp->ethdev->iscsi_l2_client_id;
4877 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00004878 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004879 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00004880 union l5cm_specific_data l5_data;
4881 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chanc7596b72009-12-02 15:15:35 +00004882 u32 off, i;
Michael Chan71034ba2009-10-10 13:46:59 +00004883
4884 rx_prods.bd_prod = 0;
4885 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4886 barrier();
4887
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004888 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4889
Michael Chanc7596b72009-12-02 15:15:35 +00004890 off = BAR_USTRORM_INTMEM +
Michael Chanee87a822010-10-13 14:06:51 +00004891 (BNX2X_CHIP_IS_E2(cp->chip_id) ?
4892 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
4893 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00004894
4895 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00004896 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00004897
Michael Chan48f753d2010-05-18 11:32:53 +00004898 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4899
Michael Chancd801532010-10-13 14:06:49 +00004900 data = udev->l2_buf;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004901
4902 memset(data, 0, sizeof(*data));
4903
4904 cnic_init_bnx2x_tx_ring(dev, data);
4905 cnic_init_bnx2x_rx_ring(dev, data);
4906
Michael Chancd801532010-10-13 14:06:49 +00004907 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
4908 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004909
Michael Chan541a7812010-10-06 03:17:22 +00004910 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4911
Michael Chan71034ba2009-10-10 13:46:59 +00004912 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00004913 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004914
Michael Chan48f753d2010-05-18 11:32:53 +00004915 i = 0;
4916 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4917 ++i < 10)
4918 msleep(1);
4919
4920 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4921 netdev_err(dev->netdev,
4922 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004923 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00004924 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chan86b53602009-10-10 13:46:57 +00004925 }
4926}
4927
4928static void cnic_shutdown_rings(struct cnic_dev *dev)
4929{
Michael Chan541a7812010-10-06 03:17:22 +00004930 struct cnic_local *cp = dev->cnic_priv;
4931
4932 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4933 return;
4934
Michael Chan86b53602009-10-10 13:46:57 +00004935 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4936 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004937 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4938 struct cnic_local *cp = dev->cnic_priv;
Michael Chan5159fdc2010-12-23 07:42:59 +00004939 u32 cli = cp->ethdev->iscsi_l2_client_id;
4940 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00004941 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00004942 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00004943
Michael Chan5159fdc2010-12-23 07:42:59 +00004944 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00004945
Michael Chan48f753d2010-05-18 11:32:53 +00004946 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4947
Michael Chan8b065b62009-12-02 15:15:36 +00004948 l5_data.phy_address.lo = cli;
4949 l5_data.phy_address.hi = 0;
4950 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00004951 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00004952 i = 0;
4953 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4954 ++i < 10)
4955 msleep(1);
4956
4957 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4958 netdev_err(dev->netdev,
4959 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004960 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00004961
4962 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004963 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00004964 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00004965 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00004966 }
Michael Chan541a7812010-10-06 03:17:22 +00004967 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan86b53602009-10-10 13:46:57 +00004968}
4969
Michael Chana3059b12009-08-14 15:49:44 +00004970static int cnic_register_netdev(struct cnic_dev *dev)
4971{
4972 struct cnic_local *cp = dev->cnic_priv;
4973 struct cnic_eth_dev *ethdev = cp->ethdev;
4974 int err;
4975
4976 if (!ethdev)
4977 return -ENODEV;
4978
4979 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
4980 return 0;
4981
4982 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
4983 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00004984 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00004985
4986 return err;
4987}
4988
4989static void cnic_unregister_netdev(struct cnic_dev *dev)
4990{
4991 struct cnic_local *cp = dev->cnic_priv;
4992 struct cnic_eth_dev *ethdev = cp->ethdev;
4993
4994 if (!ethdev)
4995 return;
4996
4997 ethdev->drv_unregister_cnic(dev->netdev);
4998}
4999
Michael Chana4636962009-06-08 18:14:43 -07005000static int cnic_start_hw(struct cnic_dev *dev)
5001{
5002 struct cnic_local *cp = dev->cnic_priv;
5003 struct cnic_eth_dev *ethdev = cp->ethdev;
5004 int err;
5005
5006 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5007 return -EALREADY;
5008
Michael Chana4636962009-06-08 18:14:43 -07005009 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005010 pci_dev_get(dev->pcidev);
5011 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005012 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005013 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5014
5015 err = cp->alloc_resc(dev);
5016 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005017 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005018 goto err1;
5019 }
5020
5021 err = cp->start_hw(dev);
5022 if (err)
5023 goto err1;
5024
5025 err = cnic_cm_open(dev);
5026 if (err)
5027 goto err1;
5028
5029 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5030
5031 cp->enable_int(dev);
5032
5033 return 0;
5034
5035err1:
Michael Chana4636962009-06-08 18:14:43 -07005036 cp->free_resc(dev);
5037 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005038 return err;
5039}
5040
5041static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5042{
Michael Chana4636962009-06-08 18:14:43 -07005043 cnic_disable_bnx2_int_sync(dev);
5044
5045 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5046 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5047
5048 cnic_init_context(dev, KWQ_CID);
5049 cnic_init_context(dev, KCQ_CID);
5050
5051 cnic_setup_5709_context(dev, 0);
5052 cnic_free_irq(dev);
5053
Michael Chana4636962009-06-08 18:14:43 -07005054 cnic_free_resc(dev);
5055}
5056
Michael Chan71034ba2009-10-10 13:46:59 +00005057
5058static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5059{
5060 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005061
5062 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005063 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005064 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005065 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005066 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005067 cnic_free_resc(dev);
5068}
5069
Michael Chana4636962009-06-08 18:14:43 -07005070static void cnic_stop_hw(struct cnic_dev *dev)
5071{
5072 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5073 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005074 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005075
Michael Chan48f753d2010-05-18 11:32:53 +00005076 /* Need to wait for the ring shutdown event to complete
5077 * before clearing the CNIC_UP flag.
5078 */
Michael Chancd801532010-10-13 14:06:49 +00005079 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005080 msleep(100);
5081 i++;
5082 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005083 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005084 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
5085 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
5086 synchronize_rcu();
5087 cnic_cm_shutdown(dev);
5088 cp->stop_hw(dev);
5089 pci_dev_put(dev->pcidev);
5090 }
5091}
5092
5093static void cnic_free_dev(struct cnic_dev *dev)
5094{
5095 int i = 0;
5096
5097 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5098 msleep(100);
5099 i++;
5100 }
5101 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005102 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005103
Joe Perchesddf79b22010-02-17 15:01:54 +00005104 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005105 dev_put(dev->netdev);
5106 kfree(dev);
5107}
5108
5109static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5110 struct pci_dev *pdev)
5111{
5112 struct cnic_dev *cdev;
5113 struct cnic_local *cp;
5114 int alloc_size;
5115
5116 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5117
5118 cdev = kzalloc(alloc_size , GFP_KERNEL);
5119 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005120 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005121 return NULL;
5122 }
5123
5124 cdev->netdev = dev;
5125 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5126 cdev->register_device = cnic_register_device;
5127 cdev->unregister_device = cnic_unregister_device;
5128 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5129
5130 cp = cdev->cnic_priv;
5131 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005132 cp->l2_single_buf_size = 0x400;
5133 cp->l2_rx_ring_size = 3;
5134
5135 spin_lock_init(&cp->cnic_ulp_lock);
5136
Joe Perchesddf79b22010-02-17 15:01:54 +00005137 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005138
5139 return cdev;
5140}
5141
5142static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5143{
5144 struct pci_dev *pdev;
5145 struct cnic_dev *cdev;
5146 struct cnic_local *cp;
5147 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005148 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005149
Michael Chane2ee3612009-06-13 17:43:02 -07005150 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005151 if (probe) {
5152 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005153 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005154 }
5155 if (!ethdev)
5156 return NULL;
5157
5158 pdev = ethdev->pdev;
5159 if (!pdev)
5160 return NULL;
5161
5162 dev_hold(dev);
5163 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005164 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5165 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5166 (pdev->revision < 0x10)) {
5167 pci_dev_put(pdev);
5168 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005169 }
5170 pci_dev_put(pdev);
5171
5172 cdev = cnic_alloc_dev(dev, pdev);
5173 if (cdev == NULL)
5174 goto cnic_err;
5175
5176 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5177 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5178
5179 cp = cdev->cnic_priv;
5180 cp->ethdev = ethdev;
5181 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005182 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005183
5184 cp->cnic_ops = &cnic_bnx2_ops;
5185 cp->start_hw = cnic_start_bnx2_hw;
5186 cp->stop_hw = cnic_stop_bnx2_hw;
5187 cp->setup_pgtbl = cnic_setup_page_tbl;
5188 cp->alloc_resc = cnic_alloc_bnx2_resc;
5189 cp->free_resc = cnic_free_resc;
5190 cp->start_cm = cnic_cm_init_bnx2_hw;
5191 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5192 cp->enable_int = cnic_enable_bnx2_int;
5193 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5194 cp->close_conn = cnic_close_bnx2_conn;
5195 cp->next_idx = cnic_bnx2_next_idx;
5196 cp->hw_idx = cnic_bnx2_hw_idx;
5197 return cdev;
5198
5199cnic_err:
5200 dev_put(dev);
5201 return NULL;
5202}
5203
Michael Chan71034ba2009-10-10 13:46:59 +00005204static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5205{
5206 struct pci_dev *pdev;
5207 struct cnic_dev *cdev;
5208 struct cnic_local *cp;
5209 struct cnic_eth_dev *ethdev = NULL;
5210 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5211
5212 probe = symbol_get(bnx2x_cnic_probe);
5213 if (probe) {
5214 ethdev = (*probe)(dev);
5215 symbol_put(bnx2x_cnic_probe);
5216 }
5217 if (!ethdev)
5218 return NULL;
5219
5220 pdev = ethdev->pdev;
5221 if (!pdev)
5222 return NULL;
5223
5224 dev_hold(dev);
5225 cdev = cnic_alloc_dev(dev, pdev);
5226 if (cdev == NULL) {
5227 dev_put(dev);
5228 return NULL;
5229 }
5230
5231 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5232 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5233
5234 cp = cdev->cnic_priv;
5235 cp->ethdev = ethdev;
5236 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005237 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005238
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005239 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5240 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5241 if (BNX2X_CHIP_IS_E2(cp->chip_id) &&
5242 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5243 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5244
5245 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5246
Michael Chan71034ba2009-10-10 13:46:59 +00005247 cp->cnic_ops = &cnic_bnx2x_ops;
5248 cp->start_hw = cnic_start_bnx2x_hw;
5249 cp->stop_hw = cnic_stop_bnx2x_hw;
5250 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5251 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5252 cp->free_resc = cnic_free_resc;
5253 cp->start_cm = cnic_cm_init_bnx2x_hw;
5254 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5255 cp->enable_int = cnic_enable_bnx2x_int;
5256 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Michael Chanee87a822010-10-13 14:06:51 +00005257 if (BNX2X_CHIP_IS_E2(cp->chip_id))
5258 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5259 else
5260 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005261 cp->close_conn = cnic_close_bnx2x_conn;
5262 cp->next_idx = cnic_bnx2x_next_idx;
5263 cp->hw_idx = cnic_bnx2x_hw_idx;
5264 return cdev;
5265}
5266
Michael Chana4636962009-06-08 18:14:43 -07005267static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5268{
5269 struct ethtool_drvinfo drvinfo;
5270 struct cnic_dev *cdev = NULL;
5271
5272 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5273 memset(&drvinfo, 0, sizeof(drvinfo));
5274 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5275
5276 if (!strcmp(drvinfo.driver, "bnx2"))
5277 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005278 if (!strcmp(drvinfo.driver, "bnx2x"))
5279 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005280 if (cdev) {
5281 write_lock(&cnic_dev_lock);
5282 list_add(&cdev->list, &cnic_dev_list);
5283 write_unlock(&cnic_dev_lock);
5284 }
5285 }
5286 return cdev;
5287}
5288
5289/**
5290 * netdev event handler
5291 */
5292static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5293 void *ptr)
5294{
5295 struct net_device *netdev = ptr;
5296 struct cnic_dev *dev;
5297 int if_type;
5298 int new_dev = 0;
5299
5300 dev = cnic_from_netdev(netdev);
5301
5302 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
5303 /* Check for the hot-plug device */
5304 dev = is_cnic_dev(netdev);
5305 if (dev) {
5306 new_dev = 1;
5307 cnic_hold(dev);
5308 }
5309 }
5310 if (dev) {
5311 struct cnic_local *cp = dev->cnic_priv;
5312
5313 if (new_dev)
5314 cnic_ulp_init(dev);
5315 else if (event == NETDEV_UNREGISTER)
5316 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005317
5318 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00005319 if (cnic_register_netdev(dev) != 0) {
5320 cnic_put(dev);
5321 goto done;
5322 }
Michael Chana4636962009-06-08 18:14:43 -07005323 if (!cnic_start_hw(dev))
5324 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005325 }
5326
5327 rcu_read_lock();
5328 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5329 struct cnic_ulp_ops *ulp_ops;
5330 void *ctx;
5331
5332 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5333 if (!ulp_ops || !ulp_ops->indicate_netevent)
5334 continue;
5335
5336 ctx = cp->ulp_handle[if_type];
5337
5338 ulp_ops->indicate_netevent(ctx, event);
5339 }
5340 rcu_read_unlock();
5341
5342 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005343 cnic_ulp_stop(dev);
5344 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005345 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005346 } else if (event == NETDEV_UNREGISTER) {
5347 write_lock(&cnic_dev_lock);
5348 list_del_init(&dev->list);
5349 write_unlock(&cnic_dev_lock);
5350
5351 cnic_put(dev);
5352 cnic_free_dev(dev);
5353 goto done;
5354 }
5355 cnic_put(dev);
5356 }
5357done:
5358 return NOTIFY_DONE;
5359}
5360
5361static struct notifier_block cnic_netdev_notifier = {
5362 .notifier_call = cnic_netdev_event
5363};
5364
5365static void cnic_release(void)
5366{
5367 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005368 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005369
5370 while (!list_empty(&cnic_dev_list)) {
5371 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5372 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5373 cnic_ulp_stop(dev);
5374 cnic_stop_hw(dev);
5375 }
5376
5377 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005378 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005379 list_del_init(&dev->list);
5380 cnic_free_dev(dev);
5381 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005382 while (!list_empty(&cnic_udev_list)) {
5383 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5384 list);
5385 cnic_free_uio(udev);
5386 }
Michael Chana4636962009-06-08 18:14:43 -07005387}
5388
5389static int __init cnic_init(void)
5390{
5391 int rc = 0;
5392
Joe Perchesddf79b22010-02-17 15:01:54 +00005393 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005394
5395 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5396 if (rc) {
5397 cnic_release();
5398 return rc;
5399 }
5400
Michael Chanfdf24082010-10-13 14:06:47 +00005401 cnic_wq = create_singlethread_workqueue("cnic_wq");
5402 if (!cnic_wq) {
5403 cnic_release();
5404 unregister_netdevice_notifier(&cnic_netdev_notifier);
5405 return -ENOMEM;
5406 }
5407
Michael Chana4636962009-06-08 18:14:43 -07005408 return 0;
5409}
5410
5411static void __exit cnic_exit(void)
5412{
5413 unregister_netdevice_notifier(&cnic_netdev_notifier);
5414 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005415 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005416}
5417
5418module_init(cnic_init);
5419module_exit(cnic_exit);