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