bnx2i_init.c 11 KB

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