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