bnx2i_init.c 15 KB

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