Merge branch 'devicetree/next' of git://git.secretlab.ca/git/linux-2.6
[pandora-kernel.git] / drivers / scsi / bnx2i / bnx2i_init.c
1 /* bnx2i.c: Broadcom NetXtreme II iSCSI driver.
2  *
3  * Copyright (c) 2006 - 2010 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  * Maintained by: Eddie Wai (eddie.wai@broadcom.com)
13  */
14
15 #include "bnx2i.h"
16
17 static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
18 static u32 adapter_count;
19
20 #define DRV_MODULE_NAME         "bnx2i"
21 #define DRV_MODULE_VERSION      "2.6.2.3"
22 #define DRV_MODULE_RELDATE      "Dec 31, 2010"
23
24 static char version[] __devinitdata =
25                 "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
26                 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
27
28
29 MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com> and "
30               "Eddie Wai <eddie.wai@broadcom.com>");
31
32 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709/57710/57711/57712"
33                    "/57800/57810/57840 iSCSI Driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(DRV_MODULE_VERSION);
36
37 static DEFINE_MUTEX(bnx2i_dev_lock);
38
39 unsigned int event_coal_min = 24;
40 module_param(event_coal_min, int, 0664);
41 MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
42
43 unsigned int event_coal_div = 1;
44 module_param(event_coal_div, int, 0664);
45 MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
46
47 unsigned int en_tcp_dack = 1;
48 module_param(en_tcp_dack, int, 0664);
49 MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
50
51 unsigned int error_mask1 = 0x00;
52 module_param(error_mask1, int, 0664);
53 MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
54
55 unsigned int error_mask2 = 0x00;
56 module_param(error_mask2, int, 0664);
57 MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
58
59 unsigned int sq_size;
60 module_param(sq_size, int, 0664);
61 MODULE_PARM_DESC(sq_size, "Configure SQ size");
62
63 unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
64 module_param(rq_size, int, 0664);
65 MODULE_PARM_DESC(rq_size, "Configure RQ size");
66
67 u64 iscsi_error_mask = 0x00;
68
69
70 /**
71  * bnx2i_identify_device - identifies NetXtreme II device type
72  * @hba:                Adapter structure pointer
73  *
74  * This function identifies the NX2 device type and sets appropriate
75  *      queue mailbox register access method, 5709 requires driver to
76  *      access MBOX regs using *bin* mode
77  */
78 void bnx2i_identify_device(struct bnx2i_hba *hba)
79 {
80         hba->cnic_dev_type = 0;
81         if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
82             (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
83                 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
84         else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
85             (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
86                 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
87         else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
88             (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
89                 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
90                 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
91         } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710    ||
92                    hba->pci_did == PCI_DEVICE_ID_NX2_57711    ||
93                    hba->pci_did == PCI_DEVICE_ID_NX2_57711E   ||
94                    hba->pci_did == PCI_DEVICE_ID_NX2_57712    ||
95                    hba->pci_did == PCI_DEVICE_ID_NX2_57712E   ||
96                    hba->pci_did == PCI_DEVICE_ID_NX2_57800    ||
97                    hba->pci_did == PCI_DEVICE_ID_NX2_57800_MF ||
98                    hba->pci_did == PCI_DEVICE_ID_NX2_57800_VF ||
99                    hba->pci_did == PCI_DEVICE_ID_NX2_57810    ||
100                    hba->pci_did == PCI_DEVICE_ID_NX2_57810_MF ||
101                    hba->pci_did == PCI_DEVICE_ID_NX2_57810_VF ||
102                    hba->pci_did == PCI_DEVICE_ID_NX2_57840    ||
103                    hba->pci_did == PCI_DEVICE_ID_NX2_57840_MF ||
104                    hba->pci_did == PCI_DEVICE_ID_NX2_57840_VF)
105                 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
106         else
107                 printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
108                                   hba->pci_did);
109 }
110
111
112 /**
113  * get_adapter_list_head - returns head of adapter list
114  */
115 struct bnx2i_hba *get_adapter_list_head(void)
116 {
117         struct bnx2i_hba *hba = NULL;
118         struct bnx2i_hba *tmp_hba;
119
120         if (!adapter_count)
121                 goto hba_not_found;
122
123         mutex_lock(&bnx2i_dev_lock);
124         list_for_each_entry(tmp_hba, &adapter_list, link) {
125                 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
126                         hba = tmp_hba;
127                         break;
128                 }
129         }
130         mutex_unlock(&bnx2i_dev_lock);
131 hba_not_found:
132         return hba;
133 }
134
135
136 /**
137  * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
138  * @cnic:       pointer to cnic device instance
139  *
140  */
141 struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
142 {
143         struct bnx2i_hba *hba, *temp;
144
145         mutex_lock(&bnx2i_dev_lock);
146         list_for_each_entry_safe(hba, temp, &adapter_list, link) {
147                 if (hba->cnic == cnic) {
148                         mutex_unlock(&bnx2i_dev_lock);
149                         return hba;
150                 }
151         }
152         mutex_unlock(&bnx2i_dev_lock);
153         return NULL;
154 }
155
156
157 /**
158  * bnx2i_start - cnic callback to initialize & start adapter instance
159  * @handle:     transparent handle pointing to adapter structure
160  *
161  * This function maps adapter structure to pcidev structure and initiates
162  *      firmware handshake to enable/initialize on chip iscsi components
163  *      This bnx2i - cnic interface api callback is issued after following
164  *      2 conditions are met -
165  *        a) underlying network interface is up (marked by event 'NETDEV_UP'
166  *              from netdev
167  *        b) bnx2i adapter instance is registered
168  */
169 void bnx2i_start(void *handle)
170 {
171 #define BNX2I_INIT_POLL_TIME    (1000 / HZ)
172         struct bnx2i_hba *hba = handle;
173         int i = HZ;
174
175         if (!hba->cnic->max_iscsi_conn) {
176                 printk(KERN_ALERT "bnx2i: dev %s does not support "
177                         "iSCSI\n", hba->netdev->name);
178
179                 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
180                         mutex_lock(&bnx2i_dev_lock);
181                         list_del_init(&hba->link);
182                         adapter_count--;
183                         hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
184                         clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
185                         mutex_unlock(&bnx2i_dev_lock);
186                         bnx2i_free_hba(hba);
187                 }
188                 return;
189         }
190         bnx2i_send_fw_iscsi_init_msg(hba);
191         while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
192                 msleep(BNX2I_INIT_POLL_TIME);
193 }
194
195
196 /**
197  * bnx2i_chip_cleanup - local routine to handle chip cleanup
198  * @hba:        Adapter instance to register
199  *
200  * Driver checks if adapter still has any active connections before
201  *      executing the cleanup process
202  */
203 static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
204 {
205         struct bnx2i_endpoint *bnx2i_ep;
206         struct list_head *pos, *tmp;
207
208         if (hba->ofld_conns_active) {
209                 /* Stage to force the disconnection
210                  * This is the case where the daemon is either slow or
211                  * not present
212                  */
213                 printk(KERN_ALERT "bnx2i: (%s) chip cleanup for %d active "
214                         "connections\n", hba->netdev->name,
215                         hba->ofld_conns_active);
216                 mutex_lock(&hba->net_dev_lock);
217                 list_for_each_safe(pos, tmp, &hba->ep_active_list) {
218                         bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
219                         /* Clean up the chip only */
220                         bnx2i_hw_ep_disconnect(bnx2i_ep);
221                         bnx2i_ep->cm_sk = NULL;
222                 }
223                 mutex_unlock(&hba->net_dev_lock);
224         }
225 }
226
227
228 /**
229  * bnx2i_stop - cnic callback to shutdown adapter instance
230  * @handle:     transparent handle pointing to adapter structure
231  *
232  * driver checks if adapter is already in shutdown mode, if not start
233  *      the shutdown process
234  */
235 void bnx2i_stop(void *handle)
236 {
237         struct bnx2i_hba *hba = handle;
238         int conns_active;
239         int wait_delay = 1 * HZ;
240
241         /* check if cleanup happened in GOING_DOWN context */
242         if (!test_and_set_bit(ADAPTER_STATE_GOING_DOWN,
243                               &hba->adapter_state)) {
244                 iscsi_host_for_each_session(hba->shost,
245                                             bnx2i_drop_session);
246                 wait_delay = hba->hba_shutdown_tmo;
247         }
248         /* Wait for inflight offload connection tasks to complete before
249          * proceeding. Forcefully terminate all connection recovery in
250          * progress at the earliest, either in bind(), send_pdu(LOGIN),
251          * or conn_start()
252          */
253         wait_event_interruptible_timeout(hba->eh_wait,
254                                          (list_empty(&hba->ep_ofld_list) &&
255                                          list_empty(&hba->ep_destroy_list)),
256                                          2 * HZ);
257         /* Wait for all endpoints to be torn down, Chip will be reset once
258          *  control returns to network driver. So it is required to cleanup and
259          * release all connection resources before returning from this routine.
260          */
261         while (hba->ofld_conns_active) {
262                 conns_active = hba->ofld_conns_active;
263                 wait_event_interruptible_timeout(hba->eh_wait,
264                                 (hba->ofld_conns_active != conns_active),
265                                 wait_delay);
266                 if (hba->ofld_conns_active == conns_active)
267                         break;
268         }
269         bnx2i_chip_cleanup(hba);
270
271         /* This flag should be cleared last so that ep_disconnect() gracefully
272          * cleans up connection context
273          */
274         clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
275         clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
276 }
277
278
279 /**
280  * bnx2i_init_one - initialize an adapter instance and allocate memory resources
281  * @hba:        bnx2i adapter instance
282  * @cnic:       cnic device handle
283  *
284  * Global resource lock is held during critical sections below. This routine is
285  *      called from either cnic_register_driver() or device hot plug context and
286  *      and does majority of device specific initialization
287  */
288 static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
289 {
290         int rc;
291
292         mutex_lock(&bnx2i_dev_lock);
293         hba->cnic = cnic;
294         rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
295         if (!rc) {
296                 hba->age++;
297                 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
298                 list_add_tail(&hba->link, &adapter_list);
299                 adapter_count++;
300         } else if (rc == -EBUSY)        /* duplicate registration */
301                 printk(KERN_ALERT "bnx2i, duplicate registration"
302                                   "hba=%p, cnic=%p\n", hba, cnic);
303         else if (rc == -EAGAIN)
304                 printk(KERN_ERR "bnx2i, driver not registered\n");
305         else if (rc == -EINVAL)
306                 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
307         else
308                 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
309
310         mutex_unlock(&bnx2i_dev_lock);
311
312         return rc;
313 }
314
315
316 /**
317  * bnx2i_ulp_init - initialize an adapter instance
318  * @dev:        cnic device handle
319  *
320  * Called from cnic_register_driver() context to initialize all enumerated
321  *      cnic devices. This routine allocate adapter structure and other
322  *      device specific resources.
323  */
324 void bnx2i_ulp_init(struct cnic_dev *dev)
325 {
326         struct bnx2i_hba *hba;
327
328         /* Allocate a HBA structure for this device */
329         hba = bnx2i_alloc_hba(dev);
330         if (!hba) {
331                 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
332                 return;
333         }
334
335         /* Get PCI related information and update hba struct members */
336         clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
337         if (bnx2i_init_one(hba, dev)) {
338                 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
339                 bnx2i_free_hba(hba);
340         }
341 }
342
343
344 /**
345  * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
346  * @dev:        cnic device handle
347  *
348  */
349 void bnx2i_ulp_exit(struct cnic_dev *dev)
350 {
351         struct bnx2i_hba *hba;
352
353         hba = bnx2i_find_hba_for_cnic(dev);
354         if (!hba) {
355                 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
356                                  "found, dev 0x%p\n", dev);
357                 return;
358         }
359         mutex_lock(&bnx2i_dev_lock);
360         list_del_init(&hba->link);
361         adapter_count--;
362
363         if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
364                 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
365                 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
366         }
367         mutex_unlock(&bnx2i_dev_lock);
368
369         bnx2i_free_hba(hba);
370 }
371
372
373 /**
374  * bnx2i_mod_init - module init entry point
375  *
376  * initialize any driver wide global data structures such as endpoint pool,
377  *      tcp port manager/queue, sysfs. finally driver will register itself
378  *      with the cnic module
379  */
380 static int __init bnx2i_mod_init(void)
381 {
382         int err;
383
384         printk(KERN_INFO "%s", version);
385
386         if (sq_size && !is_power_of_2(sq_size))
387                 sq_size = roundup_pow_of_two(sq_size);
388
389         mutex_init(&bnx2i_dev_lock);
390
391         bnx2i_scsi_xport_template =
392                         iscsi_register_transport(&bnx2i_iscsi_transport);
393         if (!bnx2i_scsi_xport_template) {
394                 printk(KERN_ERR "Could not register bnx2i transport.\n");
395                 err = -ENOMEM;
396                 goto out;
397         }
398
399         err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
400         if (err) {
401                 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
402                 goto unreg_xport;
403         }
404
405         return 0;
406
407 unreg_xport:
408         iscsi_unregister_transport(&bnx2i_iscsi_transport);
409 out:
410         return err;
411 }
412
413
414 /**
415  * bnx2i_mod_exit - module cleanup/exit entry point
416  *
417  * Global resource lock and host adapter lock is held during critical sections
418  *      in this function. Driver will browse through the adapter list, cleans-up
419  *      each instance, unregisters iscsi transport name and finally driver will
420  *      unregister itself with the cnic module
421  */
422 static void __exit bnx2i_mod_exit(void)
423 {
424         struct bnx2i_hba *hba;
425
426         mutex_lock(&bnx2i_dev_lock);
427         while (!list_empty(&adapter_list)) {
428                 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
429                 list_del(&hba->link);
430                 adapter_count--;
431
432                 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
433                         bnx2i_chip_cleanup(hba);
434                         hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
435                         clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
436                 }
437
438                 bnx2i_free_hba(hba);
439         }
440         mutex_unlock(&bnx2i_dev_lock);
441
442         iscsi_unregister_transport(&bnx2i_iscsi_transport);
443         cnic_unregister_driver(CNIC_ULP_ISCSI);
444 }
445
446 module_init(bnx2i_mod_init);
447 module_exit(bnx2i_mod_exit);