blob: 0307f85b4e2eff84824515e9a33033798e41e4e7 [file] [log] [blame]
Michael Chancf4e6362009-06-08 18:14:44 -07001/* bnx2i.c: Broadcom NetXtreme II iSCSI driver.
2 *
3 * Copyright (c) 2006 - 2009 Broadcom Corporation
4 * 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)
12 */
13
14#include "bnx2i.h"
15
16static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
17static u32 adapter_count;
Michael Chancf4e6362009-06-08 18:14:44 -070018
19#define DRV_MODULE_NAME "bnx2i"
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -070020#define DRV_MODULE_VERSION "2.0.1e"
21#define DRV_MODULE_RELDATE "June 22, 2009"
Michael Chancf4e6362009-06-08 18:14:44 -070022
23static char version[] __devinitdata =
24 "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
25 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
26
27
28MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com>");
29MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709 iSCSI Driver");
30MODULE_LICENSE("GPL");
31MODULE_VERSION(DRV_MODULE_VERSION);
32
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -070033static DEFINE_MUTEX(bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -070034
35unsigned int event_coal_div = 1;
36module_param(event_coal_div, int, 0664);
37MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
38
39unsigned int en_tcp_dack = 1;
40module_param(en_tcp_dack, int, 0664);
41MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
42
43unsigned int error_mask1 = 0x00;
44module_param(error_mask1, int, 0664);
45MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
46
47unsigned int error_mask2 = 0x00;
48module_param(error_mask2, int, 0664);
49MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
50
51unsigned int sq_size;
52module_param(sq_size, int, 0664);
53MODULE_PARM_DESC(sq_size, "Configure SQ size");
54
55unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
56module_param(rq_size, int, 0664);
57MODULE_PARM_DESC(rq_size, "Configure RQ size");
58
59u64 iscsi_error_mask = 0x00;
60
61static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
62
63
64/**
65 * bnx2i_identify_device - identifies NetXtreme II device type
66 * @hba: Adapter structure pointer
67 *
68 * This function identifies the NX2 device type and sets appropriate
69 * queue mailbox register access method, 5709 requires driver to
70 * access MBOX regs using *bin* mode
71 */
72void bnx2i_identify_device(struct bnx2i_hba *hba)
73{
74 hba->cnic_dev_type = 0;
75 if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
76 (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
77 set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
78 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
79 (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
80 set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
81 else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
82 (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
83 set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
84 hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
85 } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
Anil Veerabhadrappa5d9e1fa2009-12-07 11:39:33 -080086 hba->pci_did == PCI_DEVICE_ID_NX2_57711 ||
87 hba->pci_did == PCI_DEVICE_ID_NX2_57711E)
Michael Chancf4e6362009-06-08 18:14:44 -070088 set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
Anil Veerabhadrappa5d9e1fa2009-12-07 11:39:33 -080089 else
90 printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
91 hba->pci_did);
Michael Chancf4e6362009-06-08 18:14:44 -070092}
93
94
95/**
96 * get_adapter_list_head - returns head of adapter list
97 */
98struct bnx2i_hba *get_adapter_list_head(void)
99{
100 struct bnx2i_hba *hba = NULL;
101 struct bnx2i_hba *tmp_hba;
102
103 if (!adapter_count)
104 goto hba_not_found;
105
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700106 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700107 list_for_each_entry(tmp_hba, &adapter_list, link) {
108 if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
109 hba = tmp_hba;
110 break;
111 }
112 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700113 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700114hba_not_found:
115 return hba;
116}
117
118
119/**
120 * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
121 * @cnic: pointer to cnic device instance
122 *
123 */
124struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
125{
126 struct bnx2i_hba *hba, *temp;
127
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700128 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700129 list_for_each_entry_safe(hba, temp, &adapter_list, link) {
130 if (hba->cnic == cnic) {
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700131 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700132 return hba;
133 }
134 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700135 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700136 return NULL;
137}
138
139
140/**
141 * bnx2i_start - cnic callback to initialize & start adapter instance
142 * @handle: transparent handle pointing to adapter structure
143 *
144 * This function maps adapter structure to pcidev structure and initiates
145 * firmware handshake to enable/initialize on chip iscsi components
146 * This bnx2i - cnic interface api callback is issued after following
147 * 2 conditions are met -
148 * a) underlying network interface is up (marked by event 'NETDEV_UP'
149 * from netdev
150 * b) bnx2i adapter instance is registered
151 */
152void bnx2i_start(void *handle)
153{
154#define BNX2I_INIT_POLL_TIME (1000 / HZ)
155 struct bnx2i_hba *hba = handle;
156 int i = HZ;
157
158 bnx2i_send_fw_iscsi_init_msg(hba);
159 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
160 msleep(BNX2I_INIT_POLL_TIME);
161}
162
163
164/**
165 * bnx2i_stop - cnic callback to shutdown adapter instance
166 * @handle: transparent handle pointing to adapter structure
167 *
168 * driver checks if adapter is already in shutdown mode, if not start
169 * the shutdown process
170 */
171void bnx2i_stop(void *handle)
172{
173 struct bnx2i_hba *hba = handle;
174
175 /* check if cleanup happened in GOING_DOWN context */
176 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
177 if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
178 &hba->adapter_state))
179 iscsi_host_for_each_session(hba->shost,
180 bnx2i_drop_session);
181}
182
183/**
184 * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
185 * @hba: Adapter instance to register
186 *
187 * registers bnx2i adapter instance with the cnic driver while holding the
188 * adapter structure lock
189 */
190void bnx2i_register_device(struct bnx2i_hba *hba)
191{
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -0700192 int rc;
193
Michael Chancf4e6362009-06-08 18:14:44 -0700194 if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
195 test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
196 return;
197 }
198
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -0700199 rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700200
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -0700201 if (!rc)
202 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700203}
204
205
206/**
207 * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
208 *
209 * registers all bnx2i adapter instances with the cnic driver while holding
210 * the global resource lock
211 */
212void bnx2i_reg_dev_all(void)
213{
214 struct bnx2i_hba *hba, *temp;
215
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700216 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700217 list_for_each_entry_safe(hba, temp, &adapter_list, link)
218 bnx2i_register_device(hba);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700219 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700220}
221
222
223/**
224 * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
225 * @hba: Adapter instance to unregister
226 *
227 * registers bnx2i adapter instance with the cnic driver while holding
228 * the adapter structure lock
229 */
230static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
231{
232 if (hba->ofld_conns_active ||
233 !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
234 test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
235 return;
236
237 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
238
Michael Chancf4e6362009-06-08 18:14:44 -0700239 /* ep_disconnect could come before NETDEV_DOWN, driver won't
240 * see NETDEV_DOWN as it already unregistered itself.
241 */
242 hba->adapter_state = 0;
243 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
244}
245
246/**
247 * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
248 *
249 * unregisters all bnx2i adapter instances with the cnic driver while holding
250 * the global resource lock
251 */
252void bnx2i_unreg_dev_all(void)
253{
254 struct bnx2i_hba *hba, *temp;
255
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700256 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700257 list_for_each_entry_safe(hba, temp, &adapter_list, link)
258 bnx2i_unreg_one_device(hba);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700259 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700260}
261
262
263/**
264 * bnx2i_init_one - initialize an adapter instance and allocate memory resources
265 * @hba: bnx2i adapter instance
266 * @cnic: cnic device handle
267 *
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700268 * Global resource lock is held during critical sections below. This routine is
269 * called from either cnic_register_driver() or device hot plug context and
270 * and does majority of device specific initialization
Michael Chancf4e6362009-06-08 18:14:44 -0700271 */
272static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
273{
274 int rc;
275
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700276 mutex_lock(&bnx2i_dev_lock);
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700277 rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
278 if (!rc) {
Michael Chancf4e6362009-06-08 18:14:44 -0700279 hba->age++;
Michael Chancf4e6362009-06-08 18:14:44 -0700280 set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700281 list_add_tail(&hba->link, &adapter_list);
282 adapter_count++;
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700283 } else if (rc == -EBUSY) /* duplicate registration */
284 printk(KERN_ALERT "bnx2i, duplicate registration"
285 "hba=%p, cnic=%p\n", hba, cnic);
286 else if (rc == -EAGAIN)
287 printk(KERN_ERR "bnx2i, driver not registered\n");
288 else if (rc == -EINVAL)
289 printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
290 else
291 printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
Michael Chancf4e6362009-06-08 18:14:44 -0700292
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700293 mutex_unlock(&bnx2i_dev_lock);
Anil Veerabhadrappa4e85f152009-06-23 14:04:23 -0700294
295 return rc;
Michael Chancf4e6362009-06-08 18:14:44 -0700296}
297
298
299/**
300 * bnx2i_ulp_init - initialize an adapter instance
301 * @dev: cnic device handle
302 *
303 * Called from cnic_register_driver() context to initialize all enumerated
304 * cnic devices. This routine allocate adapter structure and other
305 * device specific resources.
306 */
307void bnx2i_ulp_init(struct cnic_dev *dev)
308{
309 struct bnx2i_hba *hba;
310
311 /* Allocate a HBA structure for this device */
312 hba = bnx2i_alloc_hba(dev);
313 if (!hba) {
314 printk(KERN_ERR "bnx2i init: hba initialization failed\n");
315 return;
316 }
317
318 /* Get PCI related information and update hba struct members */
319 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
320 if (bnx2i_init_one(hba, dev)) {
321 printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
322 bnx2i_free_hba(hba);
323 } else
324 hba->cnic = dev;
325}
326
327
328/**
329 * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
330 * @dev: cnic device handle
331 *
332 */
333void bnx2i_ulp_exit(struct cnic_dev *dev)
334{
335 struct bnx2i_hba *hba;
336
337 hba = bnx2i_find_hba_for_cnic(dev);
338 if (!hba) {
339 printk(KERN_INFO "bnx2i_ulp_exit: hba not "
340 "found, dev 0x%p\n", dev);
341 return;
342 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700343 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700344 list_del_init(&hba->link);
345 adapter_count--;
346
347 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
348 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
349 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700350 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700351 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700352
353 bnx2i_free_hba(hba);
354}
355
356
357/**
358 * bnx2i_mod_init - module init entry point
359 *
360 * initialize any driver wide global data structures such as endpoint pool,
361 * tcp port manager/queue, sysfs. finally driver will register itself
362 * with the cnic module
363 */
364static int __init bnx2i_mod_init(void)
365{
366 int err;
367
368 printk(KERN_INFO "%s", version);
369
Anil Veerabhadrappaf8c9abe2009-12-07 11:39:54 -0800370 if (sq_size && !is_power_of_2(sq_size))
Michael Chancf4e6362009-06-08 18:14:44 -0700371 sq_size = roundup_pow_of_two(sq_size);
372
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700373 mutex_init(&bnx2i_dev_lock);
374
Michael Chancf4e6362009-06-08 18:14:44 -0700375 bnx2i_scsi_xport_template =
376 iscsi_register_transport(&bnx2i_iscsi_transport);
377 if (!bnx2i_scsi_xport_template) {
378 printk(KERN_ERR "Could not register bnx2i transport.\n");
379 err = -ENOMEM;
380 goto out;
381 }
382
383 err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
384 if (err) {
385 printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
386 goto unreg_xport;
387 }
388
389 return 0;
390
391unreg_xport:
392 iscsi_unregister_transport(&bnx2i_iscsi_transport);
393out:
394 return err;
395}
396
397
398/**
399 * bnx2i_mod_exit - module cleanup/exit entry point
400 *
401 * Global resource lock and host adapter lock is held during critical sections
402 * in this function. Driver will browse through the adapter list, cleans-up
403 * each instance, unregisters iscsi transport name and finally driver will
404 * unregister itself with the cnic module
405 */
406static void __exit bnx2i_mod_exit(void)
407{
408 struct bnx2i_hba *hba;
409
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700410 mutex_lock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700411 while (!list_empty(&adapter_list)) {
412 hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
413 list_del(&hba->link);
414 adapter_count--;
415
416 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
417 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
418 clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
Michael Chancf4e6362009-06-08 18:14:44 -0700419 }
420
Michael Chancf4e6362009-06-08 18:14:44 -0700421 bnx2i_free_hba(hba);
Michael Chancf4e6362009-06-08 18:14:44 -0700422 }
Anil Veerabhadrappa3be924f2009-06-23 14:07:40 -0700423 mutex_unlock(&bnx2i_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -0700424
425 iscsi_unregister_transport(&bnx2i_iscsi_transport);
426 cnic_unregister_driver(CNIC_ULP_ISCSI);
427}
428
429module_init(bnx2i_mod_init);
430module_exit(bnx2i_mod_exit);