bnx2fc_tgt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
  328. !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
  329. tgt->dev_type = TYPE_TAPE;
  330. tgt->io_timeout = 0; /* use default ULP timeout */
  331. } else {
  332. tgt->dev_type = TYPE_DISK;
  333. tgt->io_timeout = BNX2FC_IO_TIMEOUT;
  334. }
  335. /* initialize sq doorbell */
  336. sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
  337. sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
  338. B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
  339. /* initialize rx doorbell */
  340. rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
  341. (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
  342. (B577XX_FCOE_CONNECTION_TYPE <<
  343. B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
  344. rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
  345. (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
  346. spin_lock_init(&tgt->tgt_lock);
  347. spin_lock_init(&tgt->cq_lock);
  348. /* Initialize active_cmd_queue list */
  349. INIT_LIST_HEAD(&tgt->active_cmd_queue);
  350. /* Initialize IO retire queue */
  351. INIT_LIST_HEAD(&tgt->io_retire_queue);
  352. INIT_LIST_HEAD(&tgt->els_queue);
  353. /* Initialize active_tm_queue list */
  354. INIT_LIST_HEAD(&tgt->active_tm_queue);
  355. init_waitqueue_head(&tgt->ofld_wait);
  356. init_waitqueue_head(&tgt->upld_wait);
  357. return 0;
  358. }
  359. /**
  360. * This event_callback is called after successful completion of libfc
  361. * initiated target login. bnx2fc can proceed with initiating the session
  362. * establishment.
  363. */
  364. void bnx2fc_rport_event_handler(struct fc_lport *lport,
  365. struct fc_rport_priv *rdata,
  366. enum fc_rport_event event)
  367. {
  368. struct fcoe_port *port = lport_priv(lport);
  369. struct bnx2fc_interface *interface = port->priv;
  370. struct bnx2fc_hba *hba = interface->hba;
  371. struct fc_rport *rport = rdata->rport;
  372. struct fc_rport_libfc_priv *rp;
  373. struct bnx2fc_rport *tgt;
  374. u32 port_id;
  375. BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
  376. event, rdata->ids.port_id);
  377. switch (event) {
  378. case RPORT_EV_READY:
  379. if (!rport) {
  380. printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
  381. break;
  382. }
  383. rp = rport->dd_data;
  384. if (rport->port_id == FC_FID_DIR_SERV) {
  385. /*
  386. * bnx2fc_rport structure doesn't exist for
  387. * directory server.
  388. * We should not come here, as lport will
  389. * take care of fabric login
  390. */
  391. printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
  392. rdata->ids.port_id);
  393. break;
  394. }
  395. if (rdata->spp_type != FC_TYPE_FCP) {
  396. BNX2FC_HBA_DBG(lport, "not FCP type target."
  397. " not offloading\n");
  398. break;
  399. }
  400. if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
  401. BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
  402. " not offloading\n");
  403. break;
  404. }
  405. /*
  406. * Offlaod process is protected with hba mutex.
  407. * Use the same mutex_lock for upload process too
  408. */
  409. mutex_lock(&hba->hba_mutex);
  410. tgt = (struct bnx2fc_rport *)&rp[1];
  411. /* This can happen when ADISC finds the same target */
  412. if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
  413. BNX2FC_TGT_DBG(tgt, "already offloaded\n");
  414. mutex_unlock(&hba->hba_mutex);
  415. return;
  416. }
  417. /*
  418. * Offload the session. This is a blocking call, and will
  419. * wait until the session is offloaded.
  420. */
  421. bnx2fc_offload_session(port, tgt, rdata);
  422. BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
  423. hba->num_ofld_sess);
  424. if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
  425. /*
  426. * Session is offloaded and enabled. Map
  427. * doorbell register for this target
  428. */
  429. BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
  430. /* This counter is protected with hba mutex */
  431. hba->num_ofld_sess++;
  432. set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
  433. } else {
  434. /*
  435. * Offload or enable would have failed.
  436. * In offload/enable completion path, the
  437. * rport would have already been removed
  438. */
  439. BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
  440. "offloaded flag not set\n");
  441. }
  442. mutex_unlock(&hba->hba_mutex);
  443. break;
  444. case RPORT_EV_LOGO:
  445. case RPORT_EV_FAILED:
  446. case RPORT_EV_STOP:
  447. port_id = rdata->ids.port_id;
  448. if (port_id == FC_FID_DIR_SERV)
  449. break;
  450. if (!rport) {
  451. printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
  452. port_id);
  453. break;
  454. }
  455. rp = rport->dd_data;
  456. mutex_lock(&hba->hba_mutex);
  457. /*
  458. * Perform session upload. Note that rdata->peers is already
  459. * removed from disc->rports list before we get this event.
  460. */
  461. tgt = (struct bnx2fc_rport *)&rp[1];
  462. if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
  463. mutex_unlock(&hba->hba_mutex);
  464. break;
  465. }
  466. clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
  467. bnx2fc_upload_session(port, tgt);
  468. hba->num_ofld_sess--;
  469. BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
  470. hba->num_ofld_sess);
  471. /*
  472. * Try to wake up the linkdown wait thread. If num_ofld_sess
  473. * is 0, the waiting therad wakes up
  474. */
  475. if ((hba->wait_for_link_down) &&
  476. (hba->num_ofld_sess == 0)) {
  477. wake_up_interruptible(&hba->shutdown_wait);
  478. }
  479. if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
  480. printk(KERN_ERR PFX "Relogin to the tgt\n");
  481. mutex_lock(&lport->disc.disc_mutex);
  482. lport->tt.rport_login(rdata);
  483. mutex_unlock(&lport->disc.disc_mutex);
  484. }
  485. mutex_unlock(&hba->hba_mutex);
  486. break;
  487. case RPORT_EV_NONE:
  488. break;
  489. }
  490. }
  491. /**
  492. * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
  493. *
  494. * @port: fcoe_port struct to lookup the target port on
  495. * @port_id: The remote port ID to look up
  496. */
  497. struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
  498. u32 port_id)
  499. {
  500. struct bnx2fc_interface *interface = port->priv;
  501. struct bnx2fc_hba *hba = interface->hba;
  502. struct bnx2fc_rport *tgt;
  503. struct fc_rport_priv *rdata;
  504. int i;
  505. for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
  506. tgt = hba->tgt_ofld_list[i];
  507. if ((tgt) && (tgt->port == port)) {
  508. rdata = tgt->rdata;
  509. if (rdata->ids.port_id == port_id) {
  510. if (rdata->rp_state != RPORT_ST_DELETE) {
  511. BNX2FC_TGT_DBG(tgt, "rport "
  512. "obtained\n");
  513. return tgt;
  514. } else {
  515. BNX2FC_TGT_DBG(tgt, "rport 0x%x "
  516. "is in DELETED state\n",
  517. rdata->ids.port_id);
  518. return NULL;
  519. }
  520. }
  521. }
  522. }
  523. return NULL;
  524. }
  525. /**
  526. * bnx2fc_alloc_conn_id - allocates FCOE Connection id
  527. *
  528. * @hba: pointer to adapter structure
  529. * @tgt: pointer to bnx2fc_rport structure
  530. */
  531. static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
  532. struct bnx2fc_rport *tgt)
  533. {
  534. u32 conn_id, next;
  535. /* called with hba mutex held */
  536. /*
  537. * tgt_ofld_list access is synchronized using
  538. * both hba mutex and hba lock. Atleast hba mutex or
  539. * hba lock needs to be held for read access.
  540. */
  541. spin_lock_bh(&hba->hba_lock);
  542. next = hba->next_conn_id;
  543. conn_id = hba->next_conn_id++;
  544. if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
  545. hba->next_conn_id = 0;
  546. while (hba->tgt_ofld_list[conn_id] != NULL) {
  547. conn_id++;
  548. if (conn_id == BNX2FC_NUM_MAX_SESS)
  549. conn_id = 0;
  550. if (conn_id == next) {
  551. /* No free conn_ids are available */
  552. spin_unlock_bh(&hba->hba_lock);
  553. return -1;
  554. }
  555. }
  556. hba->tgt_ofld_list[conn_id] = tgt;
  557. tgt->fcoe_conn_id = conn_id;
  558. spin_unlock_bh(&hba->hba_lock);
  559. return conn_id;
  560. }
  561. static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
  562. {
  563. /* called with hba mutex held */
  564. spin_lock_bh(&hba->hba_lock);
  565. hba->tgt_ofld_list[conn_id] = NULL;
  566. spin_unlock_bh(&hba->hba_lock);
  567. }
  568. /**
  569. *bnx2fc_alloc_session_resc - Allocate qp resources for the session
  570. *
  571. */
  572. static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
  573. struct bnx2fc_rport *tgt)
  574. {
  575. dma_addr_t page;
  576. int num_pages;
  577. u32 *pbl;
  578. /* Allocate and map SQ */
  579. tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
  580. tgt->sq_mem_size = (tgt->sq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  581. tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  582. &tgt->sq_dma, GFP_KERNEL);
  583. if (!tgt->sq) {
  584. printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
  585. tgt->sq_mem_size);
  586. goto mem_alloc_failure;
  587. }
  588. memset(tgt->sq, 0, tgt->sq_mem_size);
  589. /* Allocate and map CQ */
  590. tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
  591. tgt->cq_mem_size = (tgt->cq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  592. tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  593. &tgt->cq_dma, GFP_KERNEL);
  594. if (!tgt->cq) {
  595. printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
  596. tgt->cq_mem_size);
  597. goto mem_alloc_failure;
  598. }
  599. memset(tgt->cq, 0, tgt->cq_mem_size);
  600. /* Allocate and map RQ and RQ PBL */
  601. tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
  602. tgt->rq_mem_size = (tgt->rq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  603. tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  604. &tgt->rq_dma, GFP_KERNEL);
  605. if (!tgt->rq) {
  606. printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
  607. tgt->rq_mem_size);
  608. goto mem_alloc_failure;
  609. }
  610. memset(tgt->rq, 0, tgt->rq_mem_size);
  611. tgt->rq_pbl_size = (tgt->rq_mem_size / PAGE_SIZE) * sizeof(void *);
  612. tgt->rq_pbl_size = (tgt->rq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  613. tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  614. &tgt->rq_pbl_dma, GFP_KERNEL);
  615. if (!tgt->rq_pbl) {
  616. printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
  617. tgt->rq_pbl_size);
  618. goto mem_alloc_failure;
  619. }
  620. memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
  621. num_pages = tgt->rq_mem_size / PAGE_SIZE;
  622. page = tgt->rq_dma;
  623. pbl = (u32 *)tgt->rq_pbl;
  624. while (num_pages--) {
  625. *pbl = (u32)page;
  626. pbl++;
  627. *pbl = (u32)((u64)page >> 32);
  628. pbl++;
  629. page += PAGE_SIZE;
  630. }
  631. /* Allocate and map XFERQ */
  632. tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
  633. tgt->xferq_mem_size = (tgt->xferq_mem_size + (PAGE_SIZE - 1)) &
  634. PAGE_MASK;
  635. tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  636. &tgt->xferq_dma, GFP_KERNEL);
  637. if (!tgt->xferq) {
  638. printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
  639. tgt->xferq_mem_size);
  640. goto mem_alloc_failure;
  641. }
  642. memset(tgt->xferq, 0, tgt->xferq_mem_size);
  643. /* Allocate and map CONFQ & CONFQ PBL */
  644. tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
  645. tgt->confq_mem_size = (tgt->confq_mem_size + (PAGE_SIZE - 1)) &
  646. PAGE_MASK;
  647. tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  648. &tgt->confq_dma, GFP_KERNEL);
  649. if (!tgt->confq) {
  650. printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
  651. tgt->confq_mem_size);
  652. goto mem_alloc_failure;
  653. }
  654. memset(tgt->confq, 0, tgt->confq_mem_size);
  655. tgt->confq_pbl_size =
  656. (tgt->confq_mem_size / PAGE_SIZE) * sizeof(void *);
  657. tgt->confq_pbl_size =
  658. (tgt->confq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  659. tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
  660. tgt->confq_pbl_size,
  661. &tgt->confq_pbl_dma, GFP_KERNEL);
  662. if (!tgt->confq_pbl) {
  663. printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
  664. tgt->confq_pbl_size);
  665. goto mem_alloc_failure;
  666. }
  667. memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
  668. num_pages = tgt->confq_mem_size / PAGE_SIZE;
  669. page = tgt->confq_dma;
  670. pbl = (u32 *)tgt->confq_pbl;
  671. while (num_pages--) {
  672. *pbl = (u32)page;
  673. pbl++;
  674. *pbl = (u32)((u64)page >> 32);
  675. pbl++;
  676. page += PAGE_SIZE;
  677. }
  678. /* Allocate and map ConnDB */
  679. tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
  680. tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
  681. tgt->conn_db_mem_size,
  682. &tgt->conn_db_dma, GFP_KERNEL);
  683. if (!tgt->conn_db) {
  684. printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
  685. tgt->conn_db_mem_size);
  686. goto mem_alloc_failure;
  687. }
  688. memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
  689. /* Allocate and map LCQ */
  690. tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
  691. tgt->lcq_mem_size = (tgt->lcq_mem_size + (PAGE_SIZE - 1)) &
  692. PAGE_MASK;
  693. tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  694. &tgt->lcq_dma, GFP_KERNEL);
  695. if (!tgt->lcq) {
  696. printk(KERN_ERR PFX "unable to allocate lcq %d\n",
  697. tgt->lcq_mem_size);
  698. goto mem_alloc_failure;
  699. }
  700. memset(tgt->lcq, 0, tgt->lcq_mem_size);
  701. tgt->conn_db->rq_prod = 0x8000;
  702. return 0;
  703. mem_alloc_failure:
  704. return -ENOMEM;
  705. }
  706. /**
  707. * bnx2i_free_session_resc - free qp resources for the session
  708. *
  709. * @hba: adapter structure pointer
  710. * @tgt: bnx2fc_rport structure pointer
  711. *
  712. * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
  713. */
  714. static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
  715. struct bnx2fc_rport *tgt)
  716. {
  717. void __iomem *ctx_base_ptr;
  718. BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
  719. spin_lock_bh(&tgt->cq_lock);
  720. ctx_base_ptr = tgt->ctx_base;
  721. tgt->ctx_base = NULL;
  722. /* Free LCQ */
  723. if (tgt->lcq) {
  724. dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
  725. tgt->lcq, tgt->lcq_dma);
  726. tgt->lcq = NULL;
  727. }
  728. /* Free connDB */
  729. if (tgt->conn_db) {
  730. dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
  731. tgt->conn_db, tgt->conn_db_dma);
  732. tgt->conn_db = NULL;
  733. }
  734. /* Free confq and confq pbl */
  735. if (tgt->confq_pbl) {
  736. dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
  737. tgt->confq_pbl, tgt->confq_pbl_dma);
  738. tgt->confq_pbl = NULL;
  739. }
  740. if (tgt->confq) {
  741. dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
  742. tgt->confq, tgt->confq_dma);
  743. tgt->confq = NULL;
  744. }
  745. /* Free XFERQ */
  746. if (tgt->xferq) {
  747. dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
  748. tgt->xferq, tgt->xferq_dma);
  749. tgt->xferq = NULL;
  750. }
  751. /* Free RQ PBL and RQ */
  752. if (tgt->rq_pbl) {
  753. dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
  754. tgt->rq_pbl, tgt->rq_pbl_dma);
  755. tgt->rq_pbl = NULL;
  756. }
  757. if (tgt->rq) {
  758. dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
  759. tgt->rq, tgt->rq_dma);
  760. tgt->rq = NULL;
  761. }
  762. /* Free CQ */
  763. if (tgt->cq) {
  764. dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
  765. tgt->cq, tgt->cq_dma);
  766. tgt->cq = NULL;
  767. }
  768. /* Free SQ */
  769. if (tgt->sq) {
  770. dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
  771. tgt->sq, tgt->sq_dma);
  772. tgt->sq = NULL;
  773. }
  774. spin_unlock_bh(&tgt->cq_lock);
  775. if (ctx_base_ptr)
  776. iounmap(ctx_base_ptr);
  777. }