bnx2i_init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  150. if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
  151. &hba->adapter_state))
  152. iscsi_host_for_each_session(hba->shost,
  153. bnx2i_drop_session);
  154. }
  155. /**
  156. * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
  157. * @hba: Adapter instance to register
  158. *
  159. * registers bnx2i adapter instance with the cnic driver while holding the
  160. * adapter structure lock
  161. */
  162. void bnx2i_register_device(struct bnx2i_hba *hba)
  163. {
  164. int rc;
  165. if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
  166. test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  167. return;
  168. }
  169. rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
  170. if (!rc)
  171. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  172. }
  173. /**
  174. * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
  175. *
  176. * registers all bnx2i adapter instances with the cnic driver while holding
  177. * the global resource lock
  178. */
  179. void bnx2i_reg_dev_all(void)
  180. {
  181. struct bnx2i_hba *hba, *temp;
  182. mutex_lock(&bnx2i_dev_lock);
  183. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  184. bnx2i_register_device(hba);
  185. mutex_unlock(&bnx2i_dev_lock);
  186. }
  187. /**
  188. * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
  189. * @hba: Adapter instance to unregister
  190. *
  191. * registers bnx2i adapter instance with the cnic driver while holding
  192. * the adapter structure lock
  193. */
  194. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
  195. {
  196. if (hba->ofld_conns_active ||
  197. !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
  198. test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
  199. return;
  200. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  201. /* ep_disconnect could come before NETDEV_DOWN, driver won't
  202. * see NETDEV_DOWN as it already unregistered itself.
  203. */
  204. hba->adapter_state = 0;
  205. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  206. }
  207. /**
  208. * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
  209. *
  210. * unregisters all bnx2i adapter instances with the cnic driver while holding
  211. * the global resource lock
  212. */
  213. void bnx2i_unreg_dev_all(void)
  214. {
  215. struct bnx2i_hba *hba, *temp;
  216. mutex_lock(&bnx2i_dev_lock);
  217. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  218. bnx2i_unreg_one_device(hba);
  219. mutex_unlock(&bnx2i_dev_lock);
  220. }
  221. /**
  222. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  223. * @hba: bnx2i adapter instance
  224. * @cnic: cnic device handle
  225. *
  226. * Global resource lock is held during critical sections below. This routine is
  227. * called from either cnic_register_driver() or device hot plug context and
  228. * and does majority of device specific initialization
  229. */
  230. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  231. {
  232. int rc;
  233. mutex_lock(&bnx2i_dev_lock);
  234. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  235. if (!rc) {
  236. hba->age++;
  237. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  238. list_add_tail(&hba->link, &adapter_list);
  239. adapter_count++;
  240. } else if (rc == -EBUSY) /* duplicate registration */
  241. printk(KERN_ALERT "bnx2i, duplicate registration"
  242. "hba=%p, cnic=%p\n", hba, cnic);
  243. else if (rc == -EAGAIN)
  244. printk(KERN_ERR "bnx2i, driver not registered\n");
  245. else if (rc == -EINVAL)
  246. printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
  247. else
  248. printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
  249. mutex_unlock(&bnx2i_dev_lock);
  250. return rc;
  251. }
  252. /**
  253. * bnx2i_ulp_init - initialize an adapter instance
  254. * @dev: cnic device handle
  255. *
  256. * Called from cnic_register_driver() context to initialize all enumerated
  257. * cnic devices. This routine allocate adapter structure and other
  258. * device specific resources.
  259. */
  260. void bnx2i_ulp_init(struct cnic_dev *dev)
  261. {
  262. struct bnx2i_hba *hba;
  263. /* Allocate a HBA structure for this device */
  264. hba = bnx2i_alloc_hba(dev);
  265. if (!hba) {
  266. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  267. return;
  268. }
  269. /* Get PCI related information and update hba struct members */
  270. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  271. if (bnx2i_init_one(hba, dev)) {
  272. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  273. bnx2i_free_hba(hba);
  274. } else
  275. hba->cnic = dev;
  276. }
  277. /**
  278. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  279. * @dev: cnic device handle
  280. *
  281. */
  282. void bnx2i_ulp_exit(struct cnic_dev *dev)
  283. {
  284. struct bnx2i_hba *hba;
  285. hba = bnx2i_find_hba_for_cnic(dev);
  286. if (!hba) {
  287. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  288. "found, dev 0x%p\n", dev);
  289. return;
  290. }
  291. mutex_lock(&bnx2i_dev_lock);
  292. list_del_init(&hba->link);
  293. adapter_count--;
  294. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  295. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  296. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  297. }
  298. mutex_unlock(&bnx2i_dev_lock);
  299. bnx2i_free_hba(hba);
  300. }
  301. /**
  302. * bnx2i_mod_init - module init entry point
  303. *
  304. * initialize any driver wide global data structures such as endpoint pool,
  305. * tcp port manager/queue, sysfs. finally driver will register itself
  306. * with the cnic module
  307. */
  308. static int __init bnx2i_mod_init(void)
  309. {
  310. int err;
  311. printk(KERN_INFO "%s", version);
  312. if (sq_size && !is_power_of_2(sq_size))
  313. sq_size = roundup_pow_of_two(sq_size);
  314. mutex_init(&bnx2i_dev_lock);
  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. mutex_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. }
  353. bnx2i_free_hba(hba);
  354. }
  355. mutex_unlock(&bnx2i_dev_lock);
  356. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  357. cnic_unregister_driver(CNIC_ULP_ISCSI);
  358. }
  359. module_init(bnx2i_mod_init);
  360. module_exit(bnx2i_mod_exit);