blob: 50cded7fff519890a00adff24264a512843a554d [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
62static LIST_HEAD(cnic_dev_list);
63static DEFINE_RWLOCK(cnic_dev_lock);
64static DEFINE_MUTEX(cnic_lock);
65
66static struct cnic_ulp_ops *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
67
68static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000069static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070070static int cnic_ctl(void *, struct cnic_ctl_info *);
71
72static struct cnic_ops cnic_bnx2_ops = {
73 .cnic_owner = THIS_MODULE,
74 .cnic_handler = cnic_service_bnx2,
75 .cnic_ctl = cnic_ctl,
76};
77
Michael Chan71034ba2009-10-10 13:46:59 +000078static struct cnic_ops cnic_bnx2x_ops = {
79 .cnic_owner = THIS_MODULE,
80 .cnic_handler = cnic_service_bnx2x,
81 .cnic_ctl = cnic_ctl,
82};
83
Michael Chan86b53602009-10-10 13:46:57 +000084static void cnic_shutdown_rings(struct cnic_dev *);
85static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070086static int cnic_cm_set_pg(struct cnic_sock *);
87
88static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
89{
90 struct cnic_dev *dev = uinfo->priv;
91 struct cnic_local *cp = dev->cnic_priv;
92
93 if (!capable(CAP_NET_ADMIN))
94 return -EPERM;
95
96 if (cp->uio_dev != -1)
97 return -EBUSY;
98
Michael Chan86b53602009-10-10 13:46:57 +000099 rtnl_lock();
100 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
101 rtnl_unlock();
102 return -ENODEV;
103 }
104
Michael Chana4636962009-06-08 18:14:43 -0700105 cp->uio_dev = iminor(inode);
106
Michael Chan86b53602009-10-10 13:46:57 +0000107 cnic_init_rings(dev);
108 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700109
110 return 0;
111}
112
113static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
114{
115 struct cnic_dev *dev = uinfo->priv;
116 struct cnic_local *cp = dev->cnic_priv;
117
Michael Chan86b53602009-10-10 13:46:57 +0000118 cnic_shutdown_rings(dev);
Michael Chan6ef57a02009-09-21 15:39:37 +0000119
Michael Chana4636962009-06-08 18:14:43 -0700120 cp->uio_dev = -1;
121 return 0;
122}
123
124static inline void cnic_hold(struct cnic_dev *dev)
125{
126 atomic_inc(&dev->ref_count);
127}
128
129static inline void cnic_put(struct cnic_dev *dev)
130{
131 atomic_dec(&dev->ref_count);
132}
133
134static inline void csk_hold(struct cnic_sock *csk)
135{
136 atomic_inc(&csk->ref_count);
137}
138
139static inline void csk_put(struct cnic_sock *csk)
140{
141 atomic_dec(&csk->ref_count);
142}
143
144static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
145{
146 struct cnic_dev *cdev;
147
148 read_lock(&cnic_dev_lock);
149 list_for_each_entry(cdev, &cnic_dev_list, list) {
150 if (netdev == cdev->netdev) {
151 cnic_hold(cdev);
152 read_unlock(&cnic_dev_lock);
153 return cdev;
154 }
155 }
156 read_unlock(&cnic_dev_lock);
157 return NULL;
158}
159
Michael Chan7fc1ece2009-08-14 15:49:47 +0000160static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
161{
162 atomic_inc(&ulp_ops->ref_count);
163}
164
165static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
166{
167 atomic_dec(&ulp_ops->ref_count);
168}
169
Michael Chana4636962009-06-08 18:14:43 -0700170static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
171{
172 struct cnic_local *cp = dev->cnic_priv;
173 struct cnic_eth_dev *ethdev = cp->ethdev;
174 struct drv_ctl_info info;
175 struct drv_ctl_io *io = &info.data.io;
176
177 info.cmd = DRV_CTL_CTX_WR_CMD;
178 io->cid_addr = cid_addr;
179 io->offset = off;
180 io->data = val;
181 ethdev->drv_ctl(dev->netdev, &info);
182}
183
Michael Chan71034ba2009-10-10 13:46:59 +0000184static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
185{
186 struct cnic_local *cp = dev->cnic_priv;
187 struct cnic_eth_dev *ethdev = cp->ethdev;
188 struct drv_ctl_info info;
189 struct drv_ctl_io *io = &info.data.io;
190
191 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
192 io->offset = off;
193 io->dma_addr = addr;
194 ethdev->drv_ctl(dev->netdev, &info);
195}
196
197static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
198{
199 struct cnic_local *cp = dev->cnic_priv;
200 struct cnic_eth_dev *ethdev = cp->ethdev;
201 struct drv_ctl_info info;
202 struct drv_ctl_l2_ring *ring = &info.data.ring;
203
204 if (start)
205 info.cmd = DRV_CTL_START_L2_CMD;
206 else
207 info.cmd = DRV_CTL_STOP_L2_CMD;
208
209 ring->cid = cid;
210 ring->client_id = cl_id;
211 ethdev->drv_ctl(dev->netdev, &info);
212}
213
Michael Chana4636962009-06-08 18:14:43 -0700214static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
215{
216 struct cnic_local *cp = dev->cnic_priv;
217 struct cnic_eth_dev *ethdev = cp->ethdev;
218 struct drv_ctl_info info;
219 struct drv_ctl_io *io = &info.data.io;
220
221 info.cmd = DRV_CTL_IO_WR_CMD;
222 io->offset = off;
223 io->data = val;
224 ethdev->drv_ctl(dev->netdev, &info);
225}
226
227static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
228{
229 struct cnic_local *cp = dev->cnic_priv;
230 struct cnic_eth_dev *ethdev = cp->ethdev;
231 struct drv_ctl_info info;
232 struct drv_ctl_io *io = &info.data.io;
233
234 info.cmd = DRV_CTL_IO_RD_CMD;
235 io->offset = off;
236 ethdev->drv_ctl(dev->netdev, &info);
237 return io->data;
238}
239
240static int cnic_in_use(struct cnic_sock *csk)
241{
242 return test_bit(SK_F_INUSE, &csk->flags);
243}
244
245static void cnic_kwq_completion(struct cnic_dev *dev, u32 count)
246{
247 struct cnic_local *cp = dev->cnic_priv;
248 struct cnic_eth_dev *ethdev = cp->ethdev;
249 struct drv_ctl_info info;
250
251 info.cmd = DRV_CTL_COMPLETION_CMD;
252 info.data.comp.comp_count = count;
253 ethdev->drv_ctl(dev->netdev, &info);
254}
255
Michael Chan71034ba2009-10-10 13:46:59 +0000256static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
257{
258 u32 i;
259
Michael Chan520efdf2010-06-24 14:58:37 +0000260 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000261 if (cp->ctx_tbl[i].cid == cid) {
262 *l5_cid = i;
263 return 0;
264 }
265 }
266 return -EINVAL;
267}
268
Michael Chana4636962009-06-08 18:14:43 -0700269static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
270 struct cnic_sock *csk)
271{
272 struct iscsi_path path_req;
273 char *buf = NULL;
274 u16 len = 0;
275 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
276 struct cnic_ulp_ops *ulp_ops;
277
278 if (cp->uio_dev == -1)
279 return -ENODEV;
280
281 if (csk) {
282 len = sizeof(path_req);
283 buf = (char *) &path_req;
284 memset(&path_req, 0, len);
285
286 msg_type = ISCSI_KEVENT_PATH_REQ;
287 path_req.handle = (u64) csk->l5_cid;
288 if (test_bit(SK_F_IPV6, &csk->flags)) {
289 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
290 sizeof(struct in6_addr));
291 path_req.ip_addr_len = 16;
292 } else {
293 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
294 sizeof(struct in_addr));
295 path_req.ip_addr_len = 4;
296 }
297 path_req.vlan_id = csk->vlan_id;
298 path_req.pmtu = csk->mtu;
299 }
300
301 rcu_read_lock();
Michael Chan6d7760a2009-07-27 11:25:58 -0700302 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
Michael Chana4636962009-06-08 18:14:43 -0700303 if (ulp_ops)
304 ulp_ops->iscsi_nl_send_msg(cp->dev, msg_type, buf, len);
305 rcu_read_unlock();
306 return 0;
307}
308
309static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
310 char *buf, u16 len)
311{
312 int rc = -EINVAL;
313
314 switch (msg_type) {
315 case ISCSI_UEVENT_PATH_UPDATE: {
316 struct cnic_local *cp;
317 u32 l5_cid;
318 struct cnic_sock *csk;
319 struct iscsi_path *path_resp;
320
321 if (len < sizeof(*path_resp))
322 break;
323
324 path_resp = (struct iscsi_path *) buf;
325 cp = dev->cnic_priv;
326 l5_cid = (u32) path_resp->handle;
327 if (l5_cid >= MAX_CM_SK_TBL_SZ)
328 break;
329
Michael Chand02a5e62010-02-24 14:42:06 +0000330 rcu_read_lock();
331 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
332 rc = -ENODEV;
333 rcu_read_unlock();
334 break;
335 }
Michael Chana4636962009-06-08 18:14:43 -0700336 csk = &cp->csk_tbl[l5_cid];
337 csk_hold(csk);
338 if (cnic_in_use(csk)) {
339 memcpy(csk->ha, path_resp->mac_addr, 6);
340 if (test_bit(SK_F_IPV6, &csk->flags))
341 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
342 sizeof(struct in6_addr));
343 else
344 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
345 sizeof(struct in_addr));
346 if (is_valid_ether_addr(csk->ha))
347 cnic_cm_set_pg(csk);
348 }
349 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000350 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700351 rc = 0;
352 }
353 }
354
355 return rc;
356}
357
358static int cnic_offld_prep(struct cnic_sock *csk)
359{
360 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
361 return 0;
362
363 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
364 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
365 return 0;
366 }
367
368 return 1;
369}
370
371static int cnic_close_prep(struct cnic_sock *csk)
372{
373 clear_bit(SK_F_CONNECT_START, &csk->flags);
374 smp_mb__after_clear_bit();
375
376 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
377 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
378 msleep(1);
379
380 return 1;
381 }
382 return 0;
383}
384
385static int cnic_abort_prep(struct cnic_sock *csk)
386{
387 clear_bit(SK_F_CONNECT_START, &csk->flags);
388 smp_mb__after_clear_bit();
389
390 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
391 msleep(1);
392
393 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
394 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
395 return 1;
396 }
397
398 return 0;
399}
400
Michael Chan6d7760a2009-07-27 11:25:58 -0700401static void cnic_uio_stop(void)
402{
403 struct cnic_dev *dev;
404
405 read_lock(&cnic_dev_lock);
406 list_for_each_entry(dev, &cnic_dev_list, list) {
407 struct cnic_local *cp = dev->cnic_priv;
408
409 if (cp->cnic_uinfo)
410 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
411 }
412 read_unlock(&cnic_dev_lock);
413}
414
Michael Chana4636962009-06-08 18:14:43 -0700415int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
416{
417 struct cnic_dev *dev;
418
roel kluin0d37f362009-11-02 06:53:44 +0000419 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000420 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700421 return -EINVAL;
422 }
423 mutex_lock(&cnic_lock);
424 if (cnic_ulp_tbl[ulp_type]) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000425 pr_err("%s: Type %d has already been registered\n",
426 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700427 mutex_unlock(&cnic_lock);
428 return -EBUSY;
429 }
430
431 read_lock(&cnic_dev_lock);
432 list_for_each_entry(dev, &cnic_dev_list, list) {
433 struct cnic_local *cp = dev->cnic_priv;
434
435 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
436 }
437 read_unlock(&cnic_dev_lock);
438
Michael Chan7fc1ece2009-08-14 15:49:47 +0000439 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700440 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
441 mutex_unlock(&cnic_lock);
442
443 /* Prevent race conditions with netdev_event */
444 rtnl_lock();
445 read_lock(&cnic_dev_lock);
446 list_for_each_entry(dev, &cnic_dev_list, list) {
447 struct cnic_local *cp = dev->cnic_priv;
448
449 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
450 ulp_ops->cnic_init(dev);
451 }
452 read_unlock(&cnic_dev_lock);
453 rtnl_unlock();
454
455 return 0;
456}
457
458int cnic_unregister_driver(int ulp_type)
459{
460 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000461 struct cnic_ulp_ops *ulp_ops;
462 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700463
roel kluin0d37f362009-11-02 06:53:44 +0000464 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000465 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700466 return -EINVAL;
467 }
468 mutex_lock(&cnic_lock);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000469 ulp_ops = cnic_ulp_tbl[ulp_type];
470 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000471 pr_err("%s: Type %d has not been registered\n",
472 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700473 goto out_unlock;
474 }
475 read_lock(&cnic_dev_lock);
476 list_for_each_entry(dev, &cnic_dev_list, list) {
477 struct cnic_local *cp = dev->cnic_priv;
478
479 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000480 pr_err("%s: Type %d still has devices registered\n",
481 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700482 read_unlock(&cnic_dev_lock);
483 goto out_unlock;
484 }
485 }
486 read_unlock(&cnic_dev_lock);
487
Michael Chan6d7760a2009-07-27 11:25:58 -0700488 if (ulp_type == CNIC_ULP_ISCSI)
489 cnic_uio_stop();
490
Michael Chana4636962009-06-08 18:14:43 -0700491 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
492
493 mutex_unlock(&cnic_lock);
494 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000495 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
496 msleep(100);
497 i++;
498 }
499
500 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000501 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700502 return 0;
503
504out_unlock:
505 mutex_unlock(&cnic_lock);
506 return -EINVAL;
507}
508
509static int cnic_start_hw(struct cnic_dev *);
510static void cnic_stop_hw(struct cnic_dev *);
511
512static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
513 void *ulp_ctx)
514{
515 struct cnic_local *cp = dev->cnic_priv;
516 struct cnic_ulp_ops *ulp_ops;
517
roel kluin0d37f362009-11-02 06:53:44 +0000518 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000519 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700520 return -EINVAL;
521 }
522 mutex_lock(&cnic_lock);
523 if (cnic_ulp_tbl[ulp_type] == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000524 pr_err("%s: Driver with type %d has not been registered\n",
525 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700526 mutex_unlock(&cnic_lock);
527 return -EAGAIN;
528 }
529 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000530 pr_err("%s: Type %d has already been registered to this device\n",
531 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700532 mutex_unlock(&cnic_lock);
533 return -EBUSY;
534 }
535
536 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
537 cp->ulp_handle[ulp_type] = ulp_ctx;
538 ulp_ops = cnic_ulp_tbl[ulp_type];
539 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
540 cnic_hold(dev);
541
542 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
543 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
544 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
545
546 mutex_unlock(&cnic_lock);
547
548 return 0;
549
550}
551EXPORT_SYMBOL(cnic_register_driver);
552
553static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
554{
555 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000556 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700557
roel kluin0d37f362009-11-02 06:53:44 +0000558 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000559 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700560 return -EINVAL;
561 }
562 mutex_lock(&cnic_lock);
563 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
564 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
565 cnic_put(dev);
566 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000567 pr_err("%s: device not registered to this ulp type %d\n",
568 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700569 mutex_unlock(&cnic_lock);
570 return -EINVAL;
571 }
572 mutex_unlock(&cnic_lock);
573
574 synchronize_rcu();
575
Michael Chan681dbd72009-08-14 15:49:46 +0000576 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
577 i < 20) {
578 msleep(100);
579 i++;
580 }
581 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000582 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000583
Michael Chana4636962009-06-08 18:14:43 -0700584 return 0;
585}
586EXPORT_SYMBOL(cnic_unregister_driver);
587
588static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
589{
590 id_tbl->start = start_id;
591 id_tbl->max = size;
592 id_tbl->next = 0;
593 spin_lock_init(&id_tbl->lock);
594 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
595 if (!id_tbl->table)
596 return -ENOMEM;
597
598 return 0;
599}
600
601static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
602{
603 kfree(id_tbl->table);
604 id_tbl->table = NULL;
605}
606
607static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
608{
609 int ret = -1;
610
611 id -= id_tbl->start;
612 if (id >= id_tbl->max)
613 return ret;
614
615 spin_lock(&id_tbl->lock);
616 if (!test_bit(id, id_tbl->table)) {
617 set_bit(id, id_tbl->table);
618 ret = 0;
619 }
620 spin_unlock(&id_tbl->lock);
621 return ret;
622}
623
624/* Returns -1 if not successful */
625static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
626{
627 u32 id;
628
629 spin_lock(&id_tbl->lock);
630 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
631 if (id >= id_tbl->max) {
632 id = -1;
633 if (id_tbl->next != 0) {
634 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
635 if (id >= id_tbl->next)
636 id = -1;
637 }
638 }
639
640 if (id < id_tbl->max) {
641 set_bit(id, id_tbl->table);
642 id_tbl->next = (id + 1) & (id_tbl->max - 1);
643 id += id_tbl->start;
644 }
645
646 spin_unlock(&id_tbl->lock);
647
648 return id;
649}
650
651static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
652{
653 if (id == -1)
654 return;
655
656 id -= id_tbl->start;
657 if (id >= id_tbl->max)
658 return;
659
660 clear_bit(id, id_tbl->table);
661}
662
663static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
664{
665 int i;
666
667 if (!dma->pg_arr)
668 return;
669
670 for (i = 0; i < dma->num_pages; i++) {
671 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000672 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
673 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700674 dma->pg_arr[i] = NULL;
675 }
676 }
677 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000678 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
679 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700680 dma->pgtbl = NULL;
681 }
682 kfree(dma->pg_arr);
683 dma->pg_arr = NULL;
684 dma->num_pages = 0;
685}
686
687static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
688{
689 int i;
690 u32 *page_table = dma->pgtbl;
691
692 for (i = 0; i < dma->num_pages; i++) {
693 /* Each entry needs to be in big endian format. */
694 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
695 page_table++;
696 *page_table = (u32) dma->pg_map_arr[i];
697 page_table++;
698 }
699}
700
Michael Chan71034ba2009-10-10 13:46:59 +0000701static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
702{
703 int i;
704 u32 *page_table = dma->pgtbl;
705
706 for (i = 0; i < dma->num_pages; i++) {
707 /* Each entry needs to be in little endian format. */
708 *page_table = dma->pg_map_arr[i] & 0xffffffff;
709 page_table++;
710 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
711 page_table++;
712 }
713}
714
Michael Chana4636962009-06-08 18:14:43 -0700715static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
716 int pages, int use_pg_tbl)
717{
718 int i, size;
719 struct cnic_local *cp = dev->cnic_priv;
720
721 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
722 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
723 if (dma->pg_arr == NULL)
724 return -ENOMEM;
725
726 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
727 dma->num_pages = pages;
728
729 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000730 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
731 BCM_PAGE_SIZE,
732 &dma->pg_map_arr[i],
733 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700734 if (dma->pg_arr[i] == NULL)
735 goto error;
736 }
737 if (!use_pg_tbl)
738 return 0;
739
740 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
741 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000742 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
743 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700744 if (dma->pgtbl == NULL)
745 goto error;
746
747 cp->setup_pgtbl(dev, dma);
748
749 return 0;
750
751error:
752 cnic_free_dma(dev, dma);
753 return -ENOMEM;
754}
755
Michael Chan86b53602009-10-10 13:46:57 +0000756static void cnic_free_context(struct cnic_dev *dev)
757{
758 struct cnic_local *cp = dev->cnic_priv;
759 int i;
760
761 for (i = 0; i < cp->ctx_blks; i++) {
762 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000763 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
764 cp->ctx_arr[i].ctx,
765 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000766 cp->ctx_arr[i].ctx = NULL;
767 }
768 }
769}
770
Michael Chana4636962009-06-08 18:14:43 -0700771static void cnic_free_resc(struct cnic_dev *dev)
772{
773 struct cnic_local *cp = dev->cnic_priv;
774 int i = 0;
775
776 if (cp->cnic_uinfo) {
Michael Chana4636962009-06-08 18:14:43 -0700777 while (cp->uio_dev != -1 && i < 15) {
778 msleep(100);
779 i++;
780 }
781 uio_unregister_device(cp->cnic_uinfo);
782 kfree(cp->cnic_uinfo);
783 cp->cnic_uinfo = NULL;
784 }
785
786 if (cp->l2_buf) {
Michael Chan3248e162009-12-02 15:15:39 +0000787 dma_free_coherent(&dev->pcidev->dev, cp->l2_buf_size,
788 cp->l2_buf, cp->l2_buf_map);
Michael Chana4636962009-06-08 18:14:43 -0700789 cp->l2_buf = NULL;
790 }
791
792 if (cp->l2_ring) {
Michael Chan3248e162009-12-02 15:15:39 +0000793 dma_free_coherent(&dev->pcidev->dev, cp->l2_ring_size,
794 cp->l2_ring, cp->l2_ring_map);
Michael Chana4636962009-06-08 18:14:43 -0700795 cp->l2_ring = NULL;
796 }
797
Michael Chan86b53602009-10-10 13:46:57 +0000798 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700799 kfree(cp->ctx_arr);
800 cp->ctx_arr = NULL;
801 cp->ctx_blks = 0;
802
803 cnic_free_dma(dev, &cp->gbl_buf_info);
804 cnic_free_dma(dev, &cp->conn_buf_info);
805 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000806 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane6c28892010-06-24 14:58:39 +0000807 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700808 kfree(cp->iscsi_tbl);
809 cp->iscsi_tbl = NULL;
810 kfree(cp->ctx_tbl);
811 cp->ctx_tbl = NULL;
812
813 cnic_free_id_tbl(&cp->cid_tbl);
814}
815
816static int cnic_alloc_context(struct cnic_dev *dev)
817{
818 struct cnic_local *cp = dev->cnic_priv;
819
820 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
821 int i, k, arr_size;
822
823 cp->ctx_blk_size = BCM_PAGE_SIZE;
824 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
825 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
826 sizeof(struct cnic_ctx);
827 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
828 if (cp->ctx_arr == NULL)
829 return -ENOMEM;
830
831 k = 0;
832 for (i = 0; i < 2; i++) {
833 u32 j, reg, off, lo, hi;
834
835 if (i == 0)
836 off = BNX2_PG_CTX_MAP;
837 else
838 off = BNX2_ISCSI_CTX_MAP;
839
840 reg = cnic_reg_rd_ind(dev, off);
841 lo = reg >> 16;
842 hi = reg & 0xffff;
843 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
844 cp->ctx_arr[k].cid = j;
845 }
846
847 cp->ctx_blks = k;
848 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
849 cp->ctx_blks = 0;
850 return -ENOMEM;
851 }
852
853 for (i = 0; i < cp->ctx_blks; i++) {
854 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000855 dma_alloc_coherent(&dev->pcidev->dev,
856 BCM_PAGE_SIZE,
857 &cp->ctx_arr[i].mapping,
858 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700859 if (cp->ctx_arr[i].ctx == NULL)
860 return -ENOMEM;
861 }
862 }
863 return 0;
864}
865
Michael Chane6c28892010-06-24 14:58:39 +0000866static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
867{
868 int err, i, is_bnx2 = 0;
869 struct kcqe **kcq;
870
871 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
872 is_bnx2 = 1;
873
874 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
875 if (err)
876 return err;
877
878 kcq = (struct kcqe **) info->dma.pg_arr;
879 info->kcq = kcq;
880
881 if (is_bnx2)
882 return 0;
883
884 for (i = 0; i < KCQ_PAGE_CNT; i++) {
885 struct bnx2x_bd_chain_next *next =
886 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
887 int j = i + 1;
888
889 if (j >= KCQ_PAGE_CNT)
890 j = 0;
891 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
892 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
893 }
894 return 0;
895}
896
Michael Chanec0248e2009-08-26 09:49:22 +0000897static int cnic_alloc_l2_rings(struct cnic_dev *dev, int pages)
898{
899 struct cnic_local *cp = dev->cnic_priv;
900
901 cp->l2_ring_size = pages * BCM_PAGE_SIZE;
Michael Chan3248e162009-12-02 15:15:39 +0000902 cp->l2_ring = dma_alloc_coherent(&dev->pcidev->dev, cp->l2_ring_size,
903 &cp->l2_ring_map,
904 GFP_KERNEL | __GFP_COMP);
Michael Chanec0248e2009-08-26 09:49:22 +0000905 if (!cp->l2_ring)
906 return -ENOMEM;
907
908 cp->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
909 cp->l2_buf_size = PAGE_ALIGN(cp->l2_buf_size);
Michael Chan3248e162009-12-02 15:15:39 +0000910 cp->l2_buf = dma_alloc_coherent(&dev->pcidev->dev, cp->l2_buf_size,
911 &cp->l2_buf_map,
912 GFP_KERNEL | __GFP_COMP);
Michael Chanec0248e2009-08-26 09:49:22 +0000913 if (!cp->l2_buf)
914 return -ENOMEM;
915
916 return 0;
917}
918
Michael Chan5e9b2db2009-08-26 09:49:23 +0000919static int cnic_alloc_uio(struct cnic_dev *dev) {
920 struct cnic_local *cp = dev->cnic_priv;
921 struct uio_info *uinfo;
922 int ret;
923
924 uinfo = kzalloc(sizeof(*uinfo), GFP_ATOMIC);
925 if (!uinfo)
926 return -ENOMEM;
927
928 uinfo->mem[0].addr = dev->netdev->base_addr;
929 uinfo->mem[0].internal_addr = dev->regview;
930 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
931 uinfo->mem[0].memtype = UIO_MEM_PHYS;
932
Michael Chan5e9b2db2009-08-26 09:49:23 +0000933 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +0000934 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
935 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000936 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
937 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
938 else
939 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
940
941 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +0000942 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
943 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
944 PAGE_MASK;
945 uinfo->mem[1].size = sizeof(struct host_def_status_block);
946
947 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +0000948 }
949
950 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
951
952 uinfo->mem[2].addr = (unsigned long) cp->l2_ring;
953 uinfo->mem[2].size = cp->l2_ring_size;
954 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
955
956 uinfo->mem[3].addr = (unsigned long) cp->l2_buf;
957 uinfo->mem[3].size = cp->l2_buf_size;
958 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
959
960 uinfo->version = CNIC_MODULE_VERSION;
961 uinfo->irq = UIO_IRQ_CUSTOM;
962
963 uinfo->open = cnic_uio_open;
964 uinfo->release = cnic_uio_close;
965
966 uinfo->priv = dev;
967
968 ret = uio_register_device(&dev->pcidev->dev, uinfo);
969 if (ret) {
970 kfree(uinfo);
971 return ret;
972 }
973
974 cp->cnic_uinfo = uinfo;
975 return 0;
976}
977
Michael Chana4636962009-06-08 18:14:43 -0700978static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
979{
980 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -0700981 int ret;
982
983 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
984 if (ret)
985 goto error;
986 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
987
Michael Chane6c28892010-06-24 14:58:39 +0000988 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chana4636962009-06-08 18:14:43 -0700989 if (ret)
990 goto error;
Michael Chana4636962009-06-08 18:14:43 -0700991
992 ret = cnic_alloc_context(dev);
993 if (ret)
994 goto error;
995
Michael Chanec0248e2009-08-26 09:49:22 +0000996 ret = cnic_alloc_l2_rings(dev, 2);
997 if (ret)
Michael Chana4636962009-06-08 18:14:43 -0700998 goto error;
999
Michael Chan5e9b2db2009-08-26 09:49:23 +00001000 ret = cnic_alloc_uio(dev);
1001 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001002 goto error;
1003
Michael Chana4636962009-06-08 18:14:43 -07001004 return 0;
1005
1006error:
1007 cnic_free_resc(dev);
1008 return ret;
1009}
1010
Michael Chan71034ba2009-10-10 13:46:59 +00001011static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1012{
1013 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001014 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001015 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001016
Michael Chan520efdf2010-06-24 14:58:37 +00001017 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001018 blks = total_mem / ctx_blk_size;
1019 if (total_mem % ctx_blk_size)
1020 blks++;
1021
1022 if (blks > cp->ethdev->ctx_tbl_len)
1023 return -ENOMEM;
1024
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001025 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001026 if (cp->ctx_arr == NULL)
1027 return -ENOMEM;
1028
1029 cp->ctx_blks = blks;
1030 cp->ctx_blk_size = ctx_blk_size;
1031 if (BNX2X_CHIP_IS_E1H(cp->chip_id))
1032 cp->ctx_align = 0;
1033 else
1034 cp->ctx_align = ctx_blk_size;
1035
1036 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1037
1038 for (i = 0; i < blks; i++) {
1039 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001040 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1041 &cp->ctx_arr[i].mapping,
1042 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001043 if (cp->ctx_arr[i].ctx == NULL)
1044 return -ENOMEM;
1045
1046 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1047 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1048 cnic_free_context(dev);
1049 cp->ctx_blk_size += cp->ctx_align;
1050 i = -1;
1051 continue;
1052 }
1053 }
1054 }
1055 return 0;
1056}
1057
1058static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1059{
1060 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001061 struct cnic_eth_dev *ethdev = cp->ethdev;
1062 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001063 int i, j, n, ret, pages;
1064 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1065
Michael Chan520efdf2010-06-24 14:58:37 +00001066 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
1067 cp->iscsi_start_cid = start_cid;
1068 if (start_cid < BNX2X_ISCSI_START_CID) {
1069 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1070
1071 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
1072 cp->max_cid_space += delta;
1073 }
1074
Michael Chan71034ba2009-10-10 13:46:59 +00001075 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1076 GFP_KERNEL);
1077 if (!cp->iscsi_tbl)
1078 goto error;
1079
1080 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001081 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001082 if (!cp->ctx_tbl)
1083 goto error;
1084
1085 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1086 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1087 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1088 }
1089
Michael Chan520efdf2010-06-24 14:58:37 +00001090 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001091 PAGE_SIZE;
1092
1093 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1094 if (ret)
1095 return -ENOMEM;
1096
1097 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001098 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001099 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1100
1101 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1102 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1103 off;
1104
1105 if ((i % n) == (n - 1))
1106 j++;
1107 }
1108
Michael Chane6c28892010-06-24 14:58:39 +00001109 ret = cnic_alloc_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00001110 if (ret)
1111 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001112
1113 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1114 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1115 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1116 if (ret)
1117 goto error;
1118
1119 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1120 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1121 if (ret)
1122 goto error;
1123
1124 ret = cnic_alloc_bnx2x_context(dev);
1125 if (ret)
1126 goto error;
1127
Michael Chan71034ba2009-10-10 13:46:59 +00001128 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1129
Michael Chana4dde3a2010-02-24 14:42:08 +00001130 memset(cp->status_blk.bnx2x, 0, sizeof(*cp->status_blk.bnx2x));
Michael Chan4e9c4fd2009-12-10 15:40:58 +00001131
Michael Chan71034ba2009-10-10 13:46:59 +00001132 cp->l2_rx_ring_size = 15;
1133
1134 ret = cnic_alloc_l2_rings(dev, 4);
1135 if (ret)
1136 goto error;
1137
1138 ret = cnic_alloc_uio(dev);
1139 if (ret)
1140 goto error;
1141
1142 return 0;
1143
1144error:
1145 cnic_free_resc(dev);
1146 return -ENOMEM;
1147}
1148
Michael Chana4636962009-06-08 18:14:43 -07001149static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1150{
1151 return cp->max_kwq_idx -
1152 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1153}
1154
1155static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1156 u32 num_wqes)
1157{
1158 struct cnic_local *cp = dev->cnic_priv;
1159 struct kwqe *prod_qe;
1160 u16 prod, sw_prod, i;
1161
1162 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1163 return -EAGAIN; /* bnx2 is down */
1164
1165 spin_lock_bh(&cp->cnic_ulp_lock);
1166 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001167 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001168 spin_unlock_bh(&cp->cnic_ulp_lock);
1169 return -EAGAIN;
1170 }
1171
Michael Chan1f1332a2010-05-18 11:32:52 +00001172 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001173
1174 prod = cp->kwq_prod_idx;
1175 sw_prod = prod & MAX_KWQ_IDX;
1176 for (i = 0; i < num_wqes; i++) {
1177 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1178 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1179 prod++;
1180 sw_prod = prod & MAX_KWQ_IDX;
1181 }
1182 cp->kwq_prod_idx = prod;
1183
1184 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1185
1186 spin_unlock_bh(&cp->cnic_ulp_lock);
1187 return 0;
1188}
1189
Michael Chan71034ba2009-10-10 13:46:59 +00001190static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1191 union l5cm_specific_data *l5_data)
1192{
1193 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1194 dma_addr_t map;
1195
1196 map = ctx->kwqe_data_mapping;
1197 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1198 l5_data->phy_address.hi = (u64) map >> 32;
1199 return ctx->kwqe_data;
1200}
1201
1202static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1203 u32 type, union l5cm_specific_data *l5_data)
1204{
1205 struct cnic_local *cp = dev->cnic_priv;
1206 struct l5cm_spe kwqe;
1207 struct kwqe_16 *kwq[1];
1208 int ret;
1209
1210 kwqe.hdr.conn_and_cmd_data =
1211 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001212 BNX2X_HW_CID(cp, cid)));
Michael Chan71034ba2009-10-10 13:46:59 +00001213 kwqe.hdr.type = cpu_to_le16(type);
1214 kwqe.hdr.reserved = 0;
1215 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1216 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1217
1218 kwq[0] = (struct kwqe_16 *) &kwqe;
1219
1220 spin_lock_bh(&cp->cnic_ulp_lock);
1221 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1222 spin_unlock_bh(&cp->cnic_ulp_lock);
1223
1224 if (ret == 1)
1225 return 0;
1226
1227 return -EBUSY;
1228}
1229
1230static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1231 struct kcqe *cqes[], u32 num_cqes)
1232{
1233 struct cnic_local *cp = dev->cnic_priv;
1234 struct cnic_ulp_ops *ulp_ops;
1235
1236 rcu_read_lock();
1237 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1238 if (likely(ulp_ops)) {
1239 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1240 cqes, num_cqes);
1241 }
1242 rcu_read_unlock();
1243}
1244
1245static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1246{
1247 struct cnic_local *cp = dev->cnic_priv;
1248 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001249 int hq_bds, pages;
1250 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001251
1252 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1253 cp->num_ccells = req1->num_ccells_per_conn;
1254 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1255 cp->num_iscsi_tasks;
1256 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1257 BNX2X_ISCSI_R2TQE_SIZE;
1258 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1259 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1260 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1261 cp->num_cqs = req1->num_cqs;
1262
1263 if (!dev->max_iscsi_conn)
1264 return 0;
1265
1266 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001267 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001268 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001269 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001270 PAGE_SIZE);
1271 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001272 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001273 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001274 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001275 req1->num_tasks_per_conn);
1276
1277 /* init Ustorm RAM */
1278 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001279 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001280 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001281 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001282 PAGE_SIZE);
1283 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001284 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001285 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001286 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001287 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001288 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001289 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001290 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001291 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001292 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001293 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1294
1295 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001296 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001297 PAGE_SIZE);
1298 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001299 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001300 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001301 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001302 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001303 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001304 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001305 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001306 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001307 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001308 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1309
1310 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001311 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001312 PAGE_SIZE);
1313 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001314 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001315 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001316 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001317 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001318 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001319 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001320 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001321 hq_bds);
1322
1323 return 0;
1324}
1325
1326static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1327{
1328 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1329 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001330 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001331 struct iscsi_kcqe kcqe;
1332 struct kcqe *cqes[1];
1333
1334 memset(&kcqe, 0, sizeof(kcqe));
1335 if (!dev->max_iscsi_conn) {
1336 kcqe.completion_status =
1337 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1338 goto done;
1339 }
1340
1341 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001342 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001343 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001344 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001345 req2->error_bit_map[1]);
1346
1347 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001348 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001349 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001350 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001351 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001352 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001353 req2->error_bit_map[1]);
1354
1355 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001356 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001357
1358 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1359
1360done:
1361 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1362 cqes[0] = (struct kcqe *) &kcqe;
1363 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1364
1365 return 0;
1366}
1367
1368static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1369{
1370 struct cnic_local *cp = dev->cnic_priv;
1371 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1372
1373 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1374 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1375
1376 cnic_free_dma(dev, &iscsi->hq_info);
1377 cnic_free_dma(dev, &iscsi->r2tq_info);
1378 cnic_free_dma(dev, &iscsi->task_array_info);
1379 }
1380 cnic_free_id(&cp->cid_tbl, ctx->cid);
1381 ctx->cid = 0;
1382}
1383
1384static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1385{
1386 u32 cid;
1387 int ret, pages;
1388 struct cnic_local *cp = dev->cnic_priv;
1389 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1390 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1391
1392 cid = cnic_alloc_new_id(&cp->cid_tbl);
1393 if (cid == -1) {
1394 ret = -ENOMEM;
1395 goto error;
1396 }
1397
1398 ctx->cid = cid;
1399 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1400
1401 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1402 if (ret)
1403 goto error;
1404
1405 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1406 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1407 if (ret)
1408 goto error;
1409
1410 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1411 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1412 if (ret)
1413 goto error;
1414
1415 return 0;
1416
1417error:
1418 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1419 return ret;
1420}
1421
1422static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1423 struct regpair *ctx_addr)
1424{
1425 struct cnic_local *cp = dev->cnic_priv;
1426 struct cnic_eth_dev *ethdev = cp->ethdev;
1427 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1428 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1429 unsigned long align_off = 0;
1430 dma_addr_t ctx_map;
1431 void *ctx;
1432
1433 if (cp->ctx_align) {
1434 unsigned long mask = cp->ctx_align - 1;
1435
1436 if (cp->ctx_arr[blk].mapping & mask)
1437 align_off = cp->ctx_align -
1438 (cp->ctx_arr[blk].mapping & mask);
1439 }
1440 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1441 (off * BNX2X_CONTEXT_MEM_SIZE);
1442 ctx = cp->ctx_arr[blk].ctx + align_off +
1443 (off * BNX2X_CONTEXT_MEM_SIZE);
1444 if (init)
1445 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1446
1447 ctx_addr->lo = ctx_map & 0xffffffff;
1448 ctx_addr->hi = (u64) ctx_map >> 32;
1449 return ctx;
1450}
1451
1452static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1453 u32 num)
1454{
1455 struct cnic_local *cp = dev->cnic_priv;
1456 struct iscsi_kwqe_conn_offload1 *req1 =
1457 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1458 struct iscsi_kwqe_conn_offload2 *req2 =
1459 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1460 struct iscsi_kwqe_conn_offload3 *req3;
1461 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1462 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1463 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001464 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001465 struct iscsi_context *ictx;
1466 struct regpair context_addr;
1467 int i, j, n = 2, n_max;
1468
1469 ctx->ctx_flags = 0;
1470 if (!req2->num_additional_wqes)
1471 return -EINVAL;
1472
1473 n_max = req2->num_additional_wqes + 2;
1474
1475 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1476 if (ictx == NULL)
1477 return -ENOMEM;
1478
1479 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1480
1481 ictx->xstorm_ag_context.hq_prod = 1;
1482
1483 ictx->xstorm_st_context.iscsi.first_burst_length =
1484 ISCSI_DEF_FIRST_BURST_LEN;
1485 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1486 ISCSI_DEF_MAX_RECV_SEG_LEN;
1487 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1488 req1->sq_page_table_addr_lo;
1489 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1490 req1->sq_page_table_addr_hi;
1491 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1492 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1493 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1494 iscsi->hq_info.pgtbl_map & 0xffffffff;
1495 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1496 (u64) iscsi->hq_info.pgtbl_map >> 32;
1497 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1498 iscsi->hq_info.pgtbl[0];
1499 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1500 iscsi->hq_info.pgtbl[1];
1501 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1502 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1503 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1504 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1505 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1506 iscsi->r2tq_info.pgtbl[0];
1507 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1508 iscsi->r2tq_info.pgtbl[1];
1509 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1510 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1511 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1512 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1513 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1514 BNX2X_ISCSI_PBL_NOT_CACHED;
1515 ictx->xstorm_st_context.iscsi.flags.flags |=
1516 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1517 ictx->xstorm_st_context.iscsi.flags.flags |=
1518 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1519
1520 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1521 /* TSTORM requires the base address of RQ DB & not PTE */
1522 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1523 req2->rq_page_table_addr_lo & PAGE_MASK;
1524 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1525 req2->rq_page_table_addr_hi;
1526 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1527 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1528 ictx->tstorm_st_context.tcp.flags2 |=
1529 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
1530
1531 ictx->timers_context.flags |= ISCSI_TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
1532
1533 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001534 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001535 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001536 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001537 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1538 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1539 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1540 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1541 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1542 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1543 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1544 iscsi->r2tq_info.pgtbl[0];
1545 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1546 iscsi->r2tq_info.pgtbl[1];
1547 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1548 req1->cq_page_table_addr_lo;
1549 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1550 req1->cq_page_table_addr_hi;
1551 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1552 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1553 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1554 ictx->ustorm_st_context.task_pbe_cache_index =
1555 BNX2X_ISCSI_PBL_NOT_CACHED;
1556 ictx->ustorm_st_context.task_pdu_cache_index =
1557 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1558
1559 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1560 if (j == 3) {
1561 if (n >= n_max)
1562 break;
1563 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1564 j = 0;
1565 }
1566 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1567 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1568 req3->qp_first_pte[j].hi;
1569 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1570 req3->qp_first_pte[j].lo;
1571 }
1572
1573 ictx->ustorm_st_context.task_pbl_base.lo =
1574 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1575 ictx->ustorm_st_context.task_pbl_base.hi =
1576 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1577 ictx->ustorm_st_context.tce_phy_addr.lo =
1578 iscsi->task_array_info.pgtbl[0];
1579 ictx->ustorm_st_context.tce_phy_addr.hi =
1580 iscsi->task_array_info.pgtbl[1];
1581 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1582 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1583 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1584 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1585 ISCSI_DEF_MAX_BURST_LEN;
1586 ictx->ustorm_st_context.negotiated_rx |=
1587 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1588 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1589
1590 ictx->cstorm_st_context.hq_pbl_base.lo =
1591 iscsi->hq_info.pgtbl_map & 0xffffffff;
1592 ictx->cstorm_st_context.hq_pbl_base.hi =
1593 (u64) iscsi->hq_info.pgtbl_map >> 32;
1594 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1595 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1596 ictx->cstorm_st_context.task_pbl_base.lo =
1597 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1598 ictx->cstorm_st_context.task_pbl_base.hi =
1599 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1600 /* CSTORM and USTORM initialization is different, CSTORM requires
1601 * CQ DB base & not PTE addr */
1602 ictx->cstorm_st_context.cq_db_base.lo =
1603 req1->cq_page_table_addr_lo & PAGE_MASK;
1604 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1605 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1606 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1607 for (i = 0; i < cp->num_cqs; i++) {
1608 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1609 ISCSI_INITIAL_SN;
1610 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1611 ISCSI_INITIAL_SN;
1612 }
1613
1614 ictx->xstorm_ag_context.cdu_reserved =
1615 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1616 ISCSI_CONNECTION_TYPE);
1617 ictx->ustorm_ag_context.cdu_usage =
1618 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1619 ISCSI_CONNECTION_TYPE);
1620 return 0;
1621
1622}
1623
1624static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1625 u32 num, int *work)
1626{
1627 struct iscsi_kwqe_conn_offload1 *req1;
1628 struct iscsi_kwqe_conn_offload2 *req2;
1629 struct cnic_local *cp = dev->cnic_priv;
1630 struct iscsi_kcqe kcqe;
1631 struct kcqe *cqes[1];
1632 u32 l5_cid;
1633 int ret;
1634
1635 if (num < 2) {
1636 *work = num;
1637 return -EINVAL;
1638 }
1639
1640 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1641 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1642 if ((num - 2) < req2->num_additional_wqes) {
1643 *work = num;
1644 return -EINVAL;
1645 }
1646 *work = 2 + req2->num_additional_wqes;;
1647
1648 l5_cid = req1->iscsi_conn_id;
1649 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1650 return -EINVAL;
1651
1652 memset(&kcqe, 0, sizeof(kcqe));
1653 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1654 kcqe.iscsi_conn_id = l5_cid;
1655 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1656
1657 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1658 atomic_dec(&cp->iscsi_conn);
1659 ret = 0;
1660 goto done;
1661 }
1662 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1663 if (ret) {
1664 atomic_dec(&cp->iscsi_conn);
1665 ret = 0;
1666 goto done;
1667 }
1668 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1669 if (ret < 0) {
1670 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1671 atomic_dec(&cp->iscsi_conn);
1672 goto done;
1673 }
1674
1675 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001676 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001677
1678done:
1679 cqes[0] = (struct kcqe *) &kcqe;
1680 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1681 return ret;
1682}
1683
1684
1685static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1686{
1687 struct cnic_local *cp = dev->cnic_priv;
1688 struct iscsi_kwqe_conn_update *req =
1689 (struct iscsi_kwqe_conn_update *) kwqe;
1690 void *data;
1691 union l5cm_specific_data l5_data;
1692 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1693 int ret;
1694
1695 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1696 return -EINVAL;
1697
1698 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1699 if (!data)
1700 return -ENOMEM;
1701
1702 memcpy(data, kwqe, sizeof(struct kwqe));
1703
1704 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1705 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1706 return ret;
1707}
1708
1709static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1710{
1711 struct cnic_local *cp = dev->cnic_priv;
1712 struct iscsi_kwqe_conn_destroy *req =
1713 (struct iscsi_kwqe_conn_destroy *) kwqe;
1714 union l5cm_specific_data l5_data;
1715 u32 l5_cid = req->reserved0;
1716 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1717 int ret = 0;
1718 struct iscsi_kcqe kcqe;
1719 struct kcqe *cqes[1];
1720
1721 if (!(ctx->ctx_flags & CTX_FL_OFFLD_START))
1722 goto skip_cfc_delete;
1723
1724 while (!time_after(jiffies, ctx->timestamp + (2 * HZ)))
1725 msleep(250);
1726
1727 init_waitqueue_head(&ctx->waitq);
1728 ctx->wait_cond = 0;
1729 memset(&l5_data, 0, sizeof(l5_data));
1730 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CFC_DEL,
1731 req->context_id,
1732 ETH_CONNECTION_TYPE |
1733 (1 << SPE_HDR_COMMON_RAMROD_SHIFT),
1734 &l5_data);
1735 if (ret == 0)
1736 wait_event(ctx->waitq, ctx->wait_cond);
1737
1738skip_cfc_delete:
1739 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1740
1741 atomic_dec(&cp->iscsi_conn);
1742
1743 memset(&kcqe, 0, sizeof(kcqe));
1744 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1745 kcqe.iscsi_conn_id = l5_cid;
1746 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1747 kcqe.iscsi_conn_context_id = req->context_id;
1748
1749 cqes[0] = (struct kcqe *) &kcqe;
1750 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1751
1752 return ret;
1753}
1754
1755static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1756 struct l4_kwq_connect_req1 *kwqe1,
1757 struct l4_kwq_connect_req3 *kwqe3,
1758 struct l5cm_active_conn_buffer *conn_buf)
1759{
1760 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1761 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1762 &conn_buf->xstorm_conn_buffer;
1763 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1764 &conn_buf->tstorm_conn_buffer;
1765 struct regpair context_addr;
1766 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1767 struct in6_addr src_ip, dst_ip;
1768 int i;
1769 u32 *addrp;
1770
1771 addrp = (u32 *) &conn_addr->local_ip_addr;
1772 for (i = 0; i < 4; i++, addrp++)
1773 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1774
1775 addrp = (u32 *) &conn_addr->remote_ip_addr;
1776 for (i = 0; i < 4; i++, addrp++)
1777 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1778
1779 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1780
1781 xstorm_buf->context_addr.hi = context_addr.hi;
1782 xstorm_buf->context_addr.lo = context_addr.lo;
1783 xstorm_buf->mss = 0xffff;
1784 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1785 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1786 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1787 xstorm_buf->pseudo_header_checksum =
1788 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1789
1790 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1791 tstorm_buf->params |=
1792 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1793 if (kwqe3->ka_timeout) {
1794 tstorm_buf->ka_enable = 1;
1795 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1796 tstorm_buf->ka_interval = kwqe3->ka_interval;
1797 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1798 }
1799 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1800 tstorm_buf->snd_buf = kwqe3->snd_buf;
1801 tstorm_buf->max_rt_time = 0xffffffff;
1802}
1803
1804static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1805{
1806 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001807 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001808 u8 *mac = dev->mac_addr;
1809
1810 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001811 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001812 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001813 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001814 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001815 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001816 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001817 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001818 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001819 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001820 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001821 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001822
1823 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001824 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001825 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001826 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001827 mac[4]);
1828 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001829 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001830 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001831 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00001832 mac[2]);
1833 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001834 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00001835 mac[1]);
1836 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001837 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00001838 mac[0]);
1839}
1840
1841static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1842{
1843 struct cnic_local *cp = dev->cnic_priv;
1844 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1845 u16 tstorm_flags = 0;
1846
1847 if (tcp_ts) {
1848 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1849 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1850 }
1851
1852 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001853 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001854
1855 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001856 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001857}
1858
1859static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
1860 u32 num, int *work)
1861{
1862 struct cnic_local *cp = dev->cnic_priv;
1863 struct l4_kwq_connect_req1 *kwqe1 =
1864 (struct l4_kwq_connect_req1 *) wqes[0];
1865 struct l4_kwq_connect_req3 *kwqe3;
1866 struct l5cm_active_conn_buffer *conn_buf;
1867 struct l5cm_conn_addr_params *conn_addr;
1868 union l5cm_specific_data l5_data;
1869 u32 l5_cid = kwqe1->pg_cid;
1870 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1871 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1872 int ret;
1873
1874 if (num < 2) {
1875 *work = num;
1876 return -EINVAL;
1877 }
1878
1879 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
1880 *work = 3;
1881 else
1882 *work = 2;
1883
1884 if (num < *work) {
1885 *work = num;
1886 return -EINVAL;
1887 }
1888
1889 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00001890 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00001891 return -ENOMEM;
1892 }
1893 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1894 if (!conn_buf)
1895 return -ENOMEM;
1896
1897 memset(conn_buf, 0, sizeof(*conn_buf));
1898
1899 conn_addr = &conn_buf->conn_addr_buf;
1900 conn_addr->remote_addr_0 = csk->ha[0];
1901 conn_addr->remote_addr_1 = csk->ha[1];
1902 conn_addr->remote_addr_2 = csk->ha[2];
1903 conn_addr->remote_addr_3 = csk->ha[3];
1904 conn_addr->remote_addr_4 = csk->ha[4];
1905 conn_addr->remote_addr_5 = csk->ha[5];
1906
1907 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
1908 struct l4_kwq_connect_req2 *kwqe2 =
1909 (struct l4_kwq_connect_req2 *) wqes[1];
1910
1911 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
1912 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
1913 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
1914
1915 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
1916 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
1917 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
1918 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
1919 }
1920 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
1921
1922 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
1923 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
1924 conn_addr->local_tcp_port = kwqe1->src_port;
1925 conn_addr->remote_tcp_port = kwqe1->dst_port;
1926
1927 conn_addr->pmtu = kwqe3->pmtu;
1928 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
1929
1930 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001931 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00001932
1933 cnic_bnx2x_set_tcp_timestamp(dev,
1934 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
1935
1936 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
1937 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
1938 if (!ret)
1939 ctx->ctx_flags |= CTX_FL_OFFLD_START;
1940
1941 return ret;
1942}
1943
1944static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
1945{
1946 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
1947 union l5cm_specific_data l5_data;
1948 int ret;
1949
1950 memset(&l5_data, 0, sizeof(l5_data));
1951 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
1952 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
1953 return ret;
1954}
1955
1956static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
1957{
1958 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
1959 union l5cm_specific_data l5_data;
1960 int ret;
1961
1962 memset(&l5_data, 0, sizeof(l5_data));
1963 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
1964 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
1965 return ret;
1966}
1967static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
1968{
1969 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
1970 struct l4_kcq kcqe;
1971 struct kcqe *cqes[1];
1972
1973 memset(&kcqe, 0, sizeof(kcqe));
1974 kcqe.pg_host_opaque = req->host_opaque;
1975 kcqe.pg_cid = req->host_opaque;
1976 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
1977 cqes[0] = (struct kcqe *) &kcqe;
1978 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
1979 return 0;
1980}
1981
1982static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
1983{
1984 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
1985 struct l4_kcq kcqe;
1986 struct kcqe *cqes[1];
1987
1988 memset(&kcqe, 0, sizeof(kcqe));
1989 kcqe.pg_host_opaque = req->pg_host_opaque;
1990 kcqe.pg_cid = req->pg_cid;
1991 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
1992 cqes[0] = (struct kcqe *) &kcqe;
1993 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
1994 return 0;
1995}
1996
1997static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1998 u32 num_wqes)
1999{
2000 int i, work, ret;
2001 u32 opcode;
2002 struct kwqe *kwqe;
2003
2004 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2005 return -EAGAIN; /* bnx2 is down */
2006
2007 for (i = 0; i < num_wqes; ) {
2008 kwqe = wqes[i];
2009 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2010 work = 1;
2011
2012 switch (opcode) {
2013 case ISCSI_KWQE_OPCODE_INIT1:
2014 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2015 break;
2016 case ISCSI_KWQE_OPCODE_INIT2:
2017 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2018 break;
2019 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2020 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2021 num_wqes - i, &work);
2022 break;
2023 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2024 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2025 break;
2026 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2027 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2028 break;
2029 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2030 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2031 &work);
2032 break;
2033 case L4_KWQE_OPCODE_VALUE_CLOSE:
2034 ret = cnic_bnx2x_close(dev, kwqe);
2035 break;
2036 case L4_KWQE_OPCODE_VALUE_RESET:
2037 ret = cnic_bnx2x_reset(dev, kwqe);
2038 break;
2039 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2040 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2041 break;
2042 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2043 ret = cnic_bnx2x_update_pg(dev, kwqe);
2044 break;
2045 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2046 ret = 0;
2047 break;
2048 default:
2049 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002050 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2051 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002052 break;
2053 }
2054 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002055 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2056 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002057 i += work;
2058 }
2059 return 0;
2060}
2061
Michael Chana4636962009-06-08 18:14:43 -07002062static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2063{
2064 struct cnic_local *cp = dev->cnic_priv;
2065 int i, j;
2066
2067 i = 0;
2068 j = 1;
2069 while (num_cqes) {
2070 struct cnic_ulp_ops *ulp_ops;
2071 int ulp_type;
2072 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
2073 u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
2074
2075 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
2076 cnic_kwq_completion(dev, 1);
2077
2078 while (j < num_cqes) {
2079 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2080
2081 if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
2082 break;
2083
2084 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
2085 cnic_kwq_completion(dev, 1);
2086 j++;
2087 }
2088
2089 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2090 ulp_type = CNIC_ULP_RDMA;
2091 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2092 ulp_type = CNIC_ULP_ISCSI;
2093 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2094 ulp_type = CNIC_ULP_L4;
2095 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2096 goto end;
2097 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002098 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2099 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002100 goto end;
2101 }
2102
2103 rcu_read_lock();
2104 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2105 if (likely(ulp_ops)) {
2106 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2107 cp->completed_kcq + i, j);
2108 }
2109 rcu_read_unlock();
2110end:
2111 num_cqes -= j;
2112 i += j;
2113 j = 1;
2114 }
Michael Chana4636962009-06-08 18:14:43 -07002115}
2116
2117static u16 cnic_bnx2_next_idx(u16 idx)
2118{
2119 return idx + 1;
2120}
2121
2122static u16 cnic_bnx2_hw_idx(u16 idx)
2123{
2124 return idx;
2125}
2126
Michael Chan71034ba2009-10-10 13:46:59 +00002127static u16 cnic_bnx2x_next_idx(u16 idx)
2128{
2129 idx++;
2130 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2131 idx++;
2132
2133 return idx;
2134}
2135
2136static u16 cnic_bnx2x_hw_idx(u16 idx)
2137{
2138 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2139 idx++;
2140 return idx;
2141}
2142
Michael Chan644b9d42010-06-24 14:58:40 +00002143static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002144{
2145 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002146 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002147 struct kcqe *kcqe;
2148 int kcqe_cnt = 0, last_cnt = 0;
2149
Michael Chan644b9d42010-06-24 14:58:40 +00002150 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002151 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002152 hw_prod = *info->hw_prod_idx_ptr;
2153 hw_prod = cp->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002154
2155 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002156 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002157 cp->completed_kcq[kcqe_cnt++] = kcqe;
2158 i = cp->next_idx(i);
2159 ri = i & MAX_KCQ_IDX;
2160 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2161 last_cnt = kcqe_cnt;
2162 last = i;
2163 }
2164 }
2165
Michael Chan644b9d42010-06-24 14:58:40 +00002166 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002167 return last_cnt;
2168}
2169
Michael Chan48f753d2010-05-18 11:32:53 +00002170static int cnic_l2_completion(struct cnic_local *cp)
2171{
2172 u16 hw_cons, sw_cons;
2173 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
2174 (cp->l2_ring + (2 * BCM_PAGE_SIZE));
2175 u32 cmd;
2176 int comp = 0;
2177
2178 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2179 return 0;
2180
2181 hw_cons = *cp->rx_cons_ptr;
2182 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2183 hw_cons++;
2184
2185 sw_cons = cp->rx_cons;
2186 while (sw_cons != hw_cons) {
2187 u8 cqe_fp_flags;
2188
2189 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2190 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2191 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2192 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2193 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2194 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2195 cmd == RAMROD_CMD_ID_ETH_HALT)
2196 comp++;
2197 }
2198 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2199 }
2200 return comp;
2201}
2202
Michael Chan86b53602009-10-10 13:46:57 +00002203static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002204{
2205 u16 rx_cons = *cp->rx_cons_ptr;
2206 u16 tx_cons = *cp->tx_cons_ptr;
Michael Chan48f753d2010-05-18 11:32:53 +00002207 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002208
Michael Chan66fee9e2010-06-24 14:58:38 +00002209 if (!test_bit(CNIC_F_CNIC_UP, &cp->dev->flags))
2210 return;
2211
Michael Chana4636962009-06-08 18:14:43 -07002212 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002213 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2214 comp = cnic_l2_completion(cp);
2215
Michael Chana4636962009-06-08 18:14:43 -07002216 cp->tx_cons = tx_cons;
2217 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002218
Michael Chana4636962009-06-08 18:14:43 -07002219 uio_event_notify(cp->cnic_uinfo);
2220 }
Michael Chan48f753d2010-05-18 11:32:53 +00002221 if (comp)
2222 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002223}
2224
Michael Chanb177a5d52010-06-24 14:58:41 +00002225static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002226{
Michael Chana4636962009-06-08 18:14:43 -07002227 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002228 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002229 int kcqe_cnt;
2230
Michael Chana4636962009-06-08 18:14:43 -07002231 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2232
Michael Chan644b9d42010-06-24 14:58:40 +00002233 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002234
2235 service_kcqes(dev, kcqe_cnt);
2236
2237 /* Tell compiler that status_blk fields can change. */
2238 barrier();
Michael Chan644b9d42010-06-24 14:58:40 +00002239 if (status_idx != *cp->kcq1.status_idx_ptr) {
Michael Chanb177a5d52010-06-24 14:58:41 +00002240 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002241 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002242 } else
2243 break;
2244 }
2245
Michael Chan644b9d42010-06-24 14:58:40 +00002246 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002247
Michael Chan86b53602009-10-10 13:46:57 +00002248 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002249
Michael Chana4636962009-06-08 18:14:43 -07002250 return status_idx;
2251}
2252
Michael Chanb177a5d52010-06-24 14:58:41 +00002253static int cnic_service_bnx2(void *data, void *status_blk)
2254{
2255 struct cnic_dev *dev = data;
2256 struct cnic_local *cp = dev->cnic_priv;
2257 u32 status_idx = *cp->kcq1.status_idx_ptr;
2258
2259 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2260 return status_idx;
2261
2262 return cnic_service_bnx2_queues(dev);
2263}
2264
Michael Chana4636962009-06-08 18:14:43 -07002265static void cnic_service_bnx2_msix(unsigned long data)
2266{
2267 struct cnic_dev *dev = (struct cnic_dev *) data;
2268 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002269
Michael Chanb177a5d52010-06-24 14:58:41 +00002270 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002271
Michael Chana4636962009-06-08 18:14:43 -07002272 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2273 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2274}
2275
Michael Chan66fee9e2010-06-24 14:58:38 +00002276static void cnic_doirq(struct cnic_dev *dev)
2277{
2278 struct cnic_local *cp = dev->cnic_priv;
Michael Chane6c28892010-06-24 14:58:39 +00002279 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
Michael Chan66fee9e2010-06-24 14:58:38 +00002280
2281 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2282 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002283 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002284
2285 tasklet_schedule(&cp->cnic_irq_task);
2286 }
2287}
2288
Michael Chana4636962009-06-08 18:14:43 -07002289static irqreturn_t cnic_irq(int irq, void *dev_instance)
2290{
2291 struct cnic_dev *dev = dev_instance;
2292 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002293
2294 if (cp->ack_int)
2295 cp->ack_int(dev);
2296
Michael Chan66fee9e2010-06-24 14:58:38 +00002297 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002298
2299 return IRQ_HANDLED;
2300}
2301
Michael Chan71034ba2009-10-10 13:46:59 +00002302static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2303 u16 index, u8 op, u8 update)
2304{
2305 struct cnic_local *cp = dev->cnic_priv;
2306 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2307 COMMAND_REG_INT_ACK);
2308 struct igu_ack_register igu_ack;
2309
2310 igu_ack.status_block_index = index;
2311 igu_ack.sb_id_and_flags =
2312 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2313 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2314 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2315 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2316
2317 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2318}
2319
2320static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2321{
2322 struct cnic_local *cp = dev->cnic_priv;
2323
2324 cnic_ack_bnx2x_int(dev, cp->status_blk_num, CSTORM_ID, 0,
2325 IGU_INT_DISABLE, 0);
2326}
2327
Michael Chanb177a5d52010-06-24 14:58:41 +00002328static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002329{
Michael Chanb177a5d52010-06-24 14:58:41 +00002330 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002331 int kcqe_cnt;
2332
Michael Chanb177a5d52010-06-24 14:58:41 +00002333 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002334
2335 service_kcqes(dev, kcqe_cnt);
2336
2337 /* Tell compiler that sblk fields can change. */
2338 barrier();
Michael Chanb177a5d52010-06-24 14:58:41 +00002339 if (last_status == *info->status_idx_ptr)
Michael Chan71034ba2009-10-10 13:46:59 +00002340 break;
2341
Michael Chanb177a5d52010-06-24 14:58:41 +00002342 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002343 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002344 return last_status;
2345}
2346
2347static void cnic_service_bnx2x_bh(unsigned long data)
2348{
2349 struct cnic_dev *dev = (struct cnic_dev *) data;
2350 struct cnic_local *cp = dev->cnic_priv;
2351 u32 status_idx;
2352
2353 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2354 return;
2355
2356 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002357
Michael Chan644b9d42010-06-24 14:58:40 +00002358 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00002359 cnic_ack_bnx2x_int(dev, cp->status_blk_num, CSTORM_ID,
2360 status_idx, IGU_INT_ENABLE, 1);
Michael Chan71034ba2009-10-10 13:46:59 +00002361}
2362
2363static int cnic_service_bnx2x(void *data, void *status_blk)
2364{
2365 struct cnic_dev *dev = data;
2366 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002367
Michael Chan66fee9e2010-06-24 14:58:38 +00002368 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2369 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002370
Michael Chan66fee9e2010-06-24 14:58:38 +00002371 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002372
2373 return 0;
2374}
2375
Michael Chana4636962009-06-08 18:14:43 -07002376static void cnic_ulp_stop(struct cnic_dev *dev)
2377{
2378 struct cnic_local *cp = dev->cnic_priv;
2379 int if_type;
2380
Michael Chan6d7760a2009-07-27 11:25:58 -07002381 if (cp->cnic_uinfo)
2382 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
2383
Michael Chana4636962009-06-08 18:14:43 -07002384 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2385 struct cnic_ulp_ops *ulp_ops;
2386
Michael Chan681dbd72009-08-14 15:49:46 +00002387 mutex_lock(&cnic_lock);
2388 ulp_ops = cp->ulp_ops[if_type];
2389 if (!ulp_ops) {
2390 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002391 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002392 }
2393 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2394 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002395
2396 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2397 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002398
2399 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002400 }
Michael Chana4636962009-06-08 18:14:43 -07002401}
2402
2403static void cnic_ulp_start(struct cnic_dev *dev)
2404{
2405 struct cnic_local *cp = dev->cnic_priv;
2406 int if_type;
2407
Michael Chana4636962009-06-08 18:14:43 -07002408 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2409 struct cnic_ulp_ops *ulp_ops;
2410
Michael Chan681dbd72009-08-14 15:49:46 +00002411 mutex_lock(&cnic_lock);
2412 ulp_ops = cp->ulp_ops[if_type];
2413 if (!ulp_ops || !ulp_ops->cnic_start) {
2414 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002415 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00002416 }
2417 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2418 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002419
2420 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2421 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00002422
2423 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07002424 }
Michael Chana4636962009-06-08 18:14:43 -07002425}
2426
2427static int cnic_ctl(void *data, struct cnic_ctl_info *info)
2428{
2429 struct cnic_dev *dev = data;
2430
2431 switch (info->cmd) {
2432 case CNIC_CTL_STOP_CMD:
2433 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07002434
2435 cnic_ulp_stop(dev);
2436 cnic_stop_hw(dev);
2437
Michael Chana4636962009-06-08 18:14:43 -07002438 cnic_put(dev);
2439 break;
2440 case CNIC_CTL_START_CMD:
2441 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07002442
2443 if (!cnic_start_hw(dev))
2444 cnic_ulp_start(dev);
2445
Michael Chana4636962009-06-08 18:14:43 -07002446 cnic_put(dev);
2447 break;
Michael Chan71034ba2009-10-10 13:46:59 +00002448 case CNIC_CTL_COMPLETION_CMD: {
2449 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
2450 u32 l5_cid;
2451 struct cnic_local *cp = dev->cnic_priv;
2452
2453 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
2454 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2455
2456 ctx->wait_cond = 1;
2457 wake_up(&ctx->waitq);
2458 }
2459 break;
2460 }
Michael Chana4636962009-06-08 18:14:43 -07002461 default:
2462 return -EINVAL;
2463 }
2464 return 0;
2465}
2466
2467static void cnic_ulp_init(struct cnic_dev *dev)
2468{
2469 int i;
2470 struct cnic_local *cp = dev->cnic_priv;
2471
Michael Chana4636962009-06-08 18:14:43 -07002472 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
2473 struct cnic_ulp_ops *ulp_ops;
2474
Michael Chan7fc1ece2009-08-14 15:49:47 +00002475 mutex_lock(&cnic_lock);
2476 ulp_ops = cnic_ulp_tbl[i];
2477 if (!ulp_ops || !ulp_ops->cnic_init) {
2478 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002479 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00002480 }
2481 ulp_get(ulp_ops);
2482 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002483
2484 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
2485 ulp_ops->cnic_init(dev);
2486
Michael Chan7fc1ece2009-08-14 15:49:47 +00002487 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07002488 }
Michael Chana4636962009-06-08 18:14:43 -07002489}
2490
2491static void cnic_ulp_exit(struct cnic_dev *dev)
2492{
2493 int i;
2494 struct cnic_local *cp = dev->cnic_priv;
2495
Michael Chana4636962009-06-08 18:14:43 -07002496 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
2497 struct cnic_ulp_ops *ulp_ops;
2498
Michael Chan7fc1ece2009-08-14 15:49:47 +00002499 mutex_lock(&cnic_lock);
2500 ulp_ops = cnic_ulp_tbl[i];
2501 if (!ulp_ops || !ulp_ops->cnic_exit) {
2502 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002503 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00002504 }
2505 ulp_get(ulp_ops);
2506 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07002507
2508 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
2509 ulp_ops->cnic_exit(dev);
2510
Michael Chan7fc1ece2009-08-14 15:49:47 +00002511 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07002512 }
Michael Chana4636962009-06-08 18:14:43 -07002513}
2514
2515static int cnic_cm_offload_pg(struct cnic_sock *csk)
2516{
2517 struct cnic_dev *dev = csk->dev;
2518 struct l4_kwq_offload_pg *l4kwqe;
2519 struct kwqe *wqes[1];
2520
2521 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
2522 memset(l4kwqe, 0, sizeof(*l4kwqe));
2523 wqes[0] = (struct kwqe *) l4kwqe;
2524
2525 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
2526 l4kwqe->flags =
2527 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
2528 l4kwqe->l2hdr_nbytes = ETH_HLEN;
2529
2530 l4kwqe->da0 = csk->ha[0];
2531 l4kwqe->da1 = csk->ha[1];
2532 l4kwqe->da2 = csk->ha[2];
2533 l4kwqe->da3 = csk->ha[3];
2534 l4kwqe->da4 = csk->ha[4];
2535 l4kwqe->da5 = csk->ha[5];
2536
2537 l4kwqe->sa0 = dev->mac_addr[0];
2538 l4kwqe->sa1 = dev->mac_addr[1];
2539 l4kwqe->sa2 = dev->mac_addr[2];
2540 l4kwqe->sa3 = dev->mac_addr[3];
2541 l4kwqe->sa4 = dev->mac_addr[4];
2542 l4kwqe->sa5 = dev->mac_addr[5];
2543
2544 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00002545 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07002546 l4kwqe->host_opaque = csk->l5_cid;
2547
2548 if (csk->vlan_id) {
2549 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
2550 l4kwqe->vlan_tag = csk->vlan_id;
2551 l4kwqe->l2hdr_nbytes += 4;
2552 }
2553
2554 return dev->submit_kwqes(dev, wqes, 1);
2555}
2556
2557static int cnic_cm_update_pg(struct cnic_sock *csk)
2558{
2559 struct cnic_dev *dev = csk->dev;
2560 struct l4_kwq_update_pg *l4kwqe;
2561 struct kwqe *wqes[1];
2562
2563 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
2564 memset(l4kwqe, 0, sizeof(*l4kwqe));
2565 wqes[0] = (struct kwqe *) l4kwqe;
2566
2567 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
2568 l4kwqe->flags =
2569 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
2570 l4kwqe->pg_cid = csk->pg_cid;
2571
2572 l4kwqe->da0 = csk->ha[0];
2573 l4kwqe->da1 = csk->ha[1];
2574 l4kwqe->da2 = csk->ha[2];
2575 l4kwqe->da3 = csk->ha[3];
2576 l4kwqe->da4 = csk->ha[4];
2577 l4kwqe->da5 = csk->ha[5];
2578
2579 l4kwqe->pg_host_opaque = csk->l5_cid;
2580 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
2581
2582 return dev->submit_kwqes(dev, wqes, 1);
2583}
2584
2585static int cnic_cm_upload_pg(struct cnic_sock *csk)
2586{
2587 struct cnic_dev *dev = csk->dev;
2588 struct l4_kwq_upload *l4kwqe;
2589 struct kwqe *wqes[1];
2590
2591 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
2592 memset(l4kwqe, 0, sizeof(*l4kwqe));
2593 wqes[0] = (struct kwqe *) l4kwqe;
2594
2595 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
2596 l4kwqe->flags =
2597 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
2598 l4kwqe->cid = csk->pg_cid;
2599
2600 return dev->submit_kwqes(dev, wqes, 1);
2601}
2602
2603static int cnic_cm_conn_req(struct cnic_sock *csk)
2604{
2605 struct cnic_dev *dev = csk->dev;
2606 struct l4_kwq_connect_req1 *l4kwqe1;
2607 struct l4_kwq_connect_req2 *l4kwqe2;
2608 struct l4_kwq_connect_req3 *l4kwqe3;
2609 struct kwqe *wqes[3];
2610 u8 tcp_flags = 0;
2611 int num_wqes = 2;
2612
2613 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
2614 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
2615 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
2616 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
2617 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
2618 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
2619
2620 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
2621 l4kwqe3->flags =
2622 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
2623 l4kwqe3->ka_timeout = csk->ka_timeout;
2624 l4kwqe3->ka_interval = csk->ka_interval;
2625 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
2626 l4kwqe3->tos = csk->tos;
2627 l4kwqe3->ttl = csk->ttl;
2628 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
2629 l4kwqe3->pmtu = csk->mtu;
2630 l4kwqe3->rcv_buf = csk->rcv_buf;
2631 l4kwqe3->snd_buf = csk->snd_buf;
2632 l4kwqe3->seed = csk->seed;
2633
2634 wqes[0] = (struct kwqe *) l4kwqe1;
2635 if (test_bit(SK_F_IPV6, &csk->flags)) {
2636 wqes[1] = (struct kwqe *) l4kwqe2;
2637 wqes[2] = (struct kwqe *) l4kwqe3;
2638 num_wqes = 3;
2639
2640 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
2641 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
2642 l4kwqe2->flags =
2643 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
2644 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
2645 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
2646 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
2647 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
2648 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
2649 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
2650 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
2651 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
2652 sizeof(struct tcphdr);
2653 } else {
2654 wqes[1] = (struct kwqe *) l4kwqe3;
2655 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
2656 sizeof(struct tcphdr);
2657 }
2658
2659 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
2660 l4kwqe1->flags =
2661 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
2662 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
2663 l4kwqe1->cid = csk->cid;
2664 l4kwqe1->pg_cid = csk->pg_cid;
2665 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
2666 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
2667 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
2668 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
2669 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
2670 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
2671 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
2672 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
2673 if (csk->tcp_flags & SK_TCP_NAGLE)
2674 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
2675 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
2676 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
2677 if (csk->tcp_flags & SK_TCP_SACK)
2678 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
2679 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
2680 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
2681
2682 l4kwqe1->tcp_flags = tcp_flags;
2683
2684 return dev->submit_kwqes(dev, wqes, num_wqes);
2685}
2686
2687static int cnic_cm_close_req(struct cnic_sock *csk)
2688{
2689 struct cnic_dev *dev = csk->dev;
2690 struct l4_kwq_close_req *l4kwqe;
2691 struct kwqe *wqes[1];
2692
2693 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
2694 memset(l4kwqe, 0, sizeof(*l4kwqe));
2695 wqes[0] = (struct kwqe *) l4kwqe;
2696
2697 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
2698 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
2699 l4kwqe->cid = csk->cid;
2700
2701 return dev->submit_kwqes(dev, wqes, 1);
2702}
2703
2704static int cnic_cm_abort_req(struct cnic_sock *csk)
2705{
2706 struct cnic_dev *dev = csk->dev;
2707 struct l4_kwq_reset_req *l4kwqe;
2708 struct kwqe *wqes[1];
2709
2710 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
2711 memset(l4kwqe, 0, sizeof(*l4kwqe));
2712 wqes[0] = (struct kwqe *) l4kwqe;
2713
2714 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
2715 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
2716 l4kwqe->cid = csk->cid;
2717
2718 return dev->submit_kwqes(dev, wqes, 1);
2719}
2720
2721static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
2722 u32 l5_cid, struct cnic_sock **csk, void *context)
2723{
2724 struct cnic_local *cp = dev->cnic_priv;
2725 struct cnic_sock *csk1;
2726
2727 if (l5_cid >= MAX_CM_SK_TBL_SZ)
2728 return -EINVAL;
2729
2730 csk1 = &cp->csk_tbl[l5_cid];
2731 if (atomic_read(&csk1->ref_count))
2732 return -EAGAIN;
2733
2734 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
2735 return -EBUSY;
2736
2737 csk1->dev = dev;
2738 csk1->cid = cid;
2739 csk1->l5_cid = l5_cid;
2740 csk1->ulp_type = ulp_type;
2741 csk1->context = context;
2742
2743 csk1->ka_timeout = DEF_KA_TIMEOUT;
2744 csk1->ka_interval = DEF_KA_INTERVAL;
2745 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
2746 csk1->tos = DEF_TOS;
2747 csk1->ttl = DEF_TTL;
2748 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
2749 csk1->rcv_buf = DEF_RCV_BUF;
2750 csk1->snd_buf = DEF_SND_BUF;
2751 csk1->seed = DEF_SEED;
2752
2753 *csk = csk1;
2754 return 0;
2755}
2756
2757static void cnic_cm_cleanup(struct cnic_sock *csk)
2758{
2759 if (csk->src_port) {
2760 struct cnic_dev *dev = csk->dev;
2761 struct cnic_local *cp = dev->cnic_priv;
2762
2763 cnic_free_id(&cp->csk_port_tbl, csk->src_port);
2764 csk->src_port = 0;
2765 }
2766}
2767
2768static void cnic_close_conn(struct cnic_sock *csk)
2769{
2770 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
2771 cnic_cm_upload_pg(csk);
2772 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
2773 }
2774 cnic_cm_cleanup(csk);
2775}
2776
2777static int cnic_cm_destroy(struct cnic_sock *csk)
2778{
2779 if (!cnic_in_use(csk))
2780 return -EINVAL;
2781
2782 csk_hold(csk);
2783 clear_bit(SK_F_INUSE, &csk->flags);
2784 smp_mb__after_clear_bit();
2785 while (atomic_read(&csk->ref_count) != 1)
2786 msleep(1);
2787 cnic_cm_cleanup(csk);
2788
2789 csk->flags = 0;
2790 csk_put(csk);
2791 return 0;
2792}
2793
2794static inline u16 cnic_get_vlan(struct net_device *dev,
2795 struct net_device **vlan_dev)
2796{
2797 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2798 *vlan_dev = vlan_dev_real_dev(dev);
2799 return vlan_dev_vlan_id(dev);
2800 }
2801 *vlan_dev = dev;
2802 return 0;
2803}
2804
2805static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
2806 struct dst_entry **dst)
2807{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07002808#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07002809 struct flowi fl;
2810 int err;
2811 struct rtable *rt;
2812
2813 memset(&fl, 0, sizeof(fl));
2814 fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
2815
2816 err = ip_route_output_key(&init_net, &rt, &fl);
2817 if (!err)
Changli Gaod8d1f302010-06-10 23:31:35 -07002818 *dst = &rt->dst;
Michael Chana4636962009-06-08 18:14:43 -07002819 return err;
Randy Dunlapfaea56c2009-06-12 11:43:48 -07002820#else
2821 return -ENETUNREACH;
2822#endif
Michael Chana4636962009-06-08 18:14:43 -07002823}
2824
2825static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
2826 struct dst_entry **dst)
2827{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07002828#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
Michael Chana4636962009-06-08 18:14:43 -07002829 struct flowi fl;
2830
2831 memset(&fl, 0, sizeof(fl));
2832 ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
2833 if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
2834 fl.oif = dst_addr->sin6_scope_id;
2835
2836 *dst = ip6_route_output(&init_net, NULL, &fl);
2837 if (*dst)
2838 return 0;
2839#endif
2840
2841 return -ENETUNREACH;
2842}
2843
2844static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
2845 int ulp_type)
2846{
2847 struct cnic_dev *dev = NULL;
2848 struct dst_entry *dst;
2849 struct net_device *netdev = NULL;
2850 int err = -ENETUNREACH;
2851
2852 if (dst_addr->sin_family == AF_INET)
2853 err = cnic_get_v4_route(dst_addr, &dst);
2854 else if (dst_addr->sin_family == AF_INET6) {
2855 struct sockaddr_in6 *dst_addr6 =
2856 (struct sockaddr_in6 *) dst_addr;
2857
2858 err = cnic_get_v6_route(dst_addr6, &dst);
2859 } else
2860 return NULL;
2861
2862 if (err)
2863 return NULL;
2864
2865 if (!dst->dev)
2866 goto done;
2867
2868 cnic_get_vlan(dst->dev, &netdev);
2869
2870 dev = cnic_from_netdev(netdev);
2871
2872done:
2873 dst_release(dst);
2874 if (dev)
2875 cnic_put(dev);
2876 return dev;
2877}
2878
2879static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
2880{
2881 struct cnic_dev *dev = csk->dev;
2882 struct cnic_local *cp = dev->cnic_priv;
2883
2884 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
2885}
2886
2887static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
2888{
2889 struct cnic_dev *dev = csk->dev;
2890 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00002891 int is_v6, rc = 0;
2892 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07002893 struct net_device *realdev;
2894 u32 local_port;
2895
2896 if (saddr->local.v6.sin6_family == AF_INET6 &&
2897 saddr->remote.v6.sin6_family == AF_INET6)
2898 is_v6 = 1;
2899 else if (saddr->local.v4.sin_family == AF_INET &&
2900 saddr->remote.v4.sin_family == AF_INET)
2901 is_v6 = 0;
2902 else
2903 return -EINVAL;
2904
2905 clear_bit(SK_F_IPV6, &csk->flags);
2906
2907 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07002908 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00002909 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07002910
2911 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
2912 sizeof(struct in6_addr));
2913 csk->dst_port = saddr->remote.v6.sin6_port;
2914 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07002915
2916 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00002917 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07002918
2919 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
2920 csk->dst_port = saddr->remote.v4.sin_port;
2921 local_port = saddr->local.v4.sin_port;
2922 }
2923
Michael Chanc76284a2010-02-24 14:42:07 +00002924 csk->vlan_id = 0;
2925 csk->mtu = dev->netdev->mtu;
2926 if (dst && dst->dev) {
2927 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
2928 if (realdev == dev->netdev) {
2929 csk->vlan_id = vlan;
2930 csk->mtu = dst_mtu(dst);
2931 }
2932 }
Michael Chana4636962009-06-08 18:14:43 -07002933
2934 if (local_port >= CNIC_LOCAL_PORT_MIN &&
2935 local_port < CNIC_LOCAL_PORT_MAX) {
2936 if (cnic_alloc_id(&cp->csk_port_tbl, local_port))
2937 local_port = 0;
2938 } else
2939 local_port = 0;
2940
2941 if (!local_port) {
2942 local_port = cnic_alloc_new_id(&cp->csk_port_tbl);
2943 if (local_port == -1) {
2944 rc = -ENOMEM;
2945 goto err_out;
2946 }
2947 }
2948 csk->src_port = local_port;
2949
Michael Chana4636962009-06-08 18:14:43 -07002950err_out:
2951 dst_release(dst);
2952 return rc;
2953}
2954
2955static void cnic_init_csk_state(struct cnic_sock *csk)
2956{
2957 csk->state = 0;
2958 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
2959 clear_bit(SK_F_CLOSING, &csk->flags);
2960}
2961
2962static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
2963{
2964 int err = 0;
2965
2966 if (!cnic_in_use(csk))
2967 return -EINVAL;
2968
2969 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
2970 return -EINVAL;
2971
2972 cnic_init_csk_state(csk);
2973
2974 err = cnic_get_route(csk, saddr);
2975 if (err)
2976 goto err_out;
2977
2978 err = cnic_resolve_addr(csk, saddr);
2979 if (!err)
2980 return 0;
2981
2982err_out:
2983 clear_bit(SK_F_CONNECT_START, &csk->flags);
2984 return err;
2985}
2986
2987static int cnic_cm_abort(struct cnic_sock *csk)
2988{
2989 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00002990 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07002991
2992 if (!cnic_in_use(csk))
2993 return -EINVAL;
2994
2995 if (cnic_abort_prep(csk))
2996 return cnic_cm_abort_req(csk);
2997
2998 /* Getting here means that we haven't started connect, or
2999 * connect was not successful.
3000 */
3001
Michael Chana4636962009-06-08 18:14:43 -07003002 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003003 if (csk->state != opcode)
3004 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003005
3006 return 0;
3007}
3008
3009static int cnic_cm_close(struct cnic_sock *csk)
3010{
3011 if (!cnic_in_use(csk))
3012 return -EINVAL;
3013
3014 if (cnic_close_prep(csk)) {
3015 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3016 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003017 } else {
3018 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003019 }
3020 return 0;
3021}
3022
3023static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3024 u8 opcode)
3025{
3026 struct cnic_ulp_ops *ulp_ops;
3027 int ulp_type = csk->ulp_type;
3028
3029 rcu_read_lock();
3030 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3031 if (ulp_ops) {
3032 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3033 ulp_ops->cm_connect_complete(csk);
3034 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3035 ulp_ops->cm_close_complete(csk);
3036 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3037 ulp_ops->cm_remote_abort(csk);
3038 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3039 ulp_ops->cm_abort_complete(csk);
3040 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3041 ulp_ops->cm_remote_close(csk);
3042 }
3043 rcu_read_unlock();
3044}
3045
3046static int cnic_cm_set_pg(struct cnic_sock *csk)
3047{
3048 if (cnic_offld_prep(csk)) {
3049 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3050 cnic_cm_update_pg(csk);
3051 else
3052 cnic_cm_offload_pg(csk);
3053 }
3054 return 0;
3055}
3056
3057static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3058{
3059 struct cnic_local *cp = dev->cnic_priv;
3060 u32 l5_cid = kcqe->pg_host_opaque;
3061 u8 opcode = kcqe->op_code;
3062 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3063
3064 csk_hold(csk);
3065 if (!cnic_in_use(csk))
3066 goto done;
3067
3068 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3069 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3070 goto done;
3071 }
Eddie Waia9736c02010-02-24 14:42:04 +00003072 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3073 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3074 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3075 cnic_cm_upcall(cp, csk,
3076 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3077 goto done;
3078 }
3079
Michael Chana4636962009-06-08 18:14:43 -07003080 csk->pg_cid = kcqe->pg_cid;
3081 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3082 cnic_cm_conn_req(csk);
3083
3084done:
3085 csk_put(csk);
3086}
3087
3088static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3089{
3090 struct cnic_local *cp = dev->cnic_priv;
3091 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3092 u8 opcode = l4kcqe->op_code;
3093 u32 l5_cid;
3094 struct cnic_sock *csk;
3095
3096 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3097 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3098 cnic_cm_process_offld_pg(dev, l4kcqe);
3099 return;
3100 }
3101
3102 l5_cid = l4kcqe->conn_id;
3103 if (opcode & 0x80)
3104 l5_cid = l4kcqe->cid;
3105 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3106 return;
3107
3108 csk = &cp->csk_tbl[l5_cid];
3109 csk_hold(csk);
3110
3111 if (!cnic_in_use(csk)) {
3112 csk_put(csk);
3113 return;
3114 }
3115
3116 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003117 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3118 if (l4kcqe->status != 0) {
3119 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3120 cnic_cm_upcall(cp, csk,
3121 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3122 }
3123 break;
Michael Chana4636962009-06-08 18:14:43 -07003124 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3125 if (l4kcqe->status == 0)
3126 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3127
3128 smp_mb__before_clear_bit();
3129 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3130 cnic_cm_upcall(cp, csk, opcode);
3131 break;
3132
3133 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003134 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3135 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003136 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3137 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003138 cp->close_conn(csk, opcode);
3139 break;
3140
3141 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
3142 cnic_cm_upcall(cp, csk, opcode);
3143 break;
3144 }
3145 csk_put(csk);
3146}
3147
3148static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3149{
3150 struct cnic_dev *dev = data;
3151 int i;
3152
3153 for (i = 0; i < num; i++)
3154 cnic_cm_process_kcqe(dev, kcqe[i]);
3155}
3156
3157static struct cnic_ulp_ops cm_ulp_ops = {
3158 .indicate_kcqes = cnic_cm_indicate_kcqe,
3159};
3160
3161static void cnic_cm_free_mem(struct cnic_dev *dev)
3162{
3163 struct cnic_local *cp = dev->cnic_priv;
3164
3165 kfree(cp->csk_tbl);
3166 cp->csk_tbl = NULL;
3167 cnic_free_id_tbl(&cp->csk_port_tbl);
3168}
3169
3170static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3171{
3172 struct cnic_local *cp = dev->cnic_priv;
3173
3174 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3175 GFP_KERNEL);
3176 if (!cp->csk_tbl)
3177 return -ENOMEM;
3178
3179 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
3180 CNIC_LOCAL_PORT_MIN)) {
3181 cnic_cm_free_mem(dev);
3182 return -ENOMEM;
3183 }
3184 return 0;
3185}
3186
3187static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3188{
Michael Chan943189f2010-06-15 08:57:02 +00003189 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3190 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3191 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3192 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003193 }
Michael Chan943189f2010-06-15 08:57:02 +00003194
3195 /* 1. If event opcode matches the expected event in csk->state
3196 * 2. If the expected event is CLOSE_COMP, we accept any event
Michael Chan7b34a462010-06-15 08:57:03 +00003197 * 3. If the expected event is 0, meaning the connection was never
3198 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003199 */
Michael Chan7b34a462010-06-15 08:57:03 +00003200 if (opcode == csk->state || csk->state == 0 ||
3201 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP) {
3202 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3203 if (csk->state == 0)
3204 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003205 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003206 }
Michael Chana4636962009-06-08 18:14:43 -07003207 }
3208 return 0;
3209}
3210
3211static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3212{
3213 struct cnic_dev *dev = csk->dev;
3214 struct cnic_local *cp = dev->cnic_priv;
3215
Michael Chana1e621b2010-06-15 08:57:01 +00003216 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3217 cnic_cm_upcall(cp, csk, opcode);
3218 return;
3219 }
3220
Michael Chana4636962009-06-08 18:14:43 -07003221 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003222 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003223 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003224 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003225}
3226
3227static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3228{
3229}
3230
3231static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3232{
3233 u32 seed;
3234
3235 get_random_bytes(&seed, 4);
3236 cnic_ctx_wr(dev, 45, 0, seed);
3237 return 0;
3238}
3239
Michael Chan71034ba2009-10-10 13:46:59 +00003240static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3241{
3242 struct cnic_dev *dev = csk->dev;
3243 struct cnic_local *cp = dev->cnic_priv;
3244 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3245 union l5cm_specific_data l5_data;
3246 u32 cmd = 0;
3247 int close_complete = 0;
3248
3249 switch (opcode) {
3250 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3251 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3252 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003253 if (cnic_ready_to_close(csk, opcode)) {
3254 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3255 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3256 else
3257 close_complete = 1;
3258 }
Michael Chan71034ba2009-10-10 13:46:59 +00003259 break;
3260 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3261 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3262 break;
3263 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3264 close_complete = 1;
3265 break;
3266 }
3267 if (cmd) {
3268 memset(&l5_data, 0, sizeof(l5_data));
3269
3270 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3271 &l5_data);
3272 } else if (close_complete) {
3273 ctx->timestamp = jiffies;
3274 cnic_close_conn(csk);
3275 cnic_cm_upcall(cp, csk, csk->state);
3276 }
3277}
3278
3279static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3280{
3281}
3282
3283static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3284{
3285 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003286 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00003287
3288 cnic_init_bnx2x_mac(dev);
3289 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3290
3291 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003292 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003293
3294 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003295 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(pfid), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003296 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003297 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003298 DEF_MAX_DA_COUNT);
3299
3300 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003301 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003302 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003303 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003304 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003305 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003306 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003307 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003308
Michael Chan14203982010-10-06 03:16:06 +00003309 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003310 DEF_MAX_CWND);
3311 return 0;
3312}
3313
Michael Chana4636962009-06-08 18:14:43 -07003314static int cnic_cm_open(struct cnic_dev *dev)
3315{
3316 struct cnic_local *cp = dev->cnic_priv;
3317 int err;
3318
3319 err = cnic_cm_alloc_mem(dev);
3320 if (err)
3321 return err;
3322
3323 err = cp->start_cm(dev);
3324
3325 if (err)
3326 goto err_out;
3327
3328 dev->cm_create = cnic_cm_create;
3329 dev->cm_destroy = cnic_cm_destroy;
3330 dev->cm_connect = cnic_cm_connect;
3331 dev->cm_abort = cnic_cm_abort;
3332 dev->cm_close = cnic_cm_close;
3333 dev->cm_select_dev = cnic_cm_select_dev;
3334
3335 cp->ulp_handle[CNIC_ULP_L4] = dev;
3336 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
3337 return 0;
3338
3339err_out:
3340 cnic_cm_free_mem(dev);
3341 return err;
3342}
3343
3344static int cnic_cm_shutdown(struct cnic_dev *dev)
3345{
3346 struct cnic_local *cp = dev->cnic_priv;
3347 int i;
3348
3349 cp->stop_cm(dev);
3350
3351 if (!cp->csk_tbl)
3352 return 0;
3353
3354 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
3355 struct cnic_sock *csk = &cp->csk_tbl[i];
3356
3357 clear_bit(SK_F_INUSE, &csk->flags);
3358 cnic_cm_cleanup(csk);
3359 }
3360 cnic_cm_free_mem(dev);
3361
3362 return 0;
3363}
3364
3365static void cnic_init_context(struct cnic_dev *dev, u32 cid)
3366{
Michael Chana4636962009-06-08 18:14:43 -07003367 u32 cid_addr;
3368 int i;
3369
Michael Chana4636962009-06-08 18:14:43 -07003370 cid_addr = GET_CID_ADDR(cid);
3371
3372 for (i = 0; i < CTX_SIZE; i += 4)
3373 cnic_ctx_wr(dev, cid_addr, i, 0);
3374}
3375
3376static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
3377{
3378 struct cnic_local *cp = dev->cnic_priv;
3379 int ret = 0, i;
3380 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
3381
3382 if (CHIP_NUM(cp) != CHIP_NUM_5709)
3383 return 0;
3384
3385 for (i = 0; i < cp->ctx_blks; i++) {
3386 int j;
3387 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
3388 u32 val;
3389
3390 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
3391
3392 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
3393 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
3394 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
3395 (u64) cp->ctx_arr[i].mapping >> 32);
3396 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
3397 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
3398 for (j = 0; j < 10; j++) {
3399
3400 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
3401 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
3402 break;
3403 udelay(5);
3404 }
3405 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
3406 ret = -EBUSY;
3407 break;
3408 }
3409 }
3410 return ret;
3411}
3412
3413static void cnic_free_irq(struct cnic_dev *dev)
3414{
3415 struct cnic_local *cp = dev->cnic_priv;
3416 struct cnic_eth_dev *ethdev = cp->ethdev;
3417
3418 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3419 cp->disable_int_sync(dev);
3420 tasklet_disable(&cp->cnic_irq_task);
3421 free_irq(ethdev->irq_arr[0].vector, dev);
3422 }
3423}
3424
3425static int cnic_init_bnx2_irq(struct cnic_dev *dev)
3426{
3427 struct cnic_local *cp = dev->cnic_priv;
3428 struct cnic_eth_dev *ethdev = cp->ethdev;
3429
3430 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3431 int err, i = 0;
3432 int sblk_num = cp->status_blk_num;
3433 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
3434 BNX2_HC_SB_CONFIG_1;
3435
3436 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
3437
3438 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
3439 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
3440 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
3441
Michael Chana4dde3a2010-02-24 14:42:08 +00003442 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00003443 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07003444 (unsigned long) dev);
3445 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0,
3446 "cnic", dev);
3447 if (err) {
3448 tasklet_disable(&cp->cnic_irq_task);
3449 return err;
3450 }
Michael Chana4dde3a2010-02-24 14:42:08 +00003451 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07003452 i < 10) {
3453 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
3454 1 << (11 + sblk_num));
3455 udelay(10);
3456 i++;
3457 barrier();
3458 }
Michael Chana4dde3a2010-02-24 14:42:08 +00003459 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07003460 cnic_free_irq(dev);
3461 goto failed;
3462 }
3463
3464 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00003465 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07003466 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
3467 int i = 0;
3468
3469 while (sblk->status_completion_producer_index && i < 10) {
3470 CNIC_WR(dev, BNX2_HC_COMMAND,
3471 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
3472 udelay(10);
3473 i++;
3474 barrier();
3475 }
3476 if (sblk->status_completion_producer_index)
3477 goto failed;
3478
3479 }
3480 return 0;
3481
3482failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00003483 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07003484 return -EBUSY;
3485}
3486
3487static void cnic_enable_bnx2_int(struct cnic_dev *dev)
3488{
3489 struct cnic_local *cp = dev->cnic_priv;
3490 struct cnic_eth_dev *ethdev = cp->ethdev;
3491
3492 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3493 return;
3494
3495 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
3496 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
3497}
3498
3499static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
3500{
3501 struct cnic_local *cp = dev->cnic_priv;
3502 struct cnic_eth_dev *ethdev = cp->ethdev;
3503
3504 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3505 return;
3506
3507 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
3508 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
3509 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
3510 synchronize_irq(ethdev->irq_arr[0].vector);
3511}
3512
3513static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
3514{
3515 struct cnic_local *cp = dev->cnic_priv;
3516 struct cnic_eth_dev *ethdev = cp->ethdev;
3517 u32 cid_addr, tx_cid, sb_id;
3518 u32 val, offset0, offset1, offset2, offset3;
3519 int i;
3520 struct tx_bd *txbd;
3521 dma_addr_t buf_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00003522 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07003523
3524 sb_id = cp->status_blk_num;
3525 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07003526 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
3527 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00003528 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07003529
3530 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07003531 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
3532 (TX_TSS_CID << 7));
3533 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
3534 }
3535 cp->tx_cons = *cp->tx_cons_ptr;
3536
3537 cid_addr = GET_CID_ADDR(tx_cid);
3538 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
3539 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
3540
3541 for (i = 0; i < PHY_CTX_SIZE; i += 4)
3542 cnic_ctx_wr(dev, cid_addr2, i, 0);
3543
3544 offset0 = BNX2_L2CTX_TYPE_XI;
3545 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
3546 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
3547 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
3548 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07003549 cnic_init_context(dev, tx_cid);
3550 cnic_init_context(dev, tx_cid + 1);
3551
Michael Chana4636962009-06-08 18:14:43 -07003552 offset0 = BNX2_L2CTX_TYPE;
3553 offset1 = BNX2_L2CTX_CMD_TYPE;
3554 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
3555 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
3556 }
3557 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
3558 cnic_ctx_wr(dev, cid_addr, offset0, val);
3559
3560 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
3561 cnic_ctx_wr(dev, cid_addr, offset1, val);
3562
3563 txbd = (struct tx_bd *) cp->l2_ring;
3564
3565 buf_map = cp->l2_buf_map;
3566 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
3567 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
3568 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
3569 }
3570 val = (u64) cp->l2_ring_map >> 32;
3571 cnic_ctx_wr(dev, cid_addr, offset2, val);
3572 txbd->tx_bd_haddr_hi = val;
3573
3574 val = (u64) cp->l2_ring_map & 0xffffffff;
3575 cnic_ctx_wr(dev, cid_addr, offset3, val);
3576 txbd->tx_bd_haddr_lo = val;
3577}
3578
3579static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
3580{
3581 struct cnic_local *cp = dev->cnic_priv;
3582 struct cnic_eth_dev *ethdev = cp->ethdev;
3583 u32 cid_addr, sb_id, val, coal_reg, coal_val;
3584 int i;
3585 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00003586 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07003587
3588 sb_id = cp->status_blk_num;
3589 cnic_init_context(dev, 2);
3590 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
3591 coal_reg = BNX2_HC_COMMAND;
3592 coal_val = CNIC_RD(dev, coal_reg);
3593 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00003594 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07003595
3596 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
3597 coal_reg = BNX2_HC_COALESCE_NOW;
3598 coal_val = 1 << (11 + sb_id);
3599 }
3600 i = 0;
3601 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
3602 CNIC_WR(dev, coal_reg, coal_val);
3603 udelay(10);
3604 i++;
3605 barrier();
3606 }
3607 cp->rx_cons = *cp->rx_cons_ptr;
3608
3609 cid_addr = GET_CID_ADDR(2);
3610 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
3611 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
3612 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
3613
3614 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07003615 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07003616 else
Michael Chand0549382009-10-28 03:41:59 -07003617 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07003618 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
3619
3620 rxbd = (struct rx_bd *) (cp->l2_ring + BCM_PAGE_SIZE);
3621 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
3622 dma_addr_t buf_map;
3623 int n = (i % cp->l2_rx_ring_size) + 1;
3624
3625 buf_map = cp->l2_buf_map + (n * cp->l2_single_buf_size);
3626 rxbd->rx_bd_len = cp->l2_single_buf_size;
3627 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
3628 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
3629 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
3630 }
3631 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) >> 32;
3632 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
3633 rxbd->rx_bd_haddr_hi = val;
3634
3635 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) & 0xffffffff;
3636 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
3637 rxbd->rx_bd_haddr_lo = val;
3638
3639 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
3640 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
3641}
3642
3643static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
3644{
3645 struct kwqe *wqes[1], l2kwqe;
3646
3647 memset(&l2kwqe, 0, sizeof(l2kwqe));
3648 wqes[0] = &l2kwqe;
3649 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
3650 (L2_KWQE_OPCODE_VALUE_FLUSH <<
3651 KWQE_OPCODE_SHIFT) | 2;
3652 dev->submit_kwqes(dev, wqes, 1);
3653}
3654
3655static void cnic_set_bnx2_mac(struct cnic_dev *dev)
3656{
3657 struct cnic_local *cp = dev->cnic_priv;
3658 u32 val;
3659
3660 val = cp->func << 2;
3661
3662 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
3663
3664 val = cnic_reg_rd_ind(dev, cp->shmem_base +
3665 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
3666 dev->mac_addr[0] = (u8) (val >> 8);
3667 dev->mac_addr[1] = (u8) val;
3668
3669 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
3670
3671 val = cnic_reg_rd_ind(dev, cp->shmem_base +
3672 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
3673 dev->mac_addr[2] = (u8) (val >> 24);
3674 dev->mac_addr[3] = (u8) (val >> 16);
3675 dev->mac_addr[4] = (u8) (val >> 8);
3676 dev->mac_addr[5] = (u8) val;
3677
3678 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
3679
3680 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
3681 if (CHIP_NUM(cp) != CHIP_NUM_5709)
3682 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
3683
3684 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
3685 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
3686 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
3687}
3688
3689static int cnic_start_bnx2_hw(struct cnic_dev *dev)
3690{
3691 struct cnic_local *cp = dev->cnic_priv;
3692 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00003693 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00003694 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07003695 int err;
3696
3697 cnic_set_bnx2_mac(dev);
3698
3699 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
3700 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
3701 if (BCM_PAGE_BITS > 12)
3702 val |= (12 - 8) << 4;
3703 else
3704 val |= (BCM_PAGE_BITS - 8) << 4;
3705
3706 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
3707
3708 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
3709 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
3710 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
3711
3712 err = cnic_setup_5709_context(dev, 1);
3713 if (err)
3714 return err;
3715
3716 cnic_init_context(dev, KWQ_CID);
3717 cnic_init_context(dev, KCQ_CID);
3718
Michael Chane6c28892010-06-24 14:58:39 +00003719 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07003720 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
3721
3722 cp->max_kwq_idx = MAX_KWQ_IDX;
3723 cp->kwq_prod_idx = 0;
3724 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00003725 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07003726
3727 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
3728 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
3729 else
3730 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
3731
3732 /* Initialize the kernel work queue context. */
3733 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
3734 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00003735 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07003736
3737 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00003738 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07003739
3740 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00003741 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07003742
3743 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00003744 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07003745
3746 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00003747 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07003748
Michael Chane6c28892010-06-24 14:58:39 +00003749 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
3750 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07003751
Michael Chane6c28892010-06-24 14:58:39 +00003752 cp->kcq1.sw_prod_idx = 0;
3753 cp->kcq1.hw_prod_idx_ptr =
3754 (u16 *) &sblk->status_completion_producer_index;
3755
3756 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07003757
3758 /* Initialize the kernel complete queue context. */
3759 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
3760 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00003761 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07003762
3763 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00003764 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07003765
3766 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00003767 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07003768
Michael Chane6c28892010-06-24 14:58:39 +00003769 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
3770 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07003771
Michael Chane6c28892010-06-24 14:58:39 +00003772 val = (u32) cp->kcq1.dma.pgtbl_map;
3773 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07003774
3775 cp->int_num = 0;
3776 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00003777 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07003778 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07003779 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07003780
Michael Chane6c28892010-06-24 14:58:39 +00003781 cp->kcq1.hw_prod_idx_ptr =
3782 (u16 *) &msblk->status_completion_producer_index;
3783 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00003784 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07003785 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00003786 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
3787 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07003788 }
3789
3790 /* Enable Commnad Scheduler notification when we write to the
3791 * host producer index of the kernel contexts. */
3792 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
3793
3794 /* Enable Command Scheduler notification when we write to either
3795 * the Send Queue or Receive Queue producer indexes of the kernel
3796 * bypass contexts. */
3797 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
3798 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
3799
3800 /* Notify COM when the driver post an application buffer. */
3801 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
3802
3803 /* Set the CP and COM doorbells. These two processors polls the
3804 * doorbell for a non zero value before running. This must be done
3805 * after setting up the kernel queue contexts. */
3806 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
3807 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
3808
3809 cnic_init_bnx2_tx_ring(dev);
3810 cnic_init_bnx2_rx_ring(dev);
3811
3812 err = cnic_init_bnx2_irq(dev);
3813 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00003814 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07003815 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
3816 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
3817 return err;
3818 }
3819
3820 return 0;
3821}
3822
Michael Chan71034ba2009-10-10 13:46:59 +00003823static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
3824{
3825 struct cnic_local *cp = dev->cnic_priv;
3826 struct cnic_eth_dev *ethdev = cp->ethdev;
3827 u32 start_offset = ethdev->ctx_tbl_offset;
3828 int i;
3829
3830 for (i = 0; i < cp->ctx_blks; i++) {
3831 struct cnic_ctx *ctx = &cp->ctx_arr[i];
3832 dma_addr_t map = ctx->mapping;
3833
3834 if (cp->ctx_align) {
3835 unsigned long mask = cp->ctx_align - 1;
3836
3837 map = (map + mask) & ~mask;
3838 }
3839
3840 cnic_ctx_tbl_wr(dev, start_offset + i, map);
3841 }
3842}
3843
3844static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
3845{
3846 struct cnic_local *cp = dev->cnic_priv;
3847 struct cnic_eth_dev *ethdev = cp->ethdev;
3848 int err = 0;
3849
Joe Perches164165d2009-11-19 09:30:10 +00003850 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00003851 (unsigned long) dev);
3852 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3853 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0,
3854 "cnic", dev);
3855 if (err)
3856 tasklet_disable(&cp->cnic_irq_task);
3857 }
3858 return err;
3859}
3860
3861static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
3862{
3863 struct cnic_local *cp = dev->cnic_priv;
3864 u8 sb_id = cp->status_blk_num;
3865 int port = CNIC_PORT(cp);
3866
3867 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
3868 CSTORM_SB_HC_TIMEOUT_C_OFFSET(port, sb_id,
3869 HC_INDEX_C_ISCSI_EQ_CONS),
3870 64 / 12);
3871 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
3872 CSTORM_SB_HC_DISABLE_C_OFFSET(port, sb_id,
3873 HC_INDEX_C_ISCSI_EQ_CONS), 0);
3874}
3875
3876static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
3877{
3878}
3879
3880static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev)
3881{
3882 struct cnic_local *cp = dev->cnic_priv;
3883 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) cp->l2_ring;
3884 struct eth_context *context;
3885 struct regpair context_addr;
3886 dma_addr_t buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00003887 int port = CNIC_PORT(cp);
3888 int i;
3889 int cli = BNX2X_ISCSI_CL_ID(CNIC_E1HVN(cp));
3890 u32 val;
3891
3892 memset(txbd, 0, BCM_PAGE_SIZE);
3893
3894 buf_map = cp->l2_buf_map;
3895 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
3896 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
3897 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
3898
3899 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
3900 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
3901 reg_bd->addr_hi = start_bd->addr_hi;
3902 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
3903 start_bd->nbytes = cpu_to_le16(0x10);
3904 start_bd->nbd = cpu_to_le16(3);
3905 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
3906 start_bd->general_data = (UNICAST_ADDRESS <<
3907 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
3908 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
3909
3910 }
3911 context = cnic_get_bnx2x_ctx(dev, BNX2X_ISCSI_L2_CID, 1, &context_addr);
3912
3913 val = (u64) cp->l2_ring_map >> 32;
3914 txbd->next_bd.addr_hi = cpu_to_le32(val);
3915
3916 context->xstorm_st_context.tx_bd_page_base_hi = val;
3917
3918 val = (u64) cp->l2_ring_map & 0xffffffff;
3919 txbd->next_bd.addr_lo = cpu_to_le32(val);
3920
3921 context->xstorm_st_context.tx_bd_page_base_lo = val;
3922
3923 context->cstorm_st_context.sb_index_number =
3924 HC_INDEX_DEF_C_ETH_ISCSI_CQ_CONS;
3925 context->cstorm_st_context.status_block_id = BNX2X_DEF_SB_ID;
3926
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07003927 if (cli < MAX_X_STAT_COUNTER_ID)
3928 context->xstorm_st_context.statistics_data = cli |
3929 XSTORM_ETH_ST_CONTEXT_STATISTICS_ENABLE;
Michael Chan71034ba2009-10-10 13:46:59 +00003930
3931 context->xstorm_ag_context.cdu_reserved =
Michael Chanceb7e1c2010-10-06 03:14:54 +00003932 CDU_RSRVD_VALUE_TYPE_A(BNX2X_HW_CID(cp, BNX2X_ISCSI_L2_CID),
Michael Chan71034ba2009-10-10 13:46:59 +00003933 CDU_REGION_NUMBER_XCM_AG,
3934 ETH_CONNECTION_TYPE);
3935
3936 /* reset xstorm per client statistics */
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07003937 if (cli < MAX_X_STAT_COUNTER_ID) {
3938 val = BAR_XSTRORM_INTMEM +
3939 XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
3940 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
3941 CNIC_WR(dev, val + i * 4, 0);
3942 }
Michael Chan71034ba2009-10-10 13:46:59 +00003943
3944 cp->tx_cons_ptr =
3945 &cp->bnx2x_def_status_blk->c_def_status_block.index_values[
3946 HC_INDEX_DEF_C_ETH_ISCSI_CQ_CONS];
3947}
3948
3949static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev)
3950{
3951 struct cnic_local *cp = dev->cnic_priv;
3952 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (cp->l2_ring +
3953 BCM_PAGE_SIZE);
3954 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
3955 (cp->l2_ring + (2 * BCM_PAGE_SIZE));
3956 struct eth_context *context;
3957 struct regpair context_addr;
3958 int i;
3959 int port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003960 int cli = BNX2X_ISCSI_CL_ID(CNIC_E1HVN(cp));
3961 u32 val;
3962 struct tstorm_eth_client_config tstorm_client = {0};
3963
3964 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
3965 dma_addr_t buf_map;
3966 int n = (i % cp->l2_rx_ring_size) + 1;
3967
3968 buf_map = cp->l2_buf_map + (n * cp->l2_single_buf_size);
3969 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
3970 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
3971 }
3972 context = cnic_get_bnx2x_ctx(dev, BNX2X_ISCSI_L2_CID, 0, &context_addr);
3973
3974 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) >> 32;
3975 rxbd->addr_hi = cpu_to_le32(val);
3976
3977 context->ustorm_st_context.common.bd_page_base_hi = val;
3978
3979 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) & 0xffffffff;
3980 rxbd->addr_lo = cpu_to_le32(val);
3981
3982 context->ustorm_st_context.common.bd_page_base_lo = val;
3983
3984 context->ustorm_st_context.common.sb_index_numbers =
3985 BNX2X_ISCSI_RX_SB_INDEX_NUM;
3986 context->ustorm_st_context.common.clientId = cli;
3987 context->ustorm_st_context.common.status_block_id = BNX2X_DEF_SB_ID;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07003988 if (cli < MAX_U_STAT_COUNTER_ID) {
3989 context->ustorm_st_context.common.flags =
3990 USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_STATISTICS;
3991 context->ustorm_st_context.common.statistics_counter_id = cli;
3992 }
Michael Chan71034ba2009-10-10 13:46:59 +00003993 context->ustorm_st_context.common.mc_alignment_log_size = 0;
3994 context->ustorm_st_context.common.bd_buff_size =
3995 cp->l2_single_buf_size;
3996
3997 context->ustorm_ag_context.cdu_usage =
Michael Chanceb7e1c2010-10-06 03:14:54 +00003998 CDU_RSRVD_VALUE_TYPE_A(BNX2X_HW_CID(cp, BNX2X_ISCSI_L2_CID),
Michael Chan71034ba2009-10-10 13:46:59 +00003999 CDU_REGION_NUMBER_UCM_AG,
4000 ETH_CONNECTION_TYPE);
4001
4002 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
4003 val = (u64) (cp->l2_ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
4004 rxcqe->addr_hi = cpu_to_le32(val);
4005
4006 CNIC_WR(dev, BAR_USTRORM_INTMEM +
4007 USTORM_CQE_PAGE_BASE_OFFSET(port, cli) + 4, val);
4008
4009 CNIC_WR(dev, BAR_USTRORM_INTMEM +
4010 USTORM_CQE_PAGE_NEXT_OFFSET(port, cli) + 4, val);
4011
4012 val = (u64) (cp->l2_ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
4013 rxcqe->addr_lo = cpu_to_le32(val);
4014
4015 CNIC_WR(dev, BAR_USTRORM_INTMEM +
4016 USTORM_CQE_PAGE_BASE_OFFSET(port, cli), val);
4017
4018 CNIC_WR(dev, BAR_USTRORM_INTMEM +
4019 USTORM_CQE_PAGE_NEXT_OFFSET(port, cli), val);
4020
4021 /* client tstorm info */
4022 tstorm_client.mtu = cp->l2_single_buf_size - 14;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004023 tstorm_client.config_flags = TSTORM_ETH_CLIENT_CONFIG_E1HOV_REM_ENABLE;
4024
4025 if (cli < MAX_T_STAT_COUNTER_ID) {
4026 tstorm_client.config_flags |=
4027 TSTORM_ETH_CLIENT_CONFIG_STATSITICS_ENABLE;
4028 tstorm_client.statistics_counter_id = cli;
4029 }
Michael Chan71034ba2009-10-10 13:46:59 +00004030
4031 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4032 TSTORM_CLIENT_CONFIG_OFFSET(port, cli),
4033 ((u32 *)&tstorm_client)[0]);
4034 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4035 TSTORM_CLIENT_CONFIG_OFFSET(port, cli) + 4,
4036 ((u32 *)&tstorm_client)[1]);
4037
4038 /* reset tstorm per client statistics */
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004039 if (cli < MAX_T_STAT_COUNTER_ID) {
4040
4041 val = BAR_TSTRORM_INTMEM +
4042 TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4043 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4044 CNIC_WR(dev, val + i * 4, 0);
4045 }
Michael Chan71034ba2009-10-10 13:46:59 +00004046
4047 /* reset ustorm per client statistics */
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004048 if (cli < MAX_U_STAT_COUNTER_ID) {
4049 val = BAR_USTRORM_INTMEM +
4050 USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4051 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4052 CNIC_WR(dev, val + i * 4, 0);
4053 }
Michael Chan71034ba2009-10-10 13:46:59 +00004054
4055 cp->rx_cons_ptr =
4056 &cp->bnx2x_def_status_blk->u_def_status_block.index_values[
4057 HC_INDEX_DEF_U_ETH_ISCSI_RX_CQ_CONS];
4058}
4059
4060static void cnic_get_bnx2x_iscsi_info(struct cnic_dev *dev)
4061{
4062 struct cnic_local *cp = dev->cnic_priv;
4063 u32 base, addr, val;
4064 int port = CNIC_PORT(cp);
4065
4066 dev->max_iscsi_conn = 0;
4067 base = CNIC_RD(dev, MISC_REG_SHARED_MEM_ADDR);
4068 if (base < 0xa0000 || base >= 0xc0000)
4069 return;
4070
Michael Chandd2e4db2009-12-02 15:15:37 +00004071 addr = BNX2X_SHMEM_ADDR(base,
Michael Chan71034ba2009-10-10 13:46:59 +00004072 dev_info.port_hw_config[port].iscsi_mac_upper);
4073
Michael Chandd2e4db2009-12-02 15:15:37 +00004074 val = CNIC_RD(dev, addr);
4075
Michael Chan71034ba2009-10-10 13:46:59 +00004076 dev->mac_addr[0] = (u8) (val >> 8);
4077 dev->mac_addr[1] = (u8) val;
4078
Michael Chandd2e4db2009-12-02 15:15:37 +00004079 addr = BNX2X_SHMEM_ADDR(base,
Michael Chan71034ba2009-10-10 13:46:59 +00004080 dev_info.port_hw_config[port].iscsi_mac_lower);
4081
Michael Chandd2e4db2009-12-02 15:15:37 +00004082 val = CNIC_RD(dev, addr);
4083
Michael Chan71034ba2009-10-10 13:46:59 +00004084 dev->mac_addr[2] = (u8) (val >> 24);
4085 dev->mac_addr[3] = (u8) (val >> 16);
4086 dev->mac_addr[4] = (u8) (val >> 8);
4087 dev->mac_addr[5] = (u8) val;
4088
4089 addr = BNX2X_SHMEM_ADDR(base, validity_map[port]);
4090 val = CNIC_RD(dev, addr);
4091
4092 if (!(val & SHR_MEM_VALIDITY_LIC_NO_KEY_IN_EFFECT)) {
4093 u16 val16;
4094
4095 addr = BNX2X_SHMEM_ADDR(base,
4096 drv_lic_key[port].max_iscsi_init_conn);
4097 val16 = CNIC_RD16(dev, addr);
4098
4099 if (val16)
4100 val16 ^= 0x1e1e;
4101 dev->max_iscsi_conn = val16;
4102 }
4103 if (BNX2X_CHIP_IS_E1H(cp->chip_id)) {
4104 int func = CNIC_FUNC(cp);
4105
4106 addr = BNX2X_SHMEM_ADDR(base,
4107 mf_cfg.func_mf_config[func].e1hov_tag);
4108 val = CNIC_RD(dev, addr);
4109 val &= FUNC_MF_CFG_E1HOV_TAG_MASK;
4110 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
4111 addr = BNX2X_SHMEM_ADDR(base,
4112 mf_cfg.func_mf_config[func].config);
4113 val = CNIC_RD(dev, addr);
4114 val &= FUNC_MF_CFG_PROTOCOL_MASK;
4115 if (val != FUNC_MF_CFG_PROTOCOL_ISCSI)
4116 dev->max_iscsi_conn = 0;
4117 }
4118 }
4119}
4120
4121static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4122{
4123 struct cnic_local *cp = dev->cnic_priv;
4124 int func = CNIC_FUNC(cp), ret, i;
4125 int port = CNIC_PORT(cp);
Michael Chan14203982010-10-06 03:16:06 +00004126 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004127 u16 eq_idx;
4128 u8 sb_id = cp->status_blk_num;
4129
Michael Chan14203982010-10-06 03:16:06 +00004130 cp->pfid = func;
4131 pfid = cp->pfid;
4132
Michael Chan71034ba2009-10-10 13:46:59 +00004133 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Michael Chan520efdf2010-06-24 14:58:37 +00004134 cp->iscsi_start_cid);
Michael Chan71034ba2009-10-10 13:46:59 +00004135
4136 if (ret)
4137 return -ENOMEM;
4138
Michael Chane6c28892010-06-24 14:58:39 +00004139 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004140 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
Michael Chane6c28892010-06-24 14:58:39 +00004141 cp->kcq1.sw_prod_idx = 0;
4142
4143 cp->kcq1.hw_prod_idx_ptr =
4144 &cp->status_blk.bnx2x->c_status_block.index_values[
4145 HC_INDEX_C_ISCSI_EQ_CONS];
4146 cp->kcq1.status_idx_ptr =
4147 &cp->status_blk.bnx2x->c_status_block.status_block_index;
Michael Chan71034ba2009-10-10 13:46:59 +00004148
4149 cnic_get_bnx2x_iscsi_info(dev);
4150
4151 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00004152 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00004153 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004154 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004155 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004156 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004157 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004158 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004159 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004160 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004161 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004162 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004163 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004164 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004165 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004166 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004167 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004168 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004169 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004170 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00004171 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004172 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Michael Chan71034ba2009-10-10 13:46:59 +00004173 HC_INDEX_C_ISCSI_EQ_CONS);
4174
4175 for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4176 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004177 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
Michael Chan71034ba2009-10-10 13:46:59 +00004178 cp->conn_buf_info.pgtbl[2 * i]);
4179 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004180 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004181 cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4182 }
4183
4184 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004185 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004186 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4187 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004188 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004189 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4190
4191 cnic_setup_bnx2x_context(dev);
4192
4193 eq_idx = CNIC_RD16(dev, BAR_CSTRORM_INTMEM +
4194 CSTORM_SB_HOST_STATUS_BLOCK_C_OFFSET(port, sb_id) +
4195 offsetof(struct cstorm_status_block_c,
4196 index_values[HC_INDEX_C_ISCSI_EQ_CONS]));
4197 if (eq_idx != 0) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004198 netdev_err(dev->netdev, "EQ cons index %x != 0\n", eq_idx);
Michael Chan71034ba2009-10-10 13:46:59 +00004199 return -EBUSY;
4200 }
4201 ret = cnic_init_bnx2x_irq(dev);
4202 if (ret)
4203 return ret;
4204
4205 cnic_init_bnx2x_tx_ring(dev);
4206 cnic_init_bnx2x_rx_ring(dev);
4207
4208 return 0;
4209}
4210
Michael Chan86b53602009-10-10 13:46:57 +00004211static void cnic_init_rings(struct cnic_dev *dev)
4212{
4213 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4214 cnic_init_bnx2_tx_ring(dev);
4215 cnic_init_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004216 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4217 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00004218 u32 cli = BNX2X_ISCSI_CL_ID(CNIC_E1HVN(cp));
4219 union l5cm_specific_data l5_data;
4220 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chanc7596b72009-12-02 15:15:35 +00004221 u32 off, i;
Michael Chan71034ba2009-10-10 13:46:59 +00004222
4223 rx_prods.bd_prod = 0;
4224 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4225 barrier();
4226
Michael Chanc7596b72009-12-02 15:15:35 +00004227 off = BAR_USTRORM_INTMEM +
Michael Chan71034ba2009-10-10 13:46:59 +00004228 USTORM_RX_PRODS_OFFSET(CNIC_PORT(cp), cli);
4229
4230 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00004231 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00004232
Michael Chan48f753d2010-05-18 11:32:53 +00004233 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4234
Michael Chan71034ba2009-10-10 13:46:59 +00004235 cnic_init_bnx2x_tx_ring(dev);
4236 cnic_init_bnx2x_rx_ring(dev);
4237
4238 l5_data.phy_address.lo = cli;
4239 l5_data.phy_address.hi = 0;
4240 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
4241 BNX2X_ISCSI_L2_CID, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00004242 i = 0;
4243 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4244 ++i < 10)
4245 msleep(1);
4246
4247 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4248 netdev_err(dev->netdev,
4249 "iSCSI CLIENT_SETUP did not complete\n");
4250 cnic_kwq_completion(dev, 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004251 cnic_ring_ctl(dev, BNX2X_ISCSI_L2_CID, cli, 1);
Michael Chan86b53602009-10-10 13:46:57 +00004252 }
4253}
4254
4255static void cnic_shutdown_rings(struct cnic_dev *dev)
4256{
4257 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4258 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004259 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4260 struct cnic_local *cp = dev->cnic_priv;
4261 u32 cli = BNX2X_ISCSI_CL_ID(CNIC_E1HVN(cp));
Michael Chan8b065b62009-12-02 15:15:36 +00004262 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00004263 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00004264
4265 cnic_ring_ctl(dev, BNX2X_ISCSI_L2_CID, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00004266
Michael Chan48f753d2010-05-18 11:32:53 +00004267 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4268
Michael Chan8b065b62009-12-02 15:15:36 +00004269 l5_data.phy_address.lo = cli;
4270 l5_data.phy_address.hi = 0;
4271 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
4272 BNX2X_ISCSI_L2_CID, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00004273 i = 0;
4274 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4275 ++i < 10)
4276 msleep(1);
4277
4278 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4279 netdev_err(dev->netdev,
4280 "iSCSI CLIENT_HALT did not complete\n");
4281 cnic_kwq_completion(dev, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00004282
4283 memset(&l5_data, 0, sizeof(l5_data));
4284 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CFC_DEL,
4285 BNX2X_ISCSI_L2_CID, ETH_CONNECTION_TYPE |
4286 (1 << SPE_HDR_COMMON_RAMROD_SHIFT), &l5_data);
4287 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00004288 }
4289}
4290
Michael Chana3059b12009-08-14 15:49:44 +00004291static int cnic_register_netdev(struct cnic_dev *dev)
4292{
4293 struct cnic_local *cp = dev->cnic_priv;
4294 struct cnic_eth_dev *ethdev = cp->ethdev;
4295 int err;
4296
4297 if (!ethdev)
4298 return -ENODEV;
4299
4300 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
4301 return 0;
4302
4303 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
4304 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00004305 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00004306
4307 return err;
4308}
4309
4310static void cnic_unregister_netdev(struct cnic_dev *dev)
4311{
4312 struct cnic_local *cp = dev->cnic_priv;
4313 struct cnic_eth_dev *ethdev = cp->ethdev;
4314
4315 if (!ethdev)
4316 return;
4317
4318 ethdev->drv_unregister_cnic(dev->netdev);
4319}
4320
Michael Chana4636962009-06-08 18:14:43 -07004321static int cnic_start_hw(struct cnic_dev *dev)
4322{
4323 struct cnic_local *cp = dev->cnic_priv;
4324 struct cnic_eth_dev *ethdev = cp->ethdev;
4325 int err;
4326
4327 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
4328 return -EALREADY;
4329
Michael Chana4636962009-06-08 18:14:43 -07004330 dev->regview = ethdev->io_base;
4331 cp->chip_id = ethdev->chip_id;
4332 pci_dev_get(dev->pcidev);
4333 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00004334 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07004335 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
4336
4337 err = cp->alloc_resc(dev);
4338 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004339 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07004340 goto err1;
4341 }
4342
4343 err = cp->start_hw(dev);
4344 if (err)
4345 goto err1;
4346
4347 err = cnic_cm_open(dev);
4348 if (err)
4349 goto err1;
4350
4351 set_bit(CNIC_F_CNIC_UP, &dev->flags);
4352
4353 cp->enable_int(dev);
4354
4355 return 0;
4356
4357err1:
Michael Chana4636962009-06-08 18:14:43 -07004358 cp->free_resc(dev);
4359 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07004360 return err;
4361}
4362
4363static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
4364{
Michael Chana4636962009-06-08 18:14:43 -07004365 cnic_disable_bnx2_int_sync(dev);
4366
4367 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4368 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4369
4370 cnic_init_context(dev, KWQ_CID);
4371 cnic_init_context(dev, KCQ_CID);
4372
4373 cnic_setup_5709_context(dev, 0);
4374 cnic_free_irq(dev);
4375
Michael Chana4636962009-06-08 18:14:43 -07004376 cnic_free_resc(dev);
4377}
4378
Michael Chan71034ba2009-10-10 13:46:59 +00004379
4380static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
4381{
4382 struct cnic_local *cp = dev->cnic_priv;
4383 u8 sb_id = cp->status_blk_num;
4384 int port = CNIC_PORT(cp);
4385
4386 cnic_free_irq(dev);
4387 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
4388 CSTORM_SB_HOST_STATUS_BLOCK_C_OFFSET(port, sb_id) +
4389 offsetof(struct cstorm_status_block_c,
4390 index_values[HC_INDEX_C_ISCSI_EQ_CONS]),
4391 0);
Michael Chan4e9c4fd2009-12-10 15:40:58 +00004392 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004393 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00004394 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004395 cnic_free_resc(dev);
4396}
4397
Michael Chana4636962009-06-08 18:14:43 -07004398static void cnic_stop_hw(struct cnic_dev *dev)
4399{
4400 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
4401 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00004402 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07004403
Michael Chan48f753d2010-05-18 11:32:53 +00004404 /* Need to wait for the ring shutdown event to complete
4405 * before clearing the CNIC_UP flag.
4406 */
4407 while (cp->uio_dev != -1 && i < 15) {
4408 msleep(100);
4409 i++;
4410 }
Michael Chana4636962009-06-08 18:14:43 -07004411 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
4412 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
4413 synchronize_rcu();
4414 cnic_cm_shutdown(dev);
4415 cp->stop_hw(dev);
4416 pci_dev_put(dev->pcidev);
4417 }
4418}
4419
4420static void cnic_free_dev(struct cnic_dev *dev)
4421{
4422 int i = 0;
4423
4424 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
4425 msleep(100);
4426 i++;
4427 }
4428 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00004429 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07004430
Joe Perchesddf79b22010-02-17 15:01:54 +00004431 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07004432 dev_put(dev->netdev);
4433 kfree(dev);
4434}
4435
4436static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
4437 struct pci_dev *pdev)
4438{
4439 struct cnic_dev *cdev;
4440 struct cnic_local *cp;
4441 int alloc_size;
4442
4443 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
4444
4445 cdev = kzalloc(alloc_size , GFP_KERNEL);
4446 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004447 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07004448 return NULL;
4449 }
4450
4451 cdev->netdev = dev;
4452 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
4453 cdev->register_device = cnic_register_device;
4454 cdev->unregister_device = cnic_unregister_device;
4455 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
4456
4457 cp = cdev->cnic_priv;
4458 cp->dev = cdev;
4459 cp->uio_dev = -1;
4460 cp->l2_single_buf_size = 0x400;
4461 cp->l2_rx_ring_size = 3;
4462
4463 spin_lock_init(&cp->cnic_ulp_lock);
4464
Joe Perchesddf79b22010-02-17 15:01:54 +00004465 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07004466
4467 return cdev;
4468}
4469
4470static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
4471{
4472 struct pci_dev *pdev;
4473 struct cnic_dev *cdev;
4474 struct cnic_local *cp;
4475 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07004476 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07004477
Michael Chane2ee3612009-06-13 17:43:02 -07004478 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07004479 if (probe) {
4480 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00004481 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07004482 }
4483 if (!ethdev)
4484 return NULL;
4485
4486 pdev = ethdev->pdev;
4487 if (!pdev)
4488 return NULL;
4489
4490 dev_hold(dev);
4491 pci_dev_get(pdev);
4492 if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
4493 pdev->device == PCI_DEVICE_ID_NX2_5709S) {
4494 u8 rev;
4495
4496 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
4497 if (rev < 0x10) {
4498 pci_dev_put(pdev);
4499 goto cnic_err;
4500 }
4501 }
4502 pci_dev_put(pdev);
4503
4504 cdev = cnic_alloc_dev(dev, pdev);
4505 if (cdev == NULL)
4506 goto cnic_err;
4507
4508 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
4509 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
4510
4511 cp = cdev->cnic_priv;
4512 cp->ethdev = ethdev;
4513 cdev->pcidev = pdev;
4514
4515 cp->cnic_ops = &cnic_bnx2_ops;
4516 cp->start_hw = cnic_start_bnx2_hw;
4517 cp->stop_hw = cnic_stop_bnx2_hw;
4518 cp->setup_pgtbl = cnic_setup_page_tbl;
4519 cp->alloc_resc = cnic_alloc_bnx2_resc;
4520 cp->free_resc = cnic_free_resc;
4521 cp->start_cm = cnic_cm_init_bnx2_hw;
4522 cp->stop_cm = cnic_cm_stop_bnx2_hw;
4523 cp->enable_int = cnic_enable_bnx2_int;
4524 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
4525 cp->close_conn = cnic_close_bnx2_conn;
4526 cp->next_idx = cnic_bnx2_next_idx;
4527 cp->hw_idx = cnic_bnx2_hw_idx;
4528 return cdev;
4529
4530cnic_err:
4531 dev_put(dev);
4532 return NULL;
4533}
4534
Michael Chan71034ba2009-10-10 13:46:59 +00004535static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
4536{
4537 struct pci_dev *pdev;
4538 struct cnic_dev *cdev;
4539 struct cnic_local *cp;
4540 struct cnic_eth_dev *ethdev = NULL;
4541 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
4542
4543 probe = symbol_get(bnx2x_cnic_probe);
4544 if (probe) {
4545 ethdev = (*probe)(dev);
4546 symbol_put(bnx2x_cnic_probe);
4547 }
4548 if (!ethdev)
4549 return NULL;
4550
4551 pdev = ethdev->pdev;
4552 if (!pdev)
4553 return NULL;
4554
4555 dev_hold(dev);
4556 cdev = cnic_alloc_dev(dev, pdev);
4557 if (cdev == NULL) {
4558 dev_put(dev);
4559 return NULL;
4560 }
4561
4562 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
4563 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
4564
4565 cp = cdev->cnic_priv;
4566 cp->ethdev = ethdev;
4567 cdev->pcidev = pdev;
4568
4569 cp->cnic_ops = &cnic_bnx2x_ops;
4570 cp->start_hw = cnic_start_bnx2x_hw;
4571 cp->stop_hw = cnic_stop_bnx2x_hw;
4572 cp->setup_pgtbl = cnic_setup_page_tbl_le;
4573 cp->alloc_resc = cnic_alloc_bnx2x_resc;
4574 cp->free_resc = cnic_free_resc;
4575 cp->start_cm = cnic_cm_init_bnx2x_hw;
4576 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
4577 cp->enable_int = cnic_enable_bnx2x_int;
4578 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
4579 cp->ack_int = cnic_ack_bnx2x_msix;
4580 cp->close_conn = cnic_close_bnx2x_conn;
4581 cp->next_idx = cnic_bnx2x_next_idx;
4582 cp->hw_idx = cnic_bnx2x_hw_idx;
4583 return cdev;
4584}
4585
Michael Chana4636962009-06-08 18:14:43 -07004586static struct cnic_dev *is_cnic_dev(struct net_device *dev)
4587{
4588 struct ethtool_drvinfo drvinfo;
4589 struct cnic_dev *cdev = NULL;
4590
4591 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
4592 memset(&drvinfo, 0, sizeof(drvinfo));
4593 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
4594
4595 if (!strcmp(drvinfo.driver, "bnx2"))
4596 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004597 if (!strcmp(drvinfo.driver, "bnx2x"))
4598 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07004599 if (cdev) {
4600 write_lock(&cnic_dev_lock);
4601 list_add(&cdev->list, &cnic_dev_list);
4602 write_unlock(&cnic_dev_lock);
4603 }
4604 }
4605 return cdev;
4606}
4607
4608/**
4609 * netdev event handler
4610 */
4611static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
4612 void *ptr)
4613{
4614 struct net_device *netdev = ptr;
4615 struct cnic_dev *dev;
4616 int if_type;
4617 int new_dev = 0;
4618
4619 dev = cnic_from_netdev(netdev);
4620
4621 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
4622 /* Check for the hot-plug device */
4623 dev = is_cnic_dev(netdev);
4624 if (dev) {
4625 new_dev = 1;
4626 cnic_hold(dev);
4627 }
4628 }
4629 if (dev) {
4630 struct cnic_local *cp = dev->cnic_priv;
4631
4632 if (new_dev)
4633 cnic_ulp_init(dev);
4634 else if (event == NETDEV_UNREGISTER)
4635 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07004636
4637 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00004638 if (cnic_register_netdev(dev) != 0) {
4639 cnic_put(dev);
4640 goto done;
4641 }
Michael Chana4636962009-06-08 18:14:43 -07004642 if (!cnic_start_hw(dev))
4643 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07004644 }
4645
4646 rcu_read_lock();
4647 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
4648 struct cnic_ulp_ops *ulp_ops;
4649 void *ctx;
4650
4651 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
4652 if (!ulp_ops || !ulp_ops->indicate_netevent)
4653 continue;
4654
4655 ctx = cp->ulp_handle[if_type];
4656
4657 ulp_ops->indicate_netevent(ctx, event);
4658 }
4659 rcu_read_unlock();
4660
4661 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07004662 cnic_ulp_stop(dev);
4663 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00004664 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07004665 } else if (event == NETDEV_UNREGISTER) {
4666 write_lock(&cnic_dev_lock);
4667 list_del_init(&dev->list);
4668 write_unlock(&cnic_dev_lock);
4669
4670 cnic_put(dev);
4671 cnic_free_dev(dev);
4672 goto done;
4673 }
4674 cnic_put(dev);
4675 }
4676done:
4677 return NOTIFY_DONE;
4678}
4679
4680static struct notifier_block cnic_netdev_notifier = {
4681 .notifier_call = cnic_netdev_event
4682};
4683
4684static void cnic_release(void)
4685{
4686 struct cnic_dev *dev;
4687
4688 while (!list_empty(&cnic_dev_list)) {
4689 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
4690 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
4691 cnic_ulp_stop(dev);
4692 cnic_stop_hw(dev);
4693 }
4694
4695 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00004696 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07004697 list_del_init(&dev->list);
4698 cnic_free_dev(dev);
4699 }
4700}
4701
4702static int __init cnic_init(void)
4703{
4704 int rc = 0;
4705
Joe Perchesddf79b22010-02-17 15:01:54 +00004706 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07004707
4708 rc = register_netdevice_notifier(&cnic_netdev_notifier);
4709 if (rc) {
4710 cnic_release();
4711 return rc;
4712 }
4713
4714 return 0;
4715}
4716
4717static void __exit cnic_exit(void)
4718{
4719 unregister_netdevice_notifier(&cnic_netdev_notifier);
4720 cnic_release();
Michael Chana4636962009-06-08 18:14:43 -07004721}
4722
4723module_init(cnic_init);
4724module_exit(cnic_exit);