bnx2fc_tgt.c 24 KB

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