[SCSI] bnx2i: convert bnx2i_dev_lock to mutex
[linux-2.6-block.git] / drivers / scsi / bnx2i / bnx2i_init.c
CommitLineData
cf4e6363
MC
1/* 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;
cf4e6363
MC
18
19#define DRV_MODULE_NAME "bnx2i"
3be924fb
AV
20#define DRV_MODULE_VERSION "2.0.1e"
21#define DRV_MODULE_RELDATE "June 22, 2009"
cf4e6363
MC
22
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>");
29MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709 iSCSI Driver");
30MODULE_LICENSE("GPL");
31MODULE_VERSION(DRV_MODULE_VERSION);
32
3be924fb 33static DEFINE_MUTEX(bnx2i_dev_lock);
cf4e6363
MC
34
35unsigned int event_coal_div = 1;
36module_param(event_coal_div, int, 0664);
37MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
38
39unsigned int en_tcp_dack = 1;
40module_param(en_tcp_dack, int, 0664);
41MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
42
43unsigned int error_mask1 = 0x00;
44module_param(error_mask1, int, 0664);
45MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
46
47unsigned int error_mask2 = 0x00;
48module_param(error_mask2, int, 0664);
49MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
50
51unsigned int sq_size;
52module_param(sq_size, int, 0664);
53MODULE_PARM_DESC(sq_size, "Configure SQ size");
54
55unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
56module_param(rq_size, int, 0664);
57MODULE_PARM_DESC(rq_size, "Configure RQ size");
58
59u64 iscsi_error_mask = 0x00;
60
61static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
62
63
64/**
65 * bnx2i_identify_device - identifies NetXtreme II device type
66 * @hba: Adapter structure pointer
67 *
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
71 */
72void bnx2i_identify_device(struct bnx2i_hba *hba)
73{
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);
88}
89
90
91/**
92 * get_adapter_list_head - returns head of adapter list
93 */
94struct bnx2i_hba *get_adapter_list_head(void)
95{
96 struct bnx2i_hba *hba = NULL;
97 struct bnx2i_hba *tmp_hba;
98
99 if (!adapter_count)
100 goto hba_not_found;
101
3be924fb 102 mutex_lock(&bnx2i_dev_lock);
cf4e6363
MC
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;
107 }
108 }
3be924fb 109 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
110hba_not_found:
111 return hba;
112}
113
114
115/**
116 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
117 * @cnic: pointer to cnic device instance
118 *
119 */
120struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
121{
122 struct bnx2i_hba *hba, *temp;
123
3be924fb 124 mutex_lock(&bnx2i_dev_lock);
cf4e6363
MC
125 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
126 if (hba->cnic == cnic) {
3be924fb 127 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
128 return hba;
129 }
130 }
3be924fb 131 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
132 return NULL;
133}
134
135
136/**
137 * bnx2i_start - cnic callback to initialize & start adapter instance
138 * @handle: transparent handle pointing to adapter structure
139 *
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
147 */
148void bnx2i_start(void *handle)
149{
150#define BNX2I_INIT_POLL_TIME (1000 / HZ)
151 struct bnx2i_hba *hba = handle;
152 int i = HZ;
153
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);
157}
158
159
160/**
161 * bnx2i_stop - cnic callback to shutdown adapter instance
162 * @handle: transparent handle pointing to adapter structure
163 *
164 * driver checks if adapter is already in shutdown mode, if not start
165 * the shutdown process
166 */
167void bnx2i_stop(void *handle)
168{
169 struct bnx2i_hba *hba = handle;
170
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);
177}
178
179/**
180 * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
181 * @hba: Adapter instance to register
182 *
183 * registers bnx2i adapter instance with the cnic driver while holding the
184 * adapter structure lock
185 */
186void bnx2i_register_device(struct bnx2i_hba *hba)
187{
188 if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
189 test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
190 return;
191 }
192
193 hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
194
cf4e6363
MC
195 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
196}
197
198
199/**
200 * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
201 *
202 * registers all bnx2i adapter instances with the cnic driver while holding
203 * the global resource lock
204 */
205void bnx2i_reg_dev_all(void)
206{
207 struct bnx2i_hba *hba, *temp;
208
3be924fb 209 mutex_lock(&bnx2i_dev_lock);
cf4e6363
MC
210 list_for_each_entry_safe(hba, temp, &adapter_list, link)
211 bnx2i_register_device(hba);
3be924fb 212 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
213}
214
215
216/**
217 * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
218 * @hba: Adapter instance to unregister
219 *
220 * registers bnx2i adapter instance with the cnic driver while holding
221 * the adapter structure lock
222 */
223static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
224{
225 if (hba->ofld_conns_active ||
226 !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
227 test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
228 return;
229
230 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
231
cf4e6363
MC
232 /* ep_disconnect could come before NETDEV_DOWN, driver won't
233 * see NETDEV_DOWN as it already unregistered itself.
234 */
235 hba->adapter_state = 0;
236 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
237}
238
239/**
240 * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
241 *
242 * unregisters all bnx2i adapter instances with the cnic driver while holding
243 * the global resource lock
244 */
245void bnx2i_unreg_dev_all(void)
246{
247 struct bnx2i_hba *hba, *temp;
248
3be924fb 249 mutex_lock(&bnx2i_dev_lock);
cf4e6363
MC
250 list_for_each_entry_safe(hba, temp, &adapter_list, link)
251 bnx2i_unreg_one_device(hba);
3be924fb 252 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
253}
254
255
256/**
257 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
258 * @hba: bnx2i adapter instance
259 * @cnic: cnic device handle
260 *
3be924fb
AV
261 * Global resource lock is held during critical sections below. This routine is
262 * called from either cnic_register_driver() or device hot plug context and
263 * and does majority of device specific initialization
cf4e6363
MC
264 */
265static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
266{
267 int rc;
268
3be924fb 269 mutex_lock(&bnx2i_dev_lock);
4e85f151
AV
270 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
271 if (!rc) {
cf4e6363 272 hba->age++;
cf4e6363 273 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
3be924fb
AV
274 list_add_tail(&hba->link, &adapter_list);
275 adapter_count++;
4e85f151
AV
276 } else if (rc == -EBUSY) /* duplicate registration */
277 printk(KERN_ALERT "bnx2i, duplicate registration"
278 "hba=%p, cnic=%p\n", hba, cnic);
279 else if (rc == -EAGAIN)
280 printk(KERN_ERR "bnx2i, driver not registered\n");
281 else if (rc == -EINVAL)
282 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
283 else
284 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
cf4e6363 285
3be924fb 286 mutex_unlock(&bnx2i_dev_lock);
4e85f151
AV
287
288 return rc;
cf4e6363
MC
289}
290
291
292/**
293 * bnx2i_ulp_init - initialize an adapter instance
294 * @dev: cnic device handle
295 *
296 * Called from cnic_register_driver() context to initialize all enumerated
297 * cnic devices. This routine allocate adapter structure and other
298 * device specific resources.
299 */
300void bnx2i_ulp_init(struct cnic_dev *dev)
301{
302 struct bnx2i_hba *hba;
303
304 /* Allocate a HBA structure for this device */
305 hba = bnx2i_alloc_hba(dev);
306 if (!hba) {
307 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
308 return;
309 }
310
311 /* Get PCI related information and update hba struct members */
312 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
313 if (bnx2i_init_one(hba, dev)) {
314 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
315 bnx2i_free_hba(hba);
316 } else
317 hba->cnic = dev;
318}
319
320
321/**
322 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
323 * @dev: cnic device handle
324 *
325 */
326void bnx2i_ulp_exit(struct cnic_dev *dev)
327{
328 struct bnx2i_hba *hba;
329
330 hba = bnx2i_find_hba_for_cnic(dev);
331 if (!hba) {
332 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
333 "found, dev 0x%p\n", dev);
334 return;
335 }
3be924fb 336 mutex_lock(&bnx2i_dev_lock);
cf4e6363
MC
337 list_del_init(&hba->link);
338 adapter_count--;
339
340 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
341 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
342 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
cf4e6363 343 }
3be924fb 344 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
345
346 bnx2i_free_hba(hba);
347}
348
349
350/**
351 * bnx2i_mod_init - module init entry point
352 *
353 * initialize any driver wide global data structures such as endpoint pool,
354 * tcp port manager/queue, sysfs. finally driver will register itself
355 * with the cnic module
356 */
357static int __init bnx2i_mod_init(void)
358{
359 int err;
360
361 printk(KERN_INFO "%s", version);
362
363 if (!is_power_of_2(sq_size))
364 sq_size = roundup_pow_of_two(sq_size);
365
3be924fb
AV
366 mutex_init(&bnx2i_dev_lock);
367
cf4e6363
MC
368 bnx2i_scsi_xport_template =
369 iscsi_register_transport(&bnx2i_iscsi_transport);
370 if (!bnx2i_scsi_xport_template) {
371 printk(KERN_ERR "Could not register bnx2i transport.\n");
372 err = -ENOMEM;
373 goto out;
374 }
375
376 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
377 if (err) {
378 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
379 goto unreg_xport;
380 }
381
382 return 0;
383
384unreg_xport:
385 iscsi_unregister_transport(&bnx2i_iscsi_transport);
386out:
387 return err;
388}
389
390
391/**
392 * bnx2i_mod_exit - module cleanup/exit entry point
393 *
394 * Global resource lock and host adapter lock is held during critical sections
395 * in this function. Driver will browse through the adapter list, cleans-up
396 * each instance, unregisters iscsi transport name and finally driver will
397 * unregister itself with the cnic module
398 */
399static void __exit bnx2i_mod_exit(void)
400{
401 struct bnx2i_hba *hba;
402
3be924fb 403 mutex_lock(&bnx2i_dev_lock);
cf4e6363
MC
404 while (!list_empty(&adapter_list)) {
405 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
406 list_del(&hba->link);
407 adapter_count--;
408
409 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
410 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
411 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
cf4e6363
MC
412 }
413
cf4e6363 414 bnx2i_free_hba(hba);
cf4e6363 415 }
3be924fb 416 mutex_unlock(&bnx2i_dev_lock);
cf4e6363
MC
417
418 iscsi_unregister_transport(&bnx2i_iscsi_transport);
419 cnic_unregister_driver(CNIC_ULP_ISCSI);
420}
421
422module_init(bnx2i_mod_init);
423module_exit(bnx2i_mod_exit);