bnx2fc_tgt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /* bnx2fc_tgt.c: QLogic NetXtreme II Linux FCoE offload driver.
  2. * Handles operations such as session offload/upload etc, and manages
  3. * session resources such as connection id and qp resources.
  4. *
  5. * Copyright (c) 2008 - 2013 Broadcom Corporation
  6. * Copyright (c) 2014, 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. if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
  489. printk(KERN_ERR PFX "Relogin to the tgt\n");
  490. mutex_lock(&lport->disc.disc_mutex);
  491. lport->tt.rport_login(rdata);
  492. mutex_unlock(&lport->disc.disc_mutex);
  493. }
  494. mutex_unlock(&hba->hba_mutex);
  495. break;
  496. case RPORT_EV_NONE:
  497. break;
  498. }
  499. }
  500. /**
  501. * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
  502. *
  503. * @port: fcoe_port struct to lookup the target port on
  504. * @port_id: The remote port ID to look up
  505. */
  506. struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
  507. u32 port_id)
  508. {
  509. struct bnx2fc_interface *interface = port->priv;
  510. struct bnx2fc_hba *hba = interface->hba;
  511. struct bnx2fc_rport *tgt;
  512. struct fc_rport_priv *rdata;
  513. int i;
  514. for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
  515. tgt = hba->tgt_ofld_list[i];
  516. if ((tgt) && (tgt->port == port)) {
  517. rdata = tgt->rdata;
  518. if (rdata->ids.port_id == port_id) {
  519. if (rdata->rp_state != RPORT_ST_DELETE) {
  520. BNX2FC_TGT_DBG(tgt, "rport "
  521. "obtained\n");
  522. return tgt;
  523. } else {
  524. BNX2FC_TGT_DBG(tgt, "rport 0x%x "
  525. "is in DELETED state\n",
  526. rdata->ids.port_id);
  527. return NULL;
  528. }
  529. }
  530. }
  531. }
  532. return NULL;
  533. }
  534. /**
  535. * bnx2fc_alloc_conn_id - allocates FCOE Connection id
  536. *
  537. * @hba: pointer to adapter structure
  538. * @tgt: pointer to bnx2fc_rport structure
  539. */
  540. static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
  541. struct bnx2fc_rport *tgt)
  542. {
  543. u32 conn_id, next;
  544. /* called with hba mutex held */
  545. /*
  546. * tgt_ofld_list access is synchronized using
  547. * both hba mutex and hba lock. Atleast hba mutex or
  548. * hba lock needs to be held for read access.
  549. */
  550. spin_lock_bh(&hba->hba_lock);
  551. next = hba->next_conn_id;
  552. conn_id = hba->next_conn_id++;
  553. if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
  554. hba->next_conn_id = 0;
  555. while (hba->tgt_ofld_list[conn_id] != NULL) {
  556. conn_id++;
  557. if (conn_id == BNX2FC_NUM_MAX_SESS)
  558. conn_id = 0;
  559. if (conn_id == next) {
  560. /* No free conn_ids are available */
  561. spin_unlock_bh(&hba->hba_lock);
  562. return -1;
  563. }
  564. }
  565. hba->tgt_ofld_list[conn_id] = tgt;
  566. tgt->fcoe_conn_id = conn_id;
  567. spin_unlock_bh(&hba->hba_lock);
  568. return conn_id;
  569. }
  570. static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
  571. {
  572. /* called with hba mutex held */
  573. spin_lock_bh(&hba->hba_lock);
  574. hba->tgt_ofld_list[conn_id] = NULL;
  575. spin_unlock_bh(&hba->hba_lock);
  576. }
  577. /**
  578. *bnx2fc_alloc_session_resc - Allocate qp resources for the session
  579. *
  580. */
  581. static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
  582. struct bnx2fc_rport *tgt)
  583. {
  584. dma_addr_t page;
  585. int num_pages;
  586. u32 *pbl;
  587. /* Allocate and map SQ */
  588. tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
  589. tgt->sq_mem_size = (tgt->sq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  590. CNIC_PAGE_MASK;
  591. tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  592. &tgt->sq_dma, GFP_KERNEL);
  593. if (!tgt->sq) {
  594. printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
  595. tgt->sq_mem_size);
  596. goto mem_alloc_failure;
  597. }
  598. memset(tgt->sq, 0, tgt->sq_mem_size);
  599. /* Allocate and map CQ */
  600. tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
  601. tgt->cq_mem_size = (tgt->cq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  602. CNIC_PAGE_MASK;
  603. tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  604. &tgt->cq_dma, GFP_KERNEL);
  605. if (!tgt->cq) {
  606. printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
  607. tgt->cq_mem_size);
  608. goto mem_alloc_failure;
  609. }
  610. memset(tgt->cq, 0, tgt->cq_mem_size);
  611. /* Allocate and map RQ and RQ PBL */
  612. tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
  613. tgt->rq_mem_size = (tgt->rq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  614. CNIC_PAGE_MASK;
  615. tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  616. &tgt->rq_dma, GFP_KERNEL);
  617. if (!tgt->rq) {
  618. printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
  619. tgt->rq_mem_size);
  620. goto mem_alloc_failure;
  621. }
  622. memset(tgt->rq, 0, tgt->rq_mem_size);
  623. tgt->rq_pbl_size = (tgt->rq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
  624. tgt->rq_pbl_size = (tgt->rq_pbl_size + (CNIC_PAGE_SIZE - 1)) &
  625. CNIC_PAGE_MASK;
  626. tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  627. &tgt->rq_pbl_dma, GFP_KERNEL);
  628. if (!tgt->rq_pbl) {
  629. printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
  630. tgt->rq_pbl_size);
  631. goto mem_alloc_failure;
  632. }
  633. memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
  634. num_pages = tgt->rq_mem_size / CNIC_PAGE_SIZE;
  635. page = tgt->rq_dma;
  636. pbl = (u32 *)tgt->rq_pbl;
  637. while (num_pages--) {
  638. *pbl = (u32)page;
  639. pbl++;
  640. *pbl = (u32)((u64)page >> 32);
  641. pbl++;
  642. page += CNIC_PAGE_SIZE;
  643. }
  644. /* Allocate and map XFERQ */
  645. tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
  646. tgt->xferq_mem_size = (tgt->xferq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  647. CNIC_PAGE_MASK;
  648. tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  649. &tgt->xferq_dma, GFP_KERNEL);
  650. if (!tgt->xferq) {
  651. printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
  652. tgt->xferq_mem_size);
  653. goto mem_alloc_failure;
  654. }
  655. memset(tgt->xferq, 0, tgt->xferq_mem_size);
  656. /* Allocate and map CONFQ & CONFQ PBL */
  657. tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
  658. tgt->confq_mem_size = (tgt->confq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  659. CNIC_PAGE_MASK;
  660. tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  661. &tgt->confq_dma, GFP_KERNEL);
  662. if (!tgt->confq) {
  663. printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
  664. tgt->confq_mem_size);
  665. goto mem_alloc_failure;
  666. }
  667. memset(tgt->confq, 0, tgt->confq_mem_size);
  668. tgt->confq_pbl_size =
  669. (tgt->confq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
  670. tgt->confq_pbl_size =
  671. (tgt->confq_pbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
  672. tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
  673. tgt->confq_pbl_size,
  674. &tgt->confq_pbl_dma, GFP_KERNEL);
  675. if (!tgt->confq_pbl) {
  676. printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
  677. tgt->confq_pbl_size);
  678. goto mem_alloc_failure;
  679. }
  680. memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
  681. num_pages = tgt->confq_mem_size / CNIC_PAGE_SIZE;
  682. page = tgt->confq_dma;
  683. pbl = (u32 *)tgt->confq_pbl;
  684. while (num_pages--) {
  685. *pbl = (u32)page;
  686. pbl++;
  687. *pbl = (u32)((u64)page >> 32);
  688. pbl++;
  689. page += CNIC_PAGE_SIZE;
  690. }
  691. /* Allocate and map ConnDB */
  692. tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
  693. tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
  694. tgt->conn_db_mem_size,
  695. &tgt->conn_db_dma, GFP_KERNEL);
  696. if (!tgt->conn_db) {
  697. printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
  698. tgt->conn_db_mem_size);
  699. goto mem_alloc_failure;
  700. }
  701. memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
  702. /* Allocate and map LCQ */
  703. tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
  704. tgt->lcq_mem_size = (tgt->lcq_mem_size + (CNIC_PAGE_SIZE - 1)) &
  705. CNIC_PAGE_MASK;
  706. tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  707. &tgt->lcq_dma, GFP_KERNEL);
  708. if (!tgt->lcq) {
  709. printk(KERN_ERR PFX "unable to allocate lcq %d\n",
  710. tgt->lcq_mem_size);
  711. goto mem_alloc_failure;
  712. }
  713. memset(tgt->lcq, 0, tgt->lcq_mem_size);
  714. tgt->conn_db->rq_prod = 0x8000;
  715. return 0;
  716. mem_alloc_failure:
  717. return -ENOMEM;
  718. }
  719. /**
  720. * bnx2i_free_session_resc - free qp resources for the session
  721. *
  722. * @hba: adapter structure pointer
  723. * @tgt: bnx2fc_rport structure pointer
  724. *
  725. * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
  726. */
  727. static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
  728. struct bnx2fc_rport *tgt)
  729. {
  730. void __iomem *ctx_base_ptr;
  731. BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
  732. spin_lock_bh(&tgt->cq_lock);
  733. ctx_base_ptr = tgt->ctx_base;
  734. tgt->ctx_base = NULL;
  735. /* Free LCQ */
  736. if (tgt->lcq) {
  737. dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  738. tgt->lcq, tgt->lcq_dma);
  739. tgt->lcq = NULL;
  740. }
  741. /* Free connDB */
  742. if (tgt->conn_db) {
  743. dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
  744. tgt->conn_db, tgt->conn_db_dma);
  745. tgt->conn_db = NULL;
  746. }
  747. /* Free confq and confq pbl */
  748. if (tgt->confq_pbl) {
  749. dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
  750. tgt->confq_pbl, tgt->confq_pbl_dma);
  751. tgt->confq_pbl = NULL;
  752. }
  753. if (tgt->confq) {
  754. dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  755. tgt->confq, tgt->confq_dma);
  756. tgt->confq = NULL;
  757. }
  758. /* Free XFERQ */
  759. if (tgt->xferq) {
  760. dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  761. tgt->xferq, tgt->xferq_dma);
  762. tgt->xferq = NULL;
  763. }
  764. /* Free RQ PBL and RQ */
  765. if (tgt->rq_pbl) {
  766. dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  767. tgt->rq_pbl, tgt->rq_pbl_dma);
  768. tgt->rq_pbl = NULL;
  769. }
  770. if (tgt->rq) {
  771. dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  772. tgt->rq, tgt->rq_dma);
  773. tgt->rq = NULL;
  774. }
  775. /* Free CQ */
  776. if (tgt->cq) {
  777. dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  778. tgt->cq, tgt->cq_dma);
  779. tgt->cq = NULL;
  780. }
  781. /* Free SQ */
  782. if (tgt->sq) {
  783. dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  784. tgt->sq, tgt->sq_dma);
  785. tgt->sq = NULL;
  786. }
  787. spin_unlock_bh(&tgt->cq_lock);
  788. if (ctx_base_ptr)
  789. iounmap(ctx_base_ptr);
  790. }