1 /* bnx2i.c: Broadcom 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
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.
11 * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
12 * Maintained by: Eddie Wai (eddie.wai@broadcom.com)
17 static struct list_head adapter_list
= LIST_HEAD_INIT(adapter_list
);
18 static u32 adapter_count
;
20 #define DRV_MODULE_NAME "bnx2i"
21 #define DRV_MODULE_VERSION "2.7.6.2"
22 #define DRV_MODULE_RELDATE "Jun 06, 2013"
24 static char version
[] =
25 "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
26 " v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
29 MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com> and "
30 "Eddie Wai <eddie.wai@broadcom.com>");
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
);
37 static DEFINE_MUTEX(bnx2i_dev_lock
);
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");
43 unsigned int event_coal_div
= 2;
44 module_param(event_coal_div
, int, 0664);
45 MODULE_PARM_DESC(event_coal_div
, "Event Coalescing Divide Factor");
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");
51 unsigned int error_mask1
= 0x00;
52 module_param(error_mask1
, uint
, 0664);
53 MODULE_PARM_DESC(error_mask1
, "Config FW iSCSI Error Mask #1");
55 unsigned int error_mask2
= 0x00;
56 module_param(error_mask2
, uint
, 0664);
57 MODULE_PARM_DESC(error_mask2
, "Config FW iSCSI Error Mask #2");
60 module_param(sq_size
, int, 0664);
61 MODULE_PARM_DESC(sq_size
, "Configure SQ size");
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");
67 u64 iscsi_error_mask
= 0x00;
69 DEFINE_PER_CPU(struct bnx2i_percpu_s
, bnx2i_percpu
);
71 static int bnx2i_cpu_callback(struct notifier_block
*nfb
,
72 unsigned long action
, void *hcpu
);
73 /* notification function for CPU hotplug events */
74 static struct notifier_block bnx2i_cpu_notifier
= {
75 .notifier_call
= bnx2i_cpu_callback
,
80 * bnx2i_identify_device - identifies NetXtreme II device type
81 * @hba: Adapter structure pointer
82 * @cnic: Corresponding cnic device
84 * This function identifies the NX2 device type and sets appropriate
85 * queue mailbox register access method, 5709 requires driver to
86 * access MBOX regs using *bin* mode
88 void bnx2i_identify_device(struct bnx2i_hba
*hba
, struct cnic_dev
*dev
)
90 hba
->cnic_dev_type
= 0;
91 if (test_bit(CNIC_F_BNX2_CLASS
, &dev
->flags
)) {
92 if (hba
->pci_did
== PCI_DEVICE_ID_NX2_5706
||
93 hba
->pci_did
== PCI_DEVICE_ID_NX2_5706S
) {
94 set_bit(BNX2I_NX2_DEV_5706
, &hba
->cnic_dev_type
);
95 } else if (hba
->pci_did
== PCI_DEVICE_ID_NX2_5708
||
96 hba
->pci_did
== PCI_DEVICE_ID_NX2_5708S
) {
97 set_bit(BNX2I_NX2_DEV_5708
, &hba
->cnic_dev_type
);
98 } else if (hba
->pci_did
== PCI_DEVICE_ID_NX2_5709
||
99 hba
->pci_did
== PCI_DEVICE_ID_NX2_5709S
) {
100 set_bit(BNX2I_NX2_DEV_5709
, &hba
->cnic_dev_type
);
101 hba
->mail_queue_access
= BNX2I_MQ_BIN_MODE
;
103 } else if (test_bit(CNIC_F_BNX2X_CLASS
, &dev
->flags
)) {
104 set_bit(BNX2I_NX2_DEV_57710
, &hba
->cnic_dev_type
);
106 printk(KERN_ALERT
"bnx2i: unknown device, 0x%x\n",
113 * get_adapter_list_head - returns head of adapter list
115 struct bnx2i_hba
*get_adapter_list_head(void)
117 struct bnx2i_hba
*hba
= NULL
;
118 struct bnx2i_hba
*tmp_hba
;
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
) {
130 mutex_unlock(&bnx2i_dev_lock
);
137 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
138 * @cnic: pointer to cnic device instance
141 struct bnx2i_hba
*bnx2i_find_hba_for_cnic(struct cnic_dev
*cnic
)
143 struct bnx2i_hba
*hba
, *temp
;
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
);
152 mutex_unlock(&bnx2i_dev_lock
);
158 * bnx2i_start - cnic callback to initialize & start adapter instance
159 * @handle: transparent handle pointing to adapter structure
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'
167 * b) bnx2i adapter instance is registered
169 void bnx2i_start(void *handle
)
171 #define BNX2I_INIT_POLL_TIME (1000 / HZ)
172 struct bnx2i_hba
*hba
= handle
;
175 /* On some bnx2x devices, it is possible that iSCSI is no
176 * longer supported after firmware is downloaded. In that
177 * case, the iscsi_init_msg will return failure.
180 bnx2i_send_fw_iscsi_init_msg(hba
);
181 while (!test_bit(ADAPTER_STATE_UP
, &hba
->adapter_state
) &&
182 !test_bit(ADAPTER_STATE_INIT_FAILED
, &hba
->adapter_state
) && i
--)
183 msleep(BNX2I_INIT_POLL_TIME
);
188 * bnx2i_chip_cleanup - local routine to handle chip cleanup
189 * @hba: Adapter instance to register
191 * Driver checks if adapter still has any active connections before
192 * executing the cleanup process
194 static void bnx2i_chip_cleanup(struct bnx2i_hba
*hba
)
196 struct bnx2i_endpoint
*bnx2i_ep
;
197 struct list_head
*pos
, *tmp
;
199 if (hba
->ofld_conns_active
) {
200 /* Stage to force the disconnection
201 * This is the case where the daemon is either slow or
204 printk(KERN_ALERT
"bnx2i: (%s) chip cleanup for %d active "
205 "connections\n", hba
->netdev
->name
,
206 hba
->ofld_conns_active
);
207 mutex_lock(&hba
->net_dev_lock
);
208 list_for_each_safe(pos
, tmp
, &hba
->ep_active_list
) {
209 bnx2i_ep
= list_entry(pos
, struct bnx2i_endpoint
, link
);
210 /* Clean up the chip only */
211 bnx2i_hw_ep_disconnect(bnx2i_ep
);
212 bnx2i_ep
->cm_sk
= NULL
;
214 mutex_unlock(&hba
->net_dev_lock
);
220 * bnx2i_stop - cnic callback to shutdown adapter instance
221 * @handle: transparent handle pointing to adapter structure
223 * driver checks if adapter is already in shutdown mode, if not start
224 * the shutdown process
226 void bnx2i_stop(void *handle
)
228 struct bnx2i_hba
*hba
= handle
;
230 int wait_delay
= 1 * HZ
;
232 /* check if cleanup happened in GOING_DOWN context */
233 if (!test_and_set_bit(ADAPTER_STATE_GOING_DOWN
,
234 &hba
->adapter_state
)) {
235 iscsi_host_for_each_session(hba
->shost
,
237 wait_delay
= hba
->hba_shutdown_tmo
;
239 /* Wait for inflight offload connection tasks to complete before
240 * proceeding. Forcefully terminate all connection recovery in
241 * progress at the earliest, either in bind(), send_pdu(LOGIN),
244 wait_event_interruptible_timeout(hba
->eh_wait
,
245 (list_empty(&hba
->ep_ofld_list
) &&
246 list_empty(&hba
->ep_destroy_list
)),
248 /* Wait for all endpoints to be torn down, Chip will be reset once
249 * control returns to network driver. So it is required to cleanup and
250 * release all connection resources before returning from this routine.
252 while (hba
->ofld_conns_active
) {
253 conns_active
= hba
->ofld_conns_active
;
254 wait_event_interruptible_timeout(hba
->eh_wait
,
255 (hba
->ofld_conns_active
!= conns_active
),
257 if (hba
->ofld_conns_active
== conns_active
)
260 bnx2i_chip_cleanup(hba
);
262 /* This flag should be cleared last so that ep_disconnect() gracefully
263 * cleans up connection context
265 clear_bit(ADAPTER_STATE_GOING_DOWN
, &hba
->adapter_state
);
266 clear_bit(ADAPTER_STATE_UP
, &hba
->adapter_state
);
271 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
272 * @hba: bnx2i adapter instance
273 * @cnic: cnic device handle
275 * Global resource lock is held during critical sections below. This routine is
276 * called from either cnic_register_driver() or device hot plug context and
277 * and does majority of device specific initialization
279 static int bnx2i_init_one(struct bnx2i_hba
*hba
, struct cnic_dev
*cnic
)
283 mutex_lock(&bnx2i_dev_lock
);
284 if (!cnic
->max_iscsi_conn
) {
285 printk(KERN_ALERT
"bnx2i: dev %s does not support "
286 "iSCSI\n", hba
->netdev
->name
);
292 rc
= cnic
->register_device(cnic
, CNIC_ULP_ISCSI
, hba
);
295 set_bit(BNX2I_CNIC_REGISTERED
, &hba
->reg_with_cnic
);
296 list_add_tail(&hba
->link
, &adapter_list
);
298 } else if (rc
== -EBUSY
) /* duplicate registration */
299 printk(KERN_ALERT
"bnx2i, duplicate registration"
300 "hba=%p, cnic=%p\n", hba
, cnic
);
301 else if (rc
== -EAGAIN
)
302 printk(KERN_ERR
"bnx2i, driver not registered\n");
303 else if (rc
== -EINVAL
)
304 printk(KERN_ERR
"bnx2i, invalid type %d\n", CNIC_ULP_ISCSI
);
306 printk(KERN_ERR
"bnx2i dev reg, unknown error, %d\n", rc
);
309 mutex_unlock(&bnx2i_dev_lock
);
316 * bnx2i_ulp_init - initialize an adapter instance
317 * @dev: cnic device handle
319 * Called from cnic_register_driver() context to initialize all enumerated
320 * cnic devices. This routine allocate adapter structure and other
321 * device specific resources.
323 void bnx2i_ulp_init(struct cnic_dev
*dev
)
325 struct bnx2i_hba
*hba
;
327 /* Allocate a HBA structure for this device */
328 hba
= bnx2i_alloc_hba(dev
);
330 printk(KERN_ERR
"bnx2i init: hba initialization failed\n");
334 /* Get PCI related information and update hba struct members */
335 clear_bit(BNX2I_CNIC_REGISTERED
, &hba
->reg_with_cnic
);
336 if (bnx2i_init_one(hba
, dev
)) {
337 printk(KERN_ERR
"bnx2i - hba %p init failed\n", hba
);
344 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
345 * @dev: cnic device handle
348 void bnx2i_ulp_exit(struct cnic_dev
*dev
)
350 struct bnx2i_hba
*hba
;
352 hba
= bnx2i_find_hba_for_cnic(dev
);
354 printk(KERN_INFO
"bnx2i_ulp_exit: hba not "
355 "found, dev 0x%p\n", dev
);
358 mutex_lock(&bnx2i_dev_lock
);
359 list_del_init(&hba
->link
);
362 if (test_bit(BNX2I_CNIC_REGISTERED
, &hba
->reg_with_cnic
)) {
363 hba
->cnic
->unregister_device(hba
->cnic
, CNIC_ULP_ISCSI
);
364 clear_bit(BNX2I_CNIC_REGISTERED
, &hba
->reg_with_cnic
);
366 mutex_unlock(&bnx2i_dev_lock
);
373 * bnx2i_get_stats - Retrieve various statistic from iSCSI offload
376 * function callback exported via bnx2i - cnic driver interface to
377 * retrieve various iSCSI offload related statistics.
379 int bnx2i_get_stats(void *handle
)
381 struct bnx2i_hba
*hba
= handle
;
382 struct iscsi_stats_info
*stats
;
387 stats
= (struct iscsi_stats_info
*)hba
->cnic
->stats_addr
;
392 strlcpy(stats
->version
, DRV_MODULE_VERSION
, sizeof(stats
->version
));
393 memcpy(stats
->mac_add1
+ 2, hba
->cnic
->mac_addr
, ETH_ALEN
);
395 stats
->max_frame_size
= hba
->netdev
->mtu
;
396 stats
->txq_size
= hba
->max_sqes
;
397 stats
->rxq_size
= hba
->max_cqes
;
399 stats
->txq_avg_depth
= 0;
400 stats
->rxq_avg_depth
= 0;
402 GET_STATS_64(hba
, stats
, rx_pdus
);
403 GET_STATS_64(hba
, stats
, rx_bytes
);
405 GET_STATS_64(hba
, stats
, tx_pdus
);
406 GET_STATS_64(hba
, stats
, tx_bytes
);
413 * bnx2i_percpu_thread_create - Create a receive thread for an
416 * @cpu: cpu index for the online cpu
418 static void bnx2i_percpu_thread_create(unsigned int cpu
)
420 struct bnx2i_percpu_s
*p
;
421 struct task_struct
*thread
;
423 p
= &per_cpu(bnx2i_percpu
, cpu
);
425 thread
= kthread_create_on_node(bnx2i_percpu_io_thread
, (void *)p
,
427 "bnx2i_thread/%d", cpu
);
428 /* bind thread to the cpu */
429 if (likely(!IS_ERR(thread
))) {
430 kthread_bind(thread
, cpu
);
431 p
->iothread
= thread
;
432 wake_up_process(thread
);
437 static void bnx2i_percpu_thread_destroy(unsigned int cpu
)
439 struct bnx2i_percpu_s
*p
;
440 struct task_struct
*thread
;
441 struct bnx2i_work
*work
, *tmp
;
443 /* Prevent any new work from being queued for this CPU */
444 p
= &per_cpu(bnx2i_percpu
, cpu
);
445 spin_lock_bh(&p
->p_work_lock
);
446 thread
= p
->iothread
;
449 /* Free all work in the list */
450 list_for_each_entry_safe(work
, tmp
, &p
->work_list
, list
) {
451 list_del_init(&work
->list
);
452 bnx2i_process_scsi_cmd_resp(work
->session
,
453 work
->bnx2i_conn
, &work
->cqe
);
457 spin_unlock_bh(&p
->p_work_lock
);
459 kthread_stop(thread
);
464 * bnx2i_cpu_callback - Handler for CPU hotplug events
466 * @nfb: The callback data block
467 * @action: The event triggering the callback
468 * @hcpu: The index of the CPU that the event is for
470 * This creates or destroys per-CPU data for iSCSI
472 * Returns NOTIFY_OK always.
474 static int bnx2i_cpu_callback(struct notifier_block
*nfb
,
475 unsigned long action
, void *hcpu
)
477 unsigned cpu
= (unsigned long)hcpu
;
481 case CPU_ONLINE_FROZEN
:
482 printk(KERN_INFO
"bnx2i: CPU %x online: Create Rx thread\n",
484 bnx2i_percpu_thread_create(cpu
);
487 case CPU_DEAD_FROZEN
:
488 printk(KERN_INFO
"CPU %x offline: Remove Rx thread\n", cpu
);
489 bnx2i_percpu_thread_destroy(cpu
);
499 * bnx2i_mod_init - module init entry point
501 * initialize any driver wide global data structures such as endpoint pool,
502 * tcp port manager/queue, sysfs. finally driver will register itself
503 * with the cnic module
505 static int __init
bnx2i_mod_init(void)
509 struct bnx2i_percpu_s
*p
;
511 printk(KERN_INFO
"%s", version
);
513 if (sq_size
&& !is_power_of_2(sq_size
))
514 sq_size
= roundup_pow_of_two(sq_size
);
516 mutex_init(&bnx2i_dev_lock
);
518 bnx2i_scsi_xport_template
=
519 iscsi_register_transport(&bnx2i_iscsi_transport
);
520 if (!bnx2i_scsi_xport_template
) {
521 printk(KERN_ERR
"Could not register bnx2i transport.\n");
526 err
= cnic_register_driver(CNIC_ULP_ISCSI
, &bnx2i_cnic_cb
);
528 printk(KERN_ERR
"Could not register bnx2i cnic driver.\n");
532 /* Create percpu kernel threads to handle iSCSI I/O completions */
533 for_each_possible_cpu(cpu
) {
534 p
= &per_cpu(bnx2i_percpu
, cpu
);
535 INIT_LIST_HEAD(&p
->work_list
);
536 spin_lock_init(&p
->p_work_lock
);
540 for_each_online_cpu(cpu
)
541 bnx2i_percpu_thread_create(cpu
);
543 /* Initialize per CPU interrupt thread */
544 register_hotcpu_notifier(&bnx2i_cpu_notifier
);
549 iscsi_unregister_transport(&bnx2i_iscsi_transport
);
556 * bnx2i_mod_exit - module cleanup/exit entry point
558 * Global resource lock and host adapter lock is held during critical sections
559 * in this function. Driver will browse through the adapter list, cleans-up
560 * each instance, unregisters iscsi transport name and finally driver will
561 * unregister itself with the cnic module
563 static void __exit
bnx2i_mod_exit(void)
565 struct bnx2i_hba
*hba
;
568 mutex_lock(&bnx2i_dev_lock
);
569 while (!list_empty(&adapter_list
)) {
570 hba
= list_entry(adapter_list
.next
, struct bnx2i_hba
, link
);
571 list_del(&hba
->link
);
574 if (test_bit(BNX2I_CNIC_REGISTERED
, &hba
->reg_with_cnic
)) {
575 bnx2i_chip_cleanup(hba
);
576 hba
->cnic
->unregister_device(hba
->cnic
, CNIC_ULP_ISCSI
);
577 clear_bit(BNX2I_CNIC_REGISTERED
, &hba
->reg_with_cnic
);
582 mutex_unlock(&bnx2i_dev_lock
);
584 unregister_hotcpu_notifier(&bnx2i_cpu_notifier
);
586 for_each_online_cpu(cpu
)
587 bnx2i_percpu_thread_destroy(cpu
);
589 iscsi_unregister_transport(&bnx2i_iscsi_transport
);
590 cnic_unregister_driver(CNIC_ULP_ISCSI
);
593 module_init(bnx2i_mod_init
);
594 module_exit(bnx2i_mod_exit
);