bnx2fc_tgt.c 23 KB

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