1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * QLogic FCoE Offload Driver
4 * Copyright (c) 2016-2018 Cavium Inc.
5 */
6 #include <linux/spinlock.h>
7 #include <linux/vmalloc.h>
8 #include "qedf.h"
9 #include <scsi/scsi_tcq.h>
10
qedf_cmd_timer_set(struct qedf_ctx * qedf,struct qedf_ioreq * io_req,unsigned int timer_msec)11 void qedf_cmd_timer_set(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
12 unsigned int timer_msec)
13 {
14 queue_delayed_work(qedf->timer_work_queue, &io_req->timeout_work,
15 msecs_to_jiffies(timer_msec));
16 }
17
qedf_cmd_timeout(struct work_struct * work)18 static void qedf_cmd_timeout(struct work_struct *work)
19 {
20
21 struct qedf_ioreq *io_req =
22 container_of(work, struct qedf_ioreq, timeout_work.work);
23 struct qedf_ctx *qedf;
24 struct qedf_rport *fcport;
25
26 fcport = io_req->fcport;
27 if (io_req->fcport == NULL) {
28 QEDF_INFO(NULL, QEDF_LOG_IO, "fcport is NULL.\n");
29 return;
30 }
31
32 qedf = fcport->qedf;
33
34 switch (io_req->cmd_type) {
35 case QEDF_ABTS:
36 if (qedf == NULL) {
37 QEDF_INFO(NULL, QEDF_LOG_IO,
38 "qedf is NULL for ABTS xid=0x%x.\n",
39 io_req->xid);
40 return;
41 }
42
43 QEDF_ERR((&qedf->dbg_ctx), "ABTS timeout, xid=0x%x.\n",
44 io_req->xid);
45 /* Cleanup timed out ABTS */
46 qedf_initiate_cleanup(io_req, true);
47 complete(&io_req->abts_done);
48
49 /*
50 * Need to call kref_put for reference taken when initiate_abts
51 * was called since abts_compl won't be called now that we've
52 * cleaned up the task.
53 */
54 kref_put(&io_req->refcount, qedf_release_cmd);
55
56 /* Clear in abort bit now that we're done with the command */
57 clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
58
59 /*
60 * Now that the original I/O and the ABTS are complete see
61 * if we need to reconnect to the target.
62 */
63 qedf_restart_rport(fcport);
64 break;
65 case QEDF_ELS:
66 if (!qedf) {
67 QEDF_INFO(NULL, QEDF_LOG_IO,
68 "qedf is NULL for ELS xid=0x%x.\n",
69 io_req->xid);
70 return;
71 }
72 /* ELS request no longer outstanding since it timed out */
73 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
74
75 kref_get(&io_req->refcount);
76 /*
77 * Don't attempt to clean an ELS timeout as any subseqeunt
78 * ABTS or cleanup requests just hang. For now just free
79 * the resources of the original I/O and the RRQ
80 */
81 QEDF_ERR(&(qedf->dbg_ctx), "ELS timeout, xid=0x%x.\n",
82 io_req->xid);
83 qedf_initiate_cleanup(io_req, true);
84 io_req->event = QEDF_IOREQ_EV_ELS_TMO;
85 /* Call callback function to complete command */
86 if (io_req->cb_func && io_req->cb_arg) {
87 io_req->cb_func(io_req->cb_arg);
88 io_req->cb_arg = NULL;
89 }
90 kref_put(&io_req->refcount, qedf_release_cmd);
91 break;
92 case QEDF_SEQ_CLEANUP:
93 QEDF_ERR(&(qedf->dbg_ctx), "Sequence cleanup timeout, "
94 "xid=0x%x.\n", io_req->xid);
95 qedf_initiate_cleanup(io_req, true);
96 io_req->event = QEDF_IOREQ_EV_ELS_TMO;
97 qedf_process_seq_cleanup_compl(qedf, NULL, io_req);
98 break;
99 default:
100 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
101 "Hit default case, xid=0x%x.\n", io_req->xid);
102 break;
103 }
104 }
105
qedf_cmd_mgr_free(struct qedf_cmd_mgr * cmgr)106 void qedf_cmd_mgr_free(struct qedf_cmd_mgr *cmgr)
107 {
108 struct io_bdt *bdt_info;
109 struct qedf_ctx *qedf = cmgr->qedf;
110 size_t bd_tbl_sz;
111 u16 min_xid = 0;
112 u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
113 int num_ios;
114 int i;
115 struct qedf_ioreq *io_req;
116
117 num_ios = max_xid - min_xid + 1;
118
119 /* Free fcoe_bdt_ctx structures */
120 if (!cmgr->io_bdt_pool) {
121 QEDF_ERR(&qedf->dbg_ctx, "io_bdt_pool is NULL.\n");
122 goto free_cmd_pool;
123 }
124
125 bd_tbl_sz = QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge);
126 for (i = 0; i < num_ios; i++) {
127 bdt_info = cmgr->io_bdt_pool[i];
128 if (bdt_info->bd_tbl) {
129 dma_free_coherent(&qedf->pdev->dev, bd_tbl_sz,
130 bdt_info->bd_tbl, bdt_info->bd_tbl_dma);
131 bdt_info->bd_tbl = NULL;
132 }
133 }
134
135 /* Destroy io_bdt pool */
136 for (i = 0; i < num_ios; i++) {
137 kfree(cmgr->io_bdt_pool[i]);
138 cmgr->io_bdt_pool[i] = NULL;
139 }
140
141 kfree(cmgr->io_bdt_pool);
142 cmgr->io_bdt_pool = NULL;
143
144 free_cmd_pool:
145
146 for (i = 0; i < num_ios; i++) {
147 io_req = &cmgr->cmds[i];
148 kfree(io_req->sgl_task_params);
149 kfree(io_req->task_params);
150 /* Make sure we free per command sense buffer */
151 if (io_req->sense_buffer)
152 dma_free_coherent(&qedf->pdev->dev,
153 QEDF_SCSI_SENSE_BUFFERSIZE, io_req->sense_buffer,
154 io_req->sense_buffer_dma);
155 cancel_delayed_work_sync(&io_req->rrq_work);
156 }
157
158 /* Free command manager itself */
159 vfree(cmgr);
160 }
161
qedf_handle_rrq(struct work_struct * work)162 static void qedf_handle_rrq(struct work_struct *work)
163 {
164 struct qedf_ioreq *io_req =
165 container_of(work, struct qedf_ioreq, rrq_work.work);
166
167 atomic_set(&io_req->state, QEDFC_CMD_ST_RRQ_ACTIVE);
168 qedf_send_rrq(io_req);
169
170 }
171
qedf_cmd_mgr_alloc(struct qedf_ctx * qedf)172 struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
173 {
174 struct qedf_cmd_mgr *cmgr;
175 struct io_bdt *bdt_info;
176 struct qedf_ioreq *io_req;
177 u16 xid;
178 int i;
179 int num_ios;
180 u16 min_xid = 0;
181 u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
182
183 /* Make sure num_queues is already set before calling this function */
184 if (!qedf->num_queues) {
185 QEDF_ERR(&(qedf->dbg_ctx), "num_queues is not set.\n");
186 return NULL;
187 }
188
189 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
190 QEDF_WARN(&(qedf->dbg_ctx), "Invalid min_xid 0x%x and "
191 "max_xid 0x%x.\n", min_xid, max_xid);
192 return NULL;
193 }
194
195 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "min xid 0x%x, max xid "
196 "0x%x.\n", min_xid, max_xid);
197
198 num_ios = max_xid - min_xid + 1;
199
200 cmgr = vzalloc(sizeof(struct qedf_cmd_mgr));
201 if (!cmgr) {
202 QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc cmd mgr.\n");
203 return NULL;
204 }
205
206 cmgr->qedf = qedf;
207 spin_lock_init(&cmgr->lock);
208
209 /*
210 * Initialize I/O request fields.
211 */
212 xid = 0;
213
214 for (i = 0; i < num_ios; i++) {
215 io_req = &cmgr->cmds[i];
216 INIT_DELAYED_WORK(&io_req->timeout_work, qedf_cmd_timeout);
217
218 io_req->xid = xid++;
219
220 INIT_DELAYED_WORK(&io_req->rrq_work, qedf_handle_rrq);
221
222 /* Allocate DMA memory to hold sense buffer */
223 io_req->sense_buffer = dma_alloc_coherent(&qedf->pdev->dev,
224 QEDF_SCSI_SENSE_BUFFERSIZE, &io_req->sense_buffer_dma,
225 GFP_KERNEL);
226 if (!io_req->sense_buffer) {
227 QEDF_ERR(&qedf->dbg_ctx,
228 "Failed to alloc sense buffer.\n");
229 goto mem_err;
230 }
231
232 /* Allocate task parameters to pass to f/w init funcions */
233 io_req->task_params = kzalloc(sizeof(*io_req->task_params),
234 GFP_KERNEL);
235 if (!io_req->task_params) {
236 QEDF_ERR(&(qedf->dbg_ctx),
237 "Failed to allocate task_params for xid=0x%x\n",
238 i);
239 goto mem_err;
240 }
241
242 /*
243 * Allocate scatter/gather list info to pass to f/w init
244 * functions.
245 */
246 io_req->sgl_task_params = kzalloc(
247 sizeof(struct scsi_sgl_task_params), GFP_KERNEL);
248 if (!io_req->sgl_task_params) {
249 QEDF_ERR(&(qedf->dbg_ctx),
250 "Failed to allocate sgl_task_params for xid=0x%x\n",
251 i);
252 goto mem_err;
253 }
254 }
255
256 /* Allocate pool of io_bdts - one for each qedf_ioreq */
257 cmgr->io_bdt_pool = kmalloc_array(num_ios, sizeof(struct io_bdt *),
258 GFP_KERNEL);
259
260 if (!cmgr->io_bdt_pool) {
261 QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc io_bdt_pool.\n");
262 goto mem_err;
263 }
264
265 for (i = 0; i < num_ios; i++) {
266 cmgr->io_bdt_pool[i] = kmalloc(sizeof(struct io_bdt),
267 GFP_KERNEL);
268 if (!cmgr->io_bdt_pool[i]) {
269 QEDF_WARN(&(qedf->dbg_ctx),
270 "Failed to alloc io_bdt_pool[%d].\n", i);
271 goto mem_err;
272 }
273 }
274
275 for (i = 0; i < num_ios; i++) {
276 bdt_info = cmgr->io_bdt_pool[i];
277 bdt_info->bd_tbl = dma_alloc_coherent(&qedf->pdev->dev,
278 QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge),
279 &bdt_info->bd_tbl_dma, GFP_KERNEL);
280 if (!bdt_info->bd_tbl) {
281 QEDF_WARN(&(qedf->dbg_ctx),
282 "Failed to alloc bdt_tbl[%d].\n", i);
283 goto mem_err;
284 }
285 }
286 atomic_set(&cmgr->free_list_cnt, num_ios);
287 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
288 "cmgr->free_list_cnt=%d.\n",
289 atomic_read(&cmgr->free_list_cnt));
290
291 return cmgr;
292
293 mem_err:
294 qedf_cmd_mgr_free(cmgr);
295 return NULL;
296 }
297
qedf_alloc_cmd(struct qedf_rport * fcport,u8 cmd_type)298 struct qedf_ioreq *qedf_alloc_cmd(struct qedf_rport *fcport, u8 cmd_type)
299 {
300 struct qedf_ctx *qedf = fcport->qedf;
301 struct qedf_cmd_mgr *cmd_mgr = qedf->cmd_mgr;
302 struct qedf_ioreq *io_req = NULL;
303 struct io_bdt *bd_tbl;
304 u16 xid;
305 uint32_t free_sqes;
306 int i;
307 unsigned long flags;
308
309 free_sqes = atomic_read(&fcport->free_sqes);
310
311 if (!free_sqes) {
312 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
313 "Returning NULL, free_sqes=%d.\n",
314 free_sqes);
315 goto out_failed;
316 }
317
318 /* Limit the number of outstanding R/W tasks */
319 if ((atomic_read(&fcport->num_active_ios) >=
320 NUM_RW_TASKS_PER_CONNECTION)) {
321 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
322 "Returning NULL, num_active_ios=%d.\n",
323 atomic_read(&fcport->num_active_ios));
324 goto out_failed;
325 }
326
327 /* Limit global TIDs certain tasks */
328 if (atomic_read(&cmd_mgr->free_list_cnt) <= GBL_RSVD_TASKS) {
329 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
330 "Returning NULL, free_list_cnt=%d.\n",
331 atomic_read(&cmd_mgr->free_list_cnt));
332 goto out_failed;
333 }
334
335 spin_lock_irqsave(&cmd_mgr->lock, flags);
336 for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
337 io_req = &cmd_mgr->cmds[cmd_mgr->idx];
338 cmd_mgr->idx++;
339 if (cmd_mgr->idx == FCOE_PARAMS_NUM_TASKS)
340 cmd_mgr->idx = 0;
341
342 /* Check to make sure command was previously freed */
343 if (!io_req->alloc)
344 break;
345 }
346
347 if (i == FCOE_PARAMS_NUM_TASKS) {
348 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
349 goto out_failed;
350 }
351
352 if (test_bit(QEDF_CMD_DIRTY, &io_req->flags))
353 QEDF_ERR(&qedf->dbg_ctx,
354 "io_req found to be dirty ox_id = 0x%x.\n",
355 io_req->xid);
356
357 /* Clear any flags now that we've reallocated the xid */
358 io_req->flags = 0;
359 io_req->alloc = 1;
360 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
361
362 atomic_inc(&fcport->num_active_ios);
363 atomic_dec(&fcport->free_sqes);
364 xid = io_req->xid;
365 atomic_dec(&cmd_mgr->free_list_cnt);
366
367 io_req->cmd_mgr = cmd_mgr;
368 io_req->fcport = fcport;
369
370 /* Clear any stale sc_cmd back pointer */
371 io_req->sc_cmd = NULL;
372 io_req->lun = -1;
373
374 /* Hold the io_req against deletion */
375 kref_init(&io_req->refcount); /* ID: 001 */
376 atomic_set(&io_req->state, QEDFC_CMD_ST_IO_ACTIVE);
377
378 /* Bind io_bdt for this io_req */
379 /* Have a static link between io_req and io_bdt_pool */
380 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
381 if (bd_tbl == NULL) {
382 QEDF_ERR(&(qedf->dbg_ctx), "bd_tbl is NULL, xid=%x.\n", xid);
383 kref_put(&io_req->refcount, qedf_release_cmd);
384 goto out_failed;
385 }
386 bd_tbl->io_req = io_req;
387 io_req->cmd_type = cmd_type;
388 io_req->tm_flags = 0;
389
390 /* Reset sequence offset data */
391 io_req->rx_buf_off = 0;
392 io_req->tx_buf_off = 0;
393 io_req->rx_id = 0xffff; /* No OX_ID */
394
395 return io_req;
396
397 out_failed:
398 /* Record failure for stats and return NULL to caller */
399 qedf->alloc_failures++;
400 return NULL;
401 }
402
qedf_free_mp_resc(struct qedf_ioreq * io_req)403 static void qedf_free_mp_resc(struct qedf_ioreq *io_req)
404 {
405 struct qedf_mp_req *mp_req = &(io_req->mp_req);
406 struct qedf_ctx *qedf = io_req->fcport->qedf;
407 uint64_t sz = sizeof(struct scsi_sge);
408
409 /* clear tm flags */
410 if (mp_req->mp_req_bd) {
411 dma_free_coherent(&qedf->pdev->dev, sz,
412 mp_req->mp_req_bd, mp_req->mp_req_bd_dma);
413 mp_req->mp_req_bd = NULL;
414 }
415 if (mp_req->mp_resp_bd) {
416 dma_free_coherent(&qedf->pdev->dev, sz,
417 mp_req->mp_resp_bd, mp_req->mp_resp_bd_dma);
418 mp_req->mp_resp_bd = NULL;
419 }
420 if (mp_req->req_buf) {
421 dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
422 mp_req->req_buf, mp_req->req_buf_dma);
423 mp_req->req_buf = NULL;
424 }
425 if (mp_req->resp_buf) {
426 dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
427 mp_req->resp_buf, mp_req->resp_buf_dma);
428 mp_req->resp_buf = NULL;
429 }
430 }
431
qedf_release_cmd(struct kref * ref)432 void qedf_release_cmd(struct kref *ref)
433 {
434 struct qedf_ioreq *io_req =
435 container_of(ref, struct qedf_ioreq, refcount);
436 struct qedf_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
437 struct qedf_rport *fcport = io_req->fcport;
438 unsigned long flags;
439
440 if (io_req->cmd_type == QEDF_SCSI_CMD) {
441 QEDF_WARN(&fcport->qedf->dbg_ctx,
442 "Cmd released called without scsi_done called, io_req %p xid=0x%x.\n",
443 io_req, io_req->xid);
444 WARN_ON(io_req->sc_cmd);
445 }
446
447 if (io_req->cmd_type == QEDF_ELS ||
448 io_req->cmd_type == QEDF_TASK_MGMT_CMD)
449 qedf_free_mp_resc(io_req);
450
451 atomic_inc(&cmd_mgr->free_list_cnt);
452 atomic_dec(&fcport->num_active_ios);
453 atomic_set(&io_req->state, QEDF_CMD_ST_INACTIVE);
454 if (atomic_read(&fcport->num_active_ios) < 0) {
455 QEDF_WARN(&(fcport->qedf->dbg_ctx), "active_ios < 0.\n");
456 WARN_ON(1);
457 }
458
459 /* Increment task retry identifier now that the request is released */
460 io_req->task_retry_identifier++;
461 io_req->fcport = NULL;
462
463 clear_bit(QEDF_CMD_DIRTY, &io_req->flags);
464 io_req->cpu = 0;
465 spin_lock_irqsave(&cmd_mgr->lock, flags);
466 io_req->fcport = NULL;
467 io_req->alloc = 0;
468 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
469 }
470
qedf_map_sg(struct qedf_ioreq * io_req)471 static int qedf_map_sg(struct qedf_ioreq *io_req)
472 {
473 struct scsi_cmnd *sc = io_req->sc_cmd;
474 struct Scsi_Host *host = sc->device->host;
475 struct fc_lport *lport = shost_priv(host);
476 struct qedf_ctx *qedf = lport_priv(lport);
477 struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
478 struct scatterlist *sg;
479 int byte_count = 0;
480 int sg_count = 0;
481 int bd_count = 0;
482 u32 sg_len;
483 u64 addr;
484 int i = 0;
485
486 sg_count = dma_map_sg(&qedf->pdev->dev, scsi_sglist(sc),
487 scsi_sg_count(sc), sc->sc_data_direction);
488 sg = scsi_sglist(sc);
489
490 io_req->sge_type = QEDF_IOREQ_UNKNOWN_SGE;
491
492 if (sg_count <= 8 || io_req->io_req_flags == QEDF_READ)
493 io_req->sge_type = QEDF_IOREQ_FAST_SGE;
494
495 scsi_for_each_sg(sc, sg, sg_count, i) {
496 sg_len = (u32)sg_dma_len(sg);
497 addr = (u64)sg_dma_address(sg);
498
499 /*
500 * Intermediate s/g element so check if start address
501 * is page aligned. Only required for writes and only if the
502 * number of scatter/gather elements is 8 or more.
503 */
504 if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE && (i) &&
505 (i != (sg_count - 1)) && sg_len < QEDF_PAGE_SIZE)
506 io_req->sge_type = QEDF_IOREQ_SLOW_SGE;
507
508 bd[bd_count].sge_addr.lo = cpu_to_le32(U64_LO(addr));
509 bd[bd_count].sge_addr.hi = cpu_to_le32(U64_HI(addr));
510 bd[bd_count].sge_len = cpu_to_le32(sg_len);
511
512 bd_count++;
513 byte_count += sg_len;
514 }
515
516 /* To catch a case where FAST and SLOW nothing is set, set FAST */
517 if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE)
518 io_req->sge_type = QEDF_IOREQ_FAST_SGE;
519
520 if (byte_count != scsi_bufflen(sc))
521 QEDF_ERR(&(qedf->dbg_ctx), "byte_count = %d != "
522 "scsi_bufflen = %d, task_id = 0x%x.\n", byte_count,
523 scsi_bufflen(sc), io_req->xid);
524
525 return bd_count;
526 }
527
qedf_build_bd_list_from_sg(struct qedf_ioreq * io_req)528 static int qedf_build_bd_list_from_sg(struct qedf_ioreq *io_req)
529 {
530 struct scsi_cmnd *sc = io_req->sc_cmd;
531 struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
532 int bd_count;
533
534 if (scsi_sg_count(sc)) {
535 bd_count = qedf_map_sg(io_req);
536 if (bd_count == 0)
537 return -ENOMEM;
538 } else {
539 bd_count = 0;
540 bd[0].sge_addr.lo = bd[0].sge_addr.hi = 0;
541 bd[0].sge_len = 0;
542 }
543 io_req->bd_tbl->bd_valid = bd_count;
544
545 return 0;
546 }
547
qedf_build_fcp_cmnd(struct qedf_ioreq * io_req,struct fcp_cmnd * fcp_cmnd)548 static void qedf_build_fcp_cmnd(struct qedf_ioreq *io_req,
549 struct fcp_cmnd *fcp_cmnd)
550 {
551 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
552
553 /* fcp_cmnd is 32 bytes */
554 memset(fcp_cmnd, 0, FCP_CMND_LEN);
555
556 /* 8 bytes: SCSI LUN info */
557 if (io_req->cmd_type == QEDF_TASK_MGMT_CMD)
558 int_to_scsilun(io_req->tm_lun,
559 (struct scsi_lun *)&fcp_cmnd->fc_lun);
560 else
561 int_to_scsilun(sc_cmd->device->lun,
562 (struct scsi_lun *)&fcp_cmnd->fc_lun);
563
564 /* 4 bytes: flag info */
565 fcp_cmnd->fc_pri_ta = 0;
566 fcp_cmnd->fc_tm_flags = io_req->tm_flags;
567 fcp_cmnd->fc_flags = io_req->io_req_flags;
568 fcp_cmnd->fc_cmdref = 0;
569
570 /* Populate data direction */
571 if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
572 fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
573 } else {
574 if (sc_cmd->sc_data_direction == DMA_TO_DEVICE)
575 fcp_cmnd->fc_flags |= FCP_CFL_WRDATA;
576 else if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE)
577 fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
578 }
579
580 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
581
582 /* 16 bytes: CDB information */
583 if (io_req->cmd_type != QEDF_TASK_MGMT_CMD)
584 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
585
586 /* 4 bytes: FCP data length */
587 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
588 }
589
qedf_init_task(struct qedf_rport * fcport,struct fc_lport * lport,struct qedf_ioreq * io_req,struct fcoe_task_context * task_ctx,struct fcoe_wqe * sqe)590 static void qedf_init_task(struct qedf_rport *fcport, struct fc_lport *lport,
591 struct qedf_ioreq *io_req, struct fcoe_task_context *task_ctx,
592 struct fcoe_wqe *sqe)
593 {
594 enum fcoe_task_type task_type;
595 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
596 struct io_bdt *bd_tbl = io_req->bd_tbl;
597 u8 fcp_cmnd[32];
598 u32 tmp_fcp_cmnd[8];
599 int bd_count = 0;
600 struct qedf_ctx *qedf = fcport->qedf;
601 uint16_t cq_idx = smp_processor_id() % qedf->num_queues;
602 struct regpair sense_data_buffer_phys_addr;
603 u32 tx_io_size = 0;
604 u32 rx_io_size = 0;
605 int i, cnt;
606
607 /* Note init_initiator_rw_fcoe_task memsets the task context */
608 io_req->task = task_ctx;
609 memset(task_ctx, 0, sizeof(struct fcoe_task_context));
610 memset(io_req->task_params, 0, sizeof(struct fcoe_task_params));
611 memset(io_req->sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
612
613 /* Set task type bassed on DMA directio of command */
614 if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
615 task_type = FCOE_TASK_TYPE_READ_INITIATOR;
616 } else {
617 if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
618 task_type = FCOE_TASK_TYPE_WRITE_INITIATOR;
619 tx_io_size = io_req->data_xfer_len;
620 } else {
621 task_type = FCOE_TASK_TYPE_READ_INITIATOR;
622 rx_io_size = io_req->data_xfer_len;
623 }
624 }
625
626 /* Setup the fields for fcoe_task_params */
627 io_req->task_params->context = task_ctx;
628 io_req->task_params->sqe = sqe;
629 io_req->task_params->task_type = task_type;
630 io_req->task_params->tx_io_size = tx_io_size;
631 io_req->task_params->rx_io_size = rx_io_size;
632 io_req->task_params->conn_cid = fcport->fw_cid;
633 io_req->task_params->itid = io_req->xid;
634 io_req->task_params->cq_rss_number = cq_idx;
635 io_req->task_params->is_tape_device = fcport->dev_type;
636
637 /* Fill in information for scatter/gather list */
638 if (io_req->cmd_type != QEDF_TASK_MGMT_CMD) {
639 bd_count = bd_tbl->bd_valid;
640 io_req->sgl_task_params->sgl = bd_tbl->bd_tbl;
641 io_req->sgl_task_params->sgl_phys_addr.lo =
642 U64_LO(bd_tbl->bd_tbl_dma);
643 io_req->sgl_task_params->sgl_phys_addr.hi =
644 U64_HI(bd_tbl->bd_tbl_dma);
645 io_req->sgl_task_params->num_sges = bd_count;
646 io_req->sgl_task_params->total_buffer_size =
647 scsi_bufflen(io_req->sc_cmd);
648 if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
649 io_req->sgl_task_params->small_mid_sge = 1;
650 else
651 io_req->sgl_task_params->small_mid_sge = 0;
652 }
653
654 /* Fill in physical address of sense buffer */
655 sense_data_buffer_phys_addr.lo = U64_LO(io_req->sense_buffer_dma);
656 sense_data_buffer_phys_addr.hi = U64_HI(io_req->sense_buffer_dma);
657
658 /* fill FCP_CMND IU */
659 qedf_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tmp_fcp_cmnd);
660
661 /* Swap fcp_cmnd since FC is big endian */
662 cnt = sizeof(struct fcp_cmnd) / sizeof(u32);
663 for (i = 0; i < cnt; i++) {
664 tmp_fcp_cmnd[i] = cpu_to_be32(tmp_fcp_cmnd[i]);
665 }
666 memcpy(fcp_cmnd, tmp_fcp_cmnd, sizeof(struct fcp_cmnd));
667
668 init_initiator_rw_fcoe_task(io_req->task_params,
669 io_req->sgl_task_params,
670 sense_data_buffer_phys_addr,
671 io_req->task_retry_identifier, fcp_cmnd);
672
673 /* Increment SGL type counters */
674 if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
675 qedf->slow_sge_ios++;
676 else
677 qedf->fast_sge_ios++;
678 }
679
qedf_init_mp_task(struct qedf_ioreq * io_req,struct fcoe_task_context * task_ctx,struct fcoe_wqe * sqe)680 void qedf_init_mp_task(struct qedf_ioreq *io_req,
681 struct fcoe_task_context *task_ctx, struct fcoe_wqe *sqe)
682 {
683 struct qedf_mp_req *mp_req = &(io_req->mp_req);
684 struct qedf_rport *fcport = io_req->fcport;
685 struct qedf_ctx *qedf = io_req->fcport->qedf;
686 struct fc_frame_header *fc_hdr;
687 struct fcoe_tx_mid_path_params task_fc_hdr;
688 struct scsi_sgl_task_params tx_sgl_task_params;
689 struct scsi_sgl_task_params rx_sgl_task_params;
690
691 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
692 "Initializing MP task for cmd_type=%d\n",
693 io_req->cmd_type);
694
695 qedf->control_requests++;
696
697 memset(&tx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
698 memset(&rx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
699 memset(task_ctx, 0, sizeof(struct fcoe_task_context));
700 memset(&task_fc_hdr, 0, sizeof(struct fcoe_tx_mid_path_params));
701
702 /* Setup the task from io_req for easy reference */
703 io_req->task = task_ctx;
704
705 /* Setup the fields for fcoe_task_params */
706 io_req->task_params->context = task_ctx;
707 io_req->task_params->sqe = sqe;
708 io_req->task_params->task_type = FCOE_TASK_TYPE_MIDPATH;
709 io_req->task_params->tx_io_size = io_req->data_xfer_len;
710 /* rx_io_size tells the f/w how large a response buffer we have */
711 io_req->task_params->rx_io_size = PAGE_SIZE;
712 io_req->task_params->conn_cid = fcport->fw_cid;
713 io_req->task_params->itid = io_req->xid;
714 /* Return middle path commands on CQ 0 */
715 io_req->task_params->cq_rss_number = 0;
716 io_req->task_params->is_tape_device = fcport->dev_type;
717
718 fc_hdr = &(mp_req->req_fc_hdr);
719 /* Set OX_ID and RX_ID based on driver task id */
720 fc_hdr->fh_ox_id = io_req->xid;
721 fc_hdr->fh_rx_id = htons(0xffff);
722
723 /* Set up FC header information */
724 task_fc_hdr.parameter = fc_hdr->fh_parm_offset;
725 task_fc_hdr.r_ctl = fc_hdr->fh_r_ctl;
726 task_fc_hdr.type = fc_hdr->fh_type;
727 task_fc_hdr.cs_ctl = fc_hdr->fh_cs_ctl;
728 task_fc_hdr.df_ctl = fc_hdr->fh_df_ctl;
729 task_fc_hdr.rx_id = fc_hdr->fh_rx_id;
730 task_fc_hdr.ox_id = fc_hdr->fh_ox_id;
731
732 /* Set up s/g list parameters for request buffer */
733 tx_sgl_task_params.sgl = mp_req->mp_req_bd;
734 tx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_req_bd_dma);
735 tx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_req_bd_dma);
736 tx_sgl_task_params.num_sges = 1;
737 /* Set PAGE_SIZE for now since sg element is that size ??? */
738 tx_sgl_task_params.total_buffer_size = io_req->data_xfer_len;
739 tx_sgl_task_params.small_mid_sge = 0;
740
741 /* Set up s/g list parameters for request buffer */
742 rx_sgl_task_params.sgl = mp_req->mp_resp_bd;
743 rx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_resp_bd_dma);
744 rx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_resp_bd_dma);
745 rx_sgl_task_params.num_sges = 1;
746 /* Set PAGE_SIZE for now since sg element is that size ??? */
747 rx_sgl_task_params.total_buffer_size = PAGE_SIZE;
748 rx_sgl_task_params.small_mid_sge = 0;
749
750
751 /*
752 * Last arg is 0 as previous code did not set that we wanted the
753 * fc header information.
754 */
755 init_initiator_midpath_unsolicited_fcoe_task(io_req->task_params,
756 &task_fc_hdr,
757 &tx_sgl_task_params,
758 &rx_sgl_task_params, 0);
759 }
760
761 /* Presumed that fcport->rport_lock is held */
qedf_get_sqe_idx(struct qedf_rport * fcport)762 u16 qedf_get_sqe_idx(struct qedf_rport *fcport)
763 {
764 uint16_t total_sqe = (fcport->sq_mem_size)/(sizeof(struct fcoe_wqe));
765 u16 rval;
766
767 rval = fcport->sq_prod_idx;
768
769 /* Adjust ring index */
770 fcport->sq_prod_idx++;
771 fcport->fw_sq_prod_idx++;
772 if (fcport->sq_prod_idx == total_sqe)
773 fcport->sq_prod_idx = 0;
774
775 return rval;
776 }
777
qedf_ring_doorbell(struct qedf_rport * fcport)778 void qedf_ring_doorbell(struct qedf_rport *fcport)
779 {
780 struct fcoe_db_data dbell = { 0 };
781
782 dbell.agg_flags = 0;
783
784 dbell.params |= DB_DEST_XCM << FCOE_DB_DATA_DEST_SHIFT;
785 dbell.params |= DB_AGG_CMD_SET << FCOE_DB_DATA_AGG_CMD_SHIFT;
786 dbell.params |= DQ_XCM_FCOE_SQ_PROD_CMD <<
787 FCOE_DB_DATA_AGG_VAL_SEL_SHIFT;
788
789 dbell.sq_prod = fcport->fw_sq_prod_idx;
790 /* wmb makes sure that the BDs data is updated before updating the
791 * producer, otherwise FW may read old data from the BDs.
792 */
793 wmb();
794 barrier();
795 writel(*(u32 *)&dbell, fcport->p_doorbell);
796 /*
797 * Fence required to flush the write combined buffer, since another
798 * CPU may write to the same doorbell address and data may be lost
799 * due to relaxed order nature of write combined bar.
800 */
801 wmb();
802 }
803
qedf_trace_io(struct qedf_rport * fcport,struct qedf_ioreq * io_req,int8_t direction)804 static void qedf_trace_io(struct qedf_rport *fcport, struct qedf_ioreq *io_req,
805 int8_t direction)
806 {
807 struct qedf_ctx *qedf = fcport->qedf;
808 struct qedf_io_log *io_log;
809 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
810 unsigned long flags;
811
812 spin_lock_irqsave(&qedf->io_trace_lock, flags);
813
814 io_log = &qedf->io_trace_buf[qedf->io_trace_idx];
815 io_log->direction = direction;
816 io_log->task_id = io_req->xid;
817 io_log->port_id = fcport->rdata->ids.port_id;
818 io_log->lun = sc_cmd->device->lun;
819 io_log->op = sc_cmd->cmnd[0];
820 io_log->lba[0] = sc_cmd->cmnd[2];
821 io_log->lba[1] = sc_cmd->cmnd[3];
822 io_log->lba[2] = sc_cmd->cmnd[4];
823 io_log->lba[3] = sc_cmd->cmnd[5];
824 io_log->bufflen = scsi_bufflen(sc_cmd);
825 io_log->sg_count = scsi_sg_count(sc_cmd);
826 io_log->result = sc_cmd->result;
827 io_log->jiffies = jiffies;
828 io_log->refcount = kref_read(&io_req->refcount);
829
830 if (direction == QEDF_IO_TRACE_REQ) {
831 /* For requests we only care abot the submission CPU */
832 io_log->req_cpu = io_req->cpu;
833 io_log->int_cpu = 0;
834 io_log->rsp_cpu = 0;
835 } else if (direction == QEDF_IO_TRACE_RSP) {
836 io_log->req_cpu = io_req->cpu;
837 io_log->int_cpu = io_req->int_cpu;
838 io_log->rsp_cpu = smp_processor_id();
839 }
840
841 io_log->sge_type = io_req->sge_type;
842
843 qedf->io_trace_idx++;
844 if (qedf->io_trace_idx == QEDF_IO_TRACE_SIZE)
845 qedf->io_trace_idx = 0;
846
847 spin_unlock_irqrestore(&qedf->io_trace_lock, flags);
848 }
849
qedf_post_io_req(struct qedf_rport * fcport,struct qedf_ioreq * io_req)850 int qedf_post_io_req(struct qedf_rport *fcport, struct qedf_ioreq *io_req)
851 {
852 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
853 struct Scsi_Host *host = sc_cmd->device->host;
854 struct fc_lport *lport = shost_priv(host);
855 struct qedf_ctx *qedf = lport_priv(lport);
856 struct fcoe_task_context *task_ctx;
857 u16 xid;
858 struct fcoe_wqe *sqe;
859 u16 sqe_idx;
860
861 /* Initialize rest of io_req fileds */
862 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
863 qedf_priv(sc_cmd)->io_req = io_req;
864 io_req->sge_type = QEDF_IOREQ_FAST_SGE; /* Assume fast SGL by default */
865
866 /* Record which cpu this request is associated with */
867 io_req->cpu = smp_processor_id();
868
869 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
870 io_req->io_req_flags = QEDF_READ;
871 qedf->input_requests++;
872 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
873 io_req->io_req_flags = QEDF_WRITE;
874 qedf->output_requests++;
875 } else {
876 io_req->io_req_flags = 0;
877 qedf->control_requests++;
878 }
879
880 xid = io_req->xid;
881
882 /* Build buffer descriptor list for firmware from sg list */
883 if (qedf_build_bd_list_from_sg(io_req)) {
884 QEDF_ERR(&(qedf->dbg_ctx), "BD list creation failed.\n");
885 /* Release cmd will release io_req, but sc_cmd is assigned */
886 io_req->sc_cmd = NULL;
887 kref_put(&io_req->refcount, qedf_release_cmd);
888 return -EAGAIN;
889 }
890
891 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
892 test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
893 QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
894 /* Release cmd will release io_req, but sc_cmd is assigned */
895 io_req->sc_cmd = NULL;
896 kref_put(&io_req->refcount, qedf_release_cmd);
897 return -EINVAL;
898 }
899
900 /* Record LUN number for later use if we need them */
901 io_req->lun = (int)sc_cmd->device->lun;
902
903 /* Obtain free SQE */
904 sqe_idx = qedf_get_sqe_idx(fcport);
905 sqe = &fcport->sq[sqe_idx];
906 memset(sqe, 0, sizeof(struct fcoe_wqe));
907
908 /* Get the task context */
909 task_ctx = qedf_get_task_mem(&qedf->tasks, xid);
910 if (!task_ctx) {
911 QEDF_WARN(&(qedf->dbg_ctx), "task_ctx is NULL, xid=%d.\n",
912 xid);
913 /* Release cmd will release io_req, but sc_cmd is assigned */
914 io_req->sc_cmd = NULL;
915 kref_put(&io_req->refcount, qedf_release_cmd);
916 return -EINVAL;
917 }
918
919 qedf_init_task(fcport, lport, io_req, task_ctx, sqe);
920
921 /* Ring doorbell */
922 qedf_ring_doorbell(fcport);
923
924 /* Set that command is with the firmware now */
925 set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
926
927 if (qedf_io_tracing && io_req->sc_cmd)
928 qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_REQ);
929
930 return false;
931 }
932
933 int
qedf_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * sc_cmd)934 qedf_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc_cmd)
935 {
936 struct fc_lport *lport = shost_priv(host);
937 struct qedf_ctx *qedf = lport_priv(lport);
938 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
939 struct fc_rport_libfc_priv *rp = rport->dd_data;
940 struct qedf_rport *fcport;
941 struct qedf_ioreq *io_req;
942 int rc = 0;
943 int rval;
944 unsigned long flags = 0;
945 int num_sgs = 0;
946
947 num_sgs = scsi_sg_count(sc_cmd);
948 if (scsi_sg_count(sc_cmd) > QEDF_MAX_BDS_PER_CMD) {
949 QEDF_ERR(&qedf->dbg_ctx,
950 "Number of SG elements %d exceeds what hardware limitation of %d.\n",
951 num_sgs, QEDF_MAX_BDS_PER_CMD);
952 sc_cmd->result = DID_ERROR;
953 scsi_done(sc_cmd);
954 return 0;
955 }
956
957 if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
958 test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
959 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
960 "Returning DNC as unloading or stop io, flags 0x%lx.\n",
961 qedf->flags);
962 sc_cmd->result = DID_NO_CONNECT << 16;
963 scsi_done(sc_cmd);
964 return 0;
965 }
966
967 if (!qedf->pdev->msix_enabled) {
968 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
969 "Completing sc_cmd=%p DID_NO_CONNECT as MSI-X is not enabled.\n",
970 sc_cmd);
971 sc_cmd->result = DID_NO_CONNECT << 16;
972 scsi_done(sc_cmd);
973 return 0;
974 }
975
976 rval = fc_remote_port_chkready(rport);
977 if (rval) {
978 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
979 "fc_remote_port_chkready failed=0x%x for port_id=0x%06x.\n",
980 rval, rport->port_id);
981 sc_cmd->result = rval;
982 scsi_done(sc_cmd);
983 return 0;
984 }
985
986 /* Retry command if we are doing a qed drain operation */
987 if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
988 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Drain active.\n");
989 rc = SCSI_MLQUEUE_HOST_BUSY;
990 goto exit_qcmd;
991 }
992
993 if (lport->state != LPORT_ST_READY ||
994 atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
995 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Link down.\n");
996 rc = SCSI_MLQUEUE_HOST_BUSY;
997 goto exit_qcmd;
998 }
999
1000 /* rport and tgt are allocated together, so tgt should be non-NULL */
1001 fcport = (struct qedf_rport *)&rp[1];
1002
1003 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
1004 test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
1005 /*
1006 * Session is not offloaded yet. Let SCSI-ml retry
1007 * the command.
1008 */
1009 rc = SCSI_MLQUEUE_TARGET_BUSY;
1010 goto exit_qcmd;
1011 }
1012
1013 atomic_inc(&fcport->ios_to_queue);
1014
1015 if (fcport->retry_delay_timestamp) {
1016 /* Take fcport->rport_lock for resetting the delay_timestamp */
1017 spin_lock_irqsave(&fcport->rport_lock, flags);
1018 if (time_after(jiffies, fcport->retry_delay_timestamp)) {
1019 fcport->retry_delay_timestamp = 0;
1020 } else {
1021 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1022 /* If retry_delay timer is active, flow off the ML */
1023 rc = SCSI_MLQUEUE_TARGET_BUSY;
1024 atomic_dec(&fcport->ios_to_queue);
1025 goto exit_qcmd;
1026 }
1027 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1028 }
1029
1030 io_req = qedf_alloc_cmd(fcport, QEDF_SCSI_CMD);
1031 if (!io_req) {
1032 rc = SCSI_MLQUEUE_HOST_BUSY;
1033 atomic_dec(&fcport->ios_to_queue);
1034 goto exit_qcmd;
1035 }
1036
1037 io_req->sc_cmd = sc_cmd;
1038
1039 /* Take fcport->rport_lock for posting to fcport send queue */
1040 spin_lock_irqsave(&fcport->rport_lock, flags);
1041 if (qedf_post_io_req(fcport, io_req)) {
1042 QEDF_WARN(&(qedf->dbg_ctx), "Unable to post io_req\n");
1043 /* Return SQE to pool */
1044 atomic_inc(&fcport->free_sqes);
1045 rc = SCSI_MLQUEUE_HOST_BUSY;
1046 }
1047 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1048 atomic_dec(&fcport->ios_to_queue);
1049
1050 exit_qcmd:
1051 return rc;
1052 }
1053
qedf_parse_fcp_rsp(struct qedf_ioreq * io_req,struct fcoe_cqe_rsp_info * fcp_rsp)1054 static void qedf_parse_fcp_rsp(struct qedf_ioreq *io_req,
1055 struct fcoe_cqe_rsp_info *fcp_rsp)
1056 {
1057 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1058 struct qedf_ctx *qedf = io_req->fcport->qedf;
1059 u8 rsp_flags = fcp_rsp->rsp_flags.flags;
1060 int fcp_sns_len = 0;
1061 int fcp_rsp_len = 0;
1062 uint8_t *rsp_info, *sense_data;
1063
1064 io_req->fcp_status = FC_GOOD;
1065 io_req->fcp_resid = 0;
1066 if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1067 FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1068 io_req->fcp_resid = fcp_rsp->fcp_resid;
1069
1070 io_req->scsi_comp_flags = rsp_flags;
1071 io_req->cdb_status = fcp_rsp->scsi_status_code;
1072
1073 if (rsp_flags &
1074 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID)
1075 fcp_rsp_len = fcp_rsp->fcp_rsp_len;
1076
1077 if (rsp_flags &
1078 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID)
1079 fcp_sns_len = fcp_rsp->fcp_sns_len;
1080
1081 io_req->fcp_rsp_len = fcp_rsp_len;
1082 io_req->fcp_sns_len = fcp_sns_len;
1083 rsp_info = sense_data = io_req->sense_buffer;
1084
1085 /* fetch fcp_rsp_code */
1086 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1087 /* Only for task management function */
1088 io_req->fcp_rsp_code = rsp_info[3];
1089 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1090 "fcp_rsp_code = %d\n", io_req->fcp_rsp_code);
1091 /* Adjust sense-data location. */
1092 sense_data += fcp_rsp_len;
1093 }
1094
1095 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1096 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1097 "Truncating sense buffer\n");
1098 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1099 }
1100
1101 /* The sense buffer can be NULL for TMF commands */
1102 if (sc_cmd && sc_cmd->sense_buffer) {
1103 memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1104 if (fcp_sns_len)
1105 memcpy(sc_cmd->sense_buffer, sense_data,
1106 fcp_sns_len);
1107 }
1108 }
1109
qedf_unmap_sg_list(struct qedf_ctx * qedf,struct qedf_ioreq * io_req)1110 static void qedf_unmap_sg_list(struct qedf_ctx *qedf, struct qedf_ioreq *io_req)
1111 {
1112 struct scsi_cmnd *sc = io_req->sc_cmd;
1113
1114 if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
1115 dma_unmap_sg(&qedf->pdev->dev, scsi_sglist(sc),
1116 scsi_sg_count(sc), sc->sc_data_direction);
1117 io_req->bd_tbl->bd_valid = 0;
1118 }
1119 }
1120
qedf_scsi_completion(struct qedf_ctx * qedf,struct fcoe_cqe * cqe,struct qedf_ioreq * io_req)1121 void qedf_scsi_completion(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1122 struct qedf_ioreq *io_req)
1123 {
1124 struct scsi_cmnd *sc_cmd;
1125 struct fcoe_cqe_rsp_info *fcp_rsp;
1126 struct qedf_rport *fcport;
1127 int refcount;
1128 u16 scope, qualifier = 0;
1129 u8 fw_residual_flag = 0;
1130 unsigned long flags = 0;
1131 u16 chk_scope = 0;
1132
1133 if (!io_req)
1134 return;
1135 if (!cqe)
1136 return;
1137
1138 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
1139 test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
1140 test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
1141 QEDF_ERR(&qedf->dbg_ctx,
1142 "io_req xid=0x%x already in cleanup or abort processing or already completed.\n",
1143 io_req->xid);
1144 return;
1145 }
1146
1147 sc_cmd = io_req->sc_cmd;
1148 fcp_rsp = &cqe->cqe_info.rsp_info;
1149
1150 if (!sc_cmd) {
1151 QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
1152 return;
1153 }
1154
1155 if (!qedf_priv(sc_cmd)->io_req) {
1156 QEDF_WARN(&(qedf->dbg_ctx),
1157 "io_req is NULL, returned in another context.\n");
1158 return;
1159 }
1160
1161 if (!sc_cmd->device) {
1162 QEDF_ERR(&qedf->dbg_ctx,
1163 "Device for sc_cmd %p is NULL.\n", sc_cmd);
1164 return;
1165 }
1166
1167 if (!scsi_cmd_to_rq(sc_cmd)->q) {
1168 QEDF_WARN(&(qedf->dbg_ctx), "request->q is NULL so request "
1169 "is not valid, sc_cmd=%p.\n", sc_cmd);
1170 return;
1171 }
1172
1173 fcport = io_req->fcport;
1174
1175 /*
1176 * When flush is active, let the cmds be completed from the cleanup
1177 * context
1178 */
1179 if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
1180 (test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags) &&
1181 sc_cmd->device->lun == (u64)fcport->lun_reset_lun)) {
1182 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1183 "Dropping good completion xid=0x%x as fcport is flushing",
1184 io_req->xid);
1185 return;
1186 }
1187
1188 qedf_parse_fcp_rsp(io_req, fcp_rsp);
1189
1190 qedf_unmap_sg_list(qedf, io_req);
1191
1192 /* Check for FCP transport error */
1193 if (io_req->fcp_rsp_len > 3 && io_req->fcp_rsp_code) {
1194 QEDF_ERR(&(qedf->dbg_ctx),
1195 "FCP I/O protocol failure xid=0x%x fcp_rsp_len=%d "
1196 "fcp_rsp_code=%d.\n", io_req->xid, io_req->fcp_rsp_len,
1197 io_req->fcp_rsp_code);
1198 sc_cmd->result = DID_BUS_BUSY << 16;
1199 goto out;
1200 }
1201
1202 fw_residual_flag = GET_FIELD(cqe->cqe_info.rsp_info.fw_error_flags,
1203 FCOE_CQE_RSP_INFO_FW_UNDERRUN);
1204 if (fw_residual_flag) {
1205 QEDF_ERR(&qedf->dbg_ctx,
1206 "Firmware detected underrun: xid=0x%x fcp_rsp.flags=0x%02x fcp_resid=%d fw_residual=0x%x lba=%02x%02x%02x%02x.\n",
1207 io_req->xid, fcp_rsp->rsp_flags.flags,
1208 io_req->fcp_resid,
1209 cqe->cqe_info.rsp_info.fw_residual, sc_cmd->cmnd[2],
1210 sc_cmd->cmnd[3], sc_cmd->cmnd[4], sc_cmd->cmnd[5]);
1211
1212 if (io_req->cdb_status == 0)
1213 sc_cmd->result = (DID_ERROR << 16) | io_req->cdb_status;
1214 else
1215 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1216
1217 /*
1218 * Set resid to the whole buffer length so we won't try to resue
1219 * any previously data.
1220 */
1221 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
1222 goto out;
1223 }
1224
1225 switch (io_req->fcp_status) {
1226 case FC_GOOD:
1227 if (io_req->cdb_status == 0) {
1228 /* Good I/O completion */
1229 sc_cmd->result = DID_OK << 16;
1230 } else {
1231 refcount = kref_read(&io_req->refcount);
1232 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1233 "%d:0:%d:%lld xid=0x%0x op=0x%02x "
1234 "lba=%02x%02x%02x%02x cdb_status=%d "
1235 "fcp_resid=0x%x refcount=%d.\n",
1236 qedf->lport->host->host_no, sc_cmd->device->id,
1237 sc_cmd->device->lun, io_req->xid,
1238 sc_cmd->cmnd[0], sc_cmd->cmnd[2], sc_cmd->cmnd[3],
1239 sc_cmd->cmnd[4], sc_cmd->cmnd[5],
1240 io_req->cdb_status, io_req->fcp_resid,
1241 refcount);
1242 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1243
1244 if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1245 io_req->cdb_status == SAM_STAT_BUSY) {
1246 /*
1247 * Check whether we need to set retry_delay at
1248 * all based on retry_delay module parameter
1249 * and the status qualifier.
1250 */
1251
1252 /* Upper 2 bits */
1253 scope = fcp_rsp->retry_delay_timer & 0xC000;
1254 /* Lower 14 bits */
1255 qualifier = fcp_rsp->retry_delay_timer & 0x3FFF;
1256
1257 if (qedf_retry_delay)
1258 chk_scope = 1;
1259 /* Record stats */
1260 if (io_req->cdb_status ==
1261 SAM_STAT_TASK_SET_FULL)
1262 qedf->task_set_fulls++;
1263 else
1264 qedf->busy++;
1265 }
1266 }
1267 if (io_req->fcp_resid)
1268 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1269
1270 if (chk_scope == 1) {
1271 if ((scope == 1 || scope == 2) &&
1272 (qualifier > 0 && qualifier <= 0x3FEF)) {
1273 /* Check we don't go over the max */
1274 if (qualifier > QEDF_RETRY_DELAY_MAX) {
1275 qualifier = QEDF_RETRY_DELAY_MAX;
1276 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1277 "qualifier = %d\n",
1278 (fcp_rsp->retry_delay_timer &
1279 0x3FFF));
1280 }
1281 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1282 "Scope = %d and qualifier = %d",
1283 scope, qualifier);
1284 /* Take fcport->rport_lock to
1285 * update the retry_delay_timestamp
1286 */
1287 spin_lock_irqsave(&fcport->rport_lock, flags);
1288 fcport->retry_delay_timestamp =
1289 jiffies + (qualifier * HZ / 10);
1290 spin_unlock_irqrestore(&fcport->rport_lock,
1291 flags);
1292
1293 } else {
1294 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1295 "combination of scope = %d and qualifier = %d is not handled in qedf.\n",
1296 scope, qualifier);
1297 }
1298 }
1299 break;
1300 default:
1301 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "fcp_status=%d.\n",
1302 io_req->fcp_status);
1303 break;
1304 }
1305
1306 out:
1307 if (qedf_io_tracing)
1308 qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_RSP);
1309
1310 /*
1311 * We wait till the end of the function to clear the
1312 * outstanding bit in case we need to send an abort
1313 */
1314 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
1315
1316 io_req->sc_cmd = NULL;
1317 qedf_priv(sc_cmd)->io_req = NULL;
1318 scsi_done(sc_cmd);
1319 kref_put(&io_req->refcount, qedf_release_cmd);
1320 }
1321
1322 /* Return a SCSI command in some other context besides a normal completion */
qedf_scsi_done(struct qedf_ctx * qedf,struct qedf_ioreq * io_req,int result)1323 void qedf_scsi_done(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
1324 int result)
1325 {
1326 struct scsi_cmnd *sc_cmd;
1327 int refcount;
1328
1329 if (!io_req) {
1330 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "io_req is NULL\n");
1331 return;
1332 }
1333
1334 if (test_and_set_bit(QEDF_CMD_ERR_SCSI_DONE, &io_req->flags)) {
1335 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1336 "io_req:%p scsi_done handling already done\n",
1337 io_req);
1338 return;
1339 }
1340
1341 /*
1342 * We will be done with this command after this call so clear the
1343 * outstanding bit.
1344 */
1345 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
1346
1347 sc_cmd = io_req->sc_cmd;
1348
1349 if (!sc_cmd) {
1350 QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
1351 return;
1352 }
1353
1354 if (!virt_addr_valid(sc_cmd)) {
1355 QEDF_ERR(&qedf->dbg_ctx, "sc_cmd=%p is not valid.", sc_cmd);
1356 goto bad_scsi_ptr;
1357 }
1358
1359 if (!qedf_priv(sc_cmd)->io_req) {
1360 QEDF_WARN(&(qedf->dbg_ctx),
1361 "io_req is NULL, returned in another context.\n");
1362 return;
1363 }
1364
1365 if (!sc_cmd->device) {
1366 QEDF_ERR(&qedf->dbg_ctx, "Device for sc_cmd %p is NULL.\n",
1367 sc_cmd);
1368 goto bad_scsi_ptr;
1369 }
1370
1371 if (!virt_addr_valid(sc_cmd->device)) {
1372 QEDF_ERR(&qedf->dbg_ctx,
1373 "Device pointer for sc_cmd %p is bad.\n", sc_cmd);
1374 goto bad_scsi_ptr;
1375 }
1376
1377 if (!sc_cmd->sense_buffer) {
1378 QEDF_ERR(&qedf->dbg_ctx,
1379 "sc_cmd->sense_buffer for sc_cmd %p is NULL.\n",
1380 sc_cmd);
1381 goto bad_scsi_ptr;
1382 }
1383
1384 if (!virt_addr_valid(sc_cmd->sense_buffer)) {
1385 QEDF_ERR(&qedf->dbg_ctx,
1386 "sc_cmd->sense_buffer for sc_cmd %p is bad.\n",
1387 sc_cmd);
1388 goto bad_scsi_ptr;
1389 }
1390
1391 qedf_unmap_sg_list(qedf, io_req);
1392
1393 sc_cmd->result = result << 16;
1394 refcount = kref_read(&io_req->refcount);
1395 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "%d:0:%d:%lld: Completing "
1396 "sc_cmd=%p result=0x%08x op=0x%02x lba=0x%02x%02x%02x%02x, "
1397 "allowed=%d retries=%d refcount=%d.\n",
1398 qedf->lport->host->host_no, sc_cmd->device->id,
1399 sc_cmd->device->lun, sc_cmd, sc_cmd->result, sc_cmd->cmnd[0],
1400 sc_cmd->cmnd[2], sc_cmd->cmnd[3], sc_cmd->cmnd[4],
1401 sc_cmd->cmnd[5], sc_cmd->allowed, sc_cmd->retries,
1402 refcount);
1403
1404 /*
1405 * Set resid to the whole buffer length so we won't try to resue any
1406 * previously read data
1407 */
1408 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
1409
1410 if (qedf_io_tracing)
1411 qedf_trace_io(io_req->fcport, io_req, QEDF_IO_TRACE_RSP);
1412
1413 io_req->sc_cmd = NULL;
1414 qedf_priv(sc_cmd)->io_req = NULL;
1415 scsi_done(sc_cmd);
1416 kref_put(&io_req->refcount, qedf_release_cmd);
1417 return;
1418
1419 bad_scsi_ptr:
1420 /*
1421 * Clear the io_req->sc_cmd backpointer so we don't try to process
1422 * this again
1423 */
1424 io_req->sc_cmd = NULL;
1425 kref_put(&io_req->refcount, qedf_release_cmd); /* ID: 001 */
1426 }
1427
1428 /*
1429 * Handle warning type CQE completions. This is mainly used for REC timer
1430 * popping.
1431 */
qedf_process_warning_compl(struct qedf_ctx * qedf,struct fcoe_cqe * cqe,struct qedf_ioreq * io_req)1432 void qedf_process_warning_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1433 struct qedf_ioreq *io_req)
1434 {
1435 int rval, i;
1436 struct qedf_rport *fcport = io_req->fcport;
1437 u64 err_warn_bit_map;
1438 u8 err_warn = 0xff;
1439
1440 if (!cqe) {
1441 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1442 "cqe is NULL for io_req %p xid=0x%x\n",
1443 io_req, io_req->xid);
1444 return;
1445 }
1446
1447 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Warning CQE, "
1448 "xid=0x%x\n", io_req->xid);
1449 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
1450 "err_warn_bitmap=%08x:%08x\n",
1451 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
1452 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
1453 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
1454 "rx_buff_off=%08x, rx_id=%04x\n",
1455 le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
1456 le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
1457 le32_to_cpu(cqe->cqe_info.err_info.rx_id));
1458
1459 /* Normalize the error bitmap value to an just an unsigned int */
1460 err_warn_bit_map = (u64)
1461 ((u64)cqe->cqe_info.err_info.err_warn_bitmap_hi << 32) |
1462 (u64)cqe->cqe_info.err_info.err_warn_bitmap_lo;
1463 for (i = 0; i < 64; i++) {
1464 if (err_warn_bit_map & (u64)((u64)1 << i)) {
1465 err_warn = i;
1466 break;
1467 }
1468 }
1469
1470 /* Check if REC TOV expired if this is a tape device */
1471 if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1472 if (err_warn ==
1473 FCOE_WARNING_CODE_REC_TOV_TIMER_EXPIRATION) {
1474 QEDF_ERR(&(qedf->dbg_ctx), "REC timer expired.\n");
1475 if (!test_bit(QEDF_CMD_SRR_SENT, &io_req->flags)) {
1476 io_req->rx_buf_off =
1477 cqe->cqe_info.err_info.rx_buf_off;
1478 io_req->tx_buf_off =
1479 cqe->cqe_info.err_info.tx_buf_off;
1480 io_req->rx_id = cqe->cqe_info.err_info.rx_id;
1481 rval = qedf_send_rec(io_req);
1482 /*
1483 * We only want to abort the io_req if we
1484 * can't queue the REC command as we want to
1485 * keep the exchange open for recovery.
1486 */
1487 if (rval)
1488 goto send_abort;
1489 }
1490 return;
1491 }
1492 }
1493
1494 send_abort:
1495 init_completion(&io_req->abts_done);
1496 rval = qedf_initiate_abts(io_req, true);
1497 if (rval)
1498 QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
1499 }
1500
1501 /* Cleanup a command when we receive an error detection completion */
qedf_process_error_detect(struct qedf_ctx * qedf,struct fcoe_cqe * cqe,struct qedf_ioreq * io_req)1502 void qedf_process_error_detect(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1503 struct qedf_ioreq *io_req)
1504 {
1505 int rval;
1506
1507 if (io_req == NULL) {
1508 QEDF_INFO(NULL, QEDF_LOG_IO, "io_req is NULL.\n");
1509 return;
1510 }
1511
1512 if (io_req->fcport == NULL) {
1513 QEDF_INFO(NULL, QEDF_LOG_IO, "fcport is NULL.\n");
1514 return;
1515 }
1516
1517 if (!cqe) {
1518 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1519 "cqe is NULL for io_req %p\n", io_req);
1520 return;
1521 }
1522
1523 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Error detection CQE, "
1524 "xid=0x%x\n", io_req->xid);
1525 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
1526 "err_warn_bitmap=%08x:%08x\n",
1527 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
1528 le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
1529 QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
1530 "rx_buff_off=%08x, rx_id=%04x\n",
1531 le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
1532 le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
1533 le32_to_cpu(cqe->cqe_info.err_info.rx_id));
1534
1535 /* When flush is active, let the cmds be flushed out from the cleanup context */
1536 if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &io_req->fcport->flags) ||
1537 (test_bit(QEDF_RPORT_IN_LUN_RESET, &io_req->fcport->flags) &&
1538 io_req->sc_cmd->device->lun == (u64)io_req->fcport->lun_reset_lun)) {
1539 QEDF_ERR(&qedf->dbg_ctx,
1540 "Dropping EQE for xid=0x%x as fcport is flushing",
1541 io_req->xid);
1542 return;
1543 }
1544
1545 if (qedf->stop_io_on_error) {
1546 qedf_stop_all_io(qedf);
1547 return;
1548 }
1549
1550 init_completion(&io_req->abts_done);
1551 rval = qedf_initiate_abts(io_req, true);
1552 if (rval)
1553 QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
1554 }
1555
qedf_flush_els_req(struct qedf_ctx * qedf,struct qedf_ioreq * els_req)1556 static void qedf_flush_els_req(struct qedf_ctx *qedf,
1557 struct qedf_ioreq *els_req)
1558 {
1559 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1560 "Flushing ELS request xid=0x%x refcount=%d.\n", els_req->xid,
1561 kref_read(&els_req->refcount));
1562
1563 /*
1564 * Need to distinguish this from a timeout when calling the
1565 * els_req->cb_func.
1566 */
1567 els_req->event = QEDF_IOREQ_EV_ELS_FLUSH;
1568
1569 clear_bit(QEDF_CMD_OUTSTANDING, &els_req->flags);
1570
1571 /* Cancel the timer */
1572 cancel_delayed_work_sync(&els_req->timeout_work);
1573
1574 /* Call callback function to complete command */
1575 if (els_req->cb_func && els_req->cb_arg) {
1576 els_req->cb_func(els_req->cb_arg);
1577 els_req->cb_arg = NULL;
1578 }
1579
1580 /* Release kref for original initiate_els */
1581 kref_put(&els_req->refcount, qedf_release_cmd);
1582 }
1583
1584 /* A value of -1 for lun is a wild card that means flush all
1585 * active SCSI I/Os for the target.
1586 */
qedf_flush_active_ios(struct qedf_rport * fcport,u64 lun)1587 void qedf_flush_active_ios(struct qedf_rport *fcport, u64 lun)
1588 {
1589 struct qedf_ioreq *io_req;
1590 struct qedf_ctx *qedf;
1591 struct qedf_cmd_mgr *cmd_mgr;
1592 int i, rc;
1593 unsigned long flags;
1594 int flush_cnt = 0;
1595 int wait_cnt = 100;
1596 int refcount = 0;
1597
1598 if (!fcport) {
1599 QEDF_ERR(NULL, "fcport is NULL\n");
1600 return;
1601 }
1602
1603 /* Check that fcport is still offloaded */
1604 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1605 QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
1606 return;
1607 }
1608
1609 qedf = fcport->qedf;
1610
1611 if (!qedf) {
1612 QEDF_ERR(NULL, "qedf is NULL.\n");
1613 return;
1614 }
1615
1616 /* Only wait for all commands to be queued in the Upload context */
1617 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
1618 (lun == -1)) {
1619 while (atomic_read(&fcport->ios_to_queue)) {
1620 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1621 "Waiting for %d I/Os to be queued\n",
1622 atomic_read(&fcport->ios_to_queue));
1623 if (wait_cnt == 0) {
1624 QEDF_ERR(NULL,
1625 "%d IOs request could not be queued\n",
1626 atomic_read(&fcport->ios_to_queue));
1627 }
1628 msleep(20);
1629 wait_cnt--;
1630 }
1631 }
1632
1633 cmd_mgr = qedf->cmd_mgr;
1634
1635 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1636 "Flush active i/o's num=0x%x fcport=0x%p port_id=0x%06x scsi_id=%d.\n",
1637 atomic_read(&fcport->num_active_ios), fcport,
1638 fcport->rdata->ids.port_id, fcport->rport->scsi_target_id);
1639 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Locking flush mutex.\n");
1640
1641 mutex_lock(&qedf->flush_mutex);
1642 if (lun == -1) {
1643 set_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
1644 } else {
1645 set_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
1646 fcport->lun_reset_lun = lun;
1647 }
1648
1649 for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
1650 io_req = &cmd_mgr->cmds[i];
1651
1652 if (!io_req)
1653 continue;
1654 if (!io_req->fcport)
1655 continue;
1656
1657 spin_lock_irqsave(&cmd_mgr->lock, flags);
1658
1659 if (io_req->alloc) {
1660 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
1661 if (io_req->cmd_type == QEDF_SCSI_CMD)
1662 QEDF_ERR(&qedf->dbg_ctx,
1663 "Allocated but not queued, xid=0x%x\n",
1664 io_req->xid);
1665 }
1666 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
1667 } else {
1668 spin_unlock_irqrestore(&cmd_mgr->lock, flags);
1669 continue;
1670 }
1671
1672 if (io_req->fcport != fcport)
1673 continue;
1674
1675 /* In case of ABTS, CMD_OUTSTANDING is cleared on ABTS response,
1676 * but RRQ is still pending.
1677 * Workaround: Within qedf_send_rrq, we check if the fcport is
1678 * NULL, and we drop the ref on the io_req to clean it up.
1679 */
1680 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
1681 refcount = kref_read(&io_req->refcount);
1682 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1683 "Not outstanding, xid=0x%x, cmd_type=%d refcount=%d.\n",
1684 io_req->xid, io_req->cmd_type, refcount);
1685 /* If RRQ work has been queue, try to cancel it and
1686 * free the io_req
1687 */
1688 if (atomic_read(&io_req->state) ==
1689 QEDFC_CMD_ST_RRQ_WAIT) {
1690 if (cancel_delayed_work_sync
1691 (&io_req->rrq_work)) {
1692 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1693 "Putting reference for pending RRQ work xid=0x%x.\n",
1694 io_req->xid);
1695 /* ID: 003 */
1696 kref_put(&io_req->refcount,
1697 qedf_release_cmd);
1698 }
1699 }
1700 continue;
1701 }
1702
1703 /* Only consider flushing ELS during target reset */
1704 if (io_req->cmd_type == QEDF_ELS &&
1705 lun == -1) {
1706 rc = kref_get_unless_zero(&io_req->refcount);
1707 if (!rc) {
1708 QEDF_ERR(&(qedf->dbg_ctx),
1709 "Could not get kref for ELS io_req=0x%p xid=0x%x.\n",
1710 io_req, io_req->xid);
1711 continue;
1712 }
1713 qedf_initiate_cleanup(io_req, false);
1714 flush_cnt++;
1715 qedf_flush_els_req(qedf, io_req);
1716
1717 /*
1718 * Release the kref and go back to the top of the
1719 * loop.
1720 */
1721 goto free_cmd;
1722 }
1723
1724 if (io_req->cmd_type == QEDF_ABTS) {
1725 /* ID: 004 */
1726 rc = kref_get_unless_zero(&io_req->refcount);
1727 if (!rc) {
1728 QEDF_ERR(&(qedf->dbg_ctx),
1729 "Could not get kref for abort io_req=0x%p xid=0x%x.\n",
1730 io_req, io_req->xid);
1731 continue;
1732 }
1733 if (lun != -1 && io_req->lun != lun)
1734 goto free_cmd;
1735
1736 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1737 "Flushing abort xid=0x%x.\n", io_req->xid);
1738
1739 if (cancel_delayed_work_sync(&io_req->rrq_work)) {
1740 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1741 "Putting ref for cancelled RRQ work xid=0x%x.\n",
1742 io_req->xid);
1743 kref_put(&io_req->refcount, qedf_release_cmd);
1744 }
1745
1746 if (cancel_delayed_work_sync(&io_req->timeout_work)) {
1747 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1748 "Putting ref for cancelled tmo work xid=0x%x.\n",
1749 io_req->xid);
1750 qedf_initiate_cleanup(io_req, true);
1751 /* Notify eh_abort handler that ABTS is
1752 * complete
1753 */
1754 complete(&io_req->abts_done);
1755 clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
1756 /* ID: 002 */
1757 kref_put(&io_req->refcount, qedf_release_cmd);
1758 }
1759 flush_cnt++;
1760 goto free_cmd;
1761 }
1762
1763 if (!io_req->sc_cmd)
1764 continue;
1765 if (!io_req->sc_cmd->device) {
1766 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1767 "Device backpointer NULL for sc_cmd=%p.\n",
1768 io_req->sc_cmd);
1769 /* Put reference for non-existent scsi_cmnd */
1770 io_req->sc_cmd = NULL;
1771 qedf_initiate_cleanup(io_req, false);
1772 kref_put(&io_req->refcount, qedf_release_cmd);
1773 continue;
1774 }
1775
1776 /*
1777 * Use kref_get_unless_zero in the unlikely case the command
1778 * we're about to flush was completed in the normal SCSI path
1779 */
1780 rc = kref_get_unless_zero(&io_req->refcount);
1781 if (!rc) {
1782 QEDF_ERR(&(qedf->dbg_ctx), "Could not get kref for "
1783 "io_req=0x%p xid=0x%x\n", io_req, io_req->xid);
1784 continue;
1785 }
1786
1787 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
1788 "Cleanup xid=0x%x.\n", io_req->xid);
1789 flush_cnt++;
1790
1791 /* Cleanup task and return I/O mid-layer */
1792 qedf_initiate_cleanup(io_req, true);
1793
1794 free_cmd:
1795 kref_put(&io_req->refcount, qedf_release_cmd); /* ID: 004 */
1796 }
1797
1798 wait_cnt = 60;
1799 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1800 "Flushed 0x%x I/Os, active=0x%x.\n",
1801 flush_cnt, atomic_read(&fcport->num_active_ios));
1802 /* Only wait for all commands to complete in the Upload context */
1803 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
1804 (lun == -1)) {
1805 while (atomic_read(&fcport->num_active_ios)) {
1806 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1807 "Flushed 0x%x I/Os, active=0x%x cnt=%d.\n",
1808 flush_cnt,
1809 atomic_read(&fcport->num_active_ios),
1810 wait_cnt);
1811 if (wait_cnt == 0) {
1812 QEDF_ERR(&qedf->dbg_ctx,
1813 "Flushed %d I/Os, active=%d.\n",
1814 flush_cnt,
1815 atomic_read(&fcport->num_active_ios));
1816 for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
1817 io_req = &cmd_mgr->cmds[i];
1818 if (io_req->fcport &&
1819 io_req->fcport == fcport) {
1820 refcount =
1821 kref_read(&io_req->refcount);
1822 set_bit(QEDF_CMD_DIRTY,
1823 &io_req->flags);
1824 QEDF_ERR(&qedf->dbg_ctx,
1825 "Outstanding io_req =%p xid=0x%x flags=0x%lx, sc_cmd=%p refcount=%d cmd_type=%d.\n",
1826 io_req, io_req->xid,
1827 io_req->flags,
1828 io_req->sc_cmd,
1829 refcount,
1830 io_req->cmd_type);
1831 }
1832 }
1833 WARN_ON(1);
1834 break;
1835 }
1836 msleep(500);
1837 wait_cnt--;
1838 }
1839 }
1840
1841 clear_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
1842 clear_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
1843 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Unlocking flush mutex.\n");
1844 mutex_unlock(&qedf->flush_mutex);
1845 }
1846
1847 /*
1848 * Initiate a ABTS middle path command. Note that we don't have to initialize
1849 * the task context for an ABTS task.
1850 */
qedf_initiate_abts(struct qedf_ioreq * io_req,bool return_scsi_cmd_on_abts)1851 int qedf_initiate_abts(struct qedf_ioreq *io_req, bool return_scsi_cmd_on_abts)
1852 {
1853 struct fc_lport *lport;
1854 struct qedf_rport *fcport = io_req->fcport;
1855 struct fc_rport_priv *rdata;
1856 struct qedf_ctx *qedf;
1857 u16 xid;
1858 int rc = 0;
1859 unsigned long flags;
1860 struct fcoe_wqe *sqe;
1861 u16 sqe_idx;
1862 int refcount = 0;
1863
1864 /* Sanity check qedf_rport before dereferencing any pointers */
1865 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1866 QEDF_ERR(NULL, "tgt not offloaded\n");
1867 rc = 1;
1868 goto out;
1869 }
1870
1871 qedf = fcport->qedf;
1872 rdata = fcport->rdata;
1873
1874 if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
1875 QEDF_ERR(&qedf->dbg_ctx, "stale rport\n");
1876 rc = 1;
1877 goto out;
1878 }
1879
1880 lport = qedf->lport;
1881
1882 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
1883 QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
1884 rc = 1;
1885 goto drop_rdata_kref;
1886 }
1887
1888 if (atomic_read(&qedf->link_down_tmo_valid) > 0) {
1889 QEDF_ERR(&(qedf->dbg_ctx), "link_down_tmo active.\n");
1890 rc = 1;
1891 goto drop_rdata_kref;
1892 }
1893
1894 /* Ensure room on SQ */
1895 if (!atomic_read(&fcport->free_sqes)) {
1896 QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
1897 rc = 1;
1898 goto drop_rdata_kref;
1899 }
1900
1901 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
1902 QEDF_ERR(&qedf->dbg_ctx, "fcport is uploading.\n");
1903 rc = 1;
1904 goto drop_rdata_kref;
1905 }
1906
1907 spin_lock_irqsave(&fcport->rport_lock, flags);
1908 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
1909 test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
1910 test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
1911 QEDF_ERR(&qedf->dbg_ctx,
1912 "io_req xid=0x%x sc_cmd=%p already in cleanup or abort processing or already completed.\n",
1913 io_req->xid, io_req->sc_cmd);
1914 rc = 1;
1915 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1916 goto drop_rdata_kref;
1917 }
1918
1919 /* Set the command type to abort */
1920 io_req->cmd_type = QEDF_ABTS;
1921 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1922
1923 kref_get(&io_req->refcount);
1924
1925 xid = io_req->xid;
1926 qedf->control_requests++;
1927 qedf->packet_aborts++;
1928
1929 io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
1930
1931 set_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
1932 refcount = kref_read(&io_req->refcount);
1933 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
1934 "ABTS io_req xid = 0x%x refcount=%d\n",
1935 xid, refcount);
1936
1937 qedf_cmd_timer_set(qedf, io_req, QEDF_ABORT_TIMEOUT);
1938
1939 spin_lock_irqsave(&fcport->rport_lock, flags);
1940
1941 sqe_idx = qedf_get_sqe_idx(fcport);
1942 sqe = &fcport->sq[sqe_idx];
1943 memset(sqe, 0, sizeof(struct fcoe_wqe));
1944 io_req->task_params->sqe = sqe;
1945
1946 init_initiator_abort_fcoe_task(io_req->task_params);
1947 qedf_ring_doorbell(fcport);
1948
1949 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1950
1951 drop_rdata_kref:
1952 kref_put(&rdata->kref, fc_rport_destroy);
1953 out:
1954 return rc;
1955 }
1956
qedf_process_abts_compl(struct qedf_ctx * qedf,struct fcoe_cqe * cqe,struct qedf_ioreq * io_req)1957 void qedf_process_abts_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
1958 struct qedf_ioreq *io_req)
1959 {
1960 uint32_t r_ctl;
1961 int rc;
1962 struct qedf_rport *fcport = io_req->fcport;
1963
1964 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "Entered with xid = "
1965 "0x%x cmd_type = %d\n", io_req->xid, io_req->cmd_type);
1966
1967 r_ctl = cqe->cqe_info.abts_info.r_ctl;
1968
1969 /* This was added at a point when we were scheduling abts_compl &
1970 * cleanup_compl on different CPUs and there was a possibility of
1971 * the io_req to be freed from the other context before we got here.
1972 */
1973 if (!fcport) {
1974 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1975 "Dropping ABTS completion xid=0x%x as fcport is NULL",
1976 io_req->xid);
1977 return;
1978 }
1979
1980 /*
1981 * When flush is active, let the cmds be completed from the cleanup
1982 * context
1983 */
1984 if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
1985 test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags)) {
1986 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
1987 "Dropping ABTS completion xid=0x%x as fcport is flushing",
1988 io_req->xid);
1989 return;
1990 }
1991
1992 if (!cancel_delayed_work(&io_req->timeout_work)) {
1993 QEDF_ERR(&qedf->dbg_ctx,
1994 "Wasn't able to cancel abts timeout work.\n");
1995 }
1996
1997 switch (r_ctl) {
1998 case FC_RCTL_BA_ACC:
1999 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
2000 "ABTS response - ACC Send RRQ after R_A_TOV\n");
2001 io_req->event = QEDF_IOREQ_EV_ABORT_SUCCESS;
2002 rc = kref_get_unless_zero(&io_req->refcount); /* ID: 003 */
2003 if (!rc) {
2004 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2005 "kref is already zero so ABTS was already completed or flushed xid=0x%x.\n",
2006 io_req->xid);
2007 return;
2008 }
2009 /*
2010 * Dont release this cmd yet. It will be relesed
2011 * after we get RRQ response
2012 */
2013 queue_delayed_work(qedf->dpc_wq, &io_req->rrq_work,
2014 msecs_to_jiffies(qedf->lport->r_a_tov));
2015 atomic_set(&io_req->state, QEDFC_CMD_ST_RRQ_WAIT);
2016 break;
2017 /* For error cases let the cleanup return the command */
2018 case FC_RCTL_BA_RJT:
2019 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
2020 "ABTS response - RJT\n");
2021 io_req->event = QEDF_IOREQ_EV_ABORT_FAILED;
2022 break;
2023 default:
2024 QEDF_ERR(&(qedf->dbg_ctx), "Unknown ABTS response\n");
2025 break;
2026 }
2027
2028 clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
2029
2030 if (io_req->sc_cmd) {
2031 if (!io_req->return_scsi_cmd_on_abts)
2032 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2033 "Not call scsi_done for xid=0x%x.\n",
2034 io_req->xid);
2035 if (io_req->return_scsi_cmd_on_abts)
2036 qedf_scsi_done(qedf, io_req, DID_ERROR);
2037 }
2038
2039 /* Notify eh_abort handler that ABTS is complete */
2040 complete(&io_req->abts_done);
2041
2042 kref_put(&io_req->refcount, qedf_release_cmd);
2043 }
2044
qedf_init_mp_req(struct qedf_ioreq * io_req)2045 int qedf_init_mp_req(struct qedf_ioreq *io_req)
2046 {
2047 struct qedf_mp_req *mp_req;
2048 struct scsi_sge *mp_req_bd;
2049 struct scsi_sge *mp_resp_bd;
2050 struct qedf_ctx *qedf = io_req->fcport->qedf;
2051 dma_addr_t addr;
2052 uint64_t sz;
2053
2054 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_MP_REQ, "Entered.\n");
2055
2056 mp_req = (struct qedf_mp_req *)&(io_req->mp_req);
2057 memset(mp_req, 0, sizeof(struct qedf_mp_req));
2058
2059 if (io_req->cmd_type != QEDF_ELS) {
2060 mp_req->req_len = sizeof(struct fcp_cmnd);
2061 io_req->data_xfer_len = mp_req->req_len;
2062 } else
2063 mp_req->req_len = io_req->data_xfer_len;
2064
2065 mp_req->req_buf = dma_alloc_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2066 &mp_req->req_buf_dma, GFP_KERNEL);
2067 if (!mp_req->req_buf) {
2068 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req buffer\n");
2069 qedf_free_mp_resc(io_req);
2070 return -ENOMEM;
2071 }
2072
2073 mp_req->resp_buf = dma_alloc_coherent(&qedf->pdev->dev,
2074 QEDF_PAGE_SIZE, &mp_req->resp_buf_dma, GFP_KERNEL);
2075 if (!mp_req->resp_buf) {
2076 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc TM resp "
2077 "buffer\n");
2078 qedf_free_mp_resc(io_req);
2079 return -ENOMEM;
2080 }
2081
2082 /* Allocate and map mp_req_bd and mp_resp_bd */
2083 sz = sizeof(struct scsi_sge);
2084 mp_req->mp_req_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
2085 &mp_req->mp_req_bd_dma, GFP_KERNEL);
2086 if (!mp_req->mp_req_bd) {
2087 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req bd\n");
2088 qedf_free_mp_resc(io_req);
2089 return -ENOMEM;
2090 }
2091
2092 mp_req->mp_resp_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
2093 &mp_req->mp_resp_bd_dma, GFP_KERNEL);
2094 if (!mp_req->mp_resp_bd) {
2095 QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP resp bd\n");
2096 qedf_free_mp_resc(io_req);
2097 return -ENOMEM;
2098 }
2099
2100 /* Fill bd table */
2101 addr = mp_req->req_buf_dma;
2102 mp_req_bd = mp_req->mp_req_bd;
2103 mp_req_bd->sge_addr.lo = U64_LO(addr);
2104 mp_req_bd->sge_addr.hi = U64_HI(addr);
2105 mp_req_bd->sge_len = QEDF_PAGE_SIZE;
2106
2107 /*
2108 * MP buffer is either a task mgmt command or an ELS.
2109 * So the assumption is that it consumes a single bd
2110 * entry in the bd table
2111 */
2112 mp_resp_bd = mp_req->mp_resp_bd;
2113 addr = mp_req->resp_buf_dma;
2114 mp_resp_bd->sge_addr.lo = U64_LO(addr);
2115 mp_resp_bd->sge_addr.hi = U64_HI(addr);
2116 mp_resp_bd->sge_len = QEDF_PAGE_SIZE;
2117
2118 return 0;
2119 }
2120
2121 /*
2122 * Last ditch effort to clear the port if it's stuck. Used only after a
2123 * cleanup task times out.
2124 */
qedf_drain_request(struct qedf_ctx * qedf)2125 static void qedf_drain_request(struct qedf_ctx *qedf)
2126 {
2127 if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
2128 QEDF_ERR(&(qedf->dbg_ctx), "MCP drain already active.\n");
2129 return;
2130 }
2131
2132 /* Set bit to return all queuecommand requests as busy */
2133 set_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
2134
2135 /* Call qed drain request for function. Should be synchronous */
2136 qed_ops->common->drain(qedf->cdev);
2137
2138 /* Settle time for CQEs to be returned */
2139 msleep(100);
2140
2141 /* Unplug and continue */
2142 clear_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
2143 }
2144
2145 /*
2146 * Returns SUCCESS if the cleanup task does not timeout, otherwise return
2147 * FAILURE.
2148 */
qedf_initiate_cleanup(struct qedf_ioreq * io_req,bool return_scsi_cmd_on_abts)2149 int qedf_initiate_cleanup(struct qedf_ioreq *io_req,
2150 bool return_scsi_cmd_on_abts)
2151 {
2152 struct qedf_rport *fcport;
2153 struct qedf_ctx *qedf;
2154 int tmo = 0;
2155 int rc = SUCCESS;
2156 unsigned long flags;
2157 struct fcoe_wqe *sqe;
2158 u16 sqe_idx;
2159 int refcount = 0;
2160
2161 fcport = io_req->fcport;
2162 if (!fcport) {
2163 QEDF_ERR(NULL, "fcport is NULL.\n");
2164 return SUCCESS;
2165 }
2166
2167 /* Sanity check qedf_rport before dereferencing any pointers */
2168 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2169 QEDF_ERR(NULL, "tgt not offloaded\n");
2170 return SUCCESS;
2171 }
2172
2173 qedf = fcport->qedf;
2174 if (!qedf) {
2175 QEDF_ERR(NULL, "qedf is NULL.\n");
2176 return SUCCESS;
2177 }
2178
2179 if (io_req->cmd_type == QEDF_ELS) {
2180 goto process_els;
2181 }
2182
2183 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
2184 test_and_set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags)) {
2185 QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
2186 "cleanup processing or already completed.\n",
2187 io_req->xid);
2188 return SUCCESS;
2189 }
2190 set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2191
2192 process_els:
2193 /* Ensure room on SQ */
2194 if (!atomic_read(&fcport->free_sqes)) {
2195 QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
2196 /* Need to make sure we clear the flag since it was set */
2197 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2198 return FAILED;
2199 }
2200
2201 if (io_req->cmd_type == QEDF_CLEANUP) {
2202 QEDF_ERR(&qedf->dbg_ctx,
2203 "io_req=0x%x is already a cleanup command cmd_type=%d.\n",
2204 io_req->xid, io_req->cmd_type);
2205 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2206 return SUCCESS;
2207 }
2208
2209 refcount = kref_read(&io_req->refcount);
2210
2211 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
2212 "Entered xid=0x%x sc_cmd=%p cmd_type=%d flags=0x%lx refcount=%d fcport=%p port_id=0x%06x\n",
2213 io_req->xid, io_req->sc_cmd, io_req->cmd_type, io_req->flags,
2214 refcount, fcport, fcport->rdata->ids.port_id);
2215
2216 /* Cleanup cmds re-use the same TID as the original I/O */
2217 spin_lock_irqsave(&fcport->rport_lock, flags);
2218 io_req->cmd_type = QEDF_CLEANUP;
2219 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2220 io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
2221
2222 init_completion(&io_req->cleanup_done);
2223
2224 spin_lock_irqsave(&fcport->rport_lock, flags);
2225
2226 sqe_idx = qedf_get_sqe_idx(fcport);
2227 sqe = &fcport->sq[sqe_idx];
2228 memset(sqe, 0, sizeof(struct fcoe_wqe));
2229 io_req->task_params->sqe = sqe;
2230
2231 init_initiator_cleanup_fcoe_task(io_req->task_params);
2232 qedf_ring_doorbell(fcport);
2233
2234 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2235
2236 tmo = wait_for_completion_timeout(&io_req->cleanup_done,
2237 QEDF_CLEANUP_TIMEOUT * HZ);
2238
2239 if (!tmo) {
2240 rc = FAILED;
2241 /* Timeout case */
2242 QEDF_ERR(&(qedf->dbg_ctx), "Cleanup command timeout, "
2243 "xid=%x.\n", io_req->xid);
2244 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2245 /* Issue a drain request if cleanup task times out */
2246 QEDF_ERR(&(qedf->dbg_ctx), "Issuing MCP drain request.\n");
2247 qedf_drain_request(qedf);
2248 }
2249
2250 /* If it TASK MGMT handle it, reference will be decreased
2251 * in qedf_execute_tmf
2252 */
2253 if (io_req->tm_flags == FCP_TMF_LUN_RESET ||
2254 io_req->tm_flags == FCP_TMF_TGT_RESET) {
2255 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2256 io_req->sc_cmd = NULL;
2257 kref_put(&io_req->refcount, qedf_release_cmd);
2258 complete(&io_req->tm_done);
2259 }
2260
2261 if (io_req->sc_cmd) {
2262 if (!io_req->return_scsi_cmd_on_abts)
2263 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
2264 "Not call scsi_done for xid=0x%x.\n",
2265 io_req->xid);
2266 if (io_req->return_scsi_cmd_on_abts)
2267 qedf_scsi_done(qedf, io_req, DID_ERROR);
2268 }
2269
2270 if (rc == SUCCESS)
2271 io_req->event = QEDF_IOREQ_EV_CLEANUP_SUCCESS;
2272 else
2273 io_req->event = QEDF_IOREQ_EV_CLEANUP_FAILED;
2274
2275 return rc;
2276 }
2277
qedf_process_cleanup_compl(struct qedf_ctx * qedf,struct fcoe_cqe * cqe,struct qedf_ioreq * io_req)2278 void qedf_process_cleanup_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
2279 struct qedf_ioreq *io_req)
2280 {
2281 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "Entered xid = 0x%x\n",
2282 io_req->xid);
2283
2284 clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
2285
2286 /* Complete so we can finish cleaning up the I/O */
2287 complete(&io_req->cleanup_done);
2288 }
2289
qedf_execute_tmf(struct qedf_rport * fcport,u64 tm_lun,uint8_t tm_flags)2290 static int qedf_execute_tmf(struct qedf_rport *fcport, u64 tm_lun,
2291 uint8_t tm_flags)
2292 {
2293 struct qedf_ioreq *io_req;
2294 struct fcoe_task_context *task;
2295 struct qedf_ctx *qedf = fcport->qedf;
2296 struct fc_lport *lport = qedf->lport;
2297 int rc = 0;
2298 uint16_t xid;
2299 int tmo = 0;
2300 unsigned long flags;
2301 struct fcoe_wqe *sqe;
2302 u16 sqe_idx;
2303
2304 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2305 QEDF_ERR(&(qedf->dbg_ctx), "fcport not offloaded\n");
2306 rc = FAILED;
2307 goto no_flush;
2308 }
2309
2310 io_req = qedf_alloc_cmd(fcport, QEDF_TASK_MGMT_CMD);
2311 if (!io_req) {
2312 QEDF_ERR(&(qedf->dbg_ctx), "Failed TMF");
2313 rc = -EAGAIN;
2314 goto no_flush;
2315 }
2316
2317 if (tm_flags == FCP_TMF_LUN_RESET)
2318 qedf->lun_resets++;
2319 else if (tm_flags == FCP_TMF_TGT_RESET)
2320 qedf->target_resets++;
2321
2322 /* Initialize rest of io_req fields */
2323 io_req->sc_cmd = NULL;
2324 io_req->fcport = fcport;
2325 io_req->cmd_type = QEDF_TASK_MGMT_CMD;
2326
2327 /* Set TM flags */
2328 io_req->io_req_flags = QEDF_READ;
2329 io_req->data_xfer_len = 0;
2330 io_req->tm_flags = tm_flags;
2331
2332 /* Default is to return a SCSI command when an error occurs */
2333 io_req->return_scsi_cmd_on_abts = false;
2334 io_req->tm_lun = tm_lun;
2335
2336 /* Obtain exchange id */
2337 xid = io_req->xid;
2338
2339 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "TMF io_req xid = "
2340 "0x%x\n", xid);
2341
2342 /* Initialize task context for this IO request */
2343 task = qedf_get_task_mem(&qedf->tasks, xid);
2344
2345 init_completion(&io_req->tm_done);
2346
2347 spin_lock_irqsave(&fcport->rport_lock, flags);
2348
2349 /* Record which cpu this request is associated with */
2350 io_req->cpu = smp_processor_id();
2351
2352 sqe_idx = qedf_get_sqe_idx(fcport);
2353 sqe = &fcport->sq[sqe_idx];
2354 memset(sqe, 0, sizeof(struct fcoe_wqe));
2355
2356 qedf_init_task(fcport, lport, io_req, task, sqe);
2357 qedf_ring_doorbell(fcport);
2358
2359 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2360
2361 set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2362 tmo = wait_for_completion_timeout(&io_req->tm_done,
2363 QEDF_TM_TIMEOUT * HZ);
2364
2365 if (!tmo) {
2366 rc = FAILED;
2367 QEDF_ERR(&(qedf->dbg_ctx), "wait for tm_cmpl timeout!\n");
2368 /* Clear outstanding bit since command timed out */
2369 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2370 io_req->sc_cmd = NULL;
2371 } else {
2372 /* Check TMF response code */
2373 if (io_req->fcp_rsp_code == 0)
2374 rc = SUCCESS;
2375 else
2376 rc = FAILED;
2377 }
2378 /*
2379 * Double check that fcport has not gone into an uploading state before
2380 * executing the command flush for the LUN/target.
2381 */
2382 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2383 QEDF_ERR(&qedf->dbg_ctx,
2384 "fcport is uploading, not executing flush.\n");
2385 goto no_flush;
2386 }
2387 /* We do not need this io_req any more */
2388 kref_put(&io_req->refcount, qedf_release_cmd);
2389
2390
2391 if (tm_flags == FCP_TMF_LUN_RESET)
2392 qedf_flush_active_ios(fcport, tm_lun);
2393 else
2394 qedf_flush_active_ios(fcport, -1);
2395
2396 no_flush:
2397 if (rc != SUCCESS) {
2398 QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command failed...\n");
2399 rc = FAILED;
2400 } else {
2401 QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command success...\n");
2402 rc = SUCCESS;
2403 }
2404 return rc;
2405 }
2406
qedf_initiate_tmf(struct fc_rport * rport,u64 lun,u8 tm_flags)2407 int qedf_initiate_tmf(struct fc_rport *rport, u64 lun, u8 tm_flags)
2408 {
2409 struct fc_rport_libfc_priv *rp = rport->dd_data;
2410 struct qedf_rport *fcport = (struct qedf_rport *)&rp[1];
2411 struct qedf_ctx *qedf = fcport->qedf;
2412 struct fc_lport *lport = rp->local_port;
2413 int rc = SUCCESS;
2414 struct fc_rport_priv *rdata = fcport->rdata;
2415
2416 QEDF_ERR(NULL,
2417 "tm_flags 0x%x target_id = 0x%x lun=%llu\n",
2418 tm_flags, rport->scsi_target_id, lun);
2419
2420 if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
2421 QEDF_ERR(NULL, "stale rport\n");
2422 return FAILED;
2423 }
2424
2425 QEDF_ERR(NULL, "portid=%06x tm_flags =%s\n", rdata->ids.port_id,
2426 (tm_flags == FCP_TMF_TGT_RESET) ? "TARGET RESET" :
2427 "LUN RESET");
2428
2429 rc = fc_block_rport(rport);
2430 if (rc)
2431 goto tmf_err;
2432
2433 if (!qedf) {
2434 QEDF_ERR(NULL, "qedf is NULL.\n");
2435 rc = FAILED;
2436 goto tmf_err;
2437 }
2438
2439 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2440 QEDF_ERR(&qedf->dbg_ctx, "Connection is getting uploaded.\n");
2441 rc = SUCCESS;
2442 goto tmf_err;
2443 }
2444
2445 if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
2446 test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
2447 rc = SUCCESS;
2448 goto tmf_err;
2449 }
2450
2451 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
2452 QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
2453 rc = FAILED;
2454 goto tmf_err;
2455 }
2456
2457 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
2458 if (!fcport->rdata)
2459 QEDF_ERR(&qedf->dbg_ctx, "fcport %p is uploading.\n",
2460 fcport);
2461 else
2462 QEDF_ERR(&qedf->dbg_ctx,
2463 "fcport %p port_id=%06x is uploading.\n",
2464 fcport, fcport->rdata->ids.port_id);
2465 rc = FAILED;
2466 goto tmf_err;
2467 }
2468
2469 rc = qedf_execute_tmf(fcport, lun, tm_flags);
2470
2471 tmf_err:
2472 kref_put(&rdata->kref, fc_rport_destroy);
2473 return rc;
2474 }
2475
qedf_process_tmf_compl(struct qedf_ctx * qedf,struct fcoe_cqe * cqe,struct qedf_ioreq * io_req)2476 void qedf_process_tmf_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
2477 struct qedf_ioreq *io_req)
2478 {
2479 struct fcoe_cqe_rsp_info *fcp_rsp;
2480
2481 clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
2482
2483 fcp_rsp = &cqe->cqe_info.rsp_info;
2484 qedf_parse_fcp_rsp(io_req, fcp_rsp);
2485
2486 complete(&io_req->tm_done);
2487 }
2488
qedf_process_unsol_compl(struct qedf_ctx * qedf,uint16_t que_idx,struct fcoe_cqe * cqe)2489 void qedf_process_unsol_compl(struct qedf_ctx *qedf, uint16_t que_idx,
2490 struct fcoe_cqe *cqe)
2491 {
2492 unsigned long flags;
2493 uint16_t pktlen = cqe->cqe_info.unsolic_info.pkt_len;
2494 u32 payload_len, crc;
2495 struct fc_frame_header *fh;
2496 struct fc_frame *fp;
2497 struct qedf_io_work *io_work;
2498 u32 bdq_idx;
2499 void *bdq_addr;
2500 struct scsi_bd *p_bd_info;
2501
2502 p_bd_info = &cqe->cqe_info.unsolic_info.bd_info;
2503 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2504 "address.hi=%x, address.lo=%x, opaque_data.hi=%x, opaque_data.lo=%x, bdq_prod_idx=%u, len=%u\n",
2505 le32_to_cpu(p_bd_info->address.hi),
2506 le32_to_cpu(p_bd_info->address.lo),
2507 le32_to_cpu(p_bd_info->opaque.fcoe_opaque.hi),
2508 le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo),
2509 qedf->bdq_prod_idx, pktlen);
2510
2511 bdq_idx = le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo);
2512 if (bdq_idx >= QEDF_BDQ_SIZE) {
2513 QEDF_ERR(&(qedf->dbg_ctx), "bdq_idx is out of range %d.\n",
2514 bdq_idx);
2515 goto increment_prod;
2516 }
2517
2518 bdq_addr = qedf->bdq[bdq_idx].buf_addr;
2519 if (!bdq_addr) {
2520 QEDF_ERR(&(qedf->dbg_ctx), "bdq_addr is NULL, dropping "
2521 "unsolicited packet.\n");
2522 goto increment_prod;
2523 }
2524
2525 if (qedf_dump_frames) {
2526 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2527 "BDQ frame is at addr=%p.\n", bdq_addr);
2528 print_hex_dump(KERN_WARNING, "bdq ", DUMP_PREFIX_OFFSET, 16, 1,
2529 (void *)bdq_addr, pktlen, false);
2530 }
2531
2532 /* Allocate frame */
2533 payload_len = pktlen - sizeof(struct fc_frame_header);
2534 fp = fc_frame_alloc(qedf->lport, payload_len);
2535 if (!fp) {
2536 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate fp.\n");
2537 goto increment_prod;
2538 }
2539
2540 /* Copy data from BDQ buffer into fc_frame struct */
2541 fh = (struct fc_frame_header *)fc_frame_header_get(fp);
2542 memcpy(fh, (void *)bdq_addr, pktlen);
2543
2544 QEDF_WARN(&qedf->dbg_ctx,
2545 "Processing Unsolicated frame, src=%06x dest=%06x r_ctl=0x%x type=0x%x cmd=%02x\n",
2546 ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2547 fh->fh_type, fc_frame_payload_op(fp));
2548
2549 /* Initialize the frame so libfc sees it as a valid frame */
2550 crc = fcoe_fc_crc(fp);
2551 fc_frame_init(fp);
2552 fr_dev(fp) = qedf->lport;
2553 fr_sof(fp) = FC_SOF_I3;
2554 fr_eof(fp) = FC_EOF_T;
2555 fr_crc(fp) = cpu_to_le32(~crc);
2556
2557 /*
2558 * We need to return the frame back up to libfc in a non-atomic
2559 * context
2560 */
2561 io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
2562 if (!io_work) {
2563 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
2564 "work for I/O completion.\n");
2565 fc_frame_free(fp);
2566 goto increment_prod;
2567 }
2568 memset(io_work, 0, sizeof(struct qedf_io_work));
2569
2570 INIT_WORK(&io_work->work, qedf_fp_io_handler);
2571
2572 /* Copy contents of CQE for deferred processing */
2573 memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2574
2575 io_work->qedf = qedf;
2576 io_work->fp = fp;
2577
2578 queue_work_on(smp_processor_id(), qedf_io_wq, &io_work->work);
2579 increment_prod:
2580 spin_lock_irqsave(&qedf->hba_lock, flags);
2581
2582 /* Increment producer to let f/w know we've handled the frame */
2583 qedf->bdq_prod_idx++;
2584
2585 /* Producer index wraps at uint16_t boundary */
2586 if (qedf->bdq_prod_idx == 0xffff)
2587 qedf->bdq_prod_idx = 0;
2588
2589 writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
2590 readw(qedf->bdq_primary_prod);
2591 writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
2592 readw(qedf->bdq_secondary_prod);
2593
2594 spin_unlock_irqrestore(&qedf->hba_lock, flags);
2595 }
2596