/drivers/scsi/bnx2fc/bnx2fc_tgt.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 871 lines · 604 code · 120 blank · 147 comment · 85 complexity · 135b64068112bb10259adc5f2d12f49c MD5 · raw file

  1. /* bnx2fc_tgt.c: Broadcom NetXtreme II Linux FCoE offload driver.
  2. * Handles operations such as session offload/upload etc, and manages
  3. * session resources such as connection id and qp resources.
  4. *
  5. * Copyright (c) 2008 - 2011 Broadcom Corporation
  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: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
  12. */
  13. #include "bnx2fc.h"
  14. static void bnx2fc_upld_timer(unsigned long data);
  15. static void bnx2fc_ofld_timer(unsigned long data);
  16. static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
  17. struct fcoe_port *port,
  18. struct fc_rport_priv *rdata);
  19. static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
  20. struct bnx2fc_rport *tgt);
  21. static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
  22. struct bnx2fc_rport *tgt);
  23. static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
  24. struct bnx2fc_rport *tgt);
  25. static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id);
  26. static void bnx2fc_upld_timer(unsigned long data)
  27. {
  28. struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
  29. BNX2FC_TGT_DBG(tgt, "upld_timer - Upload compl not received!!\n");
  30. /* fake upload completion */
  31. clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
  32. set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
  33. wake_up_interruptible(&tgt->upld_wait);
  34. }
  35. static void bnx2fc_ofld_timer(unsigned long data)
  36. {
  37. struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
  38. BNX2FC_TGT_DBG(tgt, "entered bnx2fc_ofld_timer\n");
  39. /* NOTE: This function should never be called, as
  40. * offload should never timeout
  41. */
  42. /*
  43. * If the timer has expired, this session is dead
  44. * Clear offloaded flag and logout of this device.
  45. * Since OFFLOADED flag is cleared, this case
  46. * will be considered as offload error and the
  47. * port will be logged off, and conn_id, session
  48. * resources are freed up in bnx2fc_offload_session
  49. */
  50. clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
  51. set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
  52. wake_up_interruptible(&tgt->ofld_wait);
  53. }
  54. static void bnx2fc_offload_session(struct fcoe_port *port,
  55. struct bnx2fc_rport *tgt,
  56. struct fc_rport_priv *rdata)
  57. {
  58. struct fc_lport *lport = rdata->local_port;
  59. struct fc_rport *rport = rdata->rport;
  60. struct bnx2fc_interface *interface = port->priv;
  61. struct bnx2fc_hba *hba = interface->hba;
  62. int rval;
  63. int i = 0;
  64. /* Initialize bnx2fc_rport */
  65. /* NOTE: tgt is already bzero'd */
  66. rval = bnx2fc_init_tgt(tgt, port, rdata);
  67. if (rval) {
  68. printk(KERN_ERR PFX "Failed to allocate conn id for "
  69. "port_id (%6x)\n", rport->port_id);
  70. goto ofld_err;
  71. }
  72. /* Allocate session resources */
  73. rval = bnx2fc_alloc_session_resc(hba, tgt);
  74. if (rval) {
  75. printk(KERN_ERR PFX "Failed to allocate resources\n");
  76. goto ofld_err;
  77. }
  78. /*
  79. * Initialize FCoE session offload process.
  80. * Upon completion of offload process add
  81. * rport to list of rports
  82. */
  83. retry_ofld:
  84. clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
  85. rval = bnx2fc_send_session_ofld_req(port, tgt);
  86. if (rval) {
  87. printk(KERN_ERR PFX "ofld_req failed\n");
  88. goto ofld_err;
  89. }
  90. /*
  91. * wait for the session is offloaded and enabled. 3 Secs
  92. * should be ample time for this process to complete.
  93. */
  94. setup_timer(&tgt->ofld_timer, bnx2fc_ofld_timer, (unsigned long)tgt);
  95. mod_timer(&tgt->ofld_timer, jiffies + BNX2FC_FW_TIMEOUT);
  96. wait_event_interruptible(tgt->ofld_wait,
  97. (test_bit(
  98. BNX2FC_FLAG_OFLD_REQ_CMPL,
  99. &tgt->flags)));
  100. if (signal_pending(current))
  101. flush_signals(current);
  102. del_timer_sync(&tgt->ofld_timer);
  103. if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
  104. if (test_and_clear_bit(BNX2FC_FLAG_CTX_ALLOC_FAILURE,
  105. &tgt->flags)) {
  106. BNX2FC_TGT_DBG(tgt, "ctx_alloc_failure, "
  107. "retry ofld..%d\n", i++);
  108. msleep_interruptible(1000);
  109. if (i > 3) {
  110. i = 0;
  111. goto ofld_err;
  112. }
  113. goto retry_ofld;
  114. }
  115. goto ofld_err;
  116. }
  117. if (bnx2fc_map_doorbell(tgt)) {
  118. printk(KERN_ERR PFX "map doorbell failed - no mem\n");
  119. /* upload will take care of cleaning up sess resc */
  120. lport->tt.rport_logoff(rdata);
  121. }
  122. /* Arm CQ */
  123. bnx2fc_arm_cq(tgt);
  124. return;
  125. ofld_err:
  126. /* couldn't offload the session. log off from this rport */
  127. BNX2FC_TGT_DBG(tgt, "bnx2fc_offload_session - offload error\n");
  128. lport->tt.rport_logoff(rdata);
  129. /* Free session resources */
  130. bnx2fc_free_session_resc(hba, tgt);
  131. if (tgt->fcoe_conn_id != -1)
  132. bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
  133. }
  134. void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt)
  135. {
  136. struct bnx2fc_cmd *io_req;
  137. struct list_head *list;
  138. struct list_head *tmp;
  139. int rc;
  140. int i = 0;
  141. BNX2FC_TGT_DBG(tgt, "Entered flush_active_ios - %d\n",
  142. tgt->num_active_ios.counter);
  143. spin_lock_bh(&tgt->tgt_lock);
  144. tgt->flush_in_prog = 1;
  145. list_for_each_safe(list, tmp, &tgt->active_cmd_queue) {
  146. i++;
  147. io_req = (struct bnx2fc_cmd *)list;
  148. list_del_init(&io_req->link);
  149. io_req->on_active_queue = 0;
  150. BNX2FC_IO_DBG(io_req, "cmd_queue cleanup\n");
  151. if (cancel_delayed_work(&io_req->timeout_work)) {
  152. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  153. &io_req->req_flags)) {
  154. /* Handle eh_abort timeout */
  155. BNX2FC_IO_DBG(io_req, "eh_abort for IO "
  156. "cleaned up\n");
  157. complete(&io_req->tm_done);
  158. }
  159. kref_put(&io_req->refcount,
  160. bnx2fc_cmd_release); /* drop timer hold */
  161. }
  162. set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags);
  163. set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
  164. rc = bnx2fc_initiate_cleanup(io_req);
  165. BUG_ON(rc);
  166. }
  167. list_for_each_safe(list, tmp, &tgt->els_queue) {
  168. i++;
  169. io_req = (struct bnx2fc_cmd *)list;
  170. list_del_init(&io_req->link);
  171. io_req->on_active_queue = 0;
  172. BNX2FC_IO_DBG(io_req, "els_queue cleanup\n");
  173. if (cancel_delayed_work(&io_req->timeout_work))
  174. kref_put(&io_req->refcount,
  175. bnx2fc_cmd_release); /* drop timer hold */
  176. if ((io_req->cb_func) && (io_req->cb_arg)) {
  177. io_req->cb_func(io_req->cb_arg);
  178. io_req->cb_arg = NULL;
  179. }
  180. rc = bnx2fc_initiate_cleanup(io_req);
  181. BUG_ON(rc);
  182. }
  183. list_for_each_safe(list, tmp, &tgt->io_retire_queue) {
  184. i++;
  185. io_req = (struct bnx2fc_cmd *)list;
  186. list_del_init(&io_req->link);
  187. BNX2FC_IO_DBG(io_req, "retire_queue flush\n");
  188. if (cancel_delayed_work(&io_req->timeout_work))
  189. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  190. clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
  191. }
  192. BNX2FC_TGT_DBG(tgt, "IOs flushed = %d\n", i);
  193. i = 0;
  194. spin_unlock_bh(&tgt->tgt_lock);
  195. /* wait for active_ios to go to 0 */
  196. while ((tgt->num_active_ios.counter != 0) && (i++ < BNX2FC_WAIT_CNT))
  197. msleep(25);
  198. if (tgt->num_active_ios.counter != 0)
  199. printk(KERN_ERR PFX "CLEANUP on port 0x%x:"
  200. " active_ios = %d\n",
  201. tgt->rdata->ids.port_id, tgt->num_active_ios.counter);
  202. spin_lock_bh(&tgt->tgt_lock);
  203. tgt->flush_in_prog = 0;
  204. spin_unlock_bh(&tgt->tgt_lock);
  205. }
  206. static void bnx2fc_upload_session(struct fcoe_port *port,
  207. struct bnx2fc_rport *tgt)
  208. {
  209. struct bnx2fc_interface *interface = port->priv;
  210. struct bnx2fc_hba *hba = interface->hba;
  211. BNX2FC_TGT_DBG(tgt, "upload_session: active_ios = %d\n",
  212. tgt->num_active_ios.counter);
  213. /*
  214. * Called with hba->hba_mutex held.
  215. * This is a blocking call
  216. */
  217. clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
  218. bnx2fc_send_session_disable_req(port, tgt);
  219. /*
  220. * wait for upload to complete. 3 Secs
  221. * should be sufficient time for this process to complete.
  222. */
  223. setup_timer(&tgt->upld_timer, bnx2fc_upld_timer, (unsigned long)tgt);
  224. mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
  225. BNX2FC_TGT_DBG(tgt, "waiting for disable compl\n");
  226. wait_event_interruptible(tgt->upld_wait,
  227. (test_bit(
  228. BNX2FC_FLAG_UPLD_REQ_COMPL,
  229. &tgt->flags)));
  230. if (signal_pending(current))
  231. flush_signals(current);
  232. del_timer_sync(&tgt->upld_timer);
  233. /*
  234. * traverse thru the active_q and tmf_q and cleanup
  235. * IOs in these lists
  236. */
  237. BNX2FC_TGT_DBG(tgt, "flush/upload - disable wait flags = 0x%lx\n",
  238. tgt->flags);
  239. bnx2fc_flush_active_ios(tgt);
  240. /* Issue destroy KWQE */
  241. if (test_bit(BNX2FC_FLAG_DISABLED, &tgt->flags)) {
  242. BNX2FC_TGT_DBG(tgt, "send destroy req\n");
  243. clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
  244. bnx2fc_send_session_destroy_req(hba, tgt);
  245. /* wait for destroy to complete */
  246. setup_timer(&tgt->upld_timer,
  247. bnx2fc_upld_timer, (unsigned long)tgt);
  248. mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
  249. wait_event_interruptible(tgt->upld_wait,
  250. (test_bit(
  251. BNX2FC_FLAG_UPLD_REQ_COMPL,
  252. &tgt->flags)));
  253. if (!(test_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags)))
  254. printk(KERN_ERR PFX "ERROR!! destroy timed out\n");
  255. BNX2FC_TGT_DBG(tgt, "destroy wait complete flags = 0x%lx\n",
  256. tgt->flags);
  257. if (signal_pending(current))
  258. flush_signals(current);
  259. del_timer_sync(&tgt->upld_timer);
  260. } else
  261. printk(KERN_ERR PFX "ERROR!! DISABLE req timed out, destroy"
  262. " not sent to FW\n");
  263. /* Free session resources */
  264. bnx2fc_free_session_resc(hba, tgt);
  265. bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
  266. }
  267. static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
  268. struct fcoe_port *port,
  269. struct fc_rport_priv *rdata)
  270. {
  271. struct fc_rport *rport = rdata->rport;
  272. struct bnx2fc_interface *interface = port->priv;
  273. struct bnx2fc_hba *hba = interface->hba;
  274. struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
  275. struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
  276. tgt->rport = rport;
  277. tgt->rdata = rdata;
  278. tgt->port = port;
  279. if (hba->num_ofld_sess >= BNX2FC_NUM_MAX_SESS) {
  280. BNX2FC_TGT_DBG(tgt, "exceeded max sessions. logoff this tgt\n");
  281. tgt->fcoe_conn_id = -1;
  282. return -1;
  283. }
  284. tgt->fcoe_conn_id = bnx2fc_alloc_conn_id(hba, tgt);
  285. if (tgt->fcoe_conn_id == -1)
  286. return -1;
  287. BNX2FC_TGT_DBG(tgt, "init_tgt - conn_id = 0x%x\n", tgt->fcoe_conn_id);
  288. tgt->max_sqes = BNX2FC_SQ_WQES_MAX;
  289. tgt->max_rqes = BNX2FC_RQ_WQES_MAX;
  290. tgt->max_cqes = BNX2FC_CQ_WQES_MAX;
  291. atomic_set(&tgt->free_sqes, BNX2FC_SQ_WQES_MAX);
  292. /* Initialize the toggle bit */
  293. tgt->sq_curr_toggle_bit = 1;
  294. tgt->cq_curr_toggle_bit = 1;
  295. tgt->sq_prod_idx = 0;
  296. tgt->cq_cons_idx = 0;
  297. tgt->rq_prod_idx = 0x8000;
  298. tgt->rq_cons_idx = 0;
  299. atomic_set(&tgt->num_active_ios, 0);
  300. if (rdata->flags & FC_RP_FLAGS_RETRY) {
  301. tgt->dev_type = TYPE_TAPE;
  302. tgt->io_timeout = 0; /* use default ULP timeout */
  303. } else {
  304. tgt->dev_type = TYPE_DISK;
  305. tgt->io_timeout = BNX2FC_IO_TIMEOUT;
  306. }
  307. /* initialize sq doorbell */
  308. sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
  309. sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
  310. B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
  311. /* initialize rx doorbell */
  312. rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
  313. (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
  314. (B577XX_FCOE_CONNECTION_TYPE <<
  315. B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
  316. rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
  317. (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
  318. spin_lock_init(&tgt->tgt_lock);
  319. spin_lock_init(&tgt->cq_lock);
  320. /* Initialize active_cmd_queue list */
  321. INIT_LIST_HEAD(&tgt->active_cmd_queue);
  322. /* Initialize IO retire queue */
  323. INIT_LIST_HEAD(&tgt->io_retire_queue);
  324. INIT_LIST_HEAD(&tgt->els_queue);
  325. /* Initialize active_tm_queue list */
  326. INIT_LIST_HEAD(&tgt->active_tm_queue);
  327. init_waitqueue_head(&tgt->ofld_wait);
  328. init_waitqueue_head(&tgt->upld_wait);
  329. return 0;
  330. }
  331. /**
  332. * This event_callback is called after successful completion of libfc
  333. * initiated target login. bnx2fc can proceed with initiating the session
  334. * establishment.
  335. */
  336. void bnx2fc_rport_event_handler(struct fc_lport *lport,
  337. struct fc_rport_priv *rdata,
  338. enum fc_rport_event event)
  339. {
  340. struct fcoe_port *port = lport_priv(lport);
  341. struct bnx2fc_interface *interface = port->priv;
  342. struct bnx2fc_hba *hba = interface->hba;
  343. struct fc_rport *rport = rdata->rport;
  344. struct fc_rport_libfc_priv *rp;
  345. struct bnx2fc_rport *tgt;
  346. u32 port_id;
  347. BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
  348. event, rdata->ids.port_id);
  349. switch (event) {
  350. case RPORT_EV_READY:
  351. if (!rport) {
  352. printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
  353. break;
  354. }
  355. rp = rport->dd_data;
  356. if (rport->port_id == FC_FID_DIR_SERV) {
  357. /*
  358. * bnx2fc_rport structure doesn't exist for
  359. * directory server.
  360. * We should not come here, as lport will
  361. * take care of fabric login
  362. */
  363. printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
  364. rdata->ids.port_id);
  365. break;
  366. }
  367. if (rdata->spp_type != FC_TYPE_FCP) {
  368. BNX2FC_HBA_DBG(lport, "not FCP type target."
  369. " not offloading\n");
  370. break;
  371. }
  372. if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
  373. BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
  374. " not offloading\n");
  375. break;
  376. }
  377. /*
  378. * Offlaod process is protected with hba mutex.
  379. * Use the same mutex_lock for upload process too
  380. */
  381. mutex_lock(&hba->hba_mutex);
  382. tgt = (struct bnx2fc_rport *)&rp[1];
  383. /* This can happen when ADISC finds the same target */
  384. if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
  385. BNX2FC_TGT_DBG(tgt, "already offloaded\n");
  386. mutex_unlock(&hba->hba_mutex);
  387. return;
  388. }
  389. /*
  390. * Offload the session. This is a blocking call, and will
  391. * wait until the session is offloaded.
  392. */
  393. bnx2fc_offload_session(port, tgt, rdata);
  394. BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
  395. hba->num_ofld_sess);
  396. if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
  397. /*
  398. * Session is offloaded and enabled. Map
  399. * doorbell register for this target
  400. */
  401. BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
  402. /* This counter is protected with hba mutex */
  403. hba->num_ofld_sess++;
  404. set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
  405. } else {
  406. /*
  407. * Offload or enable would have failed.
  408. * In offload/enable completion path, the
  409. * rport would have already been removed
  410. */
  411. BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
  412. "offloaded flag not set\n");
  413. }
  414. mutex_unlock(&hba->hba_mutex);
  415. break;
  416. case RPORT_EV_LOGO:
  417. case RPORT_EV_FAILED:
  418. case RPORT_EV_STOP:
  419. port_id = rdata->ids.port_id;
  420. if (port_id == FC_FID_DIR_SERV)
  421. break;
  422. if (!rport) {
  423. printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
  424. port_id);
  425. break;
  426. }
  427. rp = rport->dd_data;
  428. mutex_lock(&hba->hba_mutex);
  429. /*
  430. * Perform session upload. Note that rdata->peers is already
  431. * removed from disc->rports list before we get this event.
  432. */
  433. tgt = (struct bnx2fc_rport *)&rp[1];
  434. if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
  435. mutex_unlock(&hba->hba_mutex);
  436. break;
  437. }
  438. clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
  439. bnx2fc_upload_session(port, tgt);
  440. hba->num_ofld_sess--;
  441. BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
  442. hba->num_ofld_sess);
  443. /*
  444. * Try to wake up the linkdown wait thread. If num_ofld_sess
  445. * is 0, the waiting therad wakes up
  446. */
  447. if ((hba->wait_for_link_down) &&
  448. (hba->num_ofld_sess == 0)) {
  449. wake_up_interruptible(&hba->shutdown_wait);
  450. }
  451. if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
  452. printk(KERN_ERR PFX "Relogin to the tgt\n");
  453. mutex_lock(&lport->disc.disc_mutex);
  454. lport->tt.rport_login(rdata);
  455. mutex_unlock(&lport->disc.disc_mutex);
  456. }
  457. mutex_unlock(&hba->hba_mutex);
  458. break;
  459. case RPORT_EV_NONE:
  460. break;
  461. }
  462. }
  463. /**
  464. * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
  465. *
  466. * @port: fcoe_port struct to lookup the target port on
  467. * @port_id: The remote port ID to look up
  468. */
  469. struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
  470. u32 port_id)
  471. {
  472. struct bnx2fc_interface *interface = port->priv;
  473. struct bnx2fc_hba *hba = interface->hba;
  474. struct bnx2fc_rport *tgt;
  475. struct fc_rport_priv *rdata;
  476. int i;
  477. for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
  478. tgt = hba->tgt_ofld_list[i];
  479. if ((tgt) && (tgt->port == port)) {
  480. rdata = tgt->rdata;
  481. if (rdata->ids.port_id == port_id) {
  482. if (rdata->rp_state != RPORT_ST_DELETE) {
  483. BNX2FC_TGT_DBG(tgt, "rport "
  484. "obtained\n");
  485. return tgt;
  486. } else {
  487. BNX2FC_TGT_DBG(tgt, "rport 0x%x "
  488. "is in DELETED state\n",
  489. rdata->ids.port_id);
  490. return NULL;
  491. }
  492. }
  493. }
  494. }
  495. return NULL;
  496. }
  497. /**
  498. * bnx2fc_alloc_conn_id - allocates FCOE Connection id
  499. *
  500. * @hba: pointer to adapter structure
  501. * @tgt: pointer to bnx2fc_rport structure
  502. */
  503. static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
  504. struct bnx2fc_rport *tgt)
  505. {
  506. u32 conn_id, next;
  507. /* called with hba mutex held */
  508. /*
  509. * tgt_ofld_list access is synchronized using
  510. * both hba mutex and hba lock. Atleast hba mutex or
  511. * hba lock needs to be held for read access.
  512. */
  513. spin_lock_bh(&hba->hba_lock);
  514. next = hba->next_conn_id;
  515. conn_id = hba->next_conn_id++;
  516. if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
  517. hba->next_conn_id = 0;
  518. while (hba->tgt_ofld_list[conn_id] != NULL) {
  519. conn_id++;
  520. if (conn_id == BNX2FC_NUM_MAX_SESS)
  521. conn_id = 0;
  522. if (conn_id == next) {
  523. /* No free conn_ids are available */
  524. spin_unlock_bh(&hba->hba_lock);
  525. return -1;
  526. }
  527. }
  528. hba->tgt_ofld_list[conn_id] = tgt;
  529. tgt->fcoe_conn_id = conn_id;
  530. spin_unlock_bh(&hba->hba_lock);
  531. return conn_id;
  532. }
  533. static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
  534. {
  535. /* called with hba mutex held */
  536. spin_lock_bh(&hba->hba_lock);
  537. hba->tgt_ofld_list[conn_id] = NULL;
  538. hba->next_conn_id = conn_id;
  539. spin_unlock_bh(&hba->hba_lock);
  540. }
  541. /**
  542. *bnx2fc_alloc_session_resc - Allocate qp resources for the session
  543. *
  544. */
  545. static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
  546. struct bnx2fc_rport *tgt)
  547. {
  548. dma_addr_t page;
  549. int num_pages;
  550. u32 *pbl;
  551. /* Allocate and map SQ */
  552. tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
  553. tgt->sq_mem_size = (tgt->sq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  554. tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  555. &tgt->sq_dma, GFP_KERNEL);
  556. if (!tgt->sq) {
  557. printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
  558. tgt->sq_mem_size);
  559. goto mem_alloc_failure;
  560. }
  561. memset(tgt->sq, 0, tgt->sq_mem_size);
  562. /* Allocate and map CQ */
  563. tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
  564. tgt->cq_mem_size = (tgt->cq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  565. tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  566. &tgt->cq_dma, GFP_KERNEL);
  567. if (!tgt->cq) {
  568. printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
  569. tgt->cq_mem_size);
  570. goto mem_alloc_failure;
  571. }
  572. memset(tgt->cq, 0, tgt->cq_mem_size);
  573. /* Allocate and map RQ and RQ PBL */
  574. tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
  575. tgt->rq_mem_size = (tgt->rq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  576. tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  577. &tgt->rq_dma, GFP_KERNEL);
  578. if (!tgt->rq) {
  579. printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
  580. tgt->rq_mem_size);
  581. goto mem_alloc_failure;
  582. }
  583. memset(tgt->rq, 0, tgt->rq_mem_size);
  584. tgt->rq_pbl_size = (tgt->rq_mem_size / PAGE_SIZE) * sizeof(void *);
  585. tgt->rq_pbl_size = (tgt->rq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  586. tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  587. &tgt->rq_pbl_dma, GFP_KERNEL);
  588. if (!tgt->rq_pbl) {
  589. printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
  590. tgt->rq_pbl_size);
  591. goto mem_alloc_failure;
  592. }
  593. memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
  594. num_pages = tgt->rq_mem_size / PAGE_SIZE;
  595. page = tgt->rq_dma;
  596. pbl = (u32 *)tgt->rq_pbl;
  597. while (num_pages--) {
  598. *pbl = (u32)page;
  599. pbl++;
  600. *pbl = (u32)((u64)page >> 32);
  601. pbl++;
  602. page += PAGE_SIZE;
  603. }
  604. /* Allocate and map XFERQ */
  605. tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
  606. tgt->xferq_mem_size = (tgt->xferq_mem_size + (PAGE_SIZE - 1)) &
  607. PAGE_MASK;
  608. tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  609. &tgt->xferq_dma, GFP_KERNEL);
  610. if (!tgt->xferq) {
  611. printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
  612. tgt->xferq_mem_size);
  613. goto mem_alloc_failure;
  614. }
  615. memset(tgt->xferq, 0, tgt->xferq_mem_size);
  616. /* Allocate and map CONFQ & CONFQ PBL */
  617. tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
  618. tgt->confq_mem_size = (tgt->confq_mem_size + (PAGE_SIZE - 1)) &
  619. PAGE_MASK;
  620. tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  621. &tgt->confq_dma, GFP_KERNEL);
  622. if (!tgt->confq) {
  623. printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
  624. tgt->confq_mem_size);
  625. goto mem_alloc_failure;
  626. }
  627. memset(tgt->confq, 0, tgt->confq_mem_size);
  628. tgt->confq_pbl_size =
  629. (tgt->confq_mem_size / PAGE_SIZE) * sizeof(void *);
  630. tgt->confq_pbl_size =
  631. (tgt->confq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  632. tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
  633. tgt->confq_pbl_size,
  634. &tgt->confq_pbl_dma, GFP_KERNEL);
  635. if (!tgt->confq_pbl) {
  636. printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
  637. tgt->confq_pbl_size);
  638. goto mem_alloc_failure;
  639. }
  640. memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
  641. num_pages = tgt->confq_mem_size / PAGE_SIZE;
  642. page = tgt->confq_dma;
  643. pbl = (u32 *)tgt->confq_pbl;
  644. while (num_pages--) {
  645. *pbl = (u32)page;
  646. pbl++;
  647. *pbl = (u32)((u64)page >> 32);
  648. pbl++;
  649. page += PAGE_SIZE;
  650. }
  651. /* Allocate and map ConnDB */
  652. tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
  653. tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
  654. tgt->conn_db_mem_size,
  655. &tgt->conn_db_dma, GFP_KERNEL);
  656. if (!tgt->conn_db) {
  657. printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
  658. tgt->conn_db_mem_size);
  659. goto mem_alloc_failure;
  660. }
  661. memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
  662. /* Allocate and map LCQ */
  663. tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
  664. tgt->lcq_mem_size = (tgt->lcq_mem_size + (PAGE_SIZE - 1)) &
  665. PAGE_MASK;
  666. tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  667. &tgt->lcq_dma, GFP_KERNEL);
  668. if (!tgt->lcq) {
  669. printk(KERN_ERR PFX "unable to allocate lcq %d\n",
  670. tgt->lcq_mem_size);
  671. goto mem_alloc_failure;
  672. }
  673. memset(tgt->lcq, 0, tgt->lcq_mem_size);
  674. tgt->conn_db->rq_prod = 0x8000;
  675. return 0;
  676. mem_alloc_failure:
  677. bnx2fc_free_session_resc(hba, tgt);
  678. bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
  679. return -ENOMEM;
  680. }
  681. /**
  682. * bnx2i_free_session_resc - free qp resources for the session
  683. *
  684. * @hba: adapter structure pointer
  685. * @tgt: bnx2fc_rport structure pointer
  686. *
  687. * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
  688. */
  689. static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
  690. struct bnx2fc_rport *tgt)
  691. {
  692. BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
  693. if (tgt->ctx_base) {
  694. iounmap(tgt->ctx_base);
  695. tgt->ctx_base = NULL;
  696. }
  697. spin_lock_bh(&tgt->cq_lock);
  698. /* Free LCQ */
  699. if (tgt->lcq) {
  700. dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  701. tgt->lcq, tgt->lcq_dma);
  702. tgt->lcq = NULL;
  703. }
  704. /* Free connDB */
  705. if (tgt->conn_db) {
  706. dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
  707. tgt->conn_db, tgt->conn_db_dma);
  708. tgt->conn_db = NULL;
  709. }
  710. /* Free confq and confq pbl */
  711. if (tgt->confq_pbl) {
  712. dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
  713. tgt->confq_pbl, tgt->confq_pbl_dma);
  714. tgt->confq_pbl = NULL;
  715. }
  716. if (tgt->confq) {
  717. dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  718. tgt->confq, tgt->confq_dma);
  719. tgt->confq = NULL;
  720. }
  721. /* Free XFERQ */
  722. if (tgt->xferq) {
  723. dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  724. tgt->xferq, tgt->xferq_dma);
  725. tgt->xferq = NULL;
  726. }
  727. /* Free RQ PBL and RQ */
  728. if (tgt->rq_pbl) {
  729. dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  730. tgt->rq_pbl, tgt->rq_pbl_dma);
  731. tgt->rq_pbl = NULL;
  732. }
  733. if (tgt->rq) {
  734. dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  735. tgt->rq, tgt->rq_dma);
  736. tgt->rq = NULL;
  737. }
  738. /* Free CQ */
  739. if (tgt->cq) {
  740. dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  741. tgt->cq, tgt->cq_dma);
  742. tgt->cq = NULL;
  743. }
  744. /* Free SQ */
  745. if (tgt->sq) {
  746. dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  747. tgt->sq, tgt->sq_dma);
  748. tgt->sq = NULL;
  749. }
  750. spin_unlock_bh(&tgt->cq_lock);
  751. }