bnx2i_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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.2"
  18. #define DRV_MODULE_RELDATE "Jun 28, 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>");
  23. MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708/5709/57710/57711"
  24. " iSCSI Driver");
  25. MODULE_LICENSE("GPL");
  26. MODULE_VERSION(DRV_MODULE_VERSION);
  27. static DEFINE_MUTEX(bnx2i_dev_lock);
  28. unsigned int event_coal_min = 24;
  29. module_param(event_coal_min, int, 0664);
  30. MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
  31. unsigned int event_coal_div = 1;
  32. module_param(event_coal_div, int, 0664);
  33. MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
  34. unsigned int en_tcp_dack = 1;
  35. module_param(en_tcp_dack, int, 0664);
  36. MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
  37. unsigned int error_mask1 = 0x00;
  38. module_param(error_mask1, int, 0664);
  39. MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
  40. unsigned int error_mask2 = 0x00;
  41. module_param(error_mask2, int, 0664);
  42. MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
  43. unsigned int sq_size;
  44. module_param(sq_size, int, 0664);
  45. MODULE_PARM_DESC(sq_size, "Configure SQ size");
  46. unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
  47. module_param(rq_size, int, 0664);
  48. MODULE_PARM_DESC(rq_size, "Configure RQ size");
  49. u64 iscsi_error_mask = 0x00;
  50. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba) ;
  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_stop - cnic callback to shutdown adapter instance
  141. * @handle: transparent handle pointing to adapter structure
  142. *
  143. * driver checks if adapter is already in shutdown mode, if not start
  144. * the shutdown process
  145. */
  146. void bnx2i_stop(void *handle)
  147. {
  148. struct bnx2i_hba *hba = handle;
  149. struct list_head *pos, *tmp;
  150. struct bnx2i_endpoint *bnx2i_ep;
  151. int conns_active;
  152. /* check if cleanup happened in GOING_DOWN context */
  153. if (!test_and_clear_bit(ADAPTER_STATE_GOING_DOWN,
  154. &hba->adapter_state))
  155. iscsi_host_for_each_session(hba->shost,
  156. bnx2i_drop_session);
  157. /* Wait for all endpoints to be torn down, Chip will be reset once
  158. * control returns to network driver. So it is required to cleanup and
  159. * release all connection resources before returning from this routine.
  160. */
  161. while (hba->ofld_conns_active) {
  162. conns_active = hba->ofld_conns_active;
  163. wait_event_interruptible_timeout(hba->eh_wait,
  164. (hba->ofld_conns_active != conns_active),
  165. hba->hba_shutdown_tmo);
  166. if (hba->ofld_conns_active == conns_active)
  167. break;
  168. }
  169. if (hba->ofld_conns_active) {
  170. /* Stage to force the disconnection
  171. * This is the case where the daemon is either slow or
  172. * not present
  173. */
  174. printk(KERN_ALERT "bnx2i: Wait timeout, force all eps "
  175. "to disconnect (%d)\n", hba->ofld_conns_active);
  176. mutex_lock(&hba->net_dev_lock);
  177. list_for_each_safe(pos, tmp, &hba->ep_active_list) {
  178. bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
  179. /* Clean up the chip only */
  180. bnx2i_hw_ep_disconnect(bnx2i_ep);
  181. }
  182. mutex_unlock(&hba->net_dev_lock);
  183. if (hba->ofld_conns_active)
  184. printk(KERN_ERR "bnx2i: EP disconnect timeout (%d)!\n",
  185. hba->ofld_conns_active);
  186. }
  187. /* This flag should be cleared last so that ep_disconnect() gracefully
  188. * cleans up connection context
  189. */
  190. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  191. }
  192. /**
  193. * bnx2i_register_device - register bnx2i adapter instance with the cnic driver
  194. * @hba: Adapter instance to register
  195. *
  196. * registers bnx2i adapter instance with the cnic driver while holding the
  197. * adapter structure lock
  198. */
  199. void bnx2i_register_device(struct bnx2i_hba *hba)
  200. {
  201. int rc;
  202. if (test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
  203. test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  204. return;
  205. }
  206. rc = hba->cnic->register_device(hba->cnic, CNIC_ULP_ISCSI, hba);
  207. if (!rc)
  208. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  209. }
  210. /**
  211. * bnx2i_reg_dev_all - registers all adapter instances with the cnic driver
  212. *
  213. * registers all bnx2i adapter instances with the cnic driver while holding
  214. * the global resource lock
  215. */
  216. void bnx2i_reg_dev_all(void)
  217. {
  218. struct bnx2i_hba *hba, *temp;
  219. mutex_lock(&bnx2i_dev_lock);
  220. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  221. bnx2i_register_device(hba);
  222. mutex_unlock(&bnx2i_dev_lock);
  223. }
  224. /**
  225. * bnx2i_unreg_one_device - unregister adapter instance with the cnic driver
  226. * @hba: Adapter instance to unregister
  227. *
  228. * registers bnx2i adapter instance with the cnic driver while holding
  229. * the adapter structure lock
  230. */
  231. static void bnx2i_unreg_one_device(struct bnx2i_hba *hba)
  232. {
  233. if (hba->ofld_conns_active ||
  234. !test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) ||
  235. test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state))
  236. return;
  237. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  238. /* ep_disconnect could come before NETDEV_DOWN, driver won't
  239. * see NETDEV_DOWN as it already unregistered itself.
  240. */
  241. hba->adapter_state = 0;
  242. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  243. }
  244. /**
  245. * bnx2i_unreg_dev_all - unregisters all bnx2i instances with the cnic driver
  246. *
  247. * unregisters all bnx2i adapter instances with the cnic driver while holding
  248. * the global resource lock
  249. */
  250. void bnx2i_unreg_dev_all(void)
  251. {
  252. struct bnx2i_hba *hba, *temp;
  253. mutex_lock(&bnx2i_dev_lock);
  254. list_for_each_entry_safe(hba, temp, &adapter_list, link)
  255. bnx2i_unreg_one_device(hba);
  256. mutex_unlock(&bnx2i_dev_lock);
  257. }
  258. /**
  259. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  260. * @hba: bnx2i adapter instance
  261. * @cnic: cnic device handle
  262. *
  263. * Global resource lock is held during critical sections below. This routine is
  264. * called from either cnic_register_driver() or device hot plug context and
  265. * and does majority of device specific initialization
  266. */
  267. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  268. {
  269. int rc;
  270. mutex_lock(&bnx2i_dev_lock);
  271. hba->cnic = cnic;
  272. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  273. if (!rc) {
  274. hba->age++;
  275. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  276. list_add_tail(&hba->link, &adapter_list);
  277. adapter_count++;
  278. } else if (rc == -EBUSY) /* duplicate registration */
  279. printk(KERN_ALERT "bnx2i, duplicate registration"
  280. "hba=%p, cnic=%p\n", hba, cnic);
  281. else if (rc == -EAGAIN)
  282. printk(KERN_ERR "bnx2i, driver not registered\n");
  283. else if (rc == -EINVAL)
  284. printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
  285. else
  286. printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
  287. mutex_unlock(&bnx2i_dev_lock);
  288. return rc;
  289. }
  290. /**
  291. * bnx2i_ulp_init - initialize an adapter instance
  292. * @dev: cnic device handle
  293. *
  294. * Called from cnic_register_driver() context to initialize all enumerated
  295. * cnic devices. This routine allocate adapter structure and other
  296. * device specific resources.
  297. */
  298. void bnx2i_ulp_init(struct cnic_dev *dev)
  299. {
  300. struct bnx2i_hba *hba;
  301. /* Allocate a HBA structure for this device */
  302. hba = bnx2i_alloc_hba(dev);
  303. if (!hba) {
  304. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  305. return;
  306. }
  307. /* Get PCI related information and update hba struct members */
  308. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  309. if (bnx2i_init_one(hba, dev)) {
  310. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  311. bnx2i_free_hba(hba);
  312. }
  313. }
  314. /**
  315. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  316. * @dev: cnic device handle
  317. *
  318. */
  319. void bnx2i_ulp_exit(struct cnic_dev *dev)
  320. {
  321. struct bnx2i_hba *hba;
  322. hba = bnx2i_find_hba_for_cnic(dev);
  323. if (!hba) {
  324. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  325. "found, dev 0x%p\n", dev);
  326. return;
  327. }
  328. mutex_lock(&bnx2i_dev_lock);
  329. list_del_init(&hba->link);
  330. adapter_count--;
  331. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  332. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  333. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  334. }
  335. mutex_unlock(&bnx2i_dev_lock);
  336. bnx2i_free_hba(hba);
  337. }
  338. /**
  339. * bnx2i_mod_init - module init entry point
  340. *
  341. * initialize any driver wide global data structures such as endpoint pool,
  342. * tcp port manager/queue, sysfs. finally driver will register itself
  343. * with the cnic module
  344. */
  345. static int __init bnx2i_mod_init(void)
  346. {
  347. int err;
  348. printk(KERN_INFO "%s", version);
  349. if (sq_size && !is_power_of_2(sq_size))
  350. sq_size = roundup_pow_of_two(sq_size);
  351. mutex_init(&bnx2i_dev_lock);
  352. bnx2i_scsi_xport_template =
  353. iscsi_register_transport(&bnx2i_iscsi_transport);
  354. if (!bnx2i_scsi_xport_template) {
  355. printk(KERN_ERR "Could not register bnx2i transport.\n");
  356. err = -ENOMEM;
  357. goto out;
  358. }
  359. err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
  360. if (err) {
  361. printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
  362. goto unreg_xport;
  363. }
  364. return 0;
  365. unreg_xport:
  366. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  367. out:
  368. return err;
  369. }
  370. /**
  371. * bnx2i_mod_exit - module cleanup/exit entry point
  372. *
  373. * Global resource lock and host adapter lock is held during critical sections
  374. * in this function. Driver will browse through the adapter list, cleans-up
  375. * each instance, unregisters iscsi transport name and finally driver will
  376. * unregister itself with the cnic module
  377. */
  378. static void __exit bnx2i_mod_exit(void)
  379. {
  380. struct bnx2i_hba *hba;
  381. mutex_lock(&bnx2i_dev_lock);
  382. while (!list_empty(&adapter_list)) {
  383. hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
  384. list_del(&hba->link);
  385. adapter_count--;
  386. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  387. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  388. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  389. }
  390. bnx2i_free_hba(hba);
  391. }
  392. mutex_unlock(&bnx2i_dev_lock);
  393. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  394. cnic_unregister_driver(CNIC_ULP_ISCSI);
  395. }
  396. module_init(bnx2i_mod_init);
  397. module_exit(bnx2i_mod_exit);