bnx2fc_tgt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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-2015 QLogic Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. *
  12. * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
  13. */
  14. #include "bnx2fc.h"
  15. static void bnx2fc_upld_timer(unsigned long data);
  16. static void bnx2fc_ofld_timer(unsigned long data);
  17. static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
  18. struct fcoe_port *port,
  19. struct fc_rport_priv *rdata);
  20. static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
  21. struct bnx2fc_rport *tgt);
  22. static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
  23. struct bnx2fc_rport *tgt);
  24. static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
  25. struct bnx2fc_rport *tgt);
  26. static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id);
  27. static void bnx2fc_upld_timer(unsigned long data)
  28. {
  29. struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
  30. BNX2FC_TGT_DBG(tgt, "upld_timer - Upload compl not received!!\n");
  31. /* fake upload completion */
  32. clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
  33. clear_bit(BNX2FC_FLAG_ENABLED, &tgt->flags);
  34. set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
  35. wake_up_interruptible(&tgt->upld_wait);
  36. }
  37. static void bnx2fc_ofld_timer(unsigned long data)
  38. {
  39. struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
  40. BNX2FC_TGT_DBG(tgt, "entered bnx2fc_ofld_timer\n");
  41. /* NOTE: This function should never be called, as
  42. * offload should never timeout
  43. */
  44. /*
  45. * If the timer has expired, this session is dead
  46. * Clear offloaded flag and logout of this device.
  47. * Since OFFLOADED flag is cleared, this case
  48. * will be considered as offload error and the
  49. * port will be logged off, and conn_id, session
  50. * resources are freed up in bnx2fc_offload_session
  51. */
  52. clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
  53. clear_bit(BNX2FC_FLAG_ENABLED, &tgt->flags);
  54. set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
  55. wake_up_interruptible(&tgt->ofld_wait);
  56. }
  57. static void bnx2fc_ofld_wait(struct bnx2fc_rport *tgt)
  58. {
  59. setup_timer(&tgt->ofld_timer, bnx2fc_ofld_timer, (unsigned long)tgt);
  60. mod_timer(&tgt->ofld_timer, jiffies + BNX2FC_FW_TIMEOUT);
  61. wait_event_interruptible(tgt->ofld_wait,
  62. (test_bit(
  63. BNX2FC_FLAG_OFLD_REQ_CMPL,
  64. &tgt->flags)));
  65. if (signal_pending(current))
  66. flush_signals(current);
  67. del_timer_sync(&tgt->ofld_timer);
  68. }
  69. static void bnx2fc_offload_session(struct fcoe_port *port,
  70. struct bnx2fc_rport *tgt,
  71. struct fc_rport_priv *rdata)
  72. {
  73. struct fc_lport *lport = rdata->local_port;
  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. lport->tt.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. setup_timer(&tgt->upld_timer, 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 (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_alloc_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. memset(tgt->sq, 0, tgt->sq_mem_size);
  593. /* Allocate and map CQ */
  594. tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
  595. tgt->cq_mem_size = (tgt->cq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  596. CNIC_PAGE_MASK;
  597. tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  598. &tgt->cq_dma, GFP_KERNEL);
  599. if (!tgt->cq) {
  600. printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
  601. tgt->cq_mem_size);
  602. goto mem_alloc_failure;
  603. }
  604. memset(tgt->cq, 0, tgt->cq_mem_size);
  605. /* Allocate and map RQ and RQ PBL */
  606. tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
  607. tgt->rq_mem_size = (tgt->rq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  608. CNIC_PAGE_MASK;
  609. tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  610. &tgt->rq_dma, GFP_KERNEL);
  611. if (!tgt->rq) {
  612. printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
  613. tgt->rq_mem_size);
  614. goto mem_alloc_failure;
  615. }
  616. memset(tgt->rq, 0, tgt->rq_mem_size);
  617. tgt->rq_pbl_size = (tgt->rq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
  618. tgt->rq_pbl_size = (tgt->rq_pbl_size + (CNIC_PAGE_SIZE - 1)) &
  619. CNIC_PAGE_MASK;
  620. tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  621. &tgt->rq_pbl_dma, GFP_KERNEL);
  622. if (!tgt->rq_pbl) {
  623. printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
  624. tgt->rq_pbl_size);
  625. goto mem_alloc_failure;
  626. }
  627. memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
  628. num_pages = tgt->rq_mem_size / CNIC_PAGE_SIZE;
  629. page = tgt->rq_dma;
  630. pbl = (u32 *)tgt->rq_pbl;
  631. while (num_pages--) {
  632. *pbl = (u32)page;
  633. pbl++;
  634. *pbl = (u32)((u64)page >> 32);
  635. pbl++;
  636. page += CNIC_PAGE_SIZE;
  637. }
  638. /* Allocate and map XFERQ */
  639. tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
  640. tgt->xferq_mem_size = (tgt->xferq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  641. CNIC_PAGE_MASK;
  642. tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  643. &tgt->xferq_dma, GFP_KERNEL);
  644. if (!tgt->xferq) {
  645. printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
  646. tgt->xferq_mem_size);
  647. goto mem_alloc_failure;
  648. }
  649. memset(tgt->xferq, 0, tgt->xferq_mem_size);
  650. /* Allocate and map CONFQ & CONFQ PBL */
  651. tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
  652. tgt->confq_mem_size = (tgt->confq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  653. CNIC_PAGE_MASK;
  654. tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  655. &tgt->confq_dma, GFP_KERNEL);
  656. if (!tgt->confq) {
  657. printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
  658. tgt->confq_mem_size);
  659. goto mem_alloc_failure;
  660. }
  661. memset(tgt->confq, 0, tgt->confq_mem_size);
  662. tgt->confq_pbl_size =
  663. (tgt->confq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
  664. tgt->confq_pbl_size =
  665. (tgt->confq_pbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
  666. tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
  667. tgt->confq_pbl_size,
  668. &tgt->confq_pbl_dma, GFP_KERNEL);
  669. if (!tgt->confq_pbl) {
  670. printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
  671. tgt->confq_pbl_size);
  672. goto mem_alloc_failure;
  673. }
  674. memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
  675. num_pages = tgt->confq_mem_size / CNIC_PAGE_SIZE;
  676. page = tgt->confq_dma;
  677. pbl = (u32 *)tgt->confq_pbl;
  678. while (num_pages--) {
  679. *pbl = (u32)page;
  680. pbl++;
  681. *pbl = (u32)((u64)page >> 32);
  682. pbl++;
  683. page += CNIC_PAGE_SIZE;
  684. }
  685. /* Allocate and map ConnDB */
  686. tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
  687. tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
  688. tgt->conn_db_mem_size,
  689. &tgt->conn_db_dma, GFP_KERNEL);
  690. if (!tgt->conn_db) {
  691. printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
  692. tgt->conn_db_mem_size);
  693. goto mem_alloc_failure;
  694. }
  695. memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
  696. /* Allocate and map LCQ */
  697. tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
  698. tgt->lcq_mem_size = (tgt->lcq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  699. CNIC_PAGE_MASK;
  700. tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  701. &tgt->lcq_dma, GFP_KERNEL);
  702. if (!tgt->lcq) {
  703. printk(KERN_ERR PFX "unable to allocate lcq %d\n",
  704. tgt->lcq_mem_size);
  705. goto mem_alloc_failure;
  706. }
  707. memset(tgt->lcq, 0, tgt->lcq_mem_size);
  708. tgt->conn_db->rq_prod = 0x8000;
  709. return 0;
  710. mem_alloc_failure:
  711. return -ENOMEM;
  712. }
  713. /**
  714. * bnx2i_free_session_resc - free qp resources for the session
  715. *
  716. * @hba: adapter structure pointer
  717. * @tgt: bnx2fc_rport structure pointer
  718. *
  719. * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
  720. */
  721. static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
  722. struct bnx2fc_rport *tgt)
  723. {
  724. void __iomem *ctx_base_ptr;
  725. BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
  726. spin_lock_bh(&tgt->cq_lock);
  727. ctx_base_ptr = tgt->ctx_base;
  728. tgt->ctx_base = NULL;
  729. /* Free LCQ */
  730. if (tgt->lcq) {
  731. dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  732. tgt->lcq, tgt->lcq_dma);
  733. tgt->lcq = NULL;
  734. }
  735. /* Free connDB */
  736. if (tgt->conn_db) {
  737. dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
  738. tgt->conn_db, tgt->conn_db_dma);
  739. tgt->conn_db = NULL;
  740. }
  741. /* Free confq and confq pbl */
  742. if (tgt->confq_pbl) {
  743. dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
  744. tgt->confq_pbl, tgt->confq_pbl_dma);
  745. tgt->confq_pbl = NULL;
  746. }
  747. if (tgt->confq) {
  748. dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  749. tgt->confq, tgt->confq_dma);
  750. tgt->confq = NULL;
  751. }
  752. /* Free XFERQ */
  753. if (tgt->xferq) {
  754. dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  755. tgt->xferq, tgt->xferq_dma);
  756. tgt->xferq = NULL;
  757. }
  758. /* Free RQ PBL and RQ */
  759. if (tgt->rq_pbl) {
  760. dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  761. tgt->rq_pbl, tgt->rq_pbl_dma);
  762. tgt->rq_pbl = NULL;
  763. }
  764. if (tgt->rq) {
  765. dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  766. tgt->rq, tgt->rq_dma);
  767. tgt->rq = NULL;
  768. }
  769. /* Free CQ */
  770. if (tgt->cq) {
  771. dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  772. tgt->cq, tgt->cq_dma);
  773. tgt->cq = NULL;
  774. }
  775. /* Free SQ */
  776. if (tgt->sq) {
  777. dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  778. tgt->sq, tgt->sq_dma);
  779. tgt->sq = NULL;
  780. }
  781. spin_unlock_bh(&tgt->cq_lock);
  782. if (ctx_base_ptr)
  783. iounmap(ctx_base_ptr);
  784. }