bnx2i_init.c 16 KB

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