Merge branch 'mini2440-dev-unlikely' into mini2440-dev
[linux-2.6/mini2440.git] / drivers / scsi / bnx2i / bnx2i_init.c
blob0c4210d48ee8a7d8932e0b1431c9ea4fb4c5b973
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)
14 #include "bnx2i.h"
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.0.1e"
21 #define DRV_MODULE_RELDATE "June 22, 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_div = 1;
36 module_param(event_coal_div, int, 0664);
37 MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
39 unsigned int en_tcp_dack = 1;
40 module_param(en_tcp_dack, int, 0664);
41 MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
43 unsigned int error_mask1 = 0x00;
44 module_param(error_mask1, int, 0664);
45 MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
47 unsigned int error_mask2 = 0x00;
48 module_param(error_mask2, int, 0664);
49 MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
51 unsigned int sq_size;
52 module_param(sq_size, int, 0664);
53 MODULE_PARM_DESC(sq_size, "Configure SQ size");
55 unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
56 module_param(rq_size, int, 0664);
57 MODULE_PARM_DESC(rq_size, "Configure RQ size");
59 u64 iscsi_error_mask = 0x00;
61 static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
64 /**
65 * bnx2i_identify_device - identifies NetXtreme II device type
66 * @hba: Adapter structure pointer
68 * This function identifies the NX2 device type and sets appropriate
69 * queue mailbox register access method, 5709 requires driver to
70 * access MBOX regs using *bin* mode
72 void bnx2i_identify_device(struct bnx2i_hba *hba)
74 hba->cnic_dev_type = 0;
75 if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
76 (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
77 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
78 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
79 (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
80 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
81 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
82 (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
83 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
84 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
85 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
86 hba->pci_did == PCI_DEVICE_ID_NX2_57711)
87 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
91 /**
92 * get_adapter_list_head - returns head of adapter list
94 struct bnx2i_hba *get_adapter_list_head(void)
96 struct bnx2i_hba *hba = NULL;
97 struct bnx2i_hba *tmp_hba;
99 if (!adapter_count)
100 goto hba_not_found;
102 mutex_lock(&bnx2i_dev_lock);
103 list_for_each_entry(tmp_hba, &adapter_list, link) {
104 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
105 hba = tmp_hba;
106 break;
109 mutex_unlock(&bnx2i_dev_lock);
110 hba_not_found:
111 return hba;
116 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
117 * @cnic: pointer to cnic device instance
120 struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
122 struct bnx2i_hba *hba, *temp;
124 mutex_lock(&bnx2i_dev_lock);
125 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
126 if (hba->cnic == cnic) {
127 mutex_unlock(&bnx2i_dev_lock);
128 return hba;
131 mutex_unlock(&bnx2i_dev_lock);
132 return NULL;
137 * bnx2i_start - cnic callback to initialize & start adapter instance
138 * @handle: transparent handle pointing to adapter structure
140 * This function maps adapter structure to pcidev structure and initiates
141 * firmware handshake to enable/initialize on chip iscsi components
142 * This bnx2i - cnic interface api callback is issued after following
143 * 2 conditions are met -
144 * a) underlying network interface is up (marked by event 'NETDEV_UP'
145 * from netdev
146 * b) bnx2i adapter instance is registered
148 void bnx2i_start(void *handle)
150 #define BNX2I_INIT_POLL_TIME (1000 / HZ)
151 struct bnx2i_hba *hba = handle;
152 int i = HZ;
154 bnx2i_send_fw_iscsi_init_msg(hba);
155 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
156 msleep(BNX2I_INIT_POLL_TIME);
161 * bnx2i_stop - cnic callback to shutdown adapter instance
162 * @handle: transparent handle pointing to adapter structure
164 * driver checks if adapter is already in shutdown mode, if not start
165 * the shutdown process
167 void bnx2i_stop(void *handle)
169 struct bnx2i_hba *hba = handle;
171 /* check if cleanup happened in GOING_DOWN context */
172 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
173 if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
174 &hba->adapter_state))
175 iscsi_host_for_each_session(hba->shost,
176 bnx2i_drop_session);
180 * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
181 * @hba: Adapter instance to register
183 * registers bnx2i adapter instance with the cnic driver while holding the
184 * adapter structure lock
186 void bnx2i_register_device(struct bnx2i_hba *hba)
188 int rc;
190 if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
191 test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
192 return;
195 rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
197 if (!rc)
198 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
203 * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
205 * registers all bnx2i adapter instances with the cnic driver while holding
206 * the global resource lock
208 void bnx2i_reg_dev_all(void)
210 struct bnx2i_hba *hba, *temp;
212 mutex_lock(&bnx2i_dev_lock);
213 list_for_each_entry_safe(hba, temp, &adapter_list, link)
214 bnx2i_register_device(hba);
215 mutex_unlock(&bnx2i_dev_lock);
220 * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
221 * @hba: Adapter instance to unregister
223 * registers bnx2i adapter instance with the cnic driver while holding
224 * the adapter structure lock
226 static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
228 if (hba->ofld_conns_active ||
229 !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
230 test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
231 return;
233 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
235 /* ep_disconnect could come before NETDEV_DOWN, driver won't
236 * see NETDEV_DOWN as it already unregistered itself.
238 hba->adapter_state = 0;
239 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
243 * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
245 * unregisters all bnx2i adapter instances with the cnic driver while holding
246 * the global resource lock
248 void bnx2i_unreg_dev_all(void)
250 struct bnx2i_hba *hba, *temp;
252 mutex_lock(&bnx2i_dev_lock);
253 list_for_each_entry_safe(hba, temp, &adapter_list, link)
254 bnx2i_unreg_one_device(hba);
255 mutex_unlock(&bnx2i_dev_lock);
260 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
261 * @hba: bnx2i adapter instance
262 * @cnic: cnic device handle
264 * Global resource lock is held during critical sections below. This routine is
265 * called from either cnic_register_driver() or device hot plug context and
266 * and does majority of device specific initialization
268 static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
270 int rc;
272 mutex_lock(&bnx2i_dev_lock);
273 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
274 if (!rc) {
275 hba->age++;
276 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
277 list_add_tail(&hba->link, &adapter_list);
278 adapter_count++;
279 } else if (rc == -EBUSY) /* duplicate registration */
280 printk(KERN_ALERT "bnx2i, duplicate registration"
281 "hba=%p, cnic=%p\n", hba, cnic);
282 else if (rc == -EAGAIN)
283 printk(KERN_ERR "bnx2i, driver not registered\n");
284 else if (rc == -EINVAL)
285 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
286 else
287 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
289 mutex_unlock(&bnx2i_dev_lock);
291 return rc;
296 * bnx2i_ulp_init - initialize an adapter instance
297 * @dev: cnic device handle
299 * Called from cnic_register_driver() context to initialize all enumerated
300 * cnic devices. This routine allocate adapter structure and other
301 * device specific resources.
303 void bnx2i_ulp_init(struct cnic_dev *dev)
305 struct bnx2i_hba *hba;
307 /* Allocate a HBA structure for this device */
308 hba = bnx2i_alloc_hba(dev);
309 if (!hba) {
310 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
311 return;
314 /* Get PCI related information and update hba struct members */
315 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
316 if (bnx2i_init_one(hba, dev)) {
317 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
318 bnx2i_free_hba(hba);
319 } else
320 hba->cnic = dev;
325 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
326 * @dev: cnic device handle
329 void bnx2i_ulp_exit(struct cnic_dev *dev)
331 struct bnx2i_hba *hba;
333 hba = bnx2i_find_hba_for_cnic(dev);
334 if (!hba) {
335 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
336 "found, dev 0x%p\n", dev);
337 return;
339 mutex_lock(&bnx2i_dev_lock);
340 list_del_init(&hba->link);
341 adapter_count--;
343 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
344 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
345 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
347 mutex_unlock(&bnx2i_dev_lock);
349 bnx2i_free_hba(hba);
354 * bnx2i_mod_init - module init entry point
356 * initialize any driver wide global data structures such as endpoint pool,
357 * tcp port manager/queue, sysfs. finally driver will register itself
358 * with the cnic module
360 static int __init bnx2i_mod_init(void)
362 int err;
364 printk(KERN_INFO "%s", version);
366 if (!is_power_of_2(sq_size))
367 sq_size = roundup_pow_of_two(sq_size);
369 mutex_init(&bnx2i_dev_lock);
371 bnx2i_scsi_xport_template =
372 iscsi_register_transport(&bnx2i_iscsi_transport);
373 if (!bnx2i_scsi_xport_template) {
374 printk(KERN_ERR "Could not register bnx2i transport.\n");
375 err = -ENOMEM;
376 goto out;
379 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
380 if (err) {
381 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
382 goto unreg_xport;
385 return 0;
387 unreg_xport:
388 iscsi_unregister_transport(&bnx2i_iscsi_transport);
389 out:
390 return err;
395 * bnx2i_mod_exit - module cleanup/exit entry point
397 * Global resource lock and host adapter lock is held during critical sections
398 * in this function. Driver will browse through the adapter list, cleans-up
399 * each instance, unregisters iscsi transport name and finally driver will
400 * unregister itself with the cnic module
402 static void __exit bnx2i_mod_exit(void)
404 struct bnx2i_hba *hba;
406 mutex_lock(&bnx2i_dev_lock);
407 while (!list_empty(&adapter_list)) {
408 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
409 list_del(&hba->link);
410 adapter_count--;
412 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
413 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
414 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
417 bnx2i_free_hba(hba);
419 mutex_unlock(&bnx2i_dev_lock);
421 iscsi_unregister_transport(&bnx2i_iscsi_transport);
422 cnic_unregister_driver(CNIC_ULP_ISCSI);
425 module_init(bnx2i_mod_init);
426 module_exit(bnx2i_mod_exit);