bnx2fc_tgt.c 24 KB

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