bnx2i_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
  52. /**
  53. * bnx2i_identify_device - identifies NetXtreme II device type
  54. * @hba: Adapter structure pointer
  55. *
  56. * This function identifies the NX2 device type and sets appropriate
  57. * queue mailbox register access method, 5709 requires driver to
  58. * access MBOX regs using *bin* mode
  59. */
  60. void bnx2i_identify_device(struct bnx2i_hba *hba)
  61. {
  62. hba->cnic_dev_type = 0;
  63. if ((hba->pci_did == PCI_DEVICE_ID_NX2_5706) ||
  64. (hba->pci_did == PCI_DEVICE_ID_NX2_5706S))
  65. set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
  66. else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5708) ||
  67. (hba->pci_did == PCI_DEVICE_ID_NX2_5708S))
  68. set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
  69. else if ((hba->pci_did == PCI_DEVICE_ID_NX2_5709) ||
  70. (hba->pci_did == PCI_DEVICE_ID_NX2_5709S)) {
  71. set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
  72. hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
  73. } else if (hba->pci_did == PCI_DEVICE_ID_NX2_57710 ||
  74. hba->pci_did == PCI_DEVICE_ID_NX2_57711 ||
  75. hba->pci_did == PCI_DEVICE_ID_NX2_57711E)
  76. set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
  77. else
  78. printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
  79. hba->pci_did);
  80. }
  81. /**
  82. * get_adapter_list_head - returns head of adapter list
  83. */
  84. struct bnx2i_hba *get_adapter_list_head(void)
  85. {
  86. struct bnx2i_hba *hba = NULL;
  87. struct bnx2i_hba *tmp_hba;
  88. if (!adapter_count)
  89. goto hba_not_found;
  90. mutex_lock(&bnx2i_dev_lock);
  91. list_for_each_entry(tmp_hba, &adapter_list, link) {
  92. if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
  93. hba = tmp_hba;
  94. break;
  95. }
  96. }
  97. mutex_unlock(&bnx2i_dev_lock);
  98. hba_not_found:
  99. return hba;
  100. }
  101. /**
  102. * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
  103. * @cnic: pointer to cnic device instance
  104. *
  105. */
  106. struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
  107. {
  108. struct bnx2i_hba *hba, *temp;
  109. mutex_lock(&bnx2i_dev_lock);
  110. list_for_each_entry_safe(hba, temp, &adapter_list, link) {
  111. if (hba->cnic == cnic) {
  112. mutex_unlock(&bnx2i_dev_lock);
  113. return hba;
  114. }
  115. }
  116. mutex_unlock(&bnx2i_dev_lock);
  117. return NULL;
  118. }
  119. /**
  120. * bnx2i_start - cnic callback to initialize & start adapter instance
  121. * @handle: transparent handle pointing to adapter structure
  122. *
  123. * This function maps adapter structure to pcidev structure and initiates
  124. * firmware handshake to enable/initialize on chip iscsi components
  125. * This bnx2i - cnic interface api callback is issued after following
  126. * 2 conditions are met -
  127. * a) underlying network interface is up (marked by event 'NETDEV_UP'
  128. * from netdev
  129. * b) bnx2i adapter instance is registered
  130. */
  131. void bnx2i_start(void *handle)
  132. {
  133. #define BNX2I_INIT_POLL_TIME (1000 / HZ)
  134. struct bnx2i_hba *hba = handle;
  135. int i = HZ;
  136. bnx2i_send_fw_iscsi_init_msg(hba);
  137. while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
  138. msleep(BNX2I_INIT_POLL_TIME);
  139. }
  140. /**
  141. * bnx2i_chip_cleanup - local routine to handle chip cleanup
  142. * @hba: Adapter instance to register
  143. *
  144. * Driver checks if adapter still has any active connections before
  145. * executing the cleanup process
  146. */
  147. static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
  148. {
  149. struct bnx2i_endpoint *bnx2i_ep;
  150. struct list_head *pos, *tmp;
  151. if (hba->ofld_conns_active) {
  152. /* Stage to force the disconnection
  153. * This is the case where the daemon is either slow or
  154. * not present
  155. */
  156. printk(KERN_ALERT "bnx2i: (%s) chip cleanup for %d active "
  157. "connections\n", hba->netdev->name,
  158. hba->ofld_conns_active);
  159. mutex_lock(&hba->net_dev_lock);
  160. list_for_each_safe(pos, tmp, &hba->ep_active_list) {
  161. bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
  162. /* Clean up the chip only */
  163. bnx2i_hw_ep_disconnect(bnx2i_ep);
  164. bnx2i_ep->cm_sk = NULL;
  165. }
  166. mutex_unlock(&hba->net_dev_lock);
  167. }
  168. }
  169. /**
  170. * bnx2i_stop - cnic callback to shutdown adapter instance
  171. * @handle: transparent handle pointing to adapter structure
  172. *
  173. * driver checks if adapter is already in shutdown mode, if not start
  174. * the shutdown process
  175. */
  176. void bnx2i_stop(void *handle)
  177. {
  178. struct bnx2i_hba *hba = handle;
  179. int conns_active;
  180. /* check if cleanup happened in GOING_DOWN context */
  181. if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
  182. &hba->adapter_state))
  183. iscsi_host_for_each_session(hba->shost,
  184. bnx2i_drop_session);
  185. /* Wait for all endpoints to be torn down, Chip will be reset once
  186. * control returns to network driver. So it is required to cleanup and
  187. * release all connection resources before returning from this routine.
  188. */
  189. while (hba->ofld_conns_active) {
  190. conns_active = hba->ofld_conns_active;
  191. wait_event_interruptible_timeout(hba->eh_wait,
  192. (hba->ofld_conns_active != conns_active),
  193. hba->hba_shutdown_tmo);
  194. if (hba->ofld_conns_active == conns_active)
  195. break;
  196. }
  197. bnx2i_chip_cleanup(hba);
  198. /* This flag should be cleared last so that ep_disconnect() gracefully
  199. * cleans up connection context
  200. */
  201. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  202. }
  203. /**
  204. * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
  205. * @hba: Adapter instance to register
  206. *
  207. * registers bnx2i adapter instance with the cnic driver while holding the
  208. * adapter structure lock
  209. */
  210. void bnx2i_register_device(struct bnx2i_hba *hba)
  211. {
  212. int rc;
  213. if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
  214. test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  215. return;
  216. }
  217. rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
  218. if (!rc)
  219. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  220. }
  221. /**
  222. * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
  223. *
  224. * registers all bnx2i adapter instances with the cnic driver while holding
  225. * the global resource lock
  226. */
  227. void bnx2i_reg_dev_all(void)
  228. {
  229. struct bnx2i_hba *hba, *temp;
  230. mutex_lock(&bnx2i_dev_lock);
  231. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  232. bnx2i_register_device(hba);
  233. mutex_unlock(&bnx2i_dev_lock);
  234. }
  235. /**
  236. * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
  237. * @hba: Adapter instance to unregister
  238. *
  239. * registers bnx2i adapter instance with the cnic driver while holding
  240. * the adapter structure lock
  241. */
  242. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
  243. {
  244. if (hba->ofld_conns_active ||
  245. !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
  246. test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
  247. return;
  248. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  249. /* ep_disconnect could come before NETDEV_DOWN, driver won't
  250. * see NETDEV_DOWN as it already unregistered itself.
  251. */
  252. hba->adapter_state = 0;
  253. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  254. }
  255. /**
  256. * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
  257. *
  258. * unregisters all bnx2i adapter instances with the cnic driver while holding
  259. * the global resource lock
  260. */
  261. void bnx2i_unreg_dev_all(void)
  262. {
  263. struct bnx2i_hba *hba, *temp;
  264. mutex_lock(&bnx2i_dev_lock);
  265. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  266. bnx2i_unreg_one_device(hba);
  267. mutex_unlock(&bnx2i_dev_lock);
  268. }
  269. /**
  270. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  271. * @hba: bnx2i adapter instance
  272. * @cnic: cnic device handle
  273. *
  274. * Global resource lock is held during critical sections below. This routine is
  275. * called from either cnic_register_driver() or device hot plug context and
  276. * and does majority of device specific initialization
  277. */
  278. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  279. {
  280. int rc;
  281. mutex_lock(&bnx2i_dev_lock);
  282. hba->cnic = cnic;
  283. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  284. if (!rc) {
  285. hba->age++;
  286. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  287. list_add_tail(&hba->link, &adapter_list);
  288. adapter_count++;
  289. } else if (rc == -EBUSY) /* duplicate registration */
  290. printk(KERN_ALERT "bnx2i, duplicate registration"
  291. "hba=%p, cnic=%p\n", hba, cnic);
  292. else if (rc == -EAGAIN)
  293. printk(KERN_ERR "bnx2i, driver not registered\n");
  294. else if (rc == -EINVAL)
  295. printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
  296. else
  297. printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
  298. mutex_unlock(&bnx2i_dev_lock);
  299. return rc;
  300. }
  301. /**
  302. * bnx2i_ulp_init - initialize an adapter instance
  303. * @dev: cnic device handle
  304. *
  305. * Called from cnic_register_driver() context to initialize all enumerated
  306. * cnic devices. This routine allocate adapter structure and other
  307. * device specific resources.
  308. */
  309. void bnx2i_ulp_init(struct cnic_dev *dev)
  310. {
  311. struct bnx2i_hba *hba;
  312. /* Allocate a HBA structure for this device */
  313. hba = bnx2i_alloc_hba(dev);
  314. if (!hba) {
  315. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  316. return;
  317. }
  318. /* Get PCI related information and update hba struct members */
  319. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  320. if (bnx2i_init_one(hba, dev)) {
  321. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  322. bnx2i_free_hba(hba);
  323. }
  324. }
  325. /**
  326. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  327. * @dev: cnic device handle
  328. *
  329. */
  330. void bnx2i_ulp_exit(struct cnic_dev *dev)
  331. {
  332. struct bnx2i_hba *hba;
  333. hba = bnx2i_find_hba_for_cnic(dev);
  334. if (!hba) {
  335. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  336. "found, dev 0x%p\n", dev);
  337. return;
  338. }
  339. mutex_lock(&bnx2i_dev_lock);
  340. list_del_init(&hba->link);
  341. adapter_count--;
  342. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  343. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  344. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  345. }
  346. mutex_unlock(&bnx2i_dev_lock);
  347. bnx2i_free_hba(hba);
  348. }
  349. /**
  350. * bnx2i_mod_init - module init entry point
  351. *
  352. * initialize any driver wide global data structures such as endpoint pool,
  353. * tcp port manager/queue, sysfs. finally driver will register itself
  354. * with the cnic module
  355. */
  356. static int __init bnx2i_mod_init(void)
  357. {
  358. int err;
  359. printk(KERN_INFO "%s", version);
  360. if (sq_size && !is_power_of_2(sq_size))
  361. sq_size = roundup_pow_of_two(sq_size);
  362. mutex_init(&bnx2i_dev_lock);
  363. bnx2i_scsi_xport_template =
  364. iscsi_register_transport(&bnx2i_iscsi_transport);
  365. if (!bnx2i_scsi_xport_template) {
  366. printk(KERN_ERR "Could not register bnx2i transport.\n");
  367. err = -ENOMEM;
  368. goto out;
  369. }
  370. err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
  371. if (err) {
  372. printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
  373. goto unreg_xport;
  374. }
  375. return 0;
  376. unreg_xport:
  377. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  378. out:
  379. return err;
  380. }
  381. /**
  382. * bnx2i_mod_exit - module cleanup/exit entry point
  383. *
  384. * Global resource lock and host adapter lock is held during critical sections
  385. * in this function. Driver will browse through the adapter list, cleans-up
  386. * each instance, unregisters iscsi transport name and finally driver will
  387. * unregister itself with the cnic module
  388. */
  389. static void __exit bnx2i_mod_exit(void)
  390. {
  391. struct bnx2i_hba *hba;
  392. mutex_lock(&bnx2i_dev_lock);
  393. while (!list_empty(&adapter_list)) {
  394. hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
  395. list_del(&hba->link);
  396. adapter_count--;
  397. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  398. bnx2i_chip_cleanup(hba);
  399. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  400. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  401. }
  402. bnx2i_free_hba(hba);
  403. }
  404. mutex_unlock(&bnx2i_dev_lock);
  405. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  406. cnic_unregister_driver(CNIC_ULP_ISCSI);
  407. }
  408. module_init(bnx2i_mod_init);
  409. module_exit(bnx2i_mod_exit);