blob: 1294936f4fa119687c5e19880e9f5f0ddbaecb4d [file] [log] [blame]
Michael Chancf4e6362009-06-08 18:14:44 -07001/* bnx2i.c: Broadcom NetXtreme II iSCSI driver.
2 *
3 * Copyright (c) 2006 - 2009 Broadcom Corporation
4 * Copyright (c) 2007, 2008 Red Hat, Inc. All rights reserved.
5 * Copyright (c) 2007, 2008 Mike Christie
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
10 *
11 * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
12 */
13
14#include "bnx2i.h"
15
16static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
17static u32 adapter_count;
Michael Chancf4e6362009-06-08 18:14:44 -070018
19#define DRV_MODULE_NAME "bnx2i"
Eddie Waia2f1d132010-07-01 15:34:56 -070020#define DRV_MODULE_VERSION "2.1.2"
21#define DRV_MODULE_RELDATE "Jun 28, 2010"
Michael Chancf4e6362009-06-08 18:14:44 -070022
23static char version[] __devinitdata =
24 "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
25 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
26
27
28MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com>");
Anil Veerabhadrappa457549d2010-03-25 10:54:44 -070029MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709/57710/57711"
30 " iSCSI Driver");
Michael Chancf4e6362009-06-08 18:14:44 -070031MODULE_LICENSE("GPL");
32MODULE_VERSION(DRV_MODULE_VERSION);
33
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -070034static DEFINE_MUTEX(bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -070035
Anil Veerabhadrappa87761932009-12-07 11:40:18 -080036unsigned int event_coal_min = 24;
37module_param(event_coal_min, int, 0664);
38MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
39
Michael Chancf4e6362009-06-08 18:14:44 -070040unsigned int event_coal_div = 1;
41module_param(event_coal_div, int, 0664);
42MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
43
44unsigned int en_tcp_dack = 1;
45module_param(en_tcp_dack, int, 0664);
46MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
47
48unsigned int error_mask1 = 0x00;
49module_param(error_mask1, int, 0664);
50MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
51
52unsigned int error_mask2 = 0x00;
53module_param(error_mask2, int, 0664);
54MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
55
56unsigned int sq_size;
57module_param(sq_size, int, 0664);
58MODULE_PARM_DESC(sq_size, "Configure SQ size");
59
60unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
61module_param(rq_size, int, 0664);
62MODULE_PARM_DESC(rq_size, "Configure RQ size");
63
64u64 iscsi_error_mask = 0x00;
65
66static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
67
68
69/**
70 * bnx2i_identify_device - identifies NetXtreme II device type
71 * @hba: Adapter structure pointer
72 *
73 * This function identifies the NX2 device type and sets appropriate
74 * queue mailbox register access method, 5709 requires driver to
75 * access MBOX regs using *bin* mode
76 */
77void bnx2i_identify_device(struct bnx2i_hba *hba)
78{
79 hba->cnic_dev_type = 0;
80 if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
81 (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
82 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
83 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
84 (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
85 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
86 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
87 (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
88 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
89 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
90 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
Anil Veerabhadrappa5d9e1fa2009-12-07 11:39:33 -080091 hba->pci_did == PCI_DEVICE_ID_NX2_57711 ||
92 hba->pci_did == PCI_DEVICE_ID_NX2_57711E)
Michael Chancf4e6362009-06-08 18:14:44 -070093 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
Anil Veerabhadrappa5d9e1fa2009-12-07 11:39:33 -080094 else
95 printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
96 hba->pci_did);
Michael Chancf4e6362009-06-08 18:14:44 -070097}
98
99
100/**
101 * get_adapter_list_head - returns head of adapter list
102 */
103struct bnx2i_hba *get_adapter_list_head(void)
104{
105 struct bnx2i_hba *hba = NULL;
106 struct bnx2i_hba *tmp_hba;
107
108 if (!adapter_count)
109 goto hba_not_found;
110
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700111 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700112 list_for_each_entry(tmp_hba, &adapter_list, link) {
113 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
114 hba = tmp_hba;
115 break;
116 }
117 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700118 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700119hba_not_found:
120 return hba;
121}
122
123
124/**
125 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
126 * @cnic: pointer to cnic device instance
127 *
128 */
129struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
130{
131 struct bnx2i_hba *hba, *temp;
132
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700133 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700134 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
135 if (hba->cnic == cnic) {
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700136 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700137 return hba;
138 }
139 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700140 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700141 return NULL;
142}
143
144
145/**
146 * bnx2i_start - cnic callback to initialize & start adapter instance
147 * @handle: transparent handle pointing to adapter structure
148 *
149 * This function maps adapter structure to pcidev structure and initiates
150 * firmware handshake to enable/initialize on chip iscsi components
151 * This bnx2i - cnic interface api callback is issued after following
152 * 2 conditions are met -
153 * a) underlying network interface is up (marked by event 'NETDEV_UP'
154 * from netdev
155 * b) bnx2i adapter instance is registered
156 */
157void bnx2i_start(void *handle)
158{
159#define BNX2I_INIT_POLL_TIME (1000 / HZ)
160 struct bnx2i_hba *hba = handle;
161 int i = HZ;
162
163 bnx2i_send_fw_iscsi_init_msg(hba);
164 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
165 msleep(BNX2I_INIT_POLL_TIME);
166}
167
168
169/**
Eddie Wai250ae982010-08-12 16:44:30 -0700170 * bnx2i_chip_cleanup - local routine to handle chip cleanup
171 * @hba: Adapter instance to register
172 *
173 * Driver checks if adapter still has any active connections before
174 * executing the cleanup process
175 */
176static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
177{
178 struct bnx2i_endpoint *bnx2i_ep;
179 struct list_head *pos, *tmp;
180
181 if (hba->ofld_conns_active) {
182 /* Stage to force the disconnection
183 * This is the case where the daemon is either slow or
184 * not present
185 */
186 printk(KERN_ALERT "bnx2i: (%s) chip cleanup for %d active "
187 "connections\n", hba->netdev->name,
188 hba->ofld_conns_active);
189 mutex_lock(&hba->net_dev_lock);
190 list_for_each_safe(pos, tmp, &hba->ep_active_list) {
191 bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
192 /* Clean up the chip only */
193 bnx2i_hw_ep_disconnect(bnx2i_ep);
194 bnx2i_ep->cm_sk = NULL;
195 }
196 mutex_unlock(&hba->net_dev_lock);
197 }
198}
199
200
201/**
Michael Chancf4e6362009-06-08 18:14:44 -0700202 * bnx2i_stop - cnic callback to shutdown adapter instance
203 * @handle: transparent handle pointing to adapter structure
204 *
205 * driver checks if adapter is already in shutdown mode, if not start
206 * the shutdown process
207 */
208void bnx2i_stop(void *handle)
209{
210 struct bnx2i_hba *hba = handle;
Eddie Wai55e15c92010-07-01 15:34:52 -0700211 int conns_active;
Michael Chancf4e6362009-06-08 18:14:44 -0700212
213 /* check if cleanup happened in GOING_DOWN context */
Michael Chancf4e6362009-06-08 18:14:44 -0700214 if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
215 &hba->adapter_state))
216 iscsi_host_for_each_session(hba->shost,
217 bnx2i_drop_session);
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700218
219 /* Wait for all endpoints to be torn down, Chip will be reset once
220 * control returns to network driver. So it is required to cleanup and
221 * release all connection resources before returning from this routine.
222 */
Eddie Wai55e15c92010-07-01 15:34:52 -0700223 while (hba->ofld_conns_active) {
224 conns_active = hba->ofld_conns_active;
225 wait_event_interruptible_timeout(hba->eh_wait,
226 (hba->ofld_conns_active != conns_active),
227 hba->hba_shutdown_tmo);
228 if (hba->ofld_conns_active == conns_active)
229 break;
230 }
Eddie Wai250ae982010-08-12 16:44:30 -0700231 bnx2i_chip_cleanup(hba);
Eddie Wai55e15c92010-07-01 15:34:52 -0700232
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700233 /* This flag should be cleared last so that ep_disconnect() gracefully
234 * cleans up connection context
235 */
236 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
Michael Chancf4e6362009-06-08 18:14:44 -0700237}
238
239/**
240 * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
241 * @hba: Adapter instance to register
242 *
243 * registers bnx2i adapter instance with the cnic driver while holding the
244 * adapter structure lock
245 */
246void bnx2i_register_device(struct bnx2i_hba *hba)
247{
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -0700248 int rc;
249
Michael Chancf4e6362009-06-08 18:14:44 -0700250 if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
251 test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
252 return;
253 }
254
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -0700255 rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700256
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -0700257 if (!rc)
258 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700259}
260
261
262/**
263 * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
264 *
265 * registers all bnx2i adapter instances with the cnic driver while holding
266 * the global resource lock
267 */
268void bnx2i_reg_dev_all(void)
269{
270 struct bnx2i_hba *hba, *temp;
271
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700272 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700273 list_for_each_entry_safe(hba, temp, &adapter_list, link)
274 bnx2i_register_device(hba);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700275 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700276}
277
278
279/**
280 * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
281 * @hba: Adapter instance to unregister
282 *
283 * registers bnx2i adapter instance with the cnic driver while holding
284 * the adapter structure lock
285 */
286static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
287{
288 if (hba->ofld_conns_active ||
289 !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
290 test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
291 return;
292
293 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
294
Michael Chancf4e6362009-06-08 18:14:44 -0700295 /* ep_disconnect could come before NETDEV_DOWN, driver won't
296 * see NETDEV_DOWN as it already unregistered itself.
297 */
298 hba->adapter_state = 0;
299 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
300}
301
302/**
303 * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
304 *
305 * unregisters all bnx2i adapter instances with the cnic driver while holding
306 * the global resource lock
307 */
308void bnx2i_unreg_dev_all(void)
309{
310 struct bnx2i_hba *hba, *temp;
311
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700312 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700313 list_for_each_entry_safe(hba, temp, &adapter_list, link)
314 bnx2i_unreg_one_device(hba);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700315 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700316}
317
318
319/**
320 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
321 * @hba: bnx2i adapter instance
322 * @cnic: cnic device handle
323 *
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700324 * Global resource lock is held during critical sections below. This routine is
325 * called from either cnic_register_driver() or device hot plug context and
326 * and does majority of device specific initialization
Michael Chancf4e6362009-06-08 18:14:44 -0700327 */
328static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
329{
330 int rc;
331
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700332 mutex_lock(&bnx2i_dev_lock);
Anil Veerabhadrappace2d7632010-03-25 10:54:42 -0700333 hba->cnic = cnic;
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700334 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
335 if (!rc) {
Michael Chancf4e6362009-06-08 18:14:44 -0700336 hba->age++;
Michael Chancf4e6362009-06-08 18:14:44 -0700337 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700338 list_add_tail(&hba->link, &adapter_list);
339 adapter_count++;
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700340 } else if (rc == -EBUSY) /* duplicate registration */
341 printk(KERN_ALERT "bnx2i, duplicate registration"
342 "hba=%p, cnic=%p\n", hba, cnic);
343 else if (rc == -EAGAIN)
344 printk(KERN_ERR "bnx2i, driver not registered\n");
345 else if (rc == -EINVAL)
346 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
347 else
348 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
Michael Chancf4e6362009-06-08 18:14:44 -0700349
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700350 mutex_unlock(&bnx2i_dev_lock);
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700351
352 return rc;
Michael Chancf4e6362009-06-08 18:14:44 -0700353}
354
355
356/**
357 * bnx2i_ulp_init - initialize an adapter instance
358 * @dev: cnic device handle
359 *
360 * Called from cnic_register_driver() context to initialize all enumerated
361 * cnic devices. This routine allocate adapter structure and other
362 * device specific resources.
363 */
364void bnx2i_ulp_init(struct cnic_dev *dev)
365{
366 struct bnx2i_hba *hba;
367
368 /* Allocate a HBA structure for this device */
369 hba = bnx2i_alloc_hba(dev);
370 if (!hba) {
371 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
372 return;
373 }
374
375 /* Get PCI related information and update hba struct members */
376 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
377 if (bnx2i_init_one(hba, dev)) {
378 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
379 bnx2i_free_hba(hba);
Anil Veerabhadrappace2d7632010-03-25 10:54:42 -0700380 }
Michael Chancf4e6362009-06-08 18:14:44 -0700381}
382
383
384/**
385 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
386 * @dev: cnic device handle
387 *
388 */
389void bnx2i_ulp_exit(struct cnic_dev *dev)
390{
391 struct bnx2i_hba *hba;
392
393 hba = bnx2i_find_hba_for_cnic(dev);
394 if (!hba) {
395 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
396 "found, dev 0x%p\n", dev);
397 return;
398 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700399 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700400 list_del_init(&hba->link);
401 adapter_count--;
402
403 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
404 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
405 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700406 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700407 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700408
409 bnx2i_free_hba(hba);
410}
411
412
413/**
414 * bnx2i_mod_init - module init entry point
415 *
416 * initialize any driver wide global data structures such as endpoint pool,
417 * tcp port manager/queue, sysfs. finally driver will register itself
418 * with the cnic module
419 */
420static int __init bnx2i_mod_init(void)
421{
422 int err;
423
424 printk(KERN_INFO "%s", version);
425
Anil Veerabhadrappaf8c9abe2009-12-07 11:39:54 -0800426 if (sq_size && !is_power_of_2(sq_size))
Michael Chancf4e6362009-06-08 18:14:44 -0700427 sq_size = roundup_pow_of_two(sq_size);
428
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700429 mutex_init(&bnx2i_dev_lock);
430
Michael Chancf4e6362009-06-08 18:14:44 -0700431 bnx2i_scsi_xport_template =
432 iscsi_register_transport(&bnx2i_iscsi_transport);
433 if (!bnx2i_scsi_xport_template) {
434 printk(KERN_ERR "Could not register bnx2i transport.\n");
435 err = -ENOMEM;
436 goto out;
437 }
438
439 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
440 if (err) {
441 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
442 goto unreg_xport;
443 }
444
445 return 0;
446
447unreg_xport:
448 iscsi_unregister_transport(&bnx2i_iscsi_transport);
449out:
450 return err;
451}
452
453
454/**
455 * bnx2i_mod_exit - module cleanup/exit entry point
456 *
457 * Global resource lock and host adapter lock is held during critical sections
458 * in this function. Driver will browse through the adapter list, cleans-up
459 * each instance, unregisters iscsi transport name and finally driver will
460 * unregister itself with the cnic module
461 */
462static void __exit bnx2i_mod_exit(void)
463{
464 struct bnx2i_hba *hba;
465
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700466 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700467 while (!list_empty(&adapter_list)) {
468 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
469 list_del(&hba->link);
470 adapter_count--;
471
472 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
Eddie Wai250ae982010-08-12 16:44:30 -0700473 bnx2i_chip_cleanup(hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700474 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
475 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700476 }
477
Michael Chancf4e6362009-06-08 18:14:44 -0700478 bnx2i_free_hba(hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700479 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700480 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700481
482 iscsi_unregister_transport(&bnx2i_iscsi_transport);
483 cnic_unregister_driver(CNIC_ULP_ISCSI);
484}
485
486module_init(bnx2i_mod_init);
487module_exit(bnx2i_mod_exit);