bnx2fc_tgt.c 23 KB

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