1 /* bnx2i.c: Broadcom NetXtreme II iSCSI driver.
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
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.
11 * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
16 static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
17 static u32 adapter_count;
19 #define DRV_MODULE_NAME "bnx2i"
20 #define DRV_MODULE_VERSION "2.1.0"
21 #define DRV_MODULE_RELDATE "Dec 06, 2009"
23 static char version[] __devinitdata =
24 "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
25 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
28 MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com>");
29 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709 iSCSI Driver");
30 MODULE_LICENSE("GPL");
31 MODULE_VERSION(DRV_MODULE_VERSION);
33 static DEFINE_MUTEX(bnx2i_dev_lock);
35 unsigned int event_coal_min = 24;
36 module_param(event_coal_min, int, 0664);
37 MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
39 unsigned int event_coal_div = 1;
40 module_param(event_coal_div, int, 0664);
41 MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
43 unsigned int en_tcp_dack = 1;
44 module_param(en_tcp_dack, int, 0664);
45 MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
47 unsigned int error_mask1 = 0x00;
48 module_param(error_mask1, int, 0664);
49 MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
51 unsigned int error_mask2 = 0x00;
52 module_param(error_mask2, int, 0664);
53 MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
56 module_param(sq_size, int, 0664);
57 MODULE_PARM_DESC(sq_size, "Configure SQ size");
59 unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
60 module_param(rq_size, int, 0664);
61 MODULE_PARM_DESC(rq_size, "Configure RQ size");
63 u64 iscsi_error_mask = 0x00;
65 static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
69 * bnx2i_identify_device - identifies NetXtreme II device type
70 * @hba: Adapter structure pointer
72 * This function identifies the NX2 device type and sets appropriate
73 * queue mailbox register access method, 5709 requires driver to
74 * access MBOX regs using *bin* mode
76 void bnx2i_identify_device(struct bnx2i_hba *hba)
78 hba->cnic_dev_type = 0;
79 if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
80 (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
81 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
82 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
83 (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
84 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
85 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
86 (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
87 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
88 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
89 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
90 hba->pci_did == PCI_DEVICE_ID_NX2_57711 ||
91 hba->pci_did == PCI_DEVICE_ID_NX2_57711E)
92 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
94 printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
100 * get_adapter_list_head - returns head of adapter list
102 struct bnx2i_hba *get_adapter_list_head(void)
104 struct bnx2i_hba *hba = NULL;
105 struct bnx2i_hba *tmp_hba;
110 mutex_lock(&bnx2i_dev_lock);
111 list_for_each_entry(tmp_hba, &adapter_list, link) {
112 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
117 mutex_unlock(&bnx2i_dev_lock);
124 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
125 * @cnic: pointer to cnic device instance
128 struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
130 struct bnx2i_hba *hba, *temp;
132 mutex_lock(&bnx2i_dev_lock);
133 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
134 if (hba->cnic == cnic) {
135 mutex_unlock(&bnx2i_dev_lock);
139 mutex_unlock(&bnx2i_dev_lock);
145 * bnx2i_start - cnic callback to initialize & start adapter instance
146 * @handle: transparent handle pointing to adapter structure
148 * This function maps adapter structure to pcidev structure and initiates
149 * firmware handshake to enable/initialize on chip iscsi components
150 * This bnx2i - cnic interface api callback is issued after following
151 * 2 conditions are met -
152 * a) underlying network interface is up (marked by event 'NETDEV_UP'
154 * b) bnx2i adapter instance is registered
156 void bnx2i_start(void *handle)
158 #define BNX2I_INIT_POLL_TIME (1000 / HZ)
159 struct bnx2i_hba *hba = handle;
162 bnx2i_send_fw_iscsi_init_msg(hba);
163 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
164 msleep(BNX2I_INIT_POLL_TIME);
169 * bnx2i_stop - cnic callback to shutdown adapter instance
170 * @handle: transparent handle pointing to adapter structure
172 * driver checks if adapter is already in shutdown mode, if not start
173 * the shutdown process
175 void bnx2i_stop(void *handle)
177 struct bnx2i_hba *hba = handle;
179 /* check if cleanup happened in GOING_DOWN context */
180 if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
181 &hba->adapter_state))
182 iscsi_host_for_each_session(hba->shost,
185 /* Wait for all endpoints to be torn down, Chip will be reset once
186 * control returns to network driver. So it is required to cleanup and
187 * release all connection resources before returning from this routine.
189 wait_event_interruptible_timeout(hba->eh_wait,
190 (hba->ofld_conns_active == 0),
191 hba->hba_shutdown_tmo);
192 /* This flag should be cleared last so that ep_disconnect() gracefully
193 * cleans up connection context
195 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
199 * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
200 * @hba: Adapter instance to register
202 * registers bnx2i adapter instance with the cnic driver while holding the
203 * adapter structure lock
205 void bnx2i_register_device(struct bnx2i_hba *hba)
209 if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
210 test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
214 rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
217 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
222 * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
224 * registers all bnx2i adapter instances with the cnic driver while holding
225 * the global resource lock
227 void bnx2i_reg_dev_all(void)
229 struct bnx2i_hba *hba, *temp;
231 mutex_lock(&bnx2i_dev_lock);
232 list_for_each_entry_safe(hba, temp, &adapter_list, link)
233 bnx2i_register_device(hba);
234 mutex_unlock(&bnx2i_dev_lock);
239 * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
240 * @hba: Adapter instance to unregister
242 * registers bnx2i adapter instance with the cnic driver while holding
243 * the adapter structure lock
245 static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
247 if (hba->ofld_conns_active ||
248 !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
249 test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
252 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
254 /* ep_disconnect could come before NETDEV_DOWN, driver won't
255 * see NETDEV_DOWN as it already unregistered itself.
257 hba->adapter_state = 0;
258 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
262 * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
264 * unregisters all bnx2i adapter instances with the cnic driver while holding
265 * the global resource lock
267 void bnx2i_unreg_dev_all(void)
269 struct bnx2i_hba *hba, *temp;
271 mutex_lock(&bnx2i_dev_lock);
272 list_for_each_entry_safe(hba, temp, &adapter_list, link)
273 bnx2i_unreg_one_device(hba);
274 mutex_unlock(&bnx2i_dev_lock);
279 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
280 * @hba: bnx2i adapter instance
281 * @cnic: cnic device handle
283 * Global resource lock is held during critical sections below. This routine is
284 * called from either cnic_register_driver() or device hot plug context and
285 * and does majority of device specific initialization
287 static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
291 mutex_lock(&bnx2i_dev_lock);
292 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
295 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
296 list_add_tail(&hba->link, &adapter_list);
298 } else if (rc == -EBUSY) /* duplicate registration */
299 printk(KERN_ALERT "bnx2i, duplicate registration"
300 "hba=%p, cnic=%p\n", hba, cnic);
301 else if (rc == -EAGAIN)
302 printk(KERN_ERR "bnx2i, driver not registered\n");
303 else if (rc == -EINVAL)
304 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
306 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
308 mutex_unlock(&bnx2i_dev_lock);
315 * bnx2i_ulp_init - initialize an adapter instance
316 * @dev: cnic device handle
318 * Called from cnic_register_driver() context to initialize all enumerated
319 * cnic devices. This routine allocate adapter structure and other
320 * device specific resources.
322 void bnx2i_ulp_init(struct cnic_dev *dev)
324 struct bnx2i_hba *hba;
326 /* Allocate a HBA structure for this device */
327 hba = bnx2i_alloc_hba(dev);
329 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
333 /* Get PCI related information and update hba struct members */
334 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
335 if (bnx2i_init_one(hba, dev)) {
336 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
344 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
345 * @dev: cnic device handle
348 void bnx2i_ulp_exit(struct cnic_dev *dev)
350 struct bnx2i_hba *hba;
352 hba = bnx2i_find_hba_for_cnic(dev);
354 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
355 "found, dev 0x%p\n", dev);
358 mutex_lock(&bnx2i_dev_lock);
359 list_del_init(&hba->link);
362 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
363 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
364 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
366 mutex_unlock(&bnx2i_dev_lock);
373 * bnx2i_mod_init - module init entry point
375 * initialize any driver wide global data structures such as endpoint pool,
376 * tcp port manager/queue, sysfs. finally driver will register itself
377 * with the cnic module
379 static int __init bnx2i_mod_init(void)
383 printk(KERN_INFO "%s", version);
385 if (sq_size && !is_power_of_2(sq_size))
386 sq_size = roundup_pow_of_two(sq_size);
388 mutex_init(&bnx2i_dev_lock);
390 bnx2i_scsi_xport_template =
391 iscsi_register_transport(&bnx2i_iscsi_transport);
392 if (!bnx2i_scsi_xport_template) {
393 printk(KERN_ERR "Could not register bnx2i transport.\n");
398 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
400 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
407 iscsi_unregister_transport(&bnx2i_iscsi_transport);
414 * bnx2i_mod_exit - module cleanup/exit entry point
416 * Global resource lock and host adapter lock is held during critical sections
417 * in this function. Driver will browse through the adapter list, cleans-up
418 * each instance, unregisters iscsi transport name and finally driver will
419 * unregister itself with the cnic module
421 static void __exit bnx2i_mod_exit(void)
423 struct bnx2i_hba *hba;
425 mutex_lock(&bnx2i_dev_lock);
426 while (!list_empty(&adapter_list)) {
427 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
428 list_del(&hba->link);
431 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
432 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
433 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
438 mutex_unlock(&bnx2i_dev_lock);
440 iscsi_unregister_transport(&bnx2i_iscsi_transport);
441 cnic_unregister_driver(CNIC_ULP_ISCSI);
444 module_init(bnx2i_mod_init);
445 module_exit(bnx2i_mod_exit);