Linux 4.16.11
[linux/fpc-iii.git] / drivers / scsi / bnx2i / bnx2i_init.c
blob4ebcda8d9500439941bd630f8b1187875a7beac5
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_cpu_online - Create a receive thread for an online CPU
409 * @cpu: cpu index for the online cpu
411 static int bnx2i_cpu_online(unsigned int cpu)
413 struct bnx2i_percpu_s *p;
414 struct task_struct *thread;
416 p = &per_cpu(bnx2i_percpu, cpu);
418 thread = kthread_create_on_node(bnx2i_percpu_io_thread, (void *)p,
419 cpu_to_node(cpu),
420 "bnx2i_thread/%d", cpu);
421 if (IS_ERR(thread))
422 return PTR_ERR(thread);
424 /* bind thread to the cpu */
425 kthread_bind(thread, cpu);
426 p->iothread = thread;
427 wake_up_process(thread);
428 return 0;
431 static int bnx2i_cpu_offline(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);
454 return 0;
457 static enum cpuhp_state bnx2i_online_state;
460 * bnx2i_mod_init - module init entry point
462 * initialize any driver wide global data structures such as endpoint pool,
463 * tcp port manager/queue, sysfs. finally driver will register itself
464 * with the cnic module
466 static int __init bnx2i_mod_init(void)
468 int err;
469 unsigned cpu = 0;
470 struct bnx2i_percpu_s *p;
472 printk(KERN_INFO "%s", version);
474 if (sq_size && !is_power_of_2(sq_size))
475 sq_size = roundup_pow_of_two(sq_size);
477 mutex_init(&bnx2i_dev_lock);
479 bnx2i_scsi_xport_template =
480 iscsi_register_transport(&bnx2i_iscsi_transport);
481 if (!bnx2i_scsi_xport_template) {
482 printk(KERN_ERR "Could not register bnx2i transport.\n");
483 err = -ENOMEM;
484 goto out;
487 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
488 if (err) {
489 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
490 goto unreg_xport;
493 /* Create percpu kernel threads to handle iSCSI I/O completions */
494 for_each_possible_cpu(cpu) {
495 p = &per_cpu(bnx2i_percpu, cpu);
496 INIT_LIST_HEAD(&p->work_list);
497 spin_lock_init(&p->p_work_lock);
498 p->iothread = NULL;
501 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/bnx2i:online",
502 bnx2i_cpu_online, bnx2i_cpu_offline);
503 if (err < 0)
504 goto unreg_driver;
505 bnx2i_online_state = err;
506 return 0;
508 unreg_driver:
509 cnic_unregister_driver(CNIC_ULP_ISCSI);
510 unreg_xport:
511 iscsi_unregister_transport(&bnx2i_iscsi_transport);
512 out:
513 return err;
518 * bnx2i_mod_exit - module cleanup/exit entry point
520 * Global resource lock and host adapter lock is held during critical sections
521 * in this function. Driver will browse through the adapter list, cleans-up
522 * each instance, unregisters iscsi transport name and finally driver will
523 * unregister itself with the cnic module
525 static void __exit bnx2i_mod_exit(void)
527 struct bnx2i_hba *hba;
529 mutex_lock(&bnx2i_dev_lock);
530 while (!list_empty(&adapter_list)) {
531 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
532 list_del(&hba->link);
533 adapter_count--;
535 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
536 bnx2i_chip_cleanup(hba);
537 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
538 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
541 bnx2i_free_hba(hba);
543 mutex_unlock(&bnx2i_dev_lock);
545 cpuhp_remove_state(bnx2i_online_state);
547 iscsi_unregister_transport(&bnx2i_iscsi_transport);
548 cnic_unregister_driver(CNIC_ULP_ISCSI);
551 module_init(bnx2i_mod_init);
552 module_exit(bnx2i_mod_exit);