bnx2i_init.c 12 KB

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