bnx2fc_tgt.c 22 KB

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