bnx2i_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* bnx2i.c: QLogic NetXtreme II iSCSI driver.
  2. *
  3. * Copyright (c) 2006 - 2013 Broadcom Corporation
  4. * Copyright (c) 2007, 2008 Red Hat, Inc. All rights reserved.
  5. * Copyright (c) 2007, 2008 Mike Christie
  6. * Copyright (c) 2014, QLogic Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. *
  12. * Written by: Anil Veerabhadrappa ([email protected])
  13. * Previously Maintained by: Eddie Wai ([email protected])
  14. * Maintained by: [email protected]
  15. */
  16. #include "bnx2i.h"
  17. static struct list_head adapter_list = LIST_HEAD_INIT(adapter_list);
  18. static u32 adapter_count;
  19. #define DRV_MODULE_NAME "bnx2i"
  20. #define DRV_MODULE_VERSION "2.7.10.1"
  21. #define DRV_MODULE_RELDATE "Jul 16, 2014"
  22. static char version[] =
  23. "QLogic NetXtreme II iSCSI Driver " DRV_MODULE_NAME \
  24. " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  25. MODULE_AUTHOR("Anil Veerabhadrappa <[email protected]> and "
  26. "Eddie Wai <[email protected]>");
  27. MODULE_DESCRIPTION("QLogic NetXtreme II BCM5706/5708/5709/57710/57711/57712"
  28. "/57800/57810/57840 iSCSI Driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION(DRV_MODULE_VERSION);
  31. static DEFINE_MUTEX(bnx2i_dev_lock);
  32. unsigned int event_coal_min = 24;
  33. module_param(event_coal_min, int, 0664);
  34. MODULE_PARM_DESC(event_coal_min, "Event Coalescing Minimum Commands");
  35. unsigned int event_coal_div = 2;
  36. module_param(event_coal_div, int, 0664);
  37. MODULE_PARM_DESC(event_coal_div, "Event Coalescing Divide Factor");
  38. unsigned int en_tcp_dack = 1;
  39. module_param(en_tcp_dack, int, 0664);
  40. MODULE_PARM_DESC(en_tcp_dack, "Enable TCP Delayed ACK");
  41. unsigned int error_mask1 = 0x00;
  42. module_param(error_mask1, uint, 0664);
  43. MODULE_PARM_DESC(error_mask1, "Config FW iSCSI Error Mask #1");
  44. unsigned int error_mask2 = 0x00;
  45. module_param(error_mask2, uint, 0664);
  46. MODULE_PARM_DESC(error_mask2, "Config FW iSCSI Error Mask #2");
  47. unsigned int sq_size;
  48. module_param(sq_size, int, 0664);
  49. MODULE_PARM_DESC(sq_size, "Configure SQ size");
  50. unsigned int rq_size = BNX2I_RQ_WQES_DEFAULT;
  51. module_param(rq_size, int, 0664);
  52. MODULE_PARM_DESC(rq_size, "Configure RQ size");
  53. u64 iscsi_error_mask = 0x00;
  54. DEFINE_PER_CPU(struct bnx2i_percpu_s, bnx2i_percpu);
  55. /**
  56. * bnx2i_identify_device - identifies NetXtreme II device type
  57. * @hba: Adapter structure pointer
  58. * @dev: Corresponding cnic device
  59. *
  60. * This function identifies the NX2 device type and sets appropriate
  61. * queue mailbox register access method, 5709 requires driver to
  62. * access MBOX regs using *bin* mode
  63. */
  64. void bnx2i_identify_device(struct bnx2i_hba *hba, struct cnic_dev *dev)
  65. {
  66. hba->cnic_dev_type = 0;
  67. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  68. if (hba->pci_did == PCI_DEVICE_ID_NX2_5706 ||
  69. hba->pci_did == PCI_DEVICE_ID_NX2_5706S) {
  70. set_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type);
  71. } else if (hba->pci_did == PCI_DEVICE_ID_NX2_5708 ||
  72. hba->pci_did == PCI_DEVICE_ID_NX2_5708S) {
  73. set_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type);
  74. } else if (hba->pci_did == PCI_DEVICE_ID_NX2_5709 ||
  75. hba->pci_did == PCI_DEVICE_ID_NX2_5709S) {
  76. set_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type);
  77. hba->mail_queue_access = BNX2I_MQ_BIN_MODE;
  78. }
  79. } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  80. set_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type);
  81. } else {
  82. printk(KERN_ALERT "bnx2i: unknown device, 0x%x\n",
  83. hba->pci_did);
  84. }
  85. }
  86. /**
  87. * get_adapter_list_head - returns head of adapter list
  88. */
  89. struct bnx2i_hba *get_adapter_list_head(void)
  90. {
  91. struct bnx2i_hba *hba = NULL;
  92. struct bnx2i_hba *tmp_hba;
  93. if (!adapter_count)
  94. goto hba_not_found;
  95. mutex_lock(&bnx2i_dev_lock);
  96. list_for_each_entry(tmp_hba, &adapter_list, link) {
  97. if (tmp_hba->cnic && tmp_hba->cnic->cm_select_dev) {
  98. hba = tmp_hba;
  99. break;
  100. }
  101. }
  102. mutex_unlock(&bnx2i_dev_lock);
  103. hba_not_found:
  104. return hba;
  105. }
  106. /**
  107. * bnx2i_find_hba_for_cnic - maps cnic device instance to bnx2i adapter instance
  108. * @cnic: pointer to cnic device instance
  109. *
  110. */
  111. struct bnx2i_hba *bnx2i_find_hba_for_cnic(struct cnic_dev *cnic)
  112. {
  113. struct bnx2i_hba *hba, *temp;
  114. mutex_lock(&bnx2i_dev_lock);
  115. list_for_each_entry_safe(hba, temp, &adapter_list, link) {
  116. if (hba->cnic == cnic) {
  117. mutex_unlock(&bnx2i_dev_lock);
  118. return hba;
  119. }
  120. }
  121. mutex_unlock(&bnx2i_dev_lock);
  122. return NULL;
  123. }
  124. /**
  125. * bnx2i_start - cnic callback to initialize & start adapter instance
  126. * @handle: transparent handle pointing to adapter structure
  127. *
  128. * This function maps adapter structure to pcidev structure and initiates
  129. * firmware handshake to enable/initialize on chip iscsi components
  130. * This bnx2i - cnic interface api callback is issued after following
  131. * 2 conditions are met -
  132. * a) underlying network interface is up (marked by event 'NETDEV_UP'
  133. * from netdev
  134. * b) bnx2i adapter instance is registered
  135. */
  136. void bnx2i_start(void *handle)
  137. {
  138. #define BNX2I_INIT_POLL_TIME (1000 / HZ)
  139. struct bnx2i_hba *hba = handle;
  140. int i = HZ;
  141. /* On some bnx2x devices, it is possible that iSCSI is no
  142. * longer supported after firmware is downloaded. In that
  143. * case, the iscsi_init_msg will return failure.
  144. */
  145. bnx2i_send_fw_iscsi_init_msg(hba);
  146. while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) &&
  147. !test_bit(ADAPTER_STATE_INIT_FAILED, &hba->adapter_state) && i--)
  148. msleep(BNX2I_INIT_POLL_TIME);
  149. }
  150. /**
  151. * bnx2i_chip_cleanup - local routine to handle chip cleanup
  152. * @hba: Adapter instance to register
  153. *
  154. * Driver checks if adapter still has any active connections before
  155. * executing the cleanup process
  156. */
  157. static void bnx2i_chip_cleanup(struct bnx2i_hba *hba)
  158. {
  159. struct bnx2i_endpoint *bnx2i_ep;
  160. struct list_head *pos, *tmp;
  161. if (hba->ofld_conns_active) {
  162. /* Stage to force the disconnection
  163. * This is the case where the daemon is either slow or
  164. * not present
  165. */
  166. printk(KERN_ALERT "bnx2i: (%s) chip cleanup for %d active "
  167. "connections\n", hba->netdev->name,
  168. hba->ofld_conns_active);
  169. mutex_lock(&hba->net_dev_lock);
  170. list_for_each_safe(pos, tmp, &hba->ep_active_list) {
  171. bnx2i_ep = list_entry(pos, struct bnx2i_endpoint, link);
  172. /* Clean up the chip only */
  173. bnx2i_hw_ep_disconnect(bnx2i_ep);
  174. bnx2i_ep->cm_sk = NULL;
  175. }
  176. mutex_unlock(&hba->net_dev_lock);
  177. }
  178. }
  179. /**
  180. * bnx2i_stop - cnic callback to shutdown adapter instance
  181. * @handle: transparent handle pointing to adapter structure
  182. *
  183. * driver checks if adapter is already in shutdown mode, if not start
  184. * the shutdown process
  185. */
  186. void bnx2i_stop(void *handle)
  187. {
  188. struct bnx2i_hba *hba = handle;
  189. int conns_active;
  190. int wait_delay = 1 * HZ;
  191. /* check if cleanup happened in GOING_DOWN context */
  192. if (!test_and_set_bit(ADAPTER_STATE_GOING_DOWN,
  193. &hba->adapter_state)) {
  194. iscsi_host_for_each_session(hba->shost,
  195. bnx2i_drop_session);
  196. wait_delay = hba->hba_shutdown_tmo;
  197. }
  198. /* Wait for inflight offload connection tasks to complete before
  199. * proceeding. Forcefully terminate all connection recovery in
  200. * progress at the earliest, either in bind(), send_pdu(LOGIN),
  201. * or conn_start()
  202. */
  203. wait_event_interruptible_timeout(hba->eh_wait,
  204. (list_empty(&hba->ep_ofld_list) &&
  205. list_empty(&hba->ep_destroy_list)),
  206. 2 * HZ);
  207. /* Wait for all endpoints to be torn down, Chip will be reset once
  208. * control returns to network driver. So it is required to cleanup and
  209. * release all connection resources before returning from this routine.
  210. */
  211. while (hba->ofld_conns_active) {
  212. conns_active = hba->ofld_conns_active;
  213. wait_event_interruptible_timeout(hba->eh_wait,
  214. (hba->ofld_conns_active != conns_active),
  215. wait_delay);
  216. if (hba->ofld_conns_active == conns_active)
  217. break;
  218. }
  219. bnx2i_chip_cleanup(hba);
  220. /* This flag should be cleared last so that ep_disconnect() gracefully
  221. * cleans up connection context
  222. */
  223. clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  224. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  225. }
  226. /**
  227. * bnx2i_init_one - initialize an adapter instance and allocate memory resources
  228. * @hba: bnx2i adapter instance
  229. * @cnic: cnic device handle
  230. *
  231. * Global resource lock is held during critical sections below. This routine is
  232. * called from either cnic_register_driver() or device hot plug context and
  233. * and does majority of device specific initialization
  234. */
  235. static int bnx2i_init_one(struct bnx2i_hba *hba, struct cnic_dev *cnic)
  236. {
  237. int rc;
  238. mutex_lock(&bnx2i_dev_lock);
  239. if (!cnic->max_iscsi_conn) {
  240. printk(KERN_ALERT "bnx2i: dev %s does not support "
  241. "iSCSI\n", hba->netdev->name);
  242. rc = -EOPNOTSUPP;
  243. goto out;
  244. }
  245. hba->cnic = cnic;
  246. rc = cnic->register_device(cnic, CNIC_ULP_ISCSI, hba);
  247. if (!rc) {
  248. hba->age++;
  249. set_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  250. list_add_tail(&hba->link, &adapter_list);
  251. adapter_count++;
  252. } else if (rc == -EBUSY) /* duplicate registration */
  253. printk(KERN_ALERT "bnx2i, duplicate registration"
  254. "hba=%p, cnic=%p\n", hba, cnic);
  255. else if (rc == -EAGAIN)
  256. printk(KERN_ERR "bnx2i, driver not registered\n");
  257. else if (rc == -EINVAL)
  258. printk(KERN_ERR "bnx2i, invalid type %d\n", CNIC_ULP_ISCSI);
  259. else
  260. printk(KERN_ERR "bnx2i dev reg, unknown error, %d\n", rc);
  261. out:
  262. mutex_unlock(&bnx2i_dev_lock);
  263. return rc;
  264. }
  265. /**
  266. * bnx2i_ulp_init - initialize an adapter instance
  267. * @dev: cnic device handle
  268. *
  269. * Called from cnic_register_driver() context to initialize all enumerated
  270. * cnic devices. This routine allocate adapter structure and other
  271. * device specific resources.
  272. */
  273. void bnx2i_ulp_init(struct cnic_dev *dev)
  274. {
  275. struct bnx2i_hba *hba;
  276. /* Allocate a HBA structure for this device */
  277. hba = bnx2i_alloc_hba(dev);
  278. if (!hba) {
  279. printk(KERN_ERR "bnx2i init: hba initialization failed\n");
  280. return;
  281. }
  282. /* Get PCI related information and update hba struct members */
  283. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  284. if (bnx2i_init_one(hba, dev)) {
  285. printk(KERN_ERR "bnx2i - hba %p init failed\n", hba);
  286. bnx2i_free_hba(hba);
  287. }
  288. }
  289. /**
  290. * bnx2i_ulp_exit - shuts down adapter instance and frees all resources
  291. * @dev: cnic device handle
  292. *
  293. */
  294. void bnx2i_ulp_exit(struct cnic_dev *dev)
  295. {
  296. struct bnx2i_hba *hba;
  297. hba = bnx2i_find_hba_for_cnic(dev);
  298. if (!hba) {
  299. printk(KERN_INFO "bnx2i_ulp_exit: hba not "
  300. "found, dev 0x%p\n", dev);
  301. return;
  302. }
  303. mutex_lock(&bnx2i_dev_lock);
  304. list_del_init(&hba->link);
  305. adapter_count--;
  306. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  307. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  308. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  309. }
  310. mutex_unlock(&bnx2i_dev_lock);
  311. bnx2i_free_hba(hba);
  312. }
  313. /**
  314. * bnx2i_get_stats - Retrieve various statistic from iSCSI offload
  315. * @handle: bnx2i_hba
  316. *
  317. * function callback exported via bnx2i - cnic driver interface to
  318. * retrieve various iSCSI offload related statistics.
  319. */
  320. int bnx2i_get_stats(void *handle)
  321. {
  322. struct bnx2i_hba *hba = handle;
  323. struct iscsi_stats_info *stats;
  324. if (!hba)
  325. return -EINVAL;
  326. stats = (struct iscsi_stats_info *)hba->cnic->stats_addr;
  327. if (!stats)
  328. return -ENOMEM;
  329. strlcpy(stats->version, DRV_MODULE_VERSION, sizeof(stats->version));
  330. memcpy(stats->mac_add1 + 2, hba->cnic->mac_addr, ETH_ALEN);
  331. stats->max_frame_size = hba->netdev->mtu;
  332. stats->txq_size = hba->max_sqes;
  333. stats->rxq_size = hba->max_cqes;
  334. stats->txq_avg_depth = 0;
  335. stats->rxq_avg_depth = 0;
  336. GET_STATS_64(hba, stats, rx_pdus);
  337. GET_STATS_64(hba, stats, rx_bytes);
  338. GET_STATS_64(hba, stats, tx_pdus);
  339. GET_STATS_64(hba, stats, tx_bytes);
  340. return 0;
  341. }
  342. /**
  343. * bnx2i_cpu_online - Create a receive thread for an online CPU
  344. *
  345. * @cpu: cpu index for the online cpu
  346. */
  347. static int bnx2i_cpu_online(unsigned int cpu)
  348. {
  349. struct bnx2i_percpu_s *p;
  350. struct task_struct *thread;
  351. p = &per_cpu(bnx2i_percpu, cpu);
  352. thread = kthread_create_on_node(bnx2i_percpu_io_thread, (void *)p,
  353. cpu_to_node(cpu),
  354. "bnx2i_thread/%d", cpu);
  355. if (IS_ERR(thread))
  356. return PTR_ERR(thread);
  357. /* bind thread to the cpu */
  358. kthread_bind(thread, cpu);
  359. p->iothread = thread;
  360. wake_up_process(thread);
  361. return 0;
  362. }
  363. static int bnx2i_cpu_offline(unsigned int cpu)
  364. {
  365. struct bnx2i_percpu_s *p;
  366. struct task_struct *thread;
  367. struct bnx2i_work *work, *tmp;
  368. /* Prevent any new work from being queued for this CPU */
  369. p = &per_cpu(bnx2i_percpu, cpu);
  370. spin_lock_bh(&p->p_work_lock);
  371. thread = p->iothread;
  372. p->iothread = NULL;
  373. /* Free all work in the list */
  374. list_for_each_entry_safe(work, tmp, &p->work_list, list) {
  375. list_del_init(&work->list);
  376. bnx2i_process_scsi_cmd_resp(work->session,
  377. work->bnx2i_conn, &work->cqe);
  378. kfree(work);
  379. }
  380. spin_unlock_bh(&p->p_work_lock);
  381. if (thread)
  382. kthread_stop(thread);
  383. return 0;
  384. }
  385. static enum cpuhp_state bnx2i_online_state;
  386. /**
  387. * bnx2i_mod_init - module init entry point
  388. *
  389. * initialize any driver wide global data structures such as endpoint pool,
  390. * tcp port manager/queue, sysfs. finally driver will register itself
  391. * with the cnic module
  392. */
  393. static int __init bnx2i_mod_init(void)
  394. {
  395. int err;
  396. unsigned cpu = 0;
  397. struct bnx2i_percpu_s *p;
  398. printk(KERN_INFO "%s", version);
  399. if (sq_size && !is_power_of_2(sq_size))
  400. sq_size = roundup_pow_of_two(sq_size);
  401. bnx2i_scsi_xport_template =
  402. iscsi_register_transport(&bnx2i_iscsi_transport);
  403. if (!bnx2i_scsi_xport_template) {
  404. printk(KERN_ERR "Could not register bnx2i transport.\n");
  405. err = -ENOMEM;
  406. goto out;
  407. }
  408. err = cnic_register_driver(CNIC_ULP_ISCSI, &bnx2i_cnic_cb);
  409. if (err) {
  410. printk(KERN_ERR "Could not register bnx2i cnic driver.\n");
  411. goto unreg_xport;
  412. }
  413. /* Create percpu kernel threads to handle iSCSI I/O completions */
  414. for_each_possible_cpu(cpu) {
  415. p = &per_cpu(bnx2i_percpu, cpu);
  416. INIT_LIST_HEAD(&p->work_list);
  417. spin_lock_init(&p->p_work_lock);
  418. p->iothread = NULL;
  419. }
  420. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/bnx2i:online",
  421. bnx2i_cpu_online, bnx2i_cpu_offline);
  422. if (err < 0)
  423. goto unreg_driver;
  424. bnx2i_online_state = err;
  425. return 0;
  426. unreg_driver:
  427. cnic_unregister_driver(CNIC_ULP_ISCSI);
  428. unreg_xport:
  429. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  430. out:
  431. return err;
  432. }
  433. /**
  434. * bnx2i_mod_exit - module cleanup/exit entry point
  435. *
  436. * Global resource lock and host adapter lock is held during critical sections
  437. * in this function. Driver will browse through the adapter list, cleans-up
  438. * each instance, unregisters iscsi transport name and finally driver will
  439. * unregister itself with the cnic module
  440. */
  441. static void __exit bnx2i_mod_exit(void)
  442. {
  443. struct bnx2i_hba *hba;
  444. mutex_lock(&bnx2i_dev_lock);
  445. while (!list_empty(&adapter_list)) {
  446. hba = list_entry(adapter_list.next, struct bnx2i_hba, link);
  447. list_del(&hba->link);
  448. adapter_count--;
  449. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  450. bnx2i_chip_cleanup(hba);
  451. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_ISCSI);
  452. clear_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic);
  453. }
  454. bnx2i_free_hba(hba);
  455. }
  456. mutex_unlock(&bnx2i_dev_lock);
  457. cpuhp_remove_state(bnx2i_online_state);
  458. iscsi_unregister_transport(&bnx2i_iscsi_transport);
  459. cnic_unregister_driver(CNIC_ULP_ISCSI);
  460. }
  461. module_init(bnx2i_mod_init);
  462. module_exit(bnx2i_mod_exit);