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