bnx2i_init.c 12 KB

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