xref: /linux/drivers/scsi/bnx2i/bnx2i_init.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 /* bnx2i.c: QLogic NetXtreme II iSCSI driver.
2  *
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
7  *
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.
11  *
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
15  */
16 
17 #include "bnx2i.h"
18 
19 static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
20 static u32 adapter_count;
21 
22 #define DRV_MODULE_NAME		"bnx2i"
23 #define DRV_MODULE_VERSION	"2.7.10.1"
24 #define DRV_MODULE_RELDATE	"Jul 16, 2014"
25 
26 static char version[] =
27 		"QLogic NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
28 		" v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
29 
30 
31 MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com> and "
32 	      "Eddie Wai <eddie.wai@broadcom.com>");
33 
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);
38 
39 static DEFINE_MUTEX(bnx2i_dev_lock);
40 
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");
44 
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");
48 
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");
52 
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");
56 
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");
60 
61 unsigned int sq_size;
62 module_param(sq_size, int, 0664);
63 MODULE_PARM_DESC(sq_size, "Configure SQ size");
64 
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");
68 
69 u64 iscsi_error_mask = 0x00;
70 
71 DEFINE_PER_CPU(struct bnx2i_percpu_s, bnx2i_percpu);
72 
73 /**
74  * bnx2i_identify_device - identifies NetXtreme II device type
75  * @hba: 		Adapter structure pointer
76  * @dev:		Corresponding cnic device
77  *
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
81  */
82 void bnx2i_identify_device(struct bnx2i_hba *hba, struct cnic_dev *dev)
83 {
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;
96 		}
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);
102 	}
103 }
104 
105 
106 /**
107  * get_adapter_list_head - returns head of adapter list
108  */
109 struct bnx2i_hba *get_adapter_list_head(void)
110 {
111 	struct bnx2i_hba *hba = NULL;
112 	struct bnx2i_hba *tmp_hba;
113 
114 	if (!adapter_count)
115 		goto hba_not_found;
116 
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;
122 		}
123 	}
124 	mutex_unlock(&bnx2i_dev_lock);
125 hba_not_found:
126 	return hba;
127 }
128 
129 
130 /**
131  * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
132  * @cnic:	pointer to cnic device instance
133  *
134  */
135 struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
136 {
137 	struct bnx2i_hba *hba, *temp;
138 
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;
144 		}
145 	}
146 	mutex_unlock(&bnx2i_dev_lock);
147 	return NULL;
148 }
149 
150 
151 /**
152  * bnx2i_start - cnic callback to initialize & start adapter instance
153  * @handle:	transparent handle pointing to adapter structure
154  *
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
162  */
163 void bnx2i_start(void *handle)
164 {
165 #define BNX2I_INIT_POLL_TIME	(1000 / HZ)
166 	struct bnx2i_hba *hba = handle;
167 	int i = HZ;
168 
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.
172 	 */
173 
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);
178 }
179 
180 
181 /**
182  * bnx2i_chip_cleanup - local routine to handle chip cleanup
183  * @hba:	Adapter instance to register
184  *
185  * Driver checks if adapter still has any active connections before
186  *	executing the cleanup process
187  */
188 static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
189 {
190 	struct bnx2i_endpoint *bnx2i_ep;
191 	struct list_head *pos, *tmp;
192 
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
197 		 */
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;
207 		}
208 		mutex_unlock(&hba->net_dev_lock);
209 	}
210 }
211 
212 
213 /**
214  * bnx2i_stop - cnic callback to shutdown adapter instance
215  * @handle:	transparent handle pointing to adapter structure
216  *
217  * driver checks if adapter is already in shutdown mode, if not start
218  *	the shutdown process
219  */
220 void bnx2i_stop(void *handle)
221 {
222 	struct bnx2i_hba *hba = handle;
223 	int conns_active;
224 	int wait_delay = 1 * HZ;
225 
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;
232 	}
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()
237 	 */
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.
245 	 */
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;
253 	}
254 	bnx2i_chip_cleanup(hba);
255 
256 	/* This flag should be cleared last so that ep_disconnect() gracefully
257 	 * cleans up connection context
258 	 */
259 	clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
260 	clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
261 }
262 
263 
264 /**
265  * bnx2i_init_one - initialize an adapter instance and allocate memory resources
266  * @hba:	bnx2i adapter instance
267  * @cnic:	cnic device handle
268  *
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
272  */
273 static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
274 {
275 	int rc;
276 
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;
283 	}
284 
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);
301 
302 out:
303 	mutex_unlock(&bnx2i_dev_lock);
304 
305 	return rc;
306 }
307 
308 
309 /**
310  * bnx2i_ulp_init - initialize an adapter instance
311  * @dev:	cnic device handle
312  *
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.
316  */
317 void bnx2i_ulp_init(struct cnic_dev *dev)
318 {
319 	struct bnx2i_hba *hba;
320 
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;
326 	}
327 
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);
333 	}
334 }
335 
336 
337 /**
338  * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
339  * @dev:	cnic device handle
340  *
341  */
342 void bnx2i_ulp_exit(struct cnic_dev *dev)
343 {
344 	struct bnx2i_hba *hba;
345 
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;
351 	}
352 	mutex_lock(&bnx2i_dev_lock);
353 	list_del_init(&hba->link);
354 	adapter_count--;
355 
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);
359 	}
360 	mutex_unlock(&bnx2i_dev_lock);
361 
362 	bnx2i_free_hba(hba);
363 }
364 
365 
366 /**
367  * bnx2i_get_stats - Retrieve various statistic from iSCSI offload
368  * @handle:	bnx2i_hba
369  *
370  * function callback exported via bnx2i - cnic driver interface to
371  *      retrieve various iSCSI offload related statistics.
372  */
373 int bnx2i_get_stats(void *handle)
374 {
375 	struct bnx2i_hba *hba = handle;
376 	struct iscsi_stats_info *stats;
377 
378 	if (!hba)
379 		return -EINVAL;
380 
381 	stats = (struct iscsi_stats_info *)hba->cnic->stats_addr;
382 
383 	if (!stats)
384 		return -ENOMEM;
385 
386 	strlcpy(stats->version, DRV_MODULE_VERSION, sizeof(stats->version));
387 	memcpy(stats->mac_add1 + 2, hba->cnic->mac_addr, ETH_ALEN);
388 
389 	stats->max_frame_size = hba->netdev->mtu;
390 	stats->txq_size = hba->max_sqes;
391 	stats->rxq_size = hba->max_cqes;
392 
393 	stats->txq_avg_depth = 0;
394 	stats->rxq_avg_depth = 0;
395 
396 	GET_STATS_64(hba, stats, rx_pdus);
397 	GET_STATS_64(hba, stats, rx_bytes);
398 
399 	GET_STATS_64(hba, stats, tx_pdus);
400 	GET_STATS_64(hba, stats, tx_bytes);
401 
402 	return 0;
403 }
404 
405 
406 /**
407  * bnx2i_cpu_online - Create a receive thread for an online CPU
408  *
409  * @cpu:	cpu index for the online cpu
410  */
411 static int bnx2i_cpu_online(unsigned int cpu)
412 {
413 	struct bnx2i_percpu_s *p;
414 	struct task_struct *thread;
415 
416 	p = &per_cpu(bnx2i_percpu, cpu);
417 
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);
423 
424 	/* bind thread to the cpu */
425 	kthread_bind(thread, cpu);
426 	p->iothread = thread;
427 	wake_up_process(thread);
428 	return 0;
429 }
430 
431 static int bnx2i_cpu_offline(unsigned int cpu)
432 {
433 	struct bnx2i_percpu_s *p;
434 	struct task_struct *thread;
435 	struct bnx2i_work *work, *tmp;
436 
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;
442 
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);
449 	}
450 
451 	spin_unlock_bh(&p->p_work_lock);
452 	if (thread)
453 		kthread_stop(thread);
454 	return 0;
455 }
456 
457 static enum cpuhp_state bnx2i_online_state;
458 
459 /**
460  * bnx2i_mod_init - module init entry point
461  *
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
465  */
466 static int __init bnx2i_mod_init(void)
467 {
468 	int err;
469 	unsigned cpu = 0;
470 	struct bnx2i_percpu_s *p;
471 
472 	printk(KERN_INFO "%s", version);
473 
474 	if (sq_size && !is_power_of_2(sq_size))
475 		sq_size = roundup_pow_of_two(sq_size);
476 
477 	bnx2i_scsi_xport_template =
478 			iscsi_register_transport(&bnx2i_iscsi_transport);
479 	if (!bnx2i_scsi_xport_template) {
480 		printk(KERN_ERR "Could not register bnx2i transport.\n");
481 		err = -ENOMEM;
482 		goto out;
483 	}
484 
485 	err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
486 	if (err) {
487 		printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
488 		goto unreg_xport;
489 	}
490 
491 	/* Create percpu kernel threads to handle iSCSI I/O completions */
492 	for_each_possible_cpu(cpu) {
493 		p = &per_cpu(bnx2i_percpu, cpu);
494 		INIT_LIST_HEAD(&p->work_list);
495 		spin_lock_init(&p->p_work_lock);
496 		p->iothread = NULL;
497 	}
498 
499 	err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/bnx2i:online",
500 				bnx2i_cpu_online, bnx2i_cpu_offline);
501 	if (err < 0)
502 		goto unreg_driver;
503 	bnx2i_online_state = err;
504 	return 0;
505 
506 unreg_driver:
507 	cnic_unregister_driver(CNIC_ULP_ISCSI);
508 unreg_xport:
509 	iscsi_unregister_transport(&bnx2i_iscsi_transport);
510 out:
511 	return err;
512 }
513 
514 
515 /**
516  * bnx2i_mod_exit - module cleanup/exit entry point
517  *
518  * Global resource lock and host adapter lock is held during critical sections
519  *	in this function. Driver will browse through the adapter list, cleans-up
520  *	each instance, unregisters iscsi transport name and finally driver will
521  *	unregister itself with the cnic module
522  */
523 static void __exit bnx2i_mod_exit(void)
524 {
525 	struct bnx2i_hba *hba;
526 
527 	mutex_lock(&bnx2i_dev_lock);
528 	while (!list_empty(&adapter_list)) {
529 		hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
530 		list_del(&hba->link);
531 		adapter_count--;
532 
533 		if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
534 			bnx2i_chip_cleanup(hba);
535 			hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
536 			clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
537 		}
538 
539 		bnx2i_free_hba(hba);
540 	}
541 	mutex_unlock(&bnx2i_dev_lock);
542 
543 	cpuhp_remove_state(bnx2i_online_state);
544 
545 	iscsi_unregister_transport(&bnx2i_iscsi_transport);
546 	cnic_unregister_driver(CNIC_ULP_ISCSI);
547 }
548 
549 module_init(bnx2i_mod_init);
550 module_exit(bnx2i_mod_exit);
551