bnx2fc_tgt.c 22 KB

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