bnx2fc_tgt.c 24 KB

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