bnx2fc_tgt.c 25 KB

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