bnx2fc_tgt.c 24 KB

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