blob: 6afaa34222af8cf67e6e61e0e5af146a4ea07cc3 [file] [log] [blame]
Michael Chancf4e6362009-06-08 18:14:44 -07001/* bnx2i.c: Broadcom NetXtreme II iSCSI driver.
2 *
Eddie Wai11cec1e2010-11-23 15:29:31 -08003 * Copyright (c) 2006 - 2010 Broadcom Corporation
Michael Chancf4e6362009-06-08 18:14:44 -07004 * 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)
Eddie Wai11cec1e2010-11-23 15:29:31 -080012 * Maintained by: Eddie Wai (eddie.wai@broadcom.com)
Michael Chancf4e6362009-06-08 18:14:44 -070013 */
14
15#include "bnx2i.h"
16
17static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
18static u32 adapter_count;
Michael Chancf4e6362009-06-08 18:14:44 -070019
20#define DRV_MODULE_NAME "bnx2i"
Eddie Wai3db86352010-11-23 15:29:32 -080021#define DRV_MODULE_VERSION "2.6.2.2"
22#define DRV_MODULE_RELDATE "Nov 23, 2010"
Michael Chancf4e6362009-06-08 18:14:44 -070023
24static char version[] __devinitdata =
25 "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
26 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
27
28
Eddie Waifc919612010-08-12 16:44:31 -070029MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com> and "
30 "Eddie Wai <eddie.wai@broadcom.com>");
31
Anil Veerabhadrappa457549d2010-03-25 10:54:44 -070032MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709/57710/57711"
33 " iSCSI Driver");
Michael Chancf4e6362009-06-08 18:14:44 -070034MODULE_LICENSE("GPL");
35MODULE_VERSION(DRV_MODULE_VERSION);
36
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -070037static DEFINE_MUTEX(bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -070038
Anil Veerabhadrappa87761932009-12-07 11:40:18 -080039unsigned int event_coal_min = 24;
40module_param(event_coal_min, int, 0664);
41MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
42
Michael Chancf4e6362009-06-08 18:14:44 -070043unsigned int event_coal_div = 1;
44module_param(event_coal_div, int, 0664);
45MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
46
47unsigned int en_tcp_dack = 1;
48module_param(en_tcp_dack, int, 0664);
49MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
50
51unsigned int error_mask1 = 0x00;
52module_param(error_mask1, int, 0664);
53MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
54
55unsigned int error_mask2 = 0x00;
56module_param(error_mask2, int, 0664);
57MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
58
59unsigned int sq_size;
60module_param(sq_size, int, 0664);
61MODULE_PARM_DESC(sq_size, "Configure SQ size");
62
63unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
64module_param(rq_size, int, 0664);
65MODULE_PARM_DESC(rq_size, "Configure RQ size");
66
67u64 iscsi_error_mask = 0x00;
68
Michael Chancf4e6362009-06-08 18:14:44 -070069
70/**
71 * bnx2i_identify_device - identifies NetXtreme II device type
72 * @hba: Adapter structure pointer
73 *
74 * This function identifies the NX2 device type and sets appropriate
75 * queue mailbox register access method, 5709 requires driver to
76 * access MBOX regs using *bin* mode
77 */
78void bnx2i_identify_device(struct bnx2i_hba *hba)
79{
80 hba->cnic_dev_type = 0;
81 if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
82 (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
83 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
84 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
85 (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
86 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
87 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
88 (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
89 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
90 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
91 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
Anil Veerabhadrappa5d9e1fa2009-12-07 11:39:33 -080092 hba->pci_did == PCI_DEVICE_ID_NX2_57711 ||
93 hba->pci_did == PCI_DEVICE_ID_NX2_57711E)
Michael Chancf4e6362009-06-08 18:14:44 -070094 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
Anil Veerabhadrappa5d9e1fa2009-12-07 11:39:33 -080095 else
96 printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
97 hba->pci_did);
Michael Chancf4e6362009-06-08 18:14:44 -070098}
99
100
101/**
102 * get_adapter_list_head - returns head of adapter list
103 */
104struct bnx2i_hba *get_adapter_list_head(void)
105{
106 struct bnx2i_hba *hba = NULL;
107 struct bnx2i_hba *tmp_hba;
108
109 if (!adapter_count)
110 goto hba_not_found;
111
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700112 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700113 list_for_each_entry(tmp_hba, &adapter_list, link) {
114 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
115 hba = tmp_hba;
116 break;
117 }
118 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700119 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700120hba_not_found:
121 return hba;
122}
123
124
125/**
126 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
127 * @cnic: pointer to cnic device instance
128 *
129 */
130struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
131{
132 struct bnx2i_hba *hba, *temp;
133
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700134 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700135 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
136 if (hba->cnic == cnic) {
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700137 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700138 return hba;
139 }
140 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700141 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700142 return NULL;
143}
144
145
146/**
147 * bnx2i_start - cnic callback to initialize & start adapter instance
148 * @handle: transparent handle pointing to adapter structure
149 *
150 * This function maps adapter structure to pcidev structure and initiates
151 * firmware handshake to enable/initialize on chip iscsi components
152 * This bnx2i - cnic interface api callback is issued after following
153 * 2 conditions are met -
154 * a) underlying network interface is up (marked by event 'NETDEV_UP'
155 * from netdev
156 * b) bnx2i adapter instance is registered
157 */
158void bnx2i_start(void *handle)
159{
160#define BNX2I_INIT_POLL_TIME (1000 / HZ)
161 struct bnx2i_hba *hba = handle;
162 int i = HZ;
163
Eddie Waia977d2c2011-02-16 15:04:27 -0600164 if (!hba->cnic->max_iscsi_conn) {
165 printk(KERN_ALERT "bnx2i: dev %s does not support "
166 "iSCSI\n", hba->netdev->name);
167
168 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
169 mutex_lock(&bnx2i_dev_lock);
170 list_del_init(&hba->link);
171 adapter_count--;
172 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
173 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
174 mutex_unlock(&bnx2i_dev_lock);
175 bnx2i_free_hba(hba);
176 }
177 return;
178 }
Michael Chancf4e6362009-06-08 18:14:44 -0700179 bnx2i_send_fw_iscsi_init_msg(hba);
180 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
181 msleep(BNX2I_INIT_POLL_TIME);
182}
183
184
185/**
Eddie Wai250ae982010-08-12 16:44:30 -0700186 * bnx2i_chip_cleanup - local routine to handle chip cleanup
187 * @hba: Adapter instance to register
188 *
189 * Driver checks if adapter still has any active connections before
190 * executing the cleanup process
191 */
192static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
193{
194 struct bnx2i_endpoint *bnx2i_ep;
195 struct list_head *pos, *tmp;
196
197 if (hba->ofld_conns_active) {
198 /* Stage to force the disconnection
199 * This is the case where the daemon is either slow or
200 * not present
201 */
202 printk(KERN_ALERT "bnx2i: (%s) chip cleanup for %d active "
203 "connections\n", hba->netdev->name,
204 hba->ofld_conns_active);
205 mutex_lock(&hba->net_dev_lock);
206 list_for_each_safe(pos, tmp, &hba->ep_active_list) {
207 bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
208 /* Clean up the chip only */
209 bnx2i_hw_ep_disconnect(bnx2i_ep);
210 bnx2i_ep->cm_sk = NULL;
211 }
212 mutex_unlock(&hba->net_dev_lock);
213 }
214}
215
216
217/**
Michael Chancf4e6362009-06-08 18:14:44 -0700218 * bnx2i_stop - cnic callback to shutdown adapter instance
219 * @handle: transparent handle pointing to adapter structure
220 *
221 * driver checks if adapter is already in shutdown mode, if not start
222 * the shutdown process
223 */
224void bnx2i_stop(void *handle)
225{
226 struct bnx2i_hba *hba = handle;
Eddie Wai55e15c92010-07-01 15:34:52 -0700227 int conns_active;
Eddie Wai842158d2010-11-23 15:29:28 -0800228 int wait_delay = 1 * HZ;
Michael Chancf4e6362009-06-08 18:14:44 -0700229
230 /* check if cleanup happened in GOING_DOWN context */
Eddie Wai842158d2010-11-23 15:29:28 -0800231 if (!test_and_set_bit(ADAPTER_STATE_GOING_DOWN,
232 &hba->adapter_state)) {
Michael Chancf4e6362009-06-08 18:14:44 -0700233 iscsi_host_for_each_session(hba->shost,
234 bnx2i_drop_session);
Eddie Wai842158d2010-11-23 15:29:28 -0800235 wait_delay = hba->hba_shutdown_tmo;
236 }
237 /* Wait for inflight offload connection tasks to complete before
238 * proceeding. Forcefully terminate all connection recovery in
239 * progress at the earliest, either in bind(), send_pdu(LOGIN),
240 * or conn_start()
241 */
242 wait_event_interruptible_timeout(hba->eh_wait,
243 (list_empty(&hba->ep_ofld_list) &&
244 list_empty(&hba->ep_destroy_list)),
245 10 * HZ);
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700246 /* Wait for all endpoints to be torn down, Chip will be reset once
247 * control returns to network driver. So it is required to cleanup and
248 * release all connection resources before returning from this routine.
249 */
Eddie Wai55e15c92010-07-01 15:34:52 -0700250 while (hba->ofld_conns_active) {
251 conns_active = hba->ofld_conns_active;
252 wait_event_interruptible_timeout(hba->eh_wait,
253 (hba->ofld_conns_active != conns_active),
Eddie Wai842158d2010-11-23 15:29:28 -0800254 wait_delay);
Eddie Wai55e15c92010-07-01 15:34:52 -0700255 if (hba->ofld_conns_active == conns_active)
256 break;
257 }
Eddie Wai250ae982010-08-12 16:44:30 -0700258 bnx2i_chip_cleanup(hba);
Eddie Wai55e15c92010-07-01 15:34:52 -0700259
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700260 /* This flag should be cleared last so that ep_disconnect() gracefully
261 * cleans up connection context
262 */
Eddie Wai842158d2010-11-23 15:29:28 -0800263 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700264 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
Michael Chancf4e6362009-06-08 18:14:44 -0700265}
266
Eddie Wai842158d2010-11-23 15:29:28 -0800267
Michael Chancf4e6362009-06-08 18:14:44 -0700268/**
Michael Chancf4e6362009-06-08 18:14:44 -0700269 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
270 * @hba: bnx2i adapter instance
271 * @cnic: cnic device handle
272 *
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700273 * Global resource lock is held during critical sections below. This routine is
274 * called from either cnic_register_driver() or device hot plug context and
275 * and does majority of device specific initialization
Michael Chancf4e6362009-06-08 18:14:44 -0700276 */
277static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
278{
279 int rc;
280
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700281 mutex_lock(&bnx2i_dev_lock);
Anil Veerabhadrappace2d7632010-03-25 10:54:42 -0700282 hba->cnic = cnic;
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700283 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
284 if (!rc) {
Michael Chancf4e6362009-06-08 18:14:44 -0700285 hba->age++;
Michael Chancf4e6362009-06-08 18:14:44 -0700286 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700287 list_add_tail(&hba->link, &adapter_list);
288 adapter_count++;
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700289 } else if (rc == -EBUSY) /* duplicate registration */
290 printk(KERN_ALERT "bnx2i, duplicate registration"
291 "hba=%p, cnic=%p\n", hba, cnic);
292 else if (rc == -EAGAIN)
293 printk(KERN_ERR "bnx2i, driver not registered\n");
294 else if (rc == -EINVAL)
295 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
296 else
297 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
Michael Chancf4e6362009-06-08 18:14:44 -0700298
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700299 mutex_unlock(&bnx2i_dev_lock);
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700300
301 return rc;
Michael Chancf4e6362009-06-08 18:14:44 -0700302}
303
304
305/**
306 * bnx2i_ulp_init - initialize an adapter instance
307 * @dev: cnic device handle
308 *
309 * Called from cnic_register_driver() context to initialize all enumerated
310 * cnic devices. This routine allocate adapter structure and other
311 * device specific resources.
312 */
313void bnx2i_ulp_init(struct cnic_dev *dev)
314{
315 struct bnx2i_hba *hba;
316
317 /* Allocate a HBA structure for this device */
318 hba = bnx2i_alloc_hba(dev);
319 if (!hba) {
320 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
321 return;
322 }
323
324 /* Get PCI related information and update hba struct members */
325 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
326 if (bnx2i_init_one(hba, dev)) {
327 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
328 bnx2i_free_hba(hba);
Anil Veerabhadrappace2d7632010-03-25 10:54:42 -0700329 }
Michael Chancf4e6362009-06-08 18:14:44 -0700330}
331
332
333/**
334 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
335 * @dev: cnic device handle
336 *
337 */
338void bnx2i_ulp_exit(struct cnic_dev *dev)
339{
340 struct bnx2i_hba *hba;
341
342 hba = bnx2i_find_hba_for_cnic(dev);
343 if (!hba) {
344 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
345 "found, dev 0x%p\n", dev);
346 return;
347 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700348 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700349 list_del_init(&hba->link);
350 adapter_count--;
351
352 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
353 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
354 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700355 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700356 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700357
358 bnx2i_free_hba(hba);
359}
360
361
362/**
363 * bnx2i_mod_init - module init entry point
364 *
365 * initialize any driver wide global data structures such as endpoint pool,
366 * tcp port manager/queue, sysfs. finally driver will register itself
367 * with the cnic module
368 */
369static int __init bnx2i_mod_init(void)
370{
371 int err;
372
373 printk(KERN_INFO "%s", version);
374
Anil Veerabhadrappaf8c9abe2009-12-07 11:39:54 -0800375 if (sq_size && !is_power_of_2(sq_size))
Michael Chancf4e6362009-06-08 18:14:44 -0700376 sq_size = roundup_pow_of_two(sq_size);
377
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700378 mutex_init(&bnx2i_dev_lock);
379
Michael Chancf4e6362009-06-08 18:14:44 -0700380 bnx2i_scsi_xport_template =
381 iscsi_register_transport(&bnx2i_iscsi_transport);
382 if (!bnx2i_scsi_xport_template) {
383 printk(KERN_ERR "Could not register bnx2i transport.\n");
384 err = -ENOMEM;
385 goto out;
386 }
387
388 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
389 if (err) {
390 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
391 goto unreg_xport;
392 }
393
394 return 0;
395
396unreg_xport:
397 iscsi_unregister_transport(&bnx2i_iscsi_transport);
398out:
399 return err;
400}
401
402
403/**
404 * bnx2i_mod_exit - module cleanup/exit entry point
405 *
406 * Global resource lock and host adapter lock is held during critical sections
407 * in this function. Driver will browse through the adapter list, cleans-up
408 * each instance, unregisters iscsi transport name and finally driver will
409 * unregister itself with the cnic module
410 */
411static void __exit bnx2i_mod_exit(void)
412{
413 struct bnx2i_hba *hba;
414
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700415 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700416 while (!list_empty(&adapter_list)) {
417 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
418 list_del(&hba->link);
419 adapter_count--;
420
421 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
Eddie Wai250ae982010-08-12 16:44:30 -0700422 bnx2i_chip_cleanup(hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700423 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
424 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700425 }
426
Michael Chancf4e6362009-06-08 18:14:44 -0700427 bnx2i_free_hba(hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700428 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700429 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700430
431 iscsi_unregister_transport(&bnx2i_iscsi_transport);
432 cnic_unregister_driver(CNIC_ULP_ISCSI);
433}
434
435module_init(bnx2i_mod_init);
436module_exit(bnx2i_mod_exit);