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