PageRenderTime 21ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/bnx2i/bnx2i_init.c

https://github.com/xdabravoteam/cm-kernel
C | 445 lines | 247 code | 75 blank | 123 comment | 48 complexity | ab744234243ea8bd01dc2fd21fda690b MD5 | raw file
  1. /* 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. #include "bnx2i.h"
  14. static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
  15. static u32 adapter_count;
  16. #define DRV_MODULE_NAME "bnx2i"
  17. #define DRV_MODULE_VERSION "2.1.0"
  18. #define DRV_MODULE_RELDATE "Dec 06, 2009"
  19. static char version[] __devinitdata =
  20. "Broadcom NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
  21. " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  22. MODULE_AUTHOR("Anil Veerabhadrappa <anilgv@broadcom.com>");
  23. MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709 iSCSI Driver");
  24. MODULE_LICENSE("GPL");
  25. MODULE_VERSION(DRV_MODULE_VERSION);
  26. static DEFINE_MUTEX(bnx2i_dev_lock);
  27. unsigned int event_coal_min = 24;
  28. module_param(event_coal_min, int, 0664);
  29. MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
  30. unsigned int event_coal_div = 1;
  31. module_param(event_coal_div, int, 0664);
  32. MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
  33. unsigned int en_tcp_dack = 1;
  34. module_param(en_tcp_dack, int, 0664);
  35. MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
  36. unsigned int error_mask1 = 0x00;
  37. module_param(error_mask1, int, 0664);
  38. MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
  39. unsigned int error_mask2 = 0x00;
  40. module_param(error_mask2, int, 0664);
  41. MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
  42. unsigned int sq_size;
  43. module_param(sq_size, int, 0664);
  44. MODULE_PARM_DESC(sq_size, "Configure SQ size");
  45. unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
  46. module_param(rq_size, int, 0664);
  47. MODULE_PARM_DESC(rq_size, "Configure RQ size");
  48. u64 iscsi_error_mask = 0x00;
  49. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
  50. /**
  51. * bnx2i_identify_device - identifies NetXtreme II device type
  52. * @hba: Adapter structure pointer
  53. *
  54. * This function identifies the NX2 device type and sets appropriate
  55. * queue mailbox register access method, 5709 requires driver to
  56. * access MBOX regs using *bin* mode
  57. */
  58. void bnx2i_identify_device(struct bnx2i_hba *hba)
  59. {
  60. hba->cnic_dev_type = 0;
  61. if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
  62. (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
  63. set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
  64. else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
  65. (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
  66. set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
  67. else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
  68. (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
  69. set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
  70. hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
  71. } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
  72. hba->pci_did == PCI_DEVICE_ID_NX2_57711 ||
  73. hba->pci_did == PCI_DEVICE_ID_NX2_57711E)
  74. set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
  75. else
  76. printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
  77. hba->pci_did);
  78. }
  79. /**
  80. * get_adapter_list_head - returns head of adapter list
  81. */
  82. struct bnx2i_hba *get_adapter_list_head(void)
  83. {
  84. struct bnx2i_hba *hba = NULL;
  85. struct bnx2i_hba *tmp_hba;
  86. if (!adapter_count)
  87. goto hba_not_found;
  88. mutex_lock(&bnx2i_dev_lock);
  89. list_for_each_entry(tmp_hba, &adapter_list, link) {
  90. if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
  91. hba = tmp_hba;
  92. break;
  93. }
  94. }
  95. mutex_unlock(&bnx2i_dev_lock);
  96. hba_not_found:
  97. return hba;
  98. }
  99. /**
  100. * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
  101. * @cnic: pointer to cnic device instance
  102. *
  103. */
  104. struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
  105. {
  106. struct bnx2i_hba *hba, *temp;
  107. mutex_lock(&bnx2i_dev_lock);
  108. list_for_each_entry_safe(hba, temp, &adapter_list, link) {
  109. if (hba->cnic == cnic) {
  110. mutex_unlock(&bnx2i_dev_lock);
  111. return hba;
  112. }
  113. }
  114. mutex_unlock(&bnx2i_dev_lock);
  115. return NULL;
  116. }
  117. /**
  118. * bnx2i_start - cnic callback to initialize & start adapter instance
  119. * @handle: transparent handle pointing to adapter structure
  120. *
  121. * This function maps adapter structure to pcidev structure and initiates
  122. * firmware handshake to enable/initialize on chip iscsi components
  123. * This bnx2i - cnic interface api callback is issued after following
  124. * 2 conditions are met -
  125. * a) underlying network interface is up (marked by event 'NETDEV_UP'
  126. * from netdev
  127. * b) bnx2i adapter instance is registered
  128. */
  129. void bnx2i_start(void *handle)
  130. {
  131. #define BNX2I_INIT_POLL_TIME (1000 / HZ)
  132. struct bnx2i_hba *hba = handle;
  133. int i = HZ;
  134. bnx2i_send_fw_iscsi_init_msg(hba);
  135. while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
  136. msleep(BNX2I_INIT_POLL_TIME);
  137. }
  138. /**
  139. * bnx2i_stop - cnic callback to shutdown adapter instance
  140. * @handle: transparent handle pointing to adapter structure
  141. *
  142. * driver checks if adapter is already in shutdown mode, if not start
  143. * the shutdown process
  144. */
  145. void bnx2i_stop(void *handle)
  146. {
  147. struct bnx2i_hba *hba = handle;
  148. /* check if cleanup happened in GOING_DOWN context */
  149. if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
  150. &hba->adapter_state))
  151. iscsi_host_for_each_session(hba->shost,
  152. bnx2i_drop_session);
  153. /* Wait for all endpoints to be torn down, Chip will be reset once
  154. * control returns to network driver. So it is required to cleanup and
  155. * release all connection resources before returning from this routine.
  156. */
  157. wait_event_interruptible_timeout(hba->eh_wait,
  158. (hba->ofld_conns_active == 0),
  159. hba->hba_shutdown_tmo);
  160. /* This flag should be cleared last so that ep_disconnect() gracefully
  161. * cleans up connection context
  162. */
  163. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  164. }
  165. /**
  166. * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
  167. * @hba: Adapter instance to register
  168. *
  169. * registers bnx2i adapter instance with the cnic driver while holding the
  170. * adapter structure lock
  171. */
  172. void bnx2i_register_device(struct bnx2i_hba *hba)
  173. {
  174. int rc;
  175. if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
  176. test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  177. return;
  178. }
  179. rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
  180. if (!rc)
  181. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  182. }
  183. /**
  184. * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
  185. *
  186. * registers all bnx2i adapter instances with the cnic driver while holding
  187. * the global resource lock
  188. */
  189. void bnx2i_reg_dev_all(void)
  190. {
  191. struct bnx2i_hba *hba, *temp;
  192. mutex_lock(&bnx2i_dev_lock);
  193. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  194. bnx2i_register_device(hba);
  195. mutex_unlock(&bnx2i_dev_lock);
  196. }
  197. /**
  198. * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
  199. * @hba: Adapter instance to unregister
  200. *
  201. * registers bnx2i adapter instance with the cnic driver while holding
  202. * the adapter structure lock
  203. */
  204. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
  205. {
  206. if (hba->ofld_conns_active ||
  207. !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
  208. test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
  209. return;
  210. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  211. /* ep_disconnect could come before NETDEV_DOWN, driver won't
  212. * see NETDEV_DOWN as it already unregistered itself.
  213. */
  214. hba->adapter_state = 0;
  215. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  216. }
  217. /**
  218. * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
  219. *
  220. * unregisters all bnx2i adapter instances with the cnic driver while holding
  221. * the global resource lock
  222. */
  223. void bnx2i_unreg_dev_all(void)
  224. {
  225. struct bnx2i_hba *hba, *temp;
  226. mutex_lock(&bnx2i_dev_lock);
  227. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  228. bnx2i_unreg_one_device(hba);
  229. mutex_unlock(&bnx2i_dev_lock);
  230. }
  231. /**
  232. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  233. * @hba: bnx2i adapter instance
  234. * @cnic: cnic device handle
  235. *
  236. * Global resource lock is held during critical sections below. This routine is
  237. * called from either cnic_register_driver() or device hot plug context and
  238. * and does majority of device specific initialization
  239. */
  240. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  241. {
  242. int rc;
  243. mutex_lock(&bnx2i_dev_lock);
  244. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  245. if (!rc) {
  246. hba->age++;
  247. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  248. list_add_tail(&hba->link, &adapter_list);
  249. adapter_count++;
  250. } else if (rc == -EBUSY) /* duplicate registration */
  251. printk(KERN_ALERT "bnx2i, duplicate registration"
  252. "hba=%p, cnic=%p\n", hba, cnic);
  253. else if (rc == -EAGAIN)
  254. printk(KERN_ERR "bnx2i, driver not registered\n");
  255. else if (rc == -EINVAL)
  256. printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
  257. else
  258. printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
  259. mutex_unlock(&bnx2i_dev_lock);
  260. return rc;
  261. }
  262. /**
  263. * bnx2i_ulp_init - initialize an adapter instance
  264. * @dev: cnic device handle
  265. *
  266. * Called from cnic_register_driver() context to initialize all enumerated
  267. * cnic devices. This routine allocate adapter structure and other
  268. * device specific resources.
  269. */
  270. void bnx2i_ulp_init(struct cnic_dev *dev)
  271. {
  272. struct bnx2i_hba *hba;
  273. /* Allocate a HBA structure for this device */
  274. hba = bnx2i_alloc_hba(dev);
  275. if (!hba) {
  276. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  277. return;
  278. }
  279. /* Get PCI related information and update hba struct members */
  280. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  281. if (bnx2i_init_one(hba, dev)) {
  282. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  283. bnx2i_free_hba(hba);
  284. } else
  285. hba->cnic = dev;
  286. }
  287. /**
  288. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  289. * @dev: cnic device handle
  290. *
  291. */
  292. void bnx2i_ulp_exit(struct cnic_dev *dev)
  293. {
  294. struct bnx2i_hba *hba;
  295. hba = bnx2i_find_hba_for_cnic(dev);
  296. if (!hba) {
  297. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  298. "found, dev 0x%p\n", dev);
  299. return;
  300. }
  301. mutex_lock(&bnx2i_dev_lock);
  302. list_del_init(&hba->link);
  303. adapter_count--;
  304. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  305. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  306. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  307. }
  308. mutex_unlock(&bnx2i_dev_lock);
  309. bnx2i_free_hba(hba);
  310. }
  311. /**
  312. * bnx2i_mod_init - module init entry point
  313. *
  314. * initialize any driver wide global data structures such as endpoint pool,
  315. * tcp port manager/queue, sysfs. finally driver will register itself
  316. * with the cnic module
  317. */
  318. static int __init bnx2i_mod_init(void)
  319. {
  320. int err;
  321. printk(KERN_INFO "%s", version);
  322. if (sq_size && !is_power_of_2(sq_size))
  323. sq_size = roundup_pow_of_two(sq_size);
  324. mutex_init(&bnx2i_dev_lock);
  325. bnx2i_scsi_xport_template =
  326. iscsi_register_transport(&bnx2i_iscsi_transport);
  327. if (!bnx2i_scsi_xport_template) {
  328. printk(KERN_ERR "Could not register bnx2i transport.\n");
  329. err = -ENOMEM;
  330. goto out;
  331. }
  332. err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
  333. if (err) {
  334. printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
  335. goto unreg_xport;
  336. }
  337. return 0;
  338. unreg_xport:
  339. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  340. out:
  341. return err;
  342. }
  343. /**
  344. * bnx2i_mod_exit - module cleanup/exit entry point
  345. *
  346. * Global resource lock and host adapter lock is held during critical sections
  347. * in this function. Driver will browse through the adapter list, cleans-up
  348. * each instance, unregisters iscsi transport name and finally driver will
  349. * unregister itself with the cnic module
  350. */
  351. static void __exit bnx2i_mod_exit(void)
  352. {
  353. struct bnx2i_hba *hba;
  354. mutex_lock(&bnx2i_dev_lock);
  355. while (!list_empty(&adapter_list)) {
  356. hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
  357. list_del(&hba->link);
  358. adapter_count--;
  359. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  360. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  361. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  362. }
  363. bnx2i_free_hba(hba);
  364. }
  365. mutex_unlock(&bnx2i_dev_lock);
  366. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  367. cnic_unregister_driver(CNIC_ULP_ISCSI);
  368. }
  369. module_init(bnx2i_mod_init);
  370. module_exit(bnx2i_mod_exit);