bnx2i_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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.1d"
  18. #define DRV_MODULE_RELDATE "Mar 25, 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_RWLOCK(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. read_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. read_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. read_lock(&bnx2i_dev_lock);
  101. list_for_each_entry_safe(hba, temp, &adapter_list, link) {
  102. if (hba->cnic == cnic) {
  103. read_unlock(&bnx2i_dev_lock);
  104. return hba;
  105. }
  106. }
  107. read_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. if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
  158. test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  159. return;
  160. }
  161. hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
  162. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  163. }
  164. /**
  165. * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
  166. *
  167. * registers all bnx2i adapter instances with the cnic driver while holding
  168. * the global resource lock
  169. */
  170. void bnx2i_reg_dev_all(void)
  171. {
  172. struct bnx2i_hba *hba, *temp;
  173. read_lock(&bnx2i_dev_lock);
  174. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  175. bnx2i_register_device(hba);
  176. read_unlock(&bnx2i_dev_lock);
  177. }
  178. /**
  179. * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
  180. * @hba: Adapter instance to unregister
  181. *
  182. * registers bnx2i adapter instance with the cnic driver while holding
  183. * the adapter structure lock
  184. */
  185. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
  186. {
  187. if (hba->ofld_conns_active ||
  188. !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
  189. test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
  190. return;
  191. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  192. /* ep_disconnect could come before NETDEV_DOWN, driver won't
  193. * see NETDEV_DOWN as it already unregistered itself.
  194. */
  195. hba->adapter_state = 0;
  196. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  197. }
  198. /**
  199. * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
  200. *
  201. * unregisters all bnx2i adapter instances with the cnic driver while holding
  202. * the global resource lock
  203. */
  204. void bnx2i_unreg_dev_all(void)
  205. {
  206. struct bnx2i_hba *hba, *temp;
  207. read_lock(&bnx2i_dev_lock);
  208. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  209. bnx2i_unreg_one_device(hba);
  210. read_unlock(&bnx2i_dev_lock);
  211. }
  212. /**
  213. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  214. * @hba: bnx2i adapter instance
  215. * @cnic: cnic device handle
  216. *
  217. * Global resource lock and host adapter lock is held during critical sections
  218. * below. This routine is called from cnic_register_driver() context and
  219. * work horse thread which does majority of device specific initialization
  220. */
  221. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  222. {
  223. int rc;
  224. read_lock(&bnx2i_dev_lock);
  225. if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  226. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  227. if (rc) /* duplicate registration */
  228. printk(KERN_ERR "bnx2i- dev reg failed\n");
  229. hba->age++;
  230. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  231. }
  232. read_unlock(&bnx2i_dev_lock);
  233. write_lock(&bnx2i_dev_lock);
  234. list_add_tail(&hba->link, &adapter_list);
  235. adapter_count++;
  236. write_unlock(&bnx2i_dev_lock);
  237. return 0;
  238. }
  239. /**
  240. * bnx2i_ulp_init - initialize an adapter instance
  241. * @dev: cnic device handle
  242. *
  243. * Called from cnic_register_driver() context to initialize all enumerated
  244. * cnic devices. This routine allocate adapter structure and other
  245. * device specific resources.
  246. */
  247. void bnx2i_ulp_init(struct cnic_dev *dev)
  248. {
  249. struct bnx2i_hba *hba;
  250. /* Allocate a HBA structure for this device */
  251. hba = bnx2i_alloc_hba(dev);
  252. if (!hba) {
  253. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  254. return;
  255. }
  256. /* Get PCI related information and update hba struct members */
  257. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  258. if (bnx2i_init_one(hba, dev)) {
  259. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  260. bnx2i_free_hba(hba);
  261. } else
  262. hba->cnic = dev;
  263. }
  264. /**
  265. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  266. * @dev: cnic device handle
  267. *
  268. */
  269. void bnx2i_ulp_exit(struct cnic_dev *dev)
  270. {
  271. struct bnx2i_hba *hba;
  272. hba = bnx2i_find_hba_for_cnic(dev);
  273. if (!hba) {
  274. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  275. "found, dev 0x%p\n", dev);
  276. return;
  277. }
  278. write_lock(&bnx2i_dev_lock);
  279. list_del_init(&hba->link);
  280. adapter_count--;
  281. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  282. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  283. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  284. }
  285. write_unlock(&bnx2i_dev_lock);
  286. bnx2i_free_hba(hba);
  287. }
  288. /**
  289. * bnx2i_mod_init - module init entry point
  290. *
  291. * initialize any driver wide global data structures such as endpoint pool,
  292. * tcp port manager/queue, sysfs. finally driver will register itself
  293. * with the cnic module
  294. */
  295. static int __init bnx2i_mod_init(void)
  296. {
  297. int err;
  298. printk(KERN_INFO "%s", version);
  299. if (!is_power_of_2(sq_size))
  300. sq_size = roundup_pow_of_two(sq_size);
  301. bnx2i_scsi_xport_template =
  302. iscsi_register_transport(&bnx2i_iscsi_transport);
  303. if (!bnx2i_scsi_xport_template) {
  304. printk(KERN_ERR "Could not register bnx2i transport.\n");
  305. err = -ENOMEM;
  306. goto out;
  307. }
  308. err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
  309. if (err) {
  310. printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
  311. goto unreg_xport;
  312. }
  313. return 0;
  314. unreg_xport:
  315. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  316. out:
  317. return err;
  318. }
  319. /**
  320. * bnx2i_mod_exit - module cleanup/exit entry point
  321. *
  322. * Global resource lock and host adapter lock is held during critical sections
  323. * in this function. Driver will browse through the adapter list, cleans-up
  324. * each instance, unregisters iscsi transport name and finally driver will
  325. * unregister itself with the cnic module
  326. */
  327. static void __exit bnx2i_mod_exit(void)
  328. {
  329. struct bnx2i_hba *hba;
  330. write_lock(&bnx2i_dev_lock);
  331. while (!list_empty(&adapter_list)) {
  332. hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
  333. list_del(&hba->link);
  334. adapter_count--;
  335. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  336. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  337. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  338. }
  339. write_unlock(&bnx2i_dev_lock);
  340. bnx2i_free_hba(hba);
  341. write_lock(&bnx2i_dev_lock);
  342. }
  343. write_unlock(&bnx2i_dev_lock);
  344. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  345. cnic_unregister_driver(CNIC_ULP_ISCSI);
  346. }
  347. module_init(bnx2i_mod_init);
  348. module_exit(bnx2i_mod_exit);