qedf_els.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * QLogic FCoE Offload Driver
  3. * Copyright (c) 2016-2018 Cavium Inc.
  4. *
  5. * This software is available under the terms of the GNU General Public License
  6. * (GPL) Version 2, available from the file COPYING in the main directory of
  7. * this source tree.
  8. */
  9. #include "qedf.h"
  10. /* It's assumed that the lock is held when calling this function. */
  11. static int qedf_initiate_els(struct qedf_rport *fcport, unsigned int op,
  12. void *data, uint32_t data_len,
  13. void (*cb_func)(struct qedf_els_cb_arg *cb_arg),
  14. struct qedf_els_cb_arg *cb_arg, uint32_t timer_msec)
  15. {
  16. struct qedf_ctx *qedf;
  17. struct fc_lport *lport;
  18. struct qedf_ioreq *els_req;
  19. struct qedf_mp_req *mp_req;
  20. struct fc_frame_header *fc_hdr;
  21. struct e4_fcoe_task_context *task;
  22. int rc = 0;
  23. uint32_t did, sid;
  24. uint16_t xid;
  25. struct fcoe_wqe *sqe;
  26. unsigned long flags;
  27. u16 sqe_idx;
  28. if (!fcport) {
  29. QEDF_ERR(NULL, "fcport is NULL");
  30. rc = -EINVAL;
  31. goto els_err;
  32. }
  33. qedf = fcport->qedf;
  34. lport = qedf->lport;
  35. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Sending ELS\n");
  36. rc = fc_remote_port_chkready(fcport->rport);
  37. if (rc) {
  38. QEDF_ERR(&(qedf->dbg_ctx), "els 0x%x: rport not ready\n", op);
  39. rc = -EAGAIN;
  40. goto els_err;
  41. }
  42. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  43. QEDF_ERR(&(qedf->dbg_ctx), "els 0x%x: link is not ready\n",
  44. op);
  45. rc = -EAGAIN;
  46. goto els_err;
  47. }
  48. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  49. QEDF_ERR(&(qedf->dbg_ctx), "els 0x%x: fcport not ready\n", op);
  50. rc = -EINVAL;
  51. goto els_err;
  52. }
  53. els_req = qedf_alloc_cmd(fcport, QEDF_ELS);
  54. if (!els_req) {
  55. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_ELS,
  56. "Failed to alloc ELS request 0x%x\n", op);
  57. rc = -ENOMEM;
  58. goto els_err;
  59. }
  60. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "initiate_els els_req = "
  61. "0x%p cb_arg = %p xid = %x\n", els_req, cb_arg,
  62. els_req->xid);
  63. els_req->sc_cmd = NULL;
  64. els_req->cmd_type = QEDF_ELS;
  65. els_req->fcport = fcport;
  66. els_req->cb_func = cb_func;
  67. cb_arg->io_req = els_req;
  68. cb_arg->op = op;
  69. els_req->cb_arg = cb_arg;
  70. els_req->data_xfer_len = data_len;
  71. /* Record which cpu this request is associated with */
  72. els_req->cpu = smp_processor_id();
  73. mp_req = (struct qedf_mp_req *)&(els_req->mp_req);
  74. rc = qedf_init_mp_req(els_req);
  75. if (rc) {
  76. QEDF_ERR(&(qedf->dbg_ctx), "ELS MP request init failed\n");
  77. kref_put(&els_req->refcount, qedf_release_cmd);
  78. goto els_err;
  79. } else {
  80. rc = 0;
  81. }
  82. /* Fill ELS Payload */
  83. if ((op >= ELS_LS_RJT) && (op <= ELS_AUTH_ELS)) {
  84. memcpy(mp_req->req_buf, data, data_len);
  85. } else {
  86. QEDF_ERR(&(qedf->dbg_ctx), "Invalid ELS op 0x%x\n", op);
  87. els_req->cb_func = NULL;
  88. els_req->cb_arg = NULL;
  89. kref_put(&els_req->refcount, qedf_release_cmd);
  90. rc = -EINVAL;
  91. }
  92. if (rc)
  93. goto els_err;
  94. /* Fill FC header */
  95. fc_hdr = &(mp_req->req_fc_hdr);
  96. did = fcport->rdata->ids.port_id;
  97. sid = fcport->sid;
  98. __fc_fill_fc_hdr(fc_hdr, FC_RCTL_ELS_REQ, did, sid,
  99. FC_TYPE_ELS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
  100. FC_FC_SEQ_INIT, 0);
  101. /* Obtain exchange id */
  102. xid = els_req->xid;
  103. spin_lock_irqsave(&fcport->rport_lock, flags);
  104. sqe_idx = qedf_get_sqe_idx(fcport);
  105. sqe = &fcport->sq[sqe_idx];
  106. memset(sqe, 0, sizeof(struct fcoe_wqe));
  107. /* Initialize task context for this IO request */
  108. task = qedf_get_task_mem(&qedf->tasks, xid);
  109. qedf_init_mp_task(els_req, task, sqe);
  110. /* Put timer on original I/O request */
  111. if (timer_msec)
  112. qedf_cmd_timer_set(qedf, els_req, timer_msec);
  113. /* Ring doorbell */
  114. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Ringing doorbell for ELS "
  115. "req\n");
  116. qedf_ring_doorbell(fcport);
  117. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  118. els_err:
  119. return rc;
  120. }
  121. void qedf_process_els_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  122. struct qedf_ioreq *els_req)
  123. {
  124. struct fcoe_task_context *task_ctx;
  125. struct scsi_cmnd *sc_cmd;
  126. uint16_t xid;
  127. struct fcoe_cqe_midpath_info *mp_info;
  128. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Entered with xid = 0x%x"
  129. " cmd_type = %d.\n", els_req->xid, els_req->cmd_type);
  130. /* Kill the ELS timer */
  131. cancel_delayed_work(&els_req->timeout_work);
  132. xid = els_req->xid;
  133. task_ctx = qedf_get_task_mem(&qedf->tasks, xid);
  134. sc_cmd = els_req->sc_cmd;
  135. /* Get ELS response length from CQE */
  136. mp_info = &cqe->cqe_info.midpath_info;
  137. els_req->mp_req.resp_len = mp_info->data_placement_size;
  138. /* Parse ELS response */
  139. if ((els_req->cb_func) && (els_req->cb_arg)) {
  140. els_req->cb_func(els_req->cb_arg);
  141. els_req->cb_arg = NULL;
  142. }
  143. kref_put(&els_req->refcount, qedf_release_cmd);
  144. }
  145. static void qedf_rrq_compl(struct qedf_els_cb_arg *cb_arg)
  146. {
  147. struct qedf_ioreq *orig_io_req;
  148. struct qedf_ioreq *rrq_req;
  149. struct qedf_ctx *qedf;
  150. int refcount;
  151. rrq_req = cb_arg->io_req;
  152. qedf = rrq_req->fcport->qedf;
  153. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Entered.\n");
  154. orig_io_req = cb_arg->aborted_io_req;
  155. if (!orig_io_req)
  156. goto out_free;
  157. if (rrq_req->event != QEDF_IOREQ_EV_ELS_TMO &&
  158. rrq_req->event != QEDF_IOREQ_EV_ELS_ERR_DETECT)
  159. cancel_delayed_work_sync(&orig_io_req->timeout_work);
  160. refcount = kref_read(&orig_io_req->refcount);
  161. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "rrq_compl: orig io = %p,"
  162. " orig xid = 0x%x, rrq_xid = 0x%x, refcount=%d\n",
  163. orig_io_req, orig_io_req->xid, rrq_req->xid, refcount);
  164. /* This should return the aborted io_req to the command pool */
  165. if (orig_io_req)
  166. kref_put(&orig_io_req->refcount, qedf_release_cmd);
  167. out_free:
  168. /*
  169. * Release a reference to the rrq request if we timed out as the
  170. * rrq completion handler is called directly from the timeout handler
  171. * and not from els_compl where the reference would have normally been
  172. * released.
  173. */
  174. if (rrq_req->event == QEDF_IOREQ_EV_ELS_TMO)
  175. kref_put(&rrq_req->refcount, qedf_release_cmd);
  176. kfree(cb_arg);
  177. }
  178. /* Assumes kref is already held by caller */
  179. int qedf_send_rrq(struct qedf_ioreq *aborted_io_req)
  180. {
  181. struct fc_els_rrq rrq;
  182. struct qedf_rport *fcport;
  183. struct fc_lport *lport;
  184. struct qedf_els_cb_arg *cb_arg = NULL;
  185. struct qedf_ctx *qedf;
  186. uint32_t sid;
  187. uint32_t r_a_tov;
  188. int rc;
  189. if (!aborted_io_req) {
  190. QEDF_ERR(NULL, "abort_io_req is NULL.\n");
  191. return -EINVAL;
  192. }
  193. fcport = aborted_io_req->fcport;
  194. /* Check that fcport is still offloaded */
  195. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  196. QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
  197. return -EINVAL;
  198. }
  199. if (!fcport->qedf) {
  200. QEDF_ERR(NULL, "fcport->qedf is NULL.\n");
  201. return -EINVAL;
  202. }
  203. qedf = fcport->qedf;
  204. lport = qedf->lport;
  205. sid = fcport->sid;
  206. r_a_tov = lport->r_a_tov;
  207. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Sending RRQ orig "
  208. "io = %p, orig_xid = 0x%x\n", aborted_io_req,
  209. aborted_io_req->xid);
  210. memset(&rrq, 0, sizeof(rrq));
  211. cb_arg = kzalloc(sizeof(struct qedf_els_cb_arg), GFP_NOIO);
  212. if (!cb_arg) {
  213. QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate cb_arg for "
  214. "RRQ\n");
  215. rc = -ENOMEM;
  216. goto rrq_err;
  217. }
  218. cb_arg->aborted_io_req = aborted_io_req;
  219. rrq.rrq_cmd = ELS_RRQ;
  220. hton24(rrq.rrq_s_id, sid);
  221. rrq.rrq_ox_id = htons(aborted_io_req->xid);
  222. rrq.rrq_rx_id =
  223. htons(aborted_io_req->task->tstorm_st_context.read_write.rx_id);
  224. rc = qedf_initiate_els(fcport, ELS_RRQ, &rrq, sizeof(rrq),
  225. qedf_rrq_compl, cb_arg, r_a_tov);
  226. rrq_err:
  227. if (rc) {
  228. QEDF_ERR(&(qedf->dbg_ctx), "RRQ failed - release orig io "
  229. "req 0x%x\n", aborted_io_req->xid);
  230. kfree(cb_arg);
  231. kref_put(&aborted_io_req->refcount, qedf_release_cmd);
  232. }
  233. return rc;
  234. }
  235. static void qedf_process_l2_frame_compl(struct qedf_rport *fcport,
  236. struct fc_frame *fp,
  237. u16 l2_oxid)
  238. {
  239. struct fc_lport *lport = fcport->qedf->lport;
  240. struct fc_frame_header *fh;
  241. u32 crc;
  242. fh = (struct fc_frame_header *)fc_frame_header_get(fp);
  243. /* Set the OXID we return to what libfc used */
  244. if (l2_oxid != FC_XID_UNKNOWN)
  245. fh->fh_ox_id = htons(l2_oxid);
  246. /* Setup header fields */
  247. fh->fh_r_ctl = FC_RCTL_ELS_REP;
  248. fh->fh_type = FC_TYPE_ELS;
  249. /* Last sequence, end sequence */
  250. fh->fh_f_ctl[0] = 0x98;
  251. hton24(fh->fh_d_id, lport->port_id);
  252. hton24(fh->fh_s_id, fcport->rdata->ids.port_id);
  253. fh->fh_rx_id = 0xffff;
  254. /* Set frame attributes */
  255. crc = fcoe_fc_crc(fp);
  256. fc_frame_init(fp);
  257. fr_dev(fp) = lport;
  258. fr_sof(fp) = FC_SOF_I3;
  259. fr_eof(fp) = FC_EOF_T;
  260. fr_crc(fp) = cpu_to_le32(~crc);
  261. /* Send completed request to libfc */
  262. fc_exch_recv(lport, fp);
  263. }
  264. /*
  265. * In instances where an ELS command times out we may need to restart the
  266. * rport by logging out and then logging back in.
  267. */
  268. void qedf_restart_rport(struct qedf_rport *fcport)
  269. {
  270. struct fc_lport *lport;
  271. struct fc_rport_priv *rdata;
  272. u32 port_id;
  273. if (!fcport)
  274. return;
  275. if (test_bit(QEDF_RPORT_IN_RESET, &fcport->flags) ||
  276. !test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
  277. test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  278. QEDF_ERR(&(fcport->qedf->dbg_ctx), "fcport %p already in reset or not offloaded.\n",
  279. fcport);
  280. return;
  281. }
  282. /* Set that we are now in reset */
  283. set_bit(QEDF_RPORT_IN_RESET, &fcport->flags);
  284. rdata = fcport->rdata;
  285. if (rdata) {
  286. lport = fcport->qedf->lport;
  287. port_id = rdata->ids.port_id;
  288. QEDF_ERR(&(fcport->qedf->dbg_ctx),
  289. "LOGO port_id=%x.\n", port_id);
  290. fc_rport_logoff(rdata);
  291. /* Recreate the rport and log back in */
  292. rdata = fc_rport_create(lport, port_id);
  293. if (rdata)
  294. fc_rport_login(rdata);
  295. }
  296. clear_bit(QEDF_RPORT_IN_RESET, &fcport->flags);
  297. }
  298. static void qedf_l2_els_compl(struct qedf_els_cb_arg *cb_arg)
  299. {
  300. struct qedf_ioreq *els_req;
  301. struct qedf_rport *fcport;
  302. struct qedf_mp_req *mp_req;
  303. struct fc_frame *fp;
  304. struct fc_frame_header *fh, *mp_fc_hdr;
  305. void *resp_buf, *fc_payload;
  306. u32 resp_len;
  307. u16 l2_oxid;
  308. l2_oxid = cb_arg->l2_oxid;
  309. els_req = cb_arg->io_req;
  310. if (!els_req) {
  311. QEDF_ERR(NULL, "els_req is NULL.\n");
  312. goto free_arg;
  313. }
  314. /*
  315. * If we are flushing the command just free the cb_arg as none of the
  316. * response data will be valid.
  317. */
  318. if (els_req->event == QEDF_IOREQ_EV_ELS_FLUSH)
  319. goto free_arg;
  320. fcport = els_req->fcport;
  321. mp_req = &(els_req->mp_req);
  322. mp_fc_hdr = &(mp_req->resp_fc_hdr);
  323. resp_len = mp_req->resp_len;
  324. resp_buf = mp_req->resp_buf;
  325. /*
  326. * If a middle path ELS command times out, don't try to return
  327. * the command but rather do any internal cleanup and then libfc
  328. * timeout the command and clean up its internal resources.
  329. */
  330. if (els_req->event == QEDF_IOREQ_EV_ELS_TMO) {
  331. /*
  332. * If ADISC times out, libfc will timeout the exchange and then
  333. * try to send a PLOGI which will timeout since the session is
  334. * still offloaded. Force libfc to logout the session which
  335. * will offload the connection and allow the PLOGI response to
  336. * flow over the LL2 path.
  337. */
  338. if (cb_arg->op == ELS_ADISC)
  339. qedf_restart_rport(fcport);
  340. return;
  341. }
  342. if (sizeof(struct fc_frame_header) + resp_len > QEDF_PAGE_SIZE) {
  343. QEDF_ERR(&(fcport->qedf->dbg_ctx), "resp_len is "
  344. "beyond page size.\n");
  345. goto free_arg;
  346. }
  347. fp = fc_frame_alloc(fcport->qedf->lport, resp_len);
  348. if (!fp) {
  349. QEDF_ERR(&(fcport->qedf->dbg_ctx),
  350. "fc_frame_alloc failure.\n");
  351. return;
  352. }
  353. /* Copy frame header from firmware into fp */
  354. fh = (struct fc_frame_header *)fc_frame_header_get(fp);
  355. memcpy(fh, mp_fc_hdr, sizeof(struct fc_frame_header));
  356. /* Copy payload from firmware into fp */
  357. fc_payload = fc_frame_payload_get(fp, resp_len);
  358. memcpy(fc_payload, resp_buf, resp_len);
  359. QEDF_INFO(&(fcport->qedf->dbg_ctx), QEDF_LOG_ELS,
  360. "Completing OX_ID 0x%x back to libfc.\n", l2_oxid);
  361. qedf_process_l2_frame_compl(fcport, fp, l2_oxid);
  362. free_arg:
  363. kfree(cb_arg);
  364. }
  365. int qedf_send_adisc(struct qedf_rport *fcport, struct fc_frame *fp)
  366. {
  367. struct fc_els_adisc *adisc;
  368. struct fc_frame_header *fh;
  369. struct fc_lport *lport = fcport->qedf->lport;
  370. struct qedf_els_cb_arg *cb_arg = NULL;
  371. struct qedf_ctx *qedf;
  372. uint32_t r_a_tov = lport->r_a_tov;
  373. int rc;
  374. qedf = fcport->qedf;
  375. fh = fc_frame_header_get(fp);
  376. cb_arg = kzalloc(sizeof(struct qedf_els_cb_arg), GFP_NOIO);
  377. if (!cb_arg) {
  378. QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate cb_arg for "
  379. "ADISC\n");
  380. rc = -ENOMEM;
  381. goto adisc_err;
  382. }
  383. cb_arg->l2_oxid = ntohs(fh->fh_ox_id);
  384. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  385. "Sending ADISC ox_id=0x%x.\n", cb_arg->l2_oxid);
  386. adisc = fc_frame_payload_get(fp, sizeof(*adisc));
  387. rc = qedf_initiate_els(fcport, ELS_ADISC, adisc, sizeof(*adisc),
  388. qedf_l2_els_compl, cb_arg, r_a_tov);
  389. adisc_err:
  390. if (rc) {
  391. QEDF_ERR(&(qedf->dbg_ctx), "ADISC failed.\n");
  392. kfree(cb_arg);
  393. }
  394. return rc;
  395. }
  396. static void qedf_srr_compl(struct qedf_els_cb_arg *cb_arg)
  397. {
  398. struct qedf_ioreq *orig_io_req;
  399. struct qedf_ioreq *srr_req;
  400. struct qedf_mp_req *mp_req;
  401. struct fc_frame_header *mp_fc_hdr, *fh;
  402. struct fc_frame *fp;
  403. void *resp_buf, *fc_payload;
  404. u32 resp_len;
  405. struct fc_lport *lport;
  406. struct qedf_ctx *qedf;
  407. int refcount;
  408. u8 opcode;
  409. srr_req = cb_arg->io_req;
  410. qedf = srr_req->fcport->qedf;
  411. lport = qedf->lport;
  412. orig_io_req = cb_arg->aborted_io_req;
  413. if (!orig_io_req)
  414. goto out_free;
  415. clear_bit(QEDF_CMD_SRR_SENT, &orig_io_req->flags);
  416. if (srr_req->event != QEDF_IOREQ_EV_ELS_TMO &&
  417. srr_req->event != QEDF_IOREQ_EV_ELS_ERR_DETECT)
  418. cancel_delayed_work_sync(&orig_io_req->timeout_work);
  419. refcount = kref_read(&orig_io_req->refcount);
  420. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Entered: orig_io=%p,"
  421. " orig_io_xid=0x%x, rec_xid=0x%x, refcount=%d\n",
  422. orig_io_req, orig_io_req->xid, srr_req->xid, refcount);
  423. /* If a SRR times out, simply free resources */
  424. if (srr_req->event == QEDF_IOREQ_EV_ELS_TMO)
  425. goto out_put;
  426. /* Normalize response data into struct fc_frame */
  427. mp_req = &(srr_req->mp_req);
  428. mp_fc_hdr = &(mp_req->resp_fc_hdr);
  429. resp_len = mp_req->resp_len;
  430. resp_buf = mp_req->resp_buf;
  431. fp = fc_frame_alloc(lport, resp_len);
  432. if (!fp) {
  433. QEDF_ERR(&(qedf->dbg_ctx),
  434. "fc_frame_alloc failure.\n");
  435. goto out_put;
  436. }
  437. /* Copy frame header from firmware into fp */
  438. fh = (struct fc_frame_header *)fc_frame_header_get(fp);
  439. memcpy(fh, mp_fc_hdr, sizeof(struct fc_frame_header));
  440. /* Copy payload from firmware into fp */
  441. fc_payload = fc_frame_payload_get(fp, resp_len);
  442. memcpy(fc_payload, resp_buf, resp_len);
  443. opcode = fc_frame_payload_op(fp);
  444. switch (opcode) {
  445. case ELS_LS_ACC:
  446. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  447. "SRR success.\n");
  448. break;
  449. case ELS_LS_RJT:
  450. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_ELS,
  451. "SRR rejected.\n");
  452. qedf_initiate_abts(orig_io_req, true);
  453. break;
  454. }
  455. fc_frame_free(fp);
  456. out_put:
  457. /* Put reference for original command since SRR completed */
  458. kref_put(&orig_io_req->refcount, qedf_release_cmd);
  459. out_free:
  460. kfree(cb_arg);
  461. }
  462. static int qedf_send_srr(struct qedf_ioreq *orig_io_req, u32 offset, u8 r_ctl)
  463. {
  464. struct fcp_srr srr;
  465. struct qedf_ctx *qedf;
  466. struct qedf_rport *fcport;
  467. struct fc_lport *lport;
  468. struct qedf_els_cb_arg *cb_arg = NULL;
  469. u32 sid, r_a_tov;
  470. int rc;
  471. if (!orig_io_req) {
  472. QEDF_ERR(NULL, "orig_io_req is NULL.\n");
  473. return -EINVAL;
  474. }
  475. fcport = orig_io_req->fcport;
  476. /* Check that fcport is still offloaded */
  477. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  478. QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
  479. return -EINVAL;
  480. }
  481. if (!fcport->qedf) {
  482. QEDF_ERR(NULL, "fcport->qedf is NULL.\n");
  483. return -EINVAL;
  484. }
  485. /* Take reference until SRR command completion */
  486. kref_get(&orig_io_req->refcount);
  487. qedf = fcport->qedf;
  488. lport = qedf->lport;
  489. sid = fcport->sid;
  490. r_a_tov = lport->r_a_tov;
  491. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Sending SRR orig_io=%p, "
  492. "orig_xid=0x%x\n", orig_io_req, orig_io_req->xid);
  493. memset(&srr, 0, sizeof(srr));
  494. cb_arg = kzalloc(sizeof(struct qedf_els_cb_arg), GFP_NOIO);
  495. if (!cb_arg) {
  496. QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate cb_arg for "
  497. "SRR\n");
  498. rc = -ENOMEM;
  499. goto srr_err;
  500. }
  501. cb_arg->aborted_io_req = orig_io_req;
  502. srr.srr_op = ELS_SRR;
  503. srr.srr_ox_id = htons(orig_io_req->xid);
  504. srr.srr_rx_id = htons(orig_io_req->rx_id);
  505. srr.srr_rel_off = htonl(offset);
  506. srr.srr_r_ctl = r_ctl;
  507. rc = qedf_initiate_els(fcport, ELS_SRR, &srr, sizeof(srr),
  508. qedf_srr_compl, cb_arg, r_a_tov);
  509. srr_err:
  510. if (rc) {
  511. QEDF_ERR(&(qedf->dbg_ctx), "SRR failed - release orig_io_req"
  512. "=0x%x\n", orig_io_req->xid);
  513. kfree(cb_arg);
  514. /* If we fail to queue SRR, send ABTS to orig_io */
  515. qedf_initiate_abts(orig_io_req, true);
  516. kref_put(&orig_io_req->refcount, qedf_release_cmd);
  517. } else
  518. /* Tell other threads that SRR is in progress */
  519. set_bit(QEDF_CMD_SRR_SENT, &orig_io_req->flags);
  520. return rc;
  521. }
  522. static void qedf_initiate_seq_cleanup(struct qedf_ioreq *orig_io_req,
  523. u32 offset, u8 r_ctl)
  524. {
  525. struct qedf_rport *fcport;
  526. unsigned long flags;
  527. struct qedf_els_cb_arg *cb_arg;
  528. struct fcoe_wqe *sqe;
  529. u16 sqe_idx;
  530. fcport = orig_io_req->fcport;
  531. QEDF_INFO(&(fcport->qedf->dbg_ctx), QEDF_LOG_ELS,
  532. "Doing sequence cleanup for xid=0x%x offset=%u.\n",
  533. orig_io_req->xid, offset);
  534. cb_arg = kzalloc(sizeof(struct qedf_els_cb_arg), GFP_NOIO);
  535. if (!cb_arg) {
  536. QEDF_ERR(&(fcport->qedf->dbg_ctx), "Unable to allocate cb_arg "
  537. "for sequence cleanup\n");
  538. return;
  539. }
  540. /* Get reference for cleanup request */
  541. kref_get(&orig_io_req->refcount);
  542. orig_io_req->cmd_type = QEDF_SEQ_CLEANUP;
  543. cb_arg->offset = offset;
  544. cb_arg->r_ctl = r_ctl;
  545. orig_io_req->cb_arg = cb_arg;
  546. qedf_cmd_timer_set(fcport->qedf, orig_io_req,
  547. QEDF_CLEANUP_TIMEOUT * HZ);
  548. spin_lock_irqsave(&fcport->rport_lock, flags);
  549. sqe_idx = qedf_get_sqe_idx(fcport);
  550. sqe = &fcport->sq[sqe_idx];
  551. memset(sqe, 0, sizeof(struct fcoe_wqe));
  552. orig_io_req->task_params->sqe = sqe;
  553. init_initiator_sequence_recovery_fcoe_task(orig_io_req->task_params,
  554. offset);
  555. qedf_ring_doorbell(fcport);
  556. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  557. }
  558. void qedf_process_seq_cleanup_compl(struct qedf_ctx *qedf,
  559. struct fcoe_cqe *cqe, struct qedf_ioreq *io_req)
  560. {
  561. int rc;
  562. struct qedf_els_cb_arg *cb_arg;
  563. cb_arg = io_req->cb_arg;
  564. /* If we timed out just free resources */
  565. if (io_req->event == QEDF_IOREQ_EV_ELS_TMO || !cqe)
  566. goto free;
  567. /* Kill the timer we put on the request */
  568. cancel_delayed_work_sync(&io_req->timeout_work);
  569. rc = qedf_send_srr(io_req, cb_arg->offset, cb_arg->r_ctl);
  570. if (rc)
  571. QEDF_ERR(&(qedf->dbg_ctx), "Unable to send SRR, I/O will "
  572. "abort, xid=0x%x.\n", io_req->xid);
  573. free:
  574. kfree(cb_arg);
  575. kref_put(&io_req->refcount, qedf_release_cmd);
  576. }
  577. static bool qedf_requeue_io_req(struct qedf_ioreq *orig_io_req)
  578. {
  579. struct qedf_rport *fcport;
  580. struct qedf_ioreq *new_io_req;
  581. unsigned long flags;
  582. bool rc = false;
  583. fcport = orig_io_req->fcport;
  584. if (!fcport) {
  585. QEDF_ERR(NULL, "fcport is NULL.\n");
  586. goto out;
  587. }
  588. if (!orig_io_req->sc_cmd) {
  589. QEDF_ERR(&(fcport->qedf->dbg_ctx), "sc_cmd is NULL for "
  590. "xid=0x%x.\n", orig_io_req->xid);
  591. goto out;
  592. }
  593. new_io_req = qedf_alloc_cmd(fcport, QEDF_SCSI_CMD);
  594. if (!new_io_req) {
  595. QEDF_ERR(&(fcport->qedf->dbg_ctx), "Could not allocate new "
  596. "io_req.\n");
  597. goto out;
  598. }
  599. new_io_req->sc_cmd = orig_io_req->sc_cmd;
  600. /*
  601. * This keeps the sc_cmd struct from being returned to the tape
  602. * driver and being requeued twice. We do need to put a reference
  603. * for the original I/O request since we will not do a SCSI completion
  604. * for it.
  605. */
  606. orig_io_req->sc_cmd = NULL;
  607. kref_put(&orig_io_req->refcount, qedf_release_cmd);
  608. spin_lock_irqsave(&fcport->rport_lock, flags);
  609. /* kref for new command released in qedf_post_io_req on error */
  610. if (qedf_post_io_req(fcport, new_io_req)) {
  611. QEDF_ERR(&(fcport->qedf->dbg_ctx), "Unable to post io_req\n");
  612. /* Return SQE to pool */
  613. atomic_inc(&fcport->free_sqes);
  614. } else {
  615. QEDF_INFO(&(fcport->qedf->dbg_ctx), QEDF_LOG_ELS,
  616. "Reissued SCSI command from orig_xid=0x%x on "
  617. "new_xid=0x%x.\n", orig_io_req->xid, new_io_req->xid);
  618. /*
  619. * Abort the original I/O but do not return SCSI command as
  620. * it has been reissued on another OX_ID.
  621. */
  622. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  623. qedf_initiate_abts(orig_io_req, false);
  624. goto out;
  625. }
  626. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  627. out:
  628. return rc;
  629. }
  630. static void qedf_rec_compl(struct qedf_els_cb_arg *cb_arg)
  631. {
  632. struct qedf_ioreq *orig_io_req;
  633. struct qedf_ioreq *rec_req;
  634. struct qedf_mp_req *mp_req;
  635. struct fc_frame_header *mp_fc_hdr, *fh;
  636. struct fc_frame *fp;
  637. void *resp_buf, *fc_payload;
  638. u32 resp_len;
  639. struct fc_lport *lport;
  640. struct qedf_ctx *qedf;
  641. int refcount;
  642. enum fc_rctl r_ctl;
  643. struct fc_els_ls_rjt *rjt;
  644. struct fc_els_rec_acc *acc;
  645. u8 opcode;
  646. u32 offset, e_stat;
  647. struct scsi_cmnd *sc_cmd;
  648. bool srr_needed = false;
  649. rec_req = cb_arg->io_req;
  650. qedf = rec_req->fcport->qedf;
  651. lport = qedf->lport;
  652. orig_io_req = cb_arg->aborted_io_req;
  653. if (!orig_io_req)
  654. goto out_free;
  655. if (rec_req->event != QEDF_IOREQ_EV_ELS_TMO &&
  656. rec_req->event != QEDF_IOREQ_EV_ELS_ERR_DETECT)
  657. cancel_delayed_work_sync(&orig_io_req->timeout_work);
  658. refcount = kref_read(&orig_io_req->refcount);
  659. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Entered: orig_io=%p,"
  660. " orig_io_xid=0x%x, rec_xid=0x%x, refcount=%d\n",
  661. orig_io_req, orig_io_req->xid, rec_req->xid, refcount);
  662. /* If a REC times out, free resources */
  663. if (rec_req->event == QEDF_IOREQ_EV_ELS_TMO)
  664. goto out_put;
  665. /* Normalize response data into struct fc_frame */
  666. mp_req = &(rec_req->mp_req);
  667. mp_fc_hdr = &(mp_req->resp_fc_hdr);
  668. resp_len = mp_req->resp_len;
  669. acc = resp_buf = mp_req->resp_buf;
  670. fp = fc_frame_alloc(lport, resp_len);
  671. if (!fp) {
  672. QEDF_ERR(&(qedf->dbg_ctx),
  673. "fc_frame_alloc failure.\n");
  674. goto out_put;
  675. }
  676. /* Copy frame header from firmware into fp */
  677. fh = (struct fc_frame_header *)fc_frame_header_get(fp);
  678. memcpy(fh, mp_fc_hdr, sizeof(struct fc_frame_header));
  679. /* Copy payload from firmware into fp */
  680. fc_payload = fc_frame_payload_get(fp, resp_len);
  681. memcpy(fc_payload, resp_buf, resp_len);
  682. opcode = fc_frame_payload_op(fp);
  683. if (opcode == ELS_LS_RJT) {
  684. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  685. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  686. "Received LS_RJT for REC: er_reason=0x%x, "
  687. "er_explan=0x%x.\n", rjt->er_reason, rjt->er_explan);
  688. /*
  689. * The following response(s) mean that we need to reissue the
  690. * request on another exchange. We need to do this without
  691. * informing the upper layers lest it cause an application
  692. * error.
  693. */
  694. if ((rjt->er_reason == ELS_RJT_LOGIC ||
  695. rjt->er_reason == ELS_RJT_UNAB) &&
  696. rjt->er_explan == ELS_EXPL_OXID_RXID) {
  697. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  698. "Handle CMD LOST case.\n");
  699. qedf_requeue_io_req(orig_io_req);
  700. }
  701. } else if (opcode == ELS_LS_ACC) {
  702. offset = ntohl(acc->reca_fc4value);
  703. e_stat = ntohl(acc->reca_e_stat);
  704. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  705. "Received LS_ACC for REC: offset=0x%x, e_stat=0x%x.\n",
  706. offset, e_stat);
  707. if (e_stat & ESB_ST_SEQ_INIT) {
  708. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  709. "Target has the seq init\n");
  710. goto out_free_frame;
  711. }
  712. sc_cmd = orig_io_req->sc_cmd;
  713. if (!sc_cmd) {
  714. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  715. "sc_cmd is NULL for xid=0x%x.\n",
  716. orig_io_req->xid);
  717. goto out_free_frame;
  718. }
  719. /* SCSI write case */
  720. if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  721. if (offset == orig_io_req->data_xfer_len) {
  722. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  723. "WRITE - response lost.\n");
  724. r_ctl = FC_RCTL_DD_CMD_STATUS;
  725. srr_needed = true;
  726. offset = 0;
  727. } else {
  728. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  729. "WRITE - XFER_RDY/DATA lost.\n");
  730. r_ctl = FC_RCTL_DD_DATA_DESC;
  731. /* Use data from warning CQE instead of REC */
  732. offset = orig_io_req->tx_buf_off;
  733. }
  734. /* SCSI read case */
  735. } else {
  736. if (orig_io_req->rx_buf_off ==
  737. orig_io_req->data_xfer_len) {
  738. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  739. "READ - response lost.\n");
  740. srr_needed = true;
  741. r_ctl = FC_RCTL_DD_CMD_STATUS;
  742. offset = 0;
  743. } else {
  744. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  745. "READ - DATA lost.\n");
  746. /*
  747. * For read case we always set the offset to 0
  748. * for sequence recovery task.
  749. */
  750. offset = 0;
  751. r_ctl = FC_RCTL_DD_SOL_DATA;
  752. }
  753. }
  754. if (srr_needed)
  755. qedf_send_srr(orig_io_req, offset, r_ctl);
  756. else
  757. qedf_initiate_seq_cleanup(orig_io_req, offset, r_ctl);
  758. }
  759. out_free_frame:
  760. fc_frame_free(fp);
  761. out_put:
  762. /* Put reference for original command since REC completed */
  763. kref_put(&orig_io_req->refcount, qedf_release_cmd);
  764. out_free:
  765. kfree(cb_arg);
  766. }
  767. /* Assumes kref is already held by caller */
  768. int qedf_send_rec(struct qedf_ioreq *orig_io_req)
  769. {
  770. struct fc_els_rec rec;
  771. struct qedf_rport *fcport;
  772. struct fc_lport *lport;
  773. struct qedf_els_cb_arg *cb_arg = NULL;
  774. struct qedf_ctx *qedf;
  775. uint32_t sid;
  776. uint32_t r_a_tov;
  777. int rc;
  778. if (!orig_io_req) {
  779. QEDF_ERR(NULL, "orig_io_req is NULL.\n");
  780. return -EINVAL;
  781. }
  782. fcport = orig_io_req->fcport;
  783. /* Check that fcport is still offloaded */
  784. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  785. QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
  786. return -EINVAL;
  787. }
  788. if (!fcport->qedf) {
  789. QEDF_ERR(NULL, "fcport->qedf is NULL.\n");
  790. return -EINVAL;
  791. }
  792. /* Take reference until REC command completion */
  793. kref_get(&orig_io_req->refcount);
  794. qedf = fcport->qedf;
  795. lport = qedf->lport;
  796. sid = fcport->sid;
  797. r_a_tov = lport->r_a_tov;
  798. memset(&rec, 0, sizeof(rec));
  799. cb_arg = kzalloc(sizeof(struct qedf_els_cb_arg), GFP_NOIO);
  800. if (!cb_arg) {
  801. QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate cb_arg for "
  802. "REC\n");
  803. rc = -ENOMEM;
  804. goto rec_err;
  805. }
  806. cb_arg->aborted_io_req = orig_io_req;
  807. rec.rec_cmd = ELS_REC;
  808. hton24(rec.rec_s_id, sid);
  809. rec.rec_ox_id = htons(orig_io_req->xid);
  810. rec.rec_rx_id =
  811. htons(orig_io_req->task->tstorm_st_context.read_write.rx_id);
  812. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS, "Sending REC orig_io=%p, "
  813. "orig_xid=0x%x rx_id=0x%x\n", orig_io_req,
  814. orig_io_req->xid, rec.rec_rx_id);
  815. rc = qedf_initiate_els(fcport, ELS_REC, &rec, sizeof(rec),
  816. qedf_rec_compl, cb_arg, r_a_tov);
  817. rec_err:
  818. if (rc) {
  819. QEDF_ERR(&(qedf->dbg_ctx), "REC failed - release orig_io_req"
  820. "=0x%x\n", orig_io_req->xid);
  821. kfree(cb_arg);
  822. kref_put(&orig_io_req->refcount, qedf_release_cmd);
  823. }
  824. return rc;
  825. }