bnx2i_init.c 11 KB

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