scsi/bnx2i: Convert to hotplug state machine
[linux/fpc-iii.git] / drivers / scsi / bnx2i / bnx2i_init.c
blob86afc002814cd07bbc3b55b64ca31201208a514b
1 /* bnx2i.c: QLogic NetXtreme II iSCSI driver.
3 * Copyright (c) 2006 - 2013 Broadcom Corporation
4 * Copyright (c) 2007, 2008 Red Hat, Inc. All rights reserved.
5 * Copyright (c) 2007, 2008 Mike Christie
6 * Copyright (c) 2014, QLogic Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
12 * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
13 * Previously Maintained by: Eddie Wai (eddie.wai@broadcom.com)
14 * Maintained by: QLogic-Storage-Upstream@qlogic.com
17 #include "bnx2i.h"
19 static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
20 static u32 adapter_count;
22 #define DRV_MODULE_NAME "bnx2i"
23 #define DRV_MODULE_VERSION "2.7.10.1"
24 #define DRV_MODULE_RELDATE "Jul 16, 2014"
26 static char version[] =
27 "QLogic NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
28 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com> and "
32 "Eddie Wai <eddie.wai@broadcom.com>");
34 MODULE_DESCRIPTION("QLogic NetXtreme II BCM5706/5708/5709/57710/57711/57712"
35 "/57800/57810/57840 iSCSI Driver");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
39 static DEFINE_MUTEX(bnx2i_dev_lock);
41 unsigned int event_coal_min = 24;
42 module_param(event_coal_min, int, 0664);
43 MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
45 unsigned int event_coal_div = 2;
46 module_param(event_coal_div, int, 0664);
47 MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
49 unsigned int en_tcp_dack = 1;
50 module_param(en_tcp_dack, int, 0664);
51 MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
53 unsigned int error_mask1 = 0x00;
54 module_param(error_mask1, uint, 0664);
55 MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
57 unsigned int error_mask2 = 0x00;
58 module_param(error_mask2, uint, 0664);
59 MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
61 unsigned int sq_size;
62 module_param(sq_size, int, 0664);
63 MODULE_PARM_DESC(sq_size, "Configure SQ size");
65 unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
66 module_param(rq_size, int, 0664);
67 MODULE_PARM_DESC(rq_size, "Configure RQ size");
69 u64 iscsi_error_mask = 0x00;
71 DEFINE_PER_CPU(struct bnx2i_percpu_s, bnx2i_percpu);
73 /**
74 * bnx2i_identify_device - identifies NetXtreme II device type
75 * @hba: Adapter structure pointer
76 * @cnic: Corresponding cnic device
78 * This function identifies the NX2 device type and sets appropriate
79 * queue mailbox register access method, 5709 requires driver to
80 * access MBOX regs using *bin* mode
82 void bnx2i_identify_device(struct bnx2i_hba *hba, struct cnic_dev *dev)
84 hba->cnic_dev_type = 0;
85 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
86 if (hba->pci_did == PCI_DEVICE_ID_NX2_5706 ||
87 hba->pci_did == PCI_DEVICE_ID_NX2_5706S) {
88 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
89 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_5708 ||
90 hba->pci_did == PCI_DEVICE_ID_NX2_5708S) {
91 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
92 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_5709 ||
93 hba->pci_did == PCI_DEVICE_ID_NX2_5709S) {
94 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
95 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
97 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
98 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
99 } else {
100 printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
101 hba->pci_did);
107 * get_adapter_list_head - returns head of adapter list
109 struct bnx2i_hba *get_adapter_list_head(void)
111 struct bnx2i_hba *hba = NULL;
112 struct bnx2i_hba *tmp_hba;
114 if (!adapter_count)
115 goto hba_not_found;
117 mutex_lock(&bnx2i_dev_lock);
118 list_for_each_entry(tmp_hba, &adapter_list, link) {
119 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
120 hba = tmp_hba;
121 break;
124 mutex_unlock(&bnx2i_dev_lock);
125 hba_not_found:
126 return hba;
131 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
132 * @cnic: pointer to cnic device instance
135 struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
137 struct bnx2i_hba *hba, *temp;
139 mutex_lock(&bnx2i_dev_lock);
140 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
141 if (hba->cnic == cnic) {
142 mutex_unlock(&bnx2i_dev_lock);
143 return hba;
146 mutex_unlock(&bnx2i_dev_lock);
147 return NULL;
152 * bnx2i_start - cnic callback to initialize & start adapter instance
153 * @handle: transparent handle pointing to adapter structure
155 * This function maps adapter structure to pcidev structure and initiates
156 * firmware handshake to enable/initialize on chip iscsi components
157 * This bnx2i - cnic interface api callback is issued after following
158 * 2 conditions are met -
159 * a) underlying network interface is up (marked by event 'NETDEV_UP'
160 * from netdev
161 * b) bnx2i adapter instance is registered
163 void bnx2i_start(void *handle)
165 #define BNX2I_INIT_POLL_TIME (1000 / HZ)
166 struct bnx2i_hba *hba = handle;
167 int i = HZ;
169 /* On some bnx2x devices, it is possible that iSCSI is no
170 * longer supported after firmware is downloaded. In that
171 * case, the iscsi_init_msg will return failure.
174 bnx2i_send_fw_iscsi_init_msg(hba);
175 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) &&
176 !test_bit(ADAPTER_STATE_INIT_FAILED, &hba->adapter_state) && i--)
177 msleep(BNX2I_INIT_POLL_TIME);
182 * bnx2i_chip_cleanup - local routine to handle chip cleanup
183 * @hba: Adapter instance to register
185 * Driver checks if adapter still has any active connections before
186 * executing the cleanup process
188 static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
190 struct bnx2i_endpoint *bnx2i_ep;
191 struct list_head *pos, *tmp;
193 if (hba->ofld_conns_active) {
194 /* Stage to force the disconnection
195 * This is the case where the daemon is either slow or
196 * not present
198 printk(KERN_ALERT "bnx2i: (%s) chip cleanup for %d active "
199 "connections\n", hba->netdev->name,
200 hba->ofld_conns_active);
201 mutex_lock(&hba->net_dev_lock);
202 list_for_each_safe(pos, tmp, &hba->ep_active_list) {
203 bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
204 /* Clean up the chip only */
205 bnx2i_hw_ep_disconnect(bnx2i_ep);
206 bnx2i_ep->cm_sk = NULL;
208 mutex_unlock(&hba->net_dev_lock);
214 * bnx2i_stop - cnic callback to shutdown adapter instance
215 * @handle: transparent handle pointing to adapter structure
217 * driver checks if adapter is already in shutdown mode, if not start
218 * the shutdown process
220 void bnx2i_stop(void *handle)
222 struct bnx2i_hba *hba = handle;
223 int conns_active;
224 int wait_delay = 1 * HZ;
226 /* check if cleanup happened in GOING_DOWN context */
227 if (!test_and_set_bit(ADAPTER_STATE_GOING_DOWN,
228 &hba->adapter_state)) {
229 iscsi_host_for_each_session(hba->shost,
230 bnx2i_drop_session);
231 wait_delay = hba->hba_shutdown_tmo;
233 /* Wait for inflight offload connection tasks to complete before
234 * proceeding. Forcefully terminate all connection recovery in
235 * progress at the earliest, either in bind(), send_pdu(LOGIN),
236 * or conn_start()
238 wait_event_interruptible_timeout(hba->eh_wait,
239 (list_empty(&hba->ep_ofld_list) &&
240 list_empty(&hba->ep_destroy_list)),
241 2 * HZ);
242 /* Wait for all endpoints to be torn down, Chip will be reset once
243 * control returns to network driver. So it is required to cleanup and
244 * release all connection resources before returning from this routine.
246 while (hba->ofld_conns_active) {
247 conns_active = hba->ofld_conns_active;
248 wait_event_interruptible_timeout(hba->eh_wait,
249 (hba->ofld_conns_active != conns_active),
250 wait_delay);
251 if (hba->ofld_conns_active == conns_active)
252 break;
254 bnx2i_chip_cleanup(hba);
256 /* This flag should be cleared last so that ep_disconnect() gracefully
257 * cleans up connection context
259 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
260 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
265 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
266 * @hba: bnx2i adapter instance
267 * @cnic: cnic device handle
269 * Global resource lock is held during critical sections below. This routine is
270 * called from either cnic_register_driver() or device hot plug context and
271 * and does majority of device specific initialization
273 static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
275 int rc;
277 mutex_lock(&bnx2i_dev_lock);
278 if (!cnic->max_iscsi_conn) {
279 printk(KERN_ALERT "bnx2i: dev %s does not support "
280 "iSCSI\n", hba->netdev->name);
281 rc = -EOPNOTSUPP;
282 goto out;
285 hba->cnic = cnic;
286 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
287 if (!rc) {
288 hba->age++;
289 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
290 list_add_tail(&hba->link, &adapter_list);
291 adapter_count++;
292 } else if (rc == -EBUSY) /* duplicate registration */
293 printk(KERN_ALERT "bnx2i, duplicate registration"
294 "hba=%p, cnic=%p\n", hba, cnic);
295 else if (rc == -EAGAIN)
296 printk(KERN_ERR "bnx2i, driver not registered\n");
297 else if (rc == -EINVAL)
298 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
299 else
300 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
302 out:
303 mutex_unlock(&bnx2i_dev_lock);
305 return rc;
310 * bnx2i_ulp_init - initialize an adapter instance
311 * @dev: cnic device handle
313 * Called from cnic_register_driver() context to initialize all enumerated
314 * cnic devices. This routine allocate adapter structure and other
315 * device specific resources.
317 void bnx2i_ulp_init(struct cnic_dev *dev)
319 struct bnx2i_hba *hba;
321 /* Allocate a HBA structure for this device */
322 hba = bnx2i_alloc_hba(dev);
323 if (!hba) {
324 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
325 return;
328 /* Get PCI related information and update hba struct members */
329 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
330 if (bnx2i_init_one(hba, dev)) {
331 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
332 bnx2i_free_hba(hba);
338 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
339 * @dev: cnic device handle
342 void bnx2i_ulp_exit(struct cnic_dev *dev)
344 struct bnx2i_hba *hba;
346 hba = bnx2i_find_hba_for_cnic(dev);
347 if (!hba) {
348 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
349 "found, dev 0x%p\n", dev);
350 return;
352 mutex_lock(&bnx2i_dev_lock);
353 list_del_init(&hba->link);
354 adapter_count--;
356 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
357 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
358 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
360 mutex_unlock(&bnx2i_dev_lock);
362 bnx2i_free_hba(hba);
367 * bnx2i_get_stats - Retrieve various statistic from iSCSI offload
368 * @handle: bnx2i_hba
370 * function callback exported via bnx2i - cnic driver interface to
371 * retrieve various iSCSI offload related statistics.
373 int bnx2i_get_stats(void *handle)
375 struct bnx2i_hba *hba = handle;
376 struct iscsi_stats_info *stats;
378 if (!hba)
379 return -EINVAL;
381 stats = (struct iscsi_stats_info *)hba->cnic->stats_addr;
383 if (!stats)
384 return -ENOMEM;
386 strlcpy(stats->version, DRV_MODULE_VERSION, sizeof(stats->version));
387 memcpy(stats->mac_add1 + 2, hba->cnic->mac_addr, ETH_ALEN);
389 stats->max_frame_size = hba->netdev->mtu;
390 stats->txq_size = hba->max_sqes;
391 stats->rxq_size = hba->max_cqes;
393 stats->txq_avg_depth = 0;
394 stats->rxq_avg_depth = 0;
396 GET_STATS_64(hba, stats, rx_pdus);
397 GET_STATS_64(hba, stats, rx_bytes);
399 GET_STATS_64(hba, stats, tx_pdus);
400 GET_STATS_64(hba, stats, tx_bytes);
402 return 0;
407 * bnx2i_percpu_thread_create - Create a receive thread for an
408 * online CPU
410 * @cpu: cpu index for the online cpu
412 static void bnx2i_percpu_thread_create(unsigned int cpu)
414 struct bnx2i_percpu_s *p;
415 struct task_struct *thread;
417 p = &per_cpu(bnx2i_percpu, cpu);
419 thread = kthread_create_on_node(bnx2i_percpu_io_thread, (void *)p,
420 cpu_to_node(cpu),
421 "bnx2i_thread/%d", cpu);
422 /* bind thread to the cpu */
423 if (likely(!IS_ERR(thread))) {
424 kthread_bind(thread, cpu);
425 p->iothread = thread;
426 wake_up_process(thread);
431 static void bnx2i_percpu_thread_destroy(unsigned int cpu)
433 struct bnx2i_percpu_s *p;
434 struct task_struct *thread;
435 struct bnx2i_work *work, *tmp;
437 /* Prevent any new work from being queued for this CPU */
438 p = &per_cpu(bnx2i_percpu, cpu);
439 spin_lock_bh(&p->p_work_lock);
440 thread = p->iothread;
441 p->iothread = NULL;
443 /* Free all work in the list */
444 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
445 list_del_init(&work->list);
446 bnx2i_process_scsi_cmd_resp(work->session,
447 work->bnx2i_conn, &work->cqe);
448 kfree(work);
451 spin_unlock_bh(&p->p_work_lock);
452 if (thread)
453 kthread_stop(thread);
456 static int bnx2i_cpu_online(unsigned int cpu)
458 pr_info("bnx2i: CPU %x online: Create Rx thread\n", cpu);
459 bnx2i_percpu_thread_create(cpu);
460 return 0;
463 static int bnx2i_cpu_dead(unsigned int cpu)
465 pr_info("CPU %x offline: Remove Rx thread\n", cpu);
466 bnx2i_percpu_thread_destroy(cpu);
467 return 0;
470 static enum cpuhp_state bnx2i_online_state;
473 * bnx2i_mod_init - module init entry point
475 * initialize any driver wide global data structures such as endpoint pool,
476 * tcp port manager/queue, sysfs. finally driver will register itself
477 * with the cnic module
479 static int __init bnx2i_mod_init(void)
481 int err;
482 unsigned cpu = 0;
483 struct bnx2i_percpu_s *p;
485 printk(KERN_INFO "%s", version);
487 if (sq_size && !is_power_of_2(sq_size))
488 sq_size = roundup_pow_of_two(sq_size);
490 mutex_init(&bnx2i_dev_lock);
492 bnx2i_scsi_xport_template =
493 iscsi_register_transport(&bnx2i_iscsi_transport);
494 if (!bnx2i_scsi_xport_template) {
495 printk(KERN_ERR "Could not register bnx2i transport.\n");
496 err = -ENOMEM;
497 goto out;
500 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
501 if (err) {
502 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
503 goto unreg_xport;
506 /* Create percpu kernel threads to handle iSCSI I/O completions */
507 for_each_possible_cpu(cpu) {
508 p = &per_cpu(bnx2i_percpu, cpu);
509 INIT_LIST_HEAD(&p->work_list);
510 spin_lock_init(&p->p_work_lock);
511 p->iothread = NULL;
514 get_online_cpus();
516 for_each_online_cpu(cpu)
517 bnx2i_percpu_thread_create(cpu);
519 err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
520 "scsi/bnx2i:online",
521 bnx2i_cpu_online, NULL);
522 if (err < 0)
523 goto remove_threads;
524 bnx2i_online_state = err;
526 cpuhp_setup_state_nocalls(CPUHP_SCSI_BNX2I_DEAD, "scsi/bnx2i:dead",
527 NULL, bnx2i_cpu_dead);
528 put_online_cpus();
529 return 0;
531 remove_threads:
532 for_each_online_cpu(cpu)
533 bnx2i_percpu_thread_destroy(cpu);
534 put_online_cpus();
535 cnic_unregister_driver(CNIC_ULP_ISCSI);
536 unreg_xport:
537 iscsi_unregister_transport(&bnx2i_iscsi_transport);
538 out:
539 return err;
544 * bnx2i_mod_exit - module cleanup/exit entry point
546 * Global resource lock and host adapter lock is held during critical sections
547 * in this function. Driver will browse through the adapter list, cleans-up
548 * each instance, unregisters iscsi transport name and finally driver will
549 * unregister itself with the cnic module
551 static void __exit bnx2i_mod_exit(void)
553 struct bnx2i_hba *hba;
554 unsigned cpu = 0;
556 mutex_lock(&bnx2i_dev_lock);
557 while (!list_empty(&adapter_list)) {
558 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
559 list_del(&hba->link);
560 adapter_count--;
562 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
563 bnx2i_chip_cleanup(hba);
564 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
565 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
568 bnx2i_free_hba(hba);
570 mutex_unlock(&bnx2i_dev_lock);
572 get_online_cpus();
574 for_each_online_cpu(cpu)
575 bnx2i_percpu_thread_destroy(cpu);
577 cpuhp_remove_state_nocalls(bnx2i_online_state);
578 cpuhp_remove_state_nocalls(CPUHP_SCSI_BNX2I_DEAD);
579 put_online_cpus();
581 iscsi_unregister_transport(&bnx2i_iscsi_transport);
582 cnic_unregister_driver(CNIC_ULP_ISCSI);
585 module_init(bnx2i_mod_init);
586 module_exit(bnx2i_mod_exit);