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