bnx2fc_tgt.c 24 KB

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