bnx2i_init.c 12 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.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. int wait_delay = 1 * HZ;
  180. /* check if cleanup happened in GOING_DOWN context */
  181. if (!test_and_set_bit(ADAPTER_STATE_GOING_DOWN,
  182. &hba->adapter_state)) {
  183. iscsi_host_for_each_session(hba->shost,
  184. bnx2i_drop_session);
  185. wait_delay = hba->hba_shutdown_tmo;
  186. }
  187. /* Wait for inflight offload connection tasks to complete before
  188. * proceeding. Forcefully terminate all connection recovery in
  189. * progress at the earliest, either in bind(), send_pdu(LOGIN),
  190. * or conn_start()
  191. */
  192. wait_event_interruptible_timeout(hba->eh_wait,
  193. (list_empty(&hba->ep_ofld_list) &&
  194. list_empty(&hba->ep_destroy_list)),
  195. 10 * HZ);
  196. /* Wait for all endpoints to be torn down, Chip will be reset once
  197. * control returns to network driver. So it is required to cleanup and
  198. * release all connection resources before returning from this routine.
  199. */
  200. while (hba->ofld_conns_active) {
  201. conns_active = hba->ofld_conns_active;
  202. wait_event_interruptible_timeout(hba->eh_wait,
  203. (hba->ofld_conns_active != conns_active),
  204. wait_delay);
  205. if (hba->ofld_conns_active == conns_active)
  206. break;
  207. }
  208. bnx2i_chip_cleanup(hba);
  209. /* This flag should be cleared last so that ep_disconnect() gracefully
  210. * cleans up connection context
  211. */
  212. clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  213. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  214. }
  215. /**
  216. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  217. * @hba: bnx2i adapter instance
  218. * @cnic: cnic device handle
  219. *
  220. * Global resource lock is held during critical sections below. This routine is
  221. * called from either cnic_register_driver() or device hot plug context and
  222. * and does majority of device specific initialization
  223. */
  224. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  225. {
  226. int rc;
  227. mutex_lock(&bnx2i_dev_lock);
  228. hba->cnic = cnic;
  229. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  230. if (!rc) {
  231. hba->age++;
  232. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  233. list_add_tail(&hba->link, &adapter_list);
  234. adapter_count++;
  235. } else if (rc == -EBUSY) /* duplicate registration */
  236. printk(KERN_ALERT "bnx2i, duplicate registration"
  237. "hba=%p, cnic=%p\n", hba, cnic);
  238. else if (rc == -EAGAIN)
  239. printk(KERN_ERR "bnx2i, driver not registered\n");
  240. else if (rc == -EINVAL)
  241. printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
  242. else
  243. printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
  244. mutex_unlock(&bnx2i_dev_lock);
  245. return rc;
  246. }
  247. /**
  248. * bnx2i_ulp_init - initialize an adapter instance
  249. * @dev: cnic device handle
  250. *
  251. * Called from cnic_register_driver() context to initialize all enumerated
  252. * cnic devices. This routine allocate adapter structure and other
  253. * device specific resources.
  254. */
  255. void bnx2i_ulp_init(struct cnic_dev *dev)
  256. {
  257. struct bnx2i_hba *hba;
  258. /* Allocate a HBA structure for this device */
  259. hba = bnx2i_alloc_hba(dev);
  260. if (!hba) {
  261. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  262. return;
  263. }
  264. /* Get PCI related information and update hba struct members */
  265. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  266. if (bnx2i_init_one(hba, dev)) {
  267. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  268. bnx2i_free_hba(hba);
  269. }
  270. }
  271. /**
  272. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  273. * @dev: cnic device handle
  274. *
  275. */
  276. void bnx2i_ulp_exit(struct cnic_dev *dev)
  277. {
  278. struct bnx2i_hba *hba;
  279. hba = bnx2i_find_hba_for_cnic(dev);
  280. if (!hba) {
  281. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  282. "found, dev 0x%p\n", dev);
  283. return;
  284. }
  285. mutex_lock(&bnx2i_dev_lock);
  286. list_del_init(&hba->link);
  287. adapter_count--;
  288. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  289. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  290. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  291. }
  292. mutex_unlock(&bnx2i_dev_lock);
  293. bnx2i_free_hba(hba);
  294. }
  295. /**
  296. * bnx2i_mod_init - module init entry point
  297. *
  298. * initialize any driver wide global data structures such as endpoint pool,
  299. * tcp port manager/queue, sysfs. finally driver will register itself
  300. * with the cnic module
  301. */
  302. static int __init bnx2i_mod_init(void)
  303. {
  304. int err;
  305. printk(KERN_INFO "%s", version);
  306. if (sq_size && !is_power_of_2(sq_size))
  307. sq_size = roundup_pow_of_two(sq_size);
  308. mutex_init(&bnx2i_dev_lock);
  309. bnx2i_scsi_xport_template =
  310. iscsi_register_transport(&bnx2i_iscsi_transport);
  311. if (!bnx2i_scsi_xport_template) {
  312. printk(KERN_ERR "Could not register bnx2i transport.\n");
  313. err = -ENOMEM;
  314. goto out;
  315. }
  316. err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
  317. if (err) {
  318. printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
  319. goto unreg_xport;
  320. }
  321. return 0;
  322. unreg_xport:
  323. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  324. out:
  325. return err;
  326. }
  327. /**
  328. * bnx2i_mod_exit - module cleanup/exit entry point
  329. *
  330. * Global resource lock and host adapter lock is held during critical sections
  331. * in this function. Driver will browse through the adapter list, cleans-up
  332. * each instance, unregisters iscsi transport name and finally driver will
  333. * unregister itself with the cnic module
  334. */
  335. static void __exit bnx2i_mod_exit(void)
  336. {
  337. struct bnx2i_hba *hba;
  338. mutex_lock(&bnx2i_dev_lock);
  339. while (!list_empty(&adapter_list)) {
  340. hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
  341. list_del(&hba->link);
  342. adapter_count--;
  343. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  344. bnx2i_chip_cleanup(hba);
  345. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  346. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  347. }
  348. bnx2i_free_hba(hba);
  349. }
  350. mutex_unlock(&bnx2i_dev_lock);
  351. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  352. cnic_unregister_driver(CNIC_ULP_ISCSI);
  353. }
  354. module_init(bnx2i_mod_init);
  355. module_exit(bnx2i_mod_exit);