]> git.ipfire.org Git - people/ms/linux.git/blob - drivers/scsi/qla2xxx/qla_iocb.c
Merge branch 'fixes' into misc
[people/ms/linux.git] / drivers / scsi / qla2xxx / qla_iocb.c
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2014 QLogic Corporation
4 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
8 #include "qla_target.h"
9
10 #include <linux/blkdev.h>
11 #include <linux/delay.h>
12
13 #include <scsi/scsi_tcq.h>
14
15 /**
16 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
17 * @sp: SCSI command
18 *
19 * Returns the proper CF_* direction based on CDB.
20 */
21 static inline uint16_t
22 qla2x00_get_cmd_direction(srb_t *sp)
23 {
24 uint16_t cflags;
25 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
26 struct scsi_qla_host *vha = sp->vha;
27
28 cflags = 0;
29
30 /* Set transfer direction */
31 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
32 cflags = CF_WRITE;
33 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
34 vha->qla_stats.output_requests++;
35 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
36 cflags = CF_READ;
37 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
38 vha->qla_stats.input_requests++;
39 }
40 return (cflags);
41 }
42
43 /**
44 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
45 * Continuation Type 0 IOCBs to allocate.
46 *
47 * @dsds: number of data segment decriptors needed
48 *
49 * Returns the number of IOCB entries needed to store @dsds.
50 */
51 uint16_t
52 qla2x00_calc_iocbs_32(uint16_t dsds)
53 {
54 uint16_t iocbs;
55
56 iocbs = 1;
57 if (dsds > 3) {
58 iocbs += (dsds - 3) / 7;
59 if ((dsds - 3) % 7)
60 iocbs++;
61 }
62 return (iocbs);
63 }
64
65 /**
66 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
67 * Continuation Type 1 IOCBs to allocate.
68 *
69 * @dsds: number of data segment decriptors needed
70 *
71 * Returns the number of IOCB entries needed to store @dsds.
72 */
73 uint16_t
74 qla2x00_calc_iocbs_64(uint16_t dsds)
75 {
76 uint16_t iocbs;
77
78 iocbs = 1;
79 if (dsds > 2) {
80 iocbs += (dsds - 2) / 5;
81 if ((dsds - 2) % 5)
82 iocbs++;
83 }
84 return (iocbs);
85 }
86
87 /**
88 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
89 * @vha: HA context
90 *
91 * Returns a pointer to the Continuation Type 0 IOCB packet.
92 */
93 static inline cont_entry_t *
94 qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
95 {
96 cont_entry_t *cont_pkt;
97 struct req_que *req = vha->req;
98 /* Adjust ring index. */
99 req->ring_index++;
100 if (req->ring_index == req->length) {
101 req->ring_index = 0;
102 req->ring_ptr = req->ring;
103 } else {
104 req->ring_ptr++;
105 }
106
107 cont_pkt = (cont_entry_t *)req->ring_ptr;
108
109 /* Load packet defaults. */
110 *((uint32_t *)(&cont_pkt->entry_type)) = cpu_to_le32(CONTINUE_TYPE);
111
112 return (cont_pkt);
113 }
114
115 /**
116 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
117 * @vha: HA context
118 * @req: request queue
119 *
120 * Returns a pointer to the continuation type 1 IOCB packet.
121 */
122 static inline cont_a64_entry_t *
123 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
124 {
125 cont_a64_entry_t *cont_pkt;
126
127 /* Adjust ring index. */
128 req->ring_index++;
129 if (req->ring_index == req->length) {
130 req->ring_index = 0;
131 req->ring_ptr = req->ring;
132 } else {
133 req->ring_ptr++;
134 }
135
136 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
137
138 /* Load packet defaults. */
139 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ?
140 cpu_to_le32(CONTINUE_A64_TYPE_FX00) :
141 cpu_to_le32(CONTINUE_A64_TYPE);
142
143 return (cont_pkt);
144 }
145
146 inline int
147 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
148 {
149 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
150 uint8_t guard = scsi_host_get_guard(cmd->device->host);
151
152 /* We always use DIFF Bundling for best performance */
153 *fw_prot_opts = 0;
154
155 /* Translate SCSI opcode to a protection opcode */
156 switch (scsi_get_prot_op(cmd)) {
157 case SCSI_PROT_READ_STRIP:
158 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
159 break;
160 case SCSI_PROT_WRITE_INSERT:
161 *fw_prot_opts |= PO_MODE_DIF_INSERT;
162 break;
163 case SCSI_PROT_READ_INSERT:
164 *fw_prot_opts |= PO_MODE_DIF_INSERT;
165 break;
166 case SCSI_PROT_WRITE_STRIP:
167 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
168 break;
169 case SCSI_PROT_READ_PASS:
170 case SCSI_PROT_WRITE_PASS:
171 if (guard & SHOST_DIX_GUARD_IP)
172 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
173 else
174 *fw_prot_opts |= PO_MODE_DIF_PASS;
175 break;
176 default: /* Normal Request */
177 *fw_prot_opts |= PO_MODE_DIF_PASS;
178 break;
179 }
180
181 return scsi_prot_sg_count(cmd);
182 }
183
184 /*
185 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
186 * capable IOCB types.
187 *
188 * @sp: SRB command to process
189 * @cmd_pkt: Command type 2 IOCB
190 * @tot_dsds: Total number of segments to transfer
191 */
192 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
193 uint16_t tot_dsds)
194 {
195 uint16_t avail_dsds;
196 uint32_t *cur_dsd;
197 scsi_qla_host_t *vha;
198 struct scsi_cmnd *cmd;
199 struct scatterlist *sg;
200 int i;
201
202 cmd = GET_CMD_SP(sp);
203
204 /* Update entry type to indicate Command Type 2 IOCB */
205 *((uint32_t *)(&cmd_pkt->entry_type)) =
206 cpu_to_le32(COMMAND_TYPE);
207
208 /* No data transfer */
209 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
210 cmd_pkt->byte_count = cpu_to_le32(0);
211 return;
212 }
213
214 vha = sp->vha;
215 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
216
217 /* Three DSDs are available in the Command Type 2 IOCB */
218 avail_dsds = 3;
219 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
220
221 /* Load data segments */
222 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
223 cont_entry_t *cont_pkt;
224
225 /* Allocate additional continuation packets? */
226 if (avail_dsds == 0) {
227 /*
228 * Seven DSDs are available in the Continuation
229 * Type 0 IOCB.
230 */
231 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
232 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
233 avail_dsds = 7;
234 }
235
236 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
237 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
238 avail_dsds--;
239 }
240 }
241
242 /**
243 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
244 * capable IOCB types.
245 *
246 * @sp: SRB command to process
247 * @cmd_pkt: Command type 3 IOCB
248 * @tot_dsds: Total number of segments to transfer
249 */
250 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
251 uint16_t tot_dsds)
252 {
253 uint16_t avail_dsds;
254 uint32_t *cur_dsd;
255 scsi_qla_host_t *vha;
256 struct scsi_cmnd *cmd;
257 struct scatterlist *sg;
258 int i;
259
260 cmd = GET_CMD_SP(sp);
261
262 /* Update entry type to indicate Command Type 3 IOCB */
263 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_A64_TYPE);
264
265 /* No data transfer */
266 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
267 cmd_pkt->byte_count = cpu_to_le32(0);
268 return;
269 }
270
271 vha = sp->vha;
272 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
273
274 /* Two DSDs are available in the Command Type 3 IOCB */
275 avail_dsds = 2;
276 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
277
278 /* Load data segments */
279 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
280 dma_addr_t sle_dma;
281 cont_a64_entry_t *cont_pkt;
282
283 /* Allocate additional continuation packets? */
284 if (avail_dsds == 0) {
285 /*
286 * Five DSDs are available in the Continuation
287 * Type 1 IOCB.
288 */
289 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
290 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
291 avail_dsds = 5;
292 }
293
294 sle_dma = sg_dma_address(sg);
295 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
296 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
297 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
298 avail_dsds--;
299 }
300 }
301
302 /**
303 * qla2x00_start_scsi() - Send a SCSI command to the ISP
304 * @sp: command to send to the ISP
305 *
306 * Returns non-zero if a failure occurred, else zero.
307 */
308 int
309 qla2x00_start_scsi(srb_t *sp)
310 {
311 int nseg;
312 unsigned long flags;
313 scsi_qla_host_t *vha;
314 struct scsi_cmnd *cmd;
315 uint32_t *clr_ptr;
316 uint32_t index;
317 uint32_t handle;
318 cmd_entry_t *cmd_pkt;
319 uint16_t cnt;
320 uint16_t req_cnt;
321 uint16_t tot_dsds;
322 struct device_reg_2xxx __iomem *reg;
323 struct qla_hw_data *ha;
324 struct req_que *req;
325 struct rsp_que *rsp;
326
327 /* Setup device pointers. */
328 vha = sp->vha;
329 ha = vha->hw;
330 reg = &ha->iobase->isp;
331 cmd = GET_CMD_SP(sp);
332 req = ha->req_q_map[0];
333 rsp = ha->rsp_q_map[0];
334 /* So we know we haven't pci_map'ed anything yet */
335 tot_dsds = 0;
336
337 /* Send marker if required */
338 if (vha->marker_needed != 0) {
339 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
340 QLA_SUCCESS) {
341 return (QLA_FUNCTION_FAILED);
342 }
343 vha->marker_needed = 0;
344 }
345
346 /* Acquire ring specific lock */
347 spin_lock_irqsave(&ha->hardware_lock, flags);
348
349 /* Check for room in outstanding command list. */
350 handle = req->current_outstanding_cmd;
351 for (index = 1; index < req->num_outstanding_cmds; index++) {
352 handle++;
353 if (handle == req->num_outstanding_cmds)
354 handle = 1;
355 if (!req->outstanding_cmds[handle])
356 break;
357 }
358 if (index == req->num_outstanding_cmds)
359 goto queuing_error;
360
361 /* Map the sg table so we have an accurate count of sg entries needed */
362 if (scsi_sg_count(cmd)) {
363 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
364 scsi_sg_count(cmd), cmd->sc_data_direction);
365 if (unlikely(!nseg))
366 goto queuing_error;
367 } else
368 nseg = 0;
369
370 tot_dsds = nseg;
371
372 /* Calculate the number of request entries needed. */
373 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
374 if (req->cnt < (req_cnt + 2)) {
375 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
376 if (req->ring_index < cnt)
377 req->cnt = cnt - req->ring_index;
378 else
379 req->cnt = req->length -
380 (req->ring_index - cnt);
381 /* If still no head room then bail out */
382 if (req->cnt < (req_cnt + 2))
383 goto queuing_error;
384 }
385
386 /* Build command packet */
387 req->current_outstanding_cmd = handle;
388 req->outstanding_cmds[handle] = sp;
389 sp->handle = handle;
390 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
391 req->cnt -= req_cnt;
392
393 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
394 cmd_pkt->handle = handle;
395 /* Zero out remaining portion of packet. */
396 clr_ptr = (uint32_t *)cmd_pkt + 2;
397 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
398 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
399
400 /* Set target ID and LUN number*/
401 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
402 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
403 cmd_pkt->control_flags = cpu_to_le16(CF_SIMPLE_TAG);
404
405 /* Load SCSI command packet. */
406 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
407 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
408
409 /* Build IOCB segments */
410 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
411
412 /* Set total data segment count. */
413 cmd_pkt->entry_count = (uint8_t)req_cnt;
414 wmb();
415
416 /* Adjust ring index. */
417 req->ring_index++;
418 if (req->ring_index == req->length) {
419 req->ring_index = 0;
420 req->ring_ptr = req->ring;
421 } else
422 req->ring_ptr++;
423
424 sp->flags |= SRB_DMA_VALID;
425
426 /* Set chip new ring index. */
427 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
428 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
429
430 /* Manage unprocessed RIO/ZIO commands in response queue. */
431 if (vha->flags.process_response_queue &&
432 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
433 qla2x00_process_response_queue(rsp);
434
435 spin_unlock_irqrestore(&ha->hardware_lock, flags);
436 return (QLA_SUCCESS);
437
438 queuing_error:
439 if (tot_dsds)
440 scsi_dma_unmap(cmd);
441
442 spin_unlock_irqrestore(&ha->hardware_lock, flags);
443
444 return (QLA_FUNCTION_FAILED);
445 }
446
447 /**
448 * qla2x00_start_iocbs() - Execute the IOCB command
449 * @vha: HA context
450 * @req: request queue
451 */
452 void
453 qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
454 {
455 struct qla_hw_data *ha = vha->hw;
456 device_reg_t *reg = ISP_QUE_REG(ha, req->id);
457
458 if (IS_P3P_TYPE(ha)) {
459 qla82xx_start_iocbs(vha);
460 } else {
461 /* Adjust ring index. */
462 req->ring_index++;
463 if (req->ring_index == req->length) {
464 req->ring_index = 0;
465 req->ring_ptr = req->ring;
466 } else
467 req->ring_ptr++;
468
469 /* Set chip new ring index. */
470 if (ha->mqenable || IS_QLA27XX(ha)) {
471 WRT_REG_DWORD(req->req_q_in, req->ring_index);
472 } else if (IS_QLA83XX(ha)) {
473 WRT_REG_DWORD(req->req_q_in, req->ring_index);
474 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
475 } else if (IS_QLAFX00(ha)) {
476 WRT_REG_DWORD(&reg->ispfx00.req_q_in, req->ring_index);
477 RD_REG_DWORD_RELAXED(&reg->ispfx00.req_q_in);
478 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
479 } else if (IS_FWI2_CAPABLE(ha)) {
480 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
481 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
482 } else {
483 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
484 req->ring_index);
485 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
486 }
487 }
488 }
489
490 /**
491 * qla2x00_marker() - Send a marker IOCB to the firmware.
492 * @vha: HA context
493 * @req: request queue
494 * @rsp: response queue
495 * @loop_id: loop ID
496 * @lun: LUN
497 * @type: marker modifier
498 *
499 * Can be called from both normal and interrupt context.
500 *
501 * Returns non-zero if a failure occurred, else zero.
502 */
503 static int
504 __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
505 struct rsp_que *rsp, uint16_t loop_id,
506 uint64_t lun, uint8_t type)
507 {
508 mrk_entry_t *mrk;
509 struct mrk_entry_24xx *mrk24 = NULL;
510
511 struct qla_hw_data *ha = vha->hw;
512 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
513
514 req = ha->req_q_map[0];
515 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
516 if (mrk == NULL) {
517 ql_log(ql_log_warn, base_vha, 0x3026,
518 "Failed to allocate Marker IOCB.\n");
519
520 return (QLA_FUNCTION_FAILED);
521 }
522
523 mrk->entry_type = MARKER_TYPE;
524 mrk->modifier = type;
525 if (type != MK_SYNC_ALL) {
526 if (IS_FWI2_CAPABLE(ha)) {
527 mrk24 = (struct mrk_entry_24xx *) mrk;
528 mrk24->nport_handle = cpu_to_le16(loop_id);
529 int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun);
530 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
531 mrk24->vp_index = vha->vp_idx;
532 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
533 } else {
534 SET_TARGET_ID(ha, mrk->target, loop_id);
535 mrk->lun = cpu_to_le16((uint16_t)lun);
536 }
537 }
538 wmb();
539
540 qla2x00_start_iocbs(vha, req);
541
542 return (QLA_SUCCESS);
543 }
544
545 int
546 qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
547 struct rsp_que *rsp, uint16_t loop_id, uint64_t lun,
548 uint8_t type)
549 {
550 int ret;
551 unsigned long flags = 0;
552
553 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
554 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
555 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
556
557 return (ret);
558 }
559
560 /*
561 * qla2x00_issue_marker
562 *
563 * Issue marker
564 * Caller CAN have hardware lock held as specified by ha_locked parameter.
565 * Might release it, then reaquire.
566 */
567 int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
568 {
569 if (ha_locked) {
570 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
571 MK_SYNC_ALL) != QLA_SUCCESS)
572 return QLA_FUNCTION_FAILED;
573 } else {
574 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
575 MK_SYNC_ALL) != QLA_SUCCESS)
576 return QLA_FUNCTION_FAILED;
577 }
578 vha->marker_needed = 0;
579
580 return QLA_SUCCESS;
581 }
582
583 static inline int
584 qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
585 uint16_t tot_dsds)
586 {
587 uint32_t *cur_dsd = NULL;
588 scsi_qla_host_t *vha;
589 struct qla_hw_data *ha;
590 struct scsi_cmnd *cmd;
591 struct scatterlist *cur_seg;
592 uint32_t *dsd_seg;
593 void *next_dsd;
594 uint8_t avail_dsds;
595 uint8_t first_iocb = 1;
596 uint32_t dsd_list_len;
597 struct dsd_dma *dsd_ptr;
598 struct ct6_dsd *ctx;
599
600 cmd = GET_CMD_SP(sp);
601
602 /* Update entry type to indicate Command Type 3 IOCB */
603 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_TYPE_6);
604
605 /* No data transfer */
606 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
607 cmd_pkt->byte_count = cpu_to_le32(0);
608 return 0;
609 }
610
611 vha = sp->vha;
612 ha = vha->hw;
613
614 /* Set transfer direction */
615 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
616 cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
617 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
618 vha->qla_stats.output_requests++;
619 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
620 cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
621 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
622 vha->qla_stats.input_requests++;
623 }
624
625 cur_seg = scsi_sglist(cmd);
626 ctx = GET_CMD_CTX_SP(sp);
627
628 while (tot_dsds) {
629 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
630 QLA_DSDS_PER_IOCB : tot_dsds;
631 tot_dsds -= avail_dsds;
632 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
633
634 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
635 struct dsd_dma, list);
636 next_dsd = dsd_ptr->dsd_addr;
637 list_del(&dsd_ptr->list);
638 ha->gbl_dsd_avail--;
639 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
640 ctx->dsd_use_cnt++;
641 ha->gbl_dsd_inuse++;
642
643 if (first_iocb) {
644 first_iocb = 0;
645 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
646 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
647 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
648 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
649 } else {
650 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
651 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
652 *cur_dsd++ = cpu_to_le32(dsd_list_len);
653 }
654 cur_dsd = (uint32_t *)next_dsd;
655 while (avail_dsds) {
656 dma_addr_t sle_dma;
657
658 sle_dma = sg_dma_address(cur_seg);
659 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
660 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
661 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
662 cur_seg = sg_next(cur_seg);
663 avail_dsds--;
664 }
665 }
666
667 /* Null termination */
668 *cur_dsd++ = 0;
669 *cur_dsd++ = 0;
670 *cur_dsd++ = 0;
671 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
672 return 0;
673 }
674
675 /*
676 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
677 * for Command Type 6.
678 *
679 * @dsds: number of data segment decriptors needed
680 *
681 * Returns the number of dsd list needed to store @dsds.
682 */
683 static inline uint16_t
684 qla24xx_calc_dsd_lists(uint16_t dsds)
685 {
686 uint16_t dsd_lists = 0;
687
688 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
689 if (dsds % QLA_DSDS_PER_IOCB)
690 dsd_lists++;
691 return dsd_lists;
692 }
693
694
695 /**
696 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
697 * IOCB types.
698 *
699 * @sp: SRB command to process
700 * @cmd_pkt: Command type 3 IOCB
701 * @tot_dsds: Total number of segments to transfer
702 * @req: pointer to request queue
703 */
704 inline void
705 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
706 uint16_t tot_dsds, struct req_que *req)
707 {
708 uint16_t avail_dsds;
709 uint32_t *cur_dsd;
710 scsi_qla_host_t *vha;
711 struct scsi_cmnd *cmd;
712 struct scatterlist *sg;
713 int i;
714
715 cmd = GET_CMD_SP(sp);
716
717 /* Update entry type to indicate Command Type 3 IOCB */
718 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_TYPE_7);
719
720 /* No data transfer */
721 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
722 cmd_pkt->byte_count = cpu_to_le32(0);
723 return;
724 }
725
726 vha = sp->vha;
727
728 /* Set transfer direction */
729 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
730 cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_WRITE_DATA);
731 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
732 vha->qla_stats.output_requests++;
733 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
734 cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_READ_DATA);
735 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
736 vha->qla_stats.input_requests++;
737 }
738
739 /* One DSD is available in the Command Type 3 IOCB */
740 avail_dsds = 1;
741 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
742
743 /* Load data segments */
744
745 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
746 dma_addr_t sle_dma;
747 cont_a64_entry_t *cont_pkt;
748
749 /* Allocate additional continuation packets? */
750 if (avail_dsds == 0) {
751 /*
752 * Five DSDs are available in the Continuation
753 * Type 1 IOCB.
754 */
755 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, req);
756 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
757 avail_dsds = 5;
758 }
759
760 sle_dma = sg_dma_address(sg);
761 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
762 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
763 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
764 avail_dsds--;
765 }
766 }
767
768 struct fw_dif_context {
769 uint32_t ref_tag;
770 uint16_t app_tag;
771 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
772 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
773 };
774
775 /*
776 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
777 *
778 */
779 static inline void
780 qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
781 unsigned int protcnt)
782 {
783 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
784
785 switch (scsi_get_prot_type(cmd)) {
786 case SCSI_PROT_DIF_TYPE0:
787 /*
788 * No check for ql2xenablehba_err_chk, as it would be an
789 * I/O error if hba tag generation is not done.
790 */
791 pkt->ref_tag = cpu_to_le32((uint32_t)
792 (0xffffffff & scsi_get_lba(cmd)));
793
794 if (!qla2x00_hba_err_chk_enabled(sp))
795 break;
796
797 pkt->ref_tag_mask[0] = 0xff;
798 pkt->ref_tag_mask[1] = 0xff;
799 pkt->ref_tag_mask[2] = 0xff;
800 pkt->ref_tag_mask[3] = 0xff;
801 break;
802
803 /*
804 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
805 * match LBA in CDB + N
806 */
807 case SCSI_PROT_DIF_TYPE2:
808 pkt->app_tag = cpu_to_le16(0);
809 pkt->app_tag_mask[0] = 0x0;
810 pkt->app_tag_mask[1] = 0x0;
811
812 pkt->ref_tag = cpu_to_le32((uint32_t)
813 (0xffffffff & scsi_get_lba(cmd)));
814
815 if (!qla2x00_hba_err_chk_enabled(sp))
816 break;
817
818 /* enable ALL bytes of the ref tag */
819 pkt->ref_tag_mask[0] = 0xff;
820 pkt->ref_tag_mask[1] = 0xff;
821 pkt->ref_tag_mask[2] = 0xff;
822 pkt->ref_tag_mask[3] = 0xff;
823 break;
824
825 /* For Type 3 protection: 16 bit GUARD only */
826 case SCSI_PROT_DIF_TYPE3:
827 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
828 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
829 0x00;
830 break;
831
832 /*
833 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
834 * 16 bit app tag.
835 */
836 case SCSI_PROT_DIF_TYPE1:
837 pkt->ref_tag = cpu_to_le32((uint32_t)
838 (0xffffffff & scsi_get_lba(cmd)));
839 pkt->app_tag = cpu_to_le16(0);
840 pkt->app_tag_mask[0] = 0x0;
841 pkt->app_tag_mask[1] = 0x0;
842
843 if (!qla2x00_hba_err_chk_enabled(sp))
844 break;
845
846 /* enable ALL bytes of the ref tag */
847 pkt->ref_tag_mask[0] = 0xff;
848 pkt->ref_tag_mask[1] = 0xff;
849 pkt->ref_tag_mask[2] = 0xff;
850 pkt->ref_tag_mask[3] = 0xff;
851 break;
852 }
853 }
854
855 int
856 qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
857 uint32_t *partial)
858 {
859 struct scatterlist *sg;
860 uint32_t cumulative_partial, sg_len;
861 dma_addr_t sg_dma_addr;
862
863 if (sgx->num_bytes == sgx->tot_bytes)
864 return 0;
865
866 sg = sgx->cur_sg;
867 cumulative_partial = sgx->tot_partial;
868
869 sg_dma_addr = sg_dma_address(sg);
870 sg_len = sg_dma_len(sg);
871
872 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
873
874 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
875 sgx->dma_len = (blk_sz - cumulative_partial);
876 sgx->tot_partial = 0;
877 sgx->num_bytes += blk_sz;
878 *partial = 0;
879 } else {
880 sgx->dma_len = sg_len - sgx->bytes_consumed;
881 sgx->tot_partial += sgx->dma_len;
882 *partial = 1;
883 }
884
885 sgx->bytes_consumed += sgx->dma_len;
886
887 if (sg_len == sgx->bytes_consumed) {
888 sg = sg_next(sg);
889 sgx->num_sg++;
890 sgx->cur_sg = sg;
891 sgx->bytes_consumed = 0;
892 }
893
894 return 1;
895 }
896
897 int
898 qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
899 uint32_t *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
900 {
901 void *next_dsd;
902 uint8_t avail_dsds = 0;
903 uint32_t dsd_list_len;
904 struct dsd_dma *dsd_ptr;
905 struct scatterlist *sg_prot;
906 uint32_t *cur_dsd = dsd;
907 uint16_t used_dsds = tot_dsds;
908 uint32_t prot_int; /* protection interval */
909 uint32_t partial;
910 struct qla2_sgx sgx;
911 dma_addr_t sle_dma;
912 uint32_t sle_dma_len, tot_prot_dma_len = 0;
913 struct scsi_cmnd *cmd;
914
915 memset(&sgx, 0, sizeof(struct qla2_sgx));
916 if (sp) {
917 cmd = GET_CMD_SP(sp);
918 prot_int = cmd->device->sector_size;
919
920 sgx.tot_bytes = scsi_bufflen(cmd);
921 sgx.cur_sg = scsi_sglist(cmd);
922 sgx.sp = sp;
923
924 sg_prot = scsi_prot_sglist(cmd);
925 } else if (tc) {
926 prot_int = tc->blk_sz;
927 sgx.tot_bytes = tc->bufflen;
928 sgx.cur_sg = tc->sg;
929 sg_prot = tc->prot_sg;
930 } else {
931 BUG();
932 return 1;
933 }
934
935 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
936
937 sle_dma = sgx.dma_addr;
938 sle_dma_len = sgx.dma_len;
939 alloc_and_fill:
940 /* Allocate additional continuation packets? */
941 if (avail_dsds == 0) {
942 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
943 QLA_DSDS_PER_IOCB : used_dsds;
944 dsd_list_len = (avail_dsds + 1) * 12;
945 used_dsds -= avail_dsds;
946
947 /* allocate tracking DS */
948 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
949 if (!dsd_ptr)
950 return 1;
951
952 /* allocate new list */
953 dsd_ptr->dsd_addr = next_dsd =
954 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
955 &dsd_ptr->dsd_list_dma);
956
957 if (!next_dsd) {
958 /*
959 * Need to cleanup only this dsd_ptr, rest
960 * will be done by sp_free_dma()
961 */
962 kfree(dsd_ptr);
963 return 1;
964 }
965
966 if (sp) {
967 list_add_tail(&dsd_ptr->list,
968 &((struct crc_context *)
969 sp->u.scmd.ctx)->dsd_list);
970
971 sp->flags |= SRB_CRC_CTX_DSD_VALID;
972 } else {
973 list_add_tail(&dsd_ptr->list,
974 &(tc->ctx->dsd_list));
975 *tc->ctx_dsd_alloced = 1;
976 }
977
978
979 /* add new list to cmd iocb or last list */
980 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
981 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
982 *cur_dsd++ = dsd_list_len;
983 cur_dsd = (uint32_t *)next_dsd;
984 }
985 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
986 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
987 *cur_dsd++ = cpu_to_le32(sle_dma_len);
988 avail_dsds--;
989
990 if (partial == 0) {
991 /* Got a full protection interval */
992 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
993 sle_dma_len = 8;
994
995 tot_prot_dma_len += sle_dma_len;
996 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
997 tot_prot_dma_len = 0;
998 sg_prot = sg_next(sg_prot);
999 }
1000
1001 partial = 1; /* So as to not re-enter this block */
1002 goto alloc_and_fill;
1003 }
1004 }
1005 /* Null termination */
1006 *cur_dsd++ = 0;
1007 *cur_dsd++ = 0;
1008 *cur_dsd++ = 0;
1009 return 0;
1010 }
1011
1012 int
1013 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
1014 uint16_t tot_dsds, struct qla_tc_param *tc)
1015 {
1016 void *next_dsd;
1017 uint8_t avail_dsds = 0;
1018 uint32_t dsd_list_len;
1019 struct dsd_dma *dsd_ptr;
1020 struct scatterlist *sg, *sgl;
1021 uint32_t *cur_dsd = dsd;
1022 int i;
1023 uint16_t used_dsds = tot_dsds;
1024 struct scsi_cmnd *cmd;
1025
1026 if (sp) {
1027 cmd = GET_CMD_SP(sp);
1028 sgl = scsi_sglist(cmd);
1029 } else if (tc) {
1030 sgl = tc->sg;
1031 } else {
1032 BUG();
1033 return 1;
1034 }
1035
1036
1037 for_each_sg(sgl, sg, tot_dsds, i) {
1038 dma_addr_t sle_dma;
1039
1040 /* Allocate additional continuation packets? */
1041 if (avail_dsds == 0) {
1042 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1043 QLA_DSDS_PER_IOCB : used_dsds;
1044 dsd_list_len = (avail_dsds + 1) * 12;
1045 used_dsds -= avail_dsds;
1046
1047 /* allocate tracking DS */
1048 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1049 if (!dsd_ptr)
1050 return 1;
1051
1052 /* allocate new list */
1053 dsd_ptr->dsd_addr = next_dsd =
1054 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1055 &dsd_ptr->dsd_list_dma);
1056
1057 if (!next_dsd) {
1058 /*
1059 * Need to cleanup only this dsd_ptr, rest
1060 * will be done by sp_free_dma()
1061 */
1062 kfree(dsd_ptr);
1063 return 1;
1064 }
1065
1066 if (sp) {
1067 list_add_tail(&dsd_ptr->list,
1068 &((struct crc_context *)
1069 sp->u.scmd.ctx)->dsd_list);
1070
1071 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1072 } else {
1073 list_add_tail(&dsd_ptr->list,
1074 &(tc->ctx->dsd_list));
1075 *tc->ctx_dsd_alloced = 1;
1076 }
1077
1078 /* add new list to cmd iocb or last list */
1079 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1080 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1081 *cur_dsd++ = dsd_list_len;
1082 cur_dsd = (uint32_t *)next_dsd;
1083 }
1084 sle_dma = sg_dma_address(sg);
1085
1086 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1087 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1088 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1089 avail_dsds--;
1090
1091 }
1092 /* Null termination */
1093 *cur_dsd++ = 0;
1094 *cur_dsd++ = 0;
1095 *cur_dsd++ = 0;
1096 return 0;
1097 }
1098
1099 int
1100 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1101 uint32_t *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
1102 {
1103 void *next_dsd;
1104 uint8_t avail_dsds = 0;
1105 uint32_t dsd_list_len;
1106 struct dsd_dma *dsd_ptr;
1107 struct scatterlist *sg, *sgl;
1108 int i;
1109 struct scsi_cmnd *cmd;
1110 uint32_t *cur_dsd = dsd;
1111 uint16_t used_dsds = tot_dsds;
1112 struct scsi_qla_host *vha;
1113
1114 if (sp) {
1115 cmd = GET_CMD_SP(sp);
1116 sgl = scsi_prot_sglist(cmd);
1117 vha = sp->vha;
1118 } else if (tc) {
1119 vha = tc->vha;
1120 sgl = tc->prot_sg;
1121 } else {
1122 BUG();
1123 return 1;
1124 }
1125
1126 ql_dbg(ql_dbg_tgt, vha, 0xe021,
1127 "%s: enter\n", __func__);
1128
1129 for_each_sg(sgl, sg, tot_dsds, i) {
1130 dma_addr_t sle_dma;
1131
1132 /* Allocate additional continuation packets? */
1133 if (avail_dsds == 0) {
1134 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1135 QLA_DSDS_PER_IOCB : used_dsds;
1136 dsd_list_len = (avail_dsds + 1) * 12;
1137 used_dsds -= avail_dsds;
1138
1139 /* allocate tracking DS */
1140 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1141 if (!dsd_ptr)
1142 return 1;
1143
1144 /* allocate new list */
1145 dsd_ptr->dsd_addr = next_dsd =
1146 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1147 &dsd_ptr->dsd_list_dma);
1148
1149 if (!next_dsd) {
1150 /*
1151 * Need to cleanup only this dsd_ptr, rest
1152 * will be done by sp_free_dma()
1153 */
1154 kfree(dsd_ptr);
1155 return 1;
1156 }
1157
1158 if (sp) {
1159 list_add_tail(&dsd_ptr->list,
1160 &((struct crc_context *)
1161 sp->u.scmd.ctx)->dsd_list);
1162
1163 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1164 } else {
1165 list_add_tail(&dsd_ptr->list,
1166 &(tc->ctx->dsd_list));
1167 *tc->ctx_dsd_alloced = 1;
1168 }
1169
1170 /* add new list to cmd iocb or last list */
1171 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1172 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1173 *cur_dsd++ = dsd_list_len;
1174 cur_dsd = (uint32_t *)next_dsd;
1175 }
1176 sle_dma = sg_dma_address(sg);
1177
1178 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1179 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1180 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1181
1182 avail_dsds--;
1183 }
1184 /* Null termination */
1185 *cur_dsd++ = 0;
1186 *cur_dsd++ = 0;
1187 *cur_dsd++ = 0;
1188 return 0;
1189 }
1190
1191 /**
1192 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1193 * Type 6 IOCB types.
1194 *
1195 * @sp: SRB command to process
1196 * @cmd_pkt: Command type 3 IOCB
1197 * @tot_dsds: Total number of segments to transfer
1198 * @tot_prot_dsds:
1199 * @fw_prot_opts:
1200 */
1201 inline int
1202 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1203 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1204 {
1205 uint32_t *cur_dsd, *fcp_dl;
1206 scsi_qla_host_t *vha;
1207 struct scsi_cmnd *cmd;
1208 uint32_t total_bytes = 0;
1209 uint32_t data_bytes;
1210 uint32_t dif_bytes;
1211 uint8_t bundling = 1;
1212 uint16_t blk_size;
1213 struct crc_context *crc_ctx_pkt = NULL;
1214 struct qla_hw_data *ha;
1215 uint8_t additional_fcpcdb_len;
1216 uint16_t fcp_cmnd_len;
1217 struct fcp_cmnd *fcp_cmnd;
1218 dma_addr_t crc_ctx_dma;
1219
1220 cmd = GET_CMD_SP(sp);
1221
1222 /* Update entry type to indicate Command Type CRC_2 IOCB */
1223 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_TYPE_CRC_2);
1224
1225 vha = sp->vha;
1226 ha = vha->hw;
1227
1228 /* No data transfer */
1229 data_bytes = scsi_bufflen(cmd);
1230 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1231 cmd_pkt->byte_count = cpu_to_le32(0);
1232 return QLA_SUCCESS;
1233 }
1234
1235 cmd_pkt->vp_index = sp->vha->vp_idx;
1236
1237 /* Set transfer direction */
1238 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1239 cmd_pkt->control_flags =
1240 cpu_to_le16(CF_WRITE_DATA);
1241 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1242 cmd_pkt->control_flags =
1243 cpu_to_le16(CF_READ_DATA);
1244 }
1245
1246 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1247 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1248 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1249 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
1250 bundling = 0;
1251
1252 /* Allocate CRC context from global pool */
1253 crc_ctx_pkt = sp->u.scmd.ctx =
1254 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
1255
1256 if (!crc_ctx_pkt)
1257 goto crc_queuing_error;
1258
1259 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1260
1261 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1262
1263 /* Set handle */
1264 crc_ctx_pkt->handle = cmd_pkt->handle;
1265
1266 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1267
1268 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
1269 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1270
1271 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1272 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1273 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1274
1275 /* Determine SCSI command length -- align to 4 byte boundary */
1276 if (cmd->cmd_len > 16) {
1277 additional_fcpcdb_len = cmd->cmd_len - 16;
1278 if ((cmd->cmd_len % 4) != 0) {
1279 /* SCSI cmd > 16 bytes must be multiple of 4 */
1280 goto crc_queuing_error;
1281 }
1282 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1283 } else {
1284 additional_fcpcdb_len = 0;
1285 fcp_cmnd_len = 12 + 16 + 4;
1286 }
1287
1288 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1289
1290 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1291 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1292 fcp_cmnd->additional_cdb_len |= 1;
1293 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1294 fcp_cmnd->additional_cdb_len |= 2;
1295
1296 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
1297 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1298 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1299 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1300 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1301 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1302 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1303 fcp_cmnd->task_management = 0;
1304 fcp_cmnd->task_attribute = TSK_SIMPLE;
1305
1306 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1307
1308 /* Compute dif len and adjust data len to incude protection */
1309 dif_bytes = 0;
1310 blk_size = cmd->device->sector_size;
1311 dif_bytes = (data_bytes / blk_size) * 8;
1312
1313 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
1314 case SCSI_PROT_READ_INSERT:
1315 case SCSI_PROT_WRITE_STRIP:
1316 total_bytes = data_bytes;
1317 data_bytes += dif_bytes;
1318 break;
1319
1320 case SCSI_PROT_READ_STRIP:
1321 case SCSI_PROT_WRITE_INSERT:
1322 case SCSI_PROT_READ_PASS:
1323 case SCSI_PROT_WRITE_PASS:
1324 total_bytes = data_bytes + dif_bytes;
1325 break;
1326 default:
1327 BUG();
1328 }
1329
1330 if (!qla2x00_hba_err_chk_enabled(sp))
1331 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1332 /* HBA error checking enabled */
1333 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1334 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1335 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1336 SCSI_PROT_DIF_TYPE2))
1337 fw_prot_opts |= BIT_10;
1338 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1339 SCSI_PROT_DIF_TYPE3)
1340 fw_prot_opts |= BIT_11;
1341 }
1342
1343 if (!bundling) {
1344 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1345 } else {
1346 /*
1347 * Configure Bundling if we need to fetch interlaving
1348 * protection PCI accesses
1349 */
1350 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1351 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1352 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1353 tot_prot_dsds);
1354 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1355 }
1356
1357 /* Finish the common fields of CRC pkt */
1358 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1359 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1360 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1361 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
1362 /* Fibre channel byte count */
1363 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1364 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1365 additional_fcpcdb_len);
1366 *fcp_dl = htonl(total_bytes);
1367
1368 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1369 cmd_pkt->byte_count = cpu_to_le32(0);
1370 return QLA_SUCCESS;
1371 }
1372 /* Walks data segments */
1373
1374 cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1375
1376 if (!bundling && tot_prot_dsds) {
1377 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1378 cur_dsd, tot_dsds, NULL))
1379 goto crc_queuing_error;
1380 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1381 (tot_dsds - tot_prot_dsds), NULL))
1382 goto crc_queuing_error;
1383
1384 if (bundling && tot_prot_dsds) {
1385 /* Walks dif segments */
1386 cmd_pkt->control_flags |= cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1387 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1388 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1389 tot_prot_dsds, NULL))
1390 goto crc_queuing_error;
1391 }
1392 return QLA_SUCCESS;
1393
1394 crc_queuing_error:
1395 /* Cleanup will be performed by the caller */
1396
1397 return QLA_FUNCTION_FAILED;
1398 }
1399
1400 /**
1401 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1402 * @sp: command to send to the ISP
1403 *
1404 * Returns non-zero if a failure occurred, else zero.
1405 */
1406 int
1407 qla24xx_start_scsi(srb_t *sp)
1408 {
1409 int nseg;
1410 unsigned long flags;
1411 uint32_t *clr_ptr;
1412 uint32_t index;
1413 uint32_t handle;
1414 struct cmd_type_7 *cmd_pkt;
1415 uint16_t cnt;
1416 uint16_t req_cnt;
1417 uint16_t tot_dsds;
1418 struct req_que *req = NULL;
1419 struct rsp_que *rsp = NULL;
1420 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1421 struct scsi_qla_host *vha = sp->vha;
1422 struct qla_hw_data *ha = vha->hw;
1423
1424 /* Setup device pointers. */
1425 req = vha->req;
1426 rsp = req->rsp;
1427
1428 /* So we know we haven't pci_map'ed anything yet */
1429 tot_dsds = 0;
1430
1431 /* Send marker if required */
1432 if (vha->marker_needed != 0) {
1433 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1434 QLA_SUCCESS)
1435 return QLA_FUNCTION_FAILED;
1436 vha->marker_needed = 0;
1437 }
1438
1439 /* Acquire ring specific lock */
1440 spin_lock_irqsave(&ha->hardware_lock, flags);
1441
1442 /* Check for room in outstanding command list. */
1443 handle = req->current_outstanding_cmd;
1444 for (index = 1; index < req->num_outstanding_cmds; index++) {
1445 handle++;
1446 if (handle == req->num_outstanding_cmds)
1447 handle = 1;
1448 if (!req->outstanding_cmds[handle])
1449 break;
1450 }
1451 if (index == req->num_outstanding_cmds)
1452 goto queuing_error;
1453
1454 /* Map the sg table so we have an accurate count of sg entries needed */
1455 if (scsi_sg_count(cmd)) {
1456 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1457 scsi_sg_count(cmd), cmd->sc_data_direction);
1458 if (unlikely(!nseg))
1459 goto queuing_error;
1460 } else
1461 nseg = 0;
1462
1463 tot_dsds = nseg;
1464 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1465 if (req->cnt < (req_cnt + 2)) {
1466 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1467 RD_REG_DWORD_RELAXED(req->req_q_out);
1468 if (req->ring_index < cnt)
1469 req->cnt = cnt - req->ring_index;
1470 else
1471 req->cnt = req->length -
1472 (req->ring_index - cnt);
1473 if (req->cnt < (req_cnt + 2))
1474 goto queuing_error;
1475 }
1476
1477 /* Build command packet. */
1478 req->current_outstanding_cmd = handle;
1479 req->outstanding_cmds[handle] = sp;
1480 sp->handle = handle;
1481 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1482 req->cnt -= req_cnt;
1483
1484 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1485 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1486
1487 /* Zero out remaining portion of packet. */
1488 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
1489 clr_ptr = (uint32_t *)cmd_pkt + 2;
1490 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1491 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1492
1493 /* Set NPORT-ID and LUN number*/
1494 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1495 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1496 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1497 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1498 cmd_pkt->vp_index = sp->vha->vp_idx;
1499
1500 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1501 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1502
1503 cmd_pkt->task = TSK_SIMPLE;
1504
1505 /* Load SCSI command packet. */
1506 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1507 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1508
1509 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1510
1511 /* Build IOCB segments */
1512 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
1513
1514 /* Set total data segment count. */
1515 cmd_pkt->entry_count = (uint8_t)req_cnt;
1516 wmb();
1517 /* Adjust ring index. */
1518 req->ring_index++;
1519 if (req->ring_index == req->length) {
1520 req->ring_index = 0;
1521 req->ring_ptr = req->ring;
1522 } else
1523 req->ring_ptr++;
1524
1525 sp->flags |= SRB_DMA_VALID;
1526
1527 /* Set chip new ring index. */
1528 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1529 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1530
1531 /* Manage unprocessed RIO/ZIO commands in response queue. */
1532 if (vha->flags.process_response_queue &&
1533 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1534 qla24xx_process_response_queue(vha, rsp);
1535
1536 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1537 return QLA_SUCCESS;
1538
1539 queuing_error:
1540 if (tot_dsds)
1541 scsi_dma_unmap(cmd);
1542
1543 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1544
1545 return QLA_FUNCTION_FAILED;
1546 }
1547
1548 /**
1549 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1550 * @sp: command to send to the ISP
1551 *
1552 * Returns non-zero if a failure occurred, else zero.
1553 */
1554 int
1555 qla24xx_dif_start_scsi(srb_t *sp)
1556 {
1557 int nseg;
1558 unsigned long flags;
1559 uint32_t *clr_ptr;
1560 uint32_t index;
1561 uint32_t handle;
1562 uint16_t cnt;
1563 uint16_t req_cnt = 0;
1564 uint16_t tot_dsds;
1565 uint16_t tot_prot_dsds;
1566 uint16_t fw_prot_opts = 0;
1567 struct req_que *req = NULL;
1568 struct rsp_que *rsp = NULL;
1569 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1570 struct scsi_qla_host *vha = sp->vha;
1571 struct qla_hw_data *ha = vha->hw;
1572 struct cmd_type_crc_2 *cmd_pkt;
1573 uint32_t status = 0;
1574
1575 #define QDSS_GOT_Q_SPACE BIT_0
1576
1577 /* Only process protection or >16 cdb in this routine */
1578 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1579 if (cmd->cmd_len <= 16)
1580 return qla24xx_start_scsi(sp);
1581 }
1582
1583 /* Setup device pointers. */
1584 req = vha->req;
1585 rsp = req->rsp;
1586
1587 /* So we know we haven't pci_map'ed anything yet */
1588 tot_dsds = 0;
1589
1590 /* Send marker if required */
1591 if (vha->marker_needed != 0) {
1592 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1593 QLA_SUCCESS)
1594 return QLA_FUNCTION_FAILED;
1595 vha->marker_needed = 0;
1596 }
1597
1598 /* Acquire ring specific lock */
1599 spin_lock_irqsave(&ha->hardware_lock, flags);
1600
1601 /* Check for room in outstanding command list. */
1602 handle = req->current_outstanding_cmd;
1603 for (index = 1; index < req->num_outstanding_cmds; index++) {
1604 handle++;
1605 if (handle == req->num_outstanding_cmds)
1606 handle = 1;
1607 if (!req->outstanding_cmds[handle])
1608 break;
1609 }
1610
1611 if (index == req->num_outstanding_cmds)
1612 goto queuing_error;
1613
1614 /* Compute number of required data segments */
1615 /* Map the sg table so we have an accurate count of sg entries needed */
1616 if (scsi_sg_count(cmd)) {
1617 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1618 scsi_sg_count(cmd), cmd->sc_data_direction);
1619 if (unlikely(!nseg))
1620 goto queuing_error;
1621 else
1622 sp->flags |= SRB_DMA_VALID;
1623
1624 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1625 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1626 struct qla2_sgx sgx;
1627 uint32_t partial;
1628
1629 memset(&sgx, 0, sizeof(struct qla2_sgx));
1630 sgx.tot_bytes = scsi_bufflen(cmd);
1631 sgx.cur_sg = scsi_sglist(cmd);
1632 sgx.sp = sp;
1633
1634 nseg = 0;
1635 while (qla24xx_get_one_block_sg(
1636 cmd->device->sector_size, &sgx, &partial))
1637 nseg++;
1638 }
1639 } else
1640 nseg = 0;
1641
1642 /* number of required data segments */
1643 tot_dsds = nseg;
1644
1645 /* Compute number of required protection segments */
1646 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1647 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1648 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1649 if (unlikely(!nseg))
1650 goto queuing_error;
1651 else
1652 sp->flags |= SRB_CRC_PROT_DMA_VALID;
1653
1654 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1655 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1656 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1657 }
1658 } else {
1659 nseg = 0;
1660 }
1661
1662 req_cnt = 1;
1663 /* Total Data and protection sg segment(s) */
1664 tot_prot_dsds = nseg;
1665 tot_dsds += nseg;
1666 if (req->cnt < (req_cnt + 2)) {
1667 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1668 RD_REG_DWORD_RELAXED(req->req_q_out);
1669 if (req->ring_index < cnt)
1670 req->cnt = cnt - req->ring_index;
1671 else
1672 req->cnt = req->length -
1673 (req->ring_index - cnt);
1674 if (req->cnt < (req_cnt + 2))
1675 goto queuing_error;
1676 }
1677
1678 status |= QDSS_GOT_Q_SPACE;
1679
1680 /* Build header part of command packet (excluding the OPCODE). */
1681 req->current_outstanding_cmd = handle;
1682 req->outstanding_cmds[handle] = sp;
1683 sp->handle = handle;
1684 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1685 req->cnt -= req_cnt;
1686
1687 /* Fill-in common area */
1688 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1689 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1690
1691 clr_ptr = (uint32_t *)cmd_pkt + 2;
1692 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1693
1694 /* Set NPORT-ID and LUN number*/
1695 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1696 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1697 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1698 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1699
1700 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1701 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1702
1703 /* Total Data and protection segment(s) */
1704 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1705
1706 /* Build IOCB segments and adjust for data protection segments */
1707 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1708 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1709 QLA_SUCCESS)
1710 goto queuing_error;
1711
1712 cmd_pkt->entry_count = (uint8_t)req_cnt;
1713 /* Specify response queue number where completion should happen */
1714 cmd_pkt->entry_status = (uint8_t) rsp->id;
1715 cmd_pkt->timeout = cpu_to_le16(0);
1716 wmb();
1717
1718 /* Adjust ring index. */
1719 req->ring_index++;
1720 if (req->ring_index == req->length) {
1721 req->ring_index = 0;
1722 req->ring_ptr = req->ring;
1723 } else
1724 req->ring_ptr++;
1725
1726 /* Set chip new ring index. */
1727 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1728 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1729
1730 /* Manage unprocessed RIO/ZIO commands in response queue. */
1731 if (vha->flags.process_response_queue &&
1732 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1733 qla24xx_process_response_queue(vha, rsp);
1734
1735 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1736
1737 return QLA_SUCCESS;
1738
1739 queuing_error:
1740 if (status & QDSS_GOT_Q_SPACE) {
1741 req->outstanding_cmds[handle] = NULL;
1742 req->cnt += req_cnt;
1743 }
1744 /* Cleanup will be performed by the caller (queuecommand) */
1745
1746 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1747 return QLA_FUNCTION_FAILED;
1748 }
1749
1750 /**
1751 * qla2xxx_start_scsi_mq() - Send a SCSI command to the ISP
1752 * @sp: command to send to the ISP
1753 *
1754 * Returns non-zero if a failure occurred, else zero.
1755 */
1756 static int
1757 qla2xxx_start_scsi_mq(srb_t *sp)
1758 {
1759 int nseg;
1760 unsigned long flags;
1761 uint32_t *clr_ptr;
1762 uint32_t index;
1763 uint32_t handle;
1764 struct cmd_type_7 *cmd_pkt;
1765 uint16_t cnt;
1766 uint16_t req_cnt;
1767 uint16_t tot_dsds;
1768 struct req_que *req = NULL;
1769 struct rsp_que *rsp = NULL;
1770 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1771 struct scsi_qla_host *vha = sp->fcport->vha;
1772 struct qla_hw_data *ha = vha->hw;
1773 struct qla_qpair *qpair = sp->qpair;
1774
1775 /* Acquire qpair specific lock */
1776 spin_lock_irqsave(&qpair->qp_lock, flags);
1777
1778 /* Setup qpair pointers */
1779 rsp = qpair->rsp;
1780 req = qpair->req;
1781
1782 /* So we know we haven't pci_map'ed anything yet */
1783 tot_dsds = 0;
1784
1785 /* Send marker if required */
1786 if (vha->marker_needed != 0) {
1787 if (__qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1788 QLA_SUCCESS) {
1789 spin_unlock_irqrestore(&qpair->qp_lock, flags);
1790 return QLA_FUNCTION_FAILED;
1791 }
1792 vha->marker_needed = 0;
1793 }
1794
1795 /* Check for room in outstanding command list. */
1796 handle = req->current_outstanding_cmd;
1797 for (index = 1; index < req->num_outstanding_cmds; index++) {
1798 handle++;
1799 if (handle == req->num_outstanding_cmds)
1800 handle = 1;
1801 if (!req->outstanding_cmds[handle])
1802 break;
1803 }
1804 if (index == req->num_outstanding_cmds)
1805 goto queuing_error;
1806
1807 /* Map the sg table so we have an accurate count of sg entries needed */
1808 if (scsi_sg_count(cmd)) {
1809 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1810 scsi_sg_count(cmd), cmd->sc_data_direction);
1811 if (unlikely(!nseg))
1812 goto queuing_error;
1813 } else
1814 nseg = 0;
1815
1816 tot_dsds = nseg;
1817 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1818 if (req->cnt < (req_cnt + 2)) {
1819 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1820 RD_REG_DWORD_RELAXED(req->req_q_out);
1821 if (req->ring_index < cnt)
1822 req->cnt = cnt - req->ring_index;
1823 else
1824 req->cnt = req->length -
1825 (req->ring_index - cnt);
1826 if (req->cnt < (req_cnt + 2))
1827 goto queuing_error;
1828 }
1829
1830 /* Build command packet. */
1831 req->current_outstanding_cmd = handle;
1832 req->outstanding_cmds[handle] = sp;
1833 sp->handle = handle;
1834 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1835 req->cnt -= req_cnt;
1836
1837 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1838 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1839
1840 /* Zero out remaining portion of packet. */
1841 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
1842 clr_ptr = (uint32_t *)cmd_pkt + 2;
1843 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1844 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1845
1846 /* Set NPORT-ID and LUN number*/
1847 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1848 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1849 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1850 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1851 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
1852
1853 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1854 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1855
1856 cmd_pkt->task = TSK_SIMPLE;
1857
1858 /* Load SCSI command packet. */
1859 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1860 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1861
1862 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1863
1864 /* Build IOCB segments */
1865 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
1866
1867 /* Set total data segment count. */
1868 cmd_pkt->entry_count = (uint8_t)req_cnt;
1869 wmb();
1870 /* Adjust ring index. */
1871 req->ring_index++;
1872 if (req->ring_index == req->length) {
1873 req->ring_index = 0;
1874 req->ring_ptr = req->ring;
1875 } else
1876 req->ring_ptr++;
1877
1878 sp->flags |= SRB_DMA_VALID;
1879
1880 /* Set chip new ring index. */
1881 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1882
1883 /* Manage unprocessed RIO/ZIO commands in response queue. */
1884 if (vha->flags.process_response_queue &&
1885 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1886 qla24xx_process_response_queue(vha, rsp);
1887
1888 spin_unlock_irqrestore(&qpair->qp_lock, flags);
1889 return QLA_SUCCESS;
1890
1891 queuing_error:
1892 if (tot_dsds)
1893 scsi_dma_unmap(cmd);
1894
1895 spin_unlock_irqrestore(&qpair->qp_lock, flags);
1896
1897 return QLA_FUNCTION_FAILED;
1898 }
1899
1900
1901 /**
1902 * qla2xxx_dif_start_scsi_mq() - Send a SCSI command to the ISP
1903 * @sp: command to send to the ISP
1904 *
1905 * Returns non-zero if a failure occurred, else zero.
1906 */
1907 int
1908 qla2xxx_dif_start_scsi_mq(srb_t *sp)
1909 {
1910 int nseg;
1911 unsigned long flags;
1912 uint32_t *clr_ptr;
1913 uint32_t index;
1914 uint32_t handle;
1915 uint16_t cnt;
1916 uint16_t req_cnt = 0;
1917 uint16_t tot_dsds;
1918 uint16_t tot_prot_dsds;
1919 uint16_t fw_prot_opts = 0;
1920 struct req_que *req = NULL;
1921 struct rsp_que *rsp = NULL;
1922 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1923 struct scsi_qla_host *vha = sp->fcport->vha;
1924 struct qla_hw_data *ha = vha->hw;
1925 struct cmd_type_crc_2 *cmd_pkt;
1926 uint32_t status = 0;
1927 struct qla_qpair *qpair = sp->qpair;
1928
1929 #define QDSS_GOT_Q_SPACE BIT_0
1930
1931 /* Check for host side state */
1932 if (!qpair->online) {
1933 cmd->result = DID_NO_CONNECT << 16;
1934 return QLA_INTERFACE_ERROR;
1935 }
1936
1937 if (!qpair->difdix_supported &&
1938 scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
1939 cmd->result = DID_NO_CONNECT << 16;
1940 return QLA_INTERFACE_ERROR;
1941 }
1942
1943 /* Only process protection or >16 cdb in this routine */
1944 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1945 if (cmd->cmd_len <= 16)
1946 return qla2xxx_start_scsi_mq(sp);
1947 }
1948
1949 spin_lock_irqsave(&qpair->qp_lock, flags);
1950
1951 /* Setup qpair pointers */
1952 rsp = qpair->rsp;
1953 req = qpair->req;
1954
1955 /* So we know we haven't pci_map'ed anything yet */
1956 tot_dsds = 0;
1957
1958 /* Send marker if required */
1959 if (vha->marker_needed != 0) {
1960 if (__qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1961 QLA_SUCCESS) {
1962 spin_unlock_irqrestore(&qpair->qp_lock, flags);
1963 return QLA_FUNCTION_FAILED;
1964 }
1965 vha->marker_needed = 0;
1966 }
1967
1968 /* Check for room in outstanding command list. */
1969 handle = req->current_outstanding_cmd;
1970 for (index = 1; index < req->num_outstanding_cmds; index++) {
1971 handle++;
1972 if (handle == req->num_outstanding_cmds)
1973 handle = 1;
1974 if (!req->outstanding_cmds[handle])
1975 break;
1976 }
1977
1978 if (index == req->num_outstanding_cmds)
1979 goto queuing_error;
1980
1981 /* Compute number of required data segments */
1982 /* Map the sg table so we have an accurate count of sg entries needed */
1983 if (scsi_sg_count(cmd)) {
1984 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1985 scsi_sg_count(cmd), cmd->sc_data_direction);
1986 if (unlikely(!nseg))
1987 goto queuing_error;
1988 else
1989 sp->flags |= SRB_DMA_VALID;
1990
1991 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1992 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1993 struct qla2_sgx sgx;
1994 uint32_t partial;
1995
1996 memset(&sgx, 0, sizeof(struct qla2_sgx));
1997 sgx.tot_bytes = scsi_bufflen(cmd);
1998 sgx.cur_sg = scsi_sglist(cmd);
1999 sgx.sp = sp;
2000
2001 nseg = 0;
2002 while (qla24xx_get_one_block_sg(
2003 cmd->device->sector_size, &sgx, &partial))
2004 nseg++;
2005 }
2006 } else
2007 nseg = 0;
2008
2009 /* number of required data segments */
2010 tot_dsds = nseg;
2011
2012 /* Compute number of required protection segments */
2013 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
2014 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
2015 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
2016 if (unlikely(!nseg))
2017 goto queuing_error;
2018 else
2019 sp->flags |= SRB_CRC_PROT_DMA_VALID;
2020
2021 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
2022 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
2023 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
2024 }
2025 } else {
2026 nseg = 0;
2027 }
2028
2029 req_cnt = 1;
2030 /* Total Data and protection sg segment(s) */
2031 tot_prot_dsds = nseg;
2032 tot_dsds += nseg;
2033 if (req->cnt < (req_cnt + 2)) {
2034 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
2035 RD_REG_DWORD_RELAXED(req->req_q_out);
2036 if (req->ring_index < cnt)
2037 req->cnt = cnt - req->ring_index;
2038 else
2039 req->cnt = req->length -
2040 (req->ring_index - cnt);
2041 if (req->cnt < (req_cnt + 2))
2042 goto queuing_error;
2043 }
2044
2045 status |= QDSS_GOT_Q_SPACE;
2046
2047 /* Build header part of command packet (excluding the OPCODE). */
2048 req->current_outstanding_cmd = handle;
2049 req->outstanding_cmds[handle] = sp;
2050 sp->handle = handle;
2051 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
2052 req->cnt -= req_cnt;
2053
2054 /* Fill-in common area */
2055 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
2056 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2057
2058 clr_ptr = (uint32_t *)cmd_pkt + 2;
2059 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2060
2061 /* Set NPORT-ID and LUN number*/
2062 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2063 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2064 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2065 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2066
2067 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2068 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2069
2070 /* Total Data and protection segment(s) */
2071 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2072
2073 /* Build IOCB segments and adjust for data protection segments */
2074 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
2075 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
2076 QLA_SUCCESS)
2077 goto queuing_error;
2078
2079 cmd_pkt->entry_count = (uint8_t)req_cnt;
2080 cmd_pkt->timeout = cpu_to_le16(0);
2081 wmb();
2082
2083 /* Adjust ring index. */
2084 req->ring_index++;
2085 if (req->ring_index == req->length) {
2086 req->ring_index = 0;
2087 req->ring_ptr = req->ring;
2088 } else
2089 req->ring_ptr++;
2090
2091 /* Set chip new ring index. */
2092 WRT_REG_DWORD(req->req_q_in, req->ring_index);
2093
2094 /* Manage unprocessed RIO/ZIO commands in response queue. */
2095 if (vha->flags.process_response_queue &&
2096 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2097 qla24xx_process_response_queue(vha, rsp);
2098
2099 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2100
2101 return QLA_SUCCESS;
2102
2103 queuing_error:
2104 if (status & QDSS_GOT_Q_SPACE) {
2105 req->outstanding_cmds[handle] = NULL;
2106 req->cnt += req_cnt;
2107 }
2108 /* Cleanup will be performed by the caller (queuecommand) */
2109
2110 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2111 return QLA_FUNCTION_FAILED;
2112 }
2113
2114 /* Generic Control-SRB manipulation functions. */
2115
2116 /* hardware_lock assumed to be held. */
2117
2118 void *
2119 __qla2x00_alloc_iocbs(struct qla_qpair *qpair, srb_t *sp)
2120 {
2121 scsi_qla_host_t *vha = qpair->vha;
2122 struct qla_hw_data *ha = vha->hw;
2123 struct req_que *req = qpair->req;
2124 device_reg_t *reg = ISP_QUE_REG(ha, req->id);
2125 uint32_t index, handle;
2126 request_t *pkt;
2127 uint16_t cnt, req_cnt;
2128
2129 pkt = NULL;
2130 req_cnt = 1;
2131 handle = 0;
2132
2133 if (!sp)
2134 goto skip_cmd_array;
2135
2136 /* Check for room in outstanding command list. */
2137 handle = req->current_outstanding_cmd;
2138 for (index = 1; index < req->num_outstanding_cmds; index++) {
2139 handle++;
2140 if (handle == req->num_outstanding_cmds)
2141 handle = 1;
2142 if (!req->outstanding_cmds[handle])
2143 break;
2144 }
2145 if (index == req->num_outstanding_cmds) {
2146 ql_log(ql_log_warn, vha, 0x700b,
2147 "No room on outstanding cmd array.\n");
2148 goto queuing_error;
2149 }
2150
2151 /* Prep command array. */
2152 req->current_outstanding_cmd = handle;
2153 req->outstanding_cmds[handle] = sp;
2154 sp->handle = handle;
2155
2156 /* Adjust entry-counts as needed. */
2157 if (sp->type != SRB_SCSI_CMD)
2158 req_cnt = sp->iocbs;
2159
2160 skip_cmd_array:
2161 /* Check for room on request queue. */
2162 if (req->cnt < req_cnt + 2) {
2163 if (qpair->use_shadow_reg)
2164 cnt = *req->out_ptr;
2165 else if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha))
2166 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
2167 else if (IS_P3P_TYPE(ha))
2168 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
2169 else if (IS_FWI2_CAPABLE(ha))
2170 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
2171 else if (IS_QLAFX00(ha))
2172 cnt = RD_REG_DWORD(&reg->ispfx00.req_q_out);
2173 else
2174 cnt = qla2x00_debounce_register(
2175 ISP_REQ_Q_OUT(ha, &reg->isp));
2176
2177 if (req->ring_index < cnt)
2178 req->cnt = cnt - req->ring_index;
2179 else
2180 req->cnt = req->length -
2181 (req->ring_index - cnt);
2182 }
2183 if (req->cnt < req_cnt + 2)
2184 goto queuing_error;
2185
2186 /* Prep packet */
2187 req->cnt -= req_cnt;
2188 pkt = req->ring_ptr;
2189 memset(pkt, 0, REQUEST_ENTRY_SIZE);
2190 if (IS_QLAFX00(ha)) {
2191 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt);
2192 WRT_REG_WORD((void __iomem *)&pkt->handle, handle);
2193 } else {
2194 pkt->entry_count = req_cnt;
2195 pkt->handle = handle;
2196 }
2197
2198 queuing_error:
2199 qpair->tgt_counters.num_alloc_iocb_failed++;
2200 return pkt;
2201 }
2202
2203 void *
2204 qla2x00_alloc_iocbs_ready(struct qla_qpair *qpair, srb_t *sp)
2205 {
2206 scsi_qla_host_t *vha = qpair->vha;
2207
2208 if (qla2x00_reset_active(vha))
2209 return NULL;
2210
2211 return __qla2x00_alloc_iocbs(qpair, sp);
2212 }
2213
2214 void *
2215 qla2x00_alloc_iocbs(struct scsi_qla_host *vha, srb_t *sp)
2216 {
2217 return __qla2x00_alloc_iocbs(vha->hw->base_qpair, sp);
2218 }
2219
2220 static void
2221 qla24xx_prli_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2222 {
2223 struct srb_iocb *lio = &sp->u.iocb_cmd;
2224
2225 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2226 logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
2227 if (lio->u.logio.flags & SRB_LOGIN_NVME_PRLI)
2228 logio->control_flags |= LCF_NVME_PRLI;
2229
2230 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2231 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2232 logio->port_id[1] = sp->fcport->d_id.b.area;
2233 logio->port_id[2] = sp->fcport->d_id.b.domain;
2234 logio->vp_index = sp->vha->vp_idx;
2235 }
2236
2237 static void
2238 qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2239 {
2240 struct srb_iocb *lio = &sp->u.iocb_cmd;
2241
2242 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2243 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
2244
2245 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
2246 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
2247 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
2248 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
2249 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2250 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2251 logio->port_id[1] = sp->fcport->d_id.b.area;
2252 logio->port_id[2] = sp->fcport->d_id.b.domain;
2253 logio->vp_index = sp->vha->vp_idx;
2254 }
2255
2256 static void
2257 qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
2258 {
2259 struct qla_hw_data *ha = sp->vha->hw;
2260 struct srb_iocb *lio = &sp->u.iocb_cmd;
2261 uint16_t opts;
2262
2263 mbx->entry_type = MBX_IOCB_TYPE;
2264 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2265 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
2266 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
2267 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
2268 if (HAS_EXTENDED_IDS(ha)) {
2269 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
2270 mbx->mb10 = cpu_to_le16(opts);
2271 } else {
2272 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
2273 }
2274 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
2275 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
2276 sp->fcport->d_id.b.al_pa);
2277 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2278 }
2279
2280 static void
2281 qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2282 {
2283 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2284 logio->control_flags =
2285 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
2286 if (!sp->fcport->se_sess ||
2287 !sp->fcport->keep_nport_handle)
2288 logio->control_flags |= cpu_to_le16(LCF_FREE_NPORT);
2289 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2290 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2291 logio->port_id[1] = sp->fcport->d_id.b.area;
2292 logio->port_id[2] = sp->fcport->d_id.b.domain;
2293 logio->vp_index = sp->vha->vp_idx;
2294 }
2295
2296 static void
2297 qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
2298 {
2299 struct qla_hw_data *ha = sp->vha->hw;
2300
2301 mbx->entry_type = MBX_IOCB_TYPE;
2302 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2303 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
2304 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
2305 cpu_to_le16(sp->fcport->loop_id):
2306 cpu_to_le16(sp->fcport->loop_id << 8);
2307 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
2308 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
2309 sp->fcport->d_id.b.al_pa);
2310 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2311 /* Implicit: mbx->mbx10 = 0. */
2312 }
2313
2314 static void
2315 qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2316 {
2317 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2318 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
2319 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2320 logio->vp_index = sp->vha->vp_idx;
2321 }
2322
2323 static void
2324 qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
2325 {
2326 struct qla_hw_data *ha = sp->vha->hw;
2327
2328 mbx->entry_type = MBX_IOCB_TYPE;
2329 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2330 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
2331 if (HAS_EXTENDED_IDS(ha)) {
2332 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
2333 mbx->mb10 = cpu_to_le16(BIT_0);
2334 } else {
2335 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
2336 }
2337 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
2338 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
2339 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
2340 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
2341 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2342 }
2343
2344 static void
2345 qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
2346 {
2347 uint32_t flags;
2348 uint64_t lun;
2349 struct fc_port *fcport = sp->fcport;
2350 scsi_qla_host_t *vha = fcport->vha;
2351 struct qla_hw_data *ha = vha->hw;
2352 struct srb_iocb *iocb = &sp->u.iocb_cmd;
2353 struct req_que *req = vha->req;
2354
2355 flags = iocb->u.tmf.flags;
2356 lun = iocb->u.tmf.lun;
2357
2358 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2359 tsk->entry_count = 1;
2360 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
2361 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2362 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2363 tsk->control_flags = cpu_to_le32(flags);
2364 tsk->port_id[0] = fcport->d_id.b.al_pa;
2365 tsk->port_id[1] = fcport->d_id.b.area;
2366 tsk->port_id[2] = fcport->d_id.b.domain;
2367 tsk->vp_index = fcport->vha->vp_idx;
2368
2369 if (flags == TCF_LUN_RESET) {
2370 int_to_scsilun(lun, &tsk->lun);
2371 host_to_fcp_swap((uint8_t *)&tsk->lun,
2372 sizeof(tsk->lun));
2373 }
2374 }
2375
2376 static void
2377 qla2x00_els_dcmd_sp_free(void *data)
2378 {
2379 srb_t *sp = data;
2380 struct srb_iocb *elsio = &sp->u.iocb_cmd;
2381
2382 kfree(sp->fcport);
2383
2384 if (elsio->u.els_logo.els_logo_pyld)
2385 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE,
2386 elsio->u.els_logo.els_logo_pyld,
2387 elsio->u.els_logo.els_logo_pyld_dma);
2388
2389 del_timer(&elsio->timer);
2390 qla2x00_rel_sp(sp);
2391 }
2392
2393 static void
2394 qla2x00_els_dcmd_iocb_timeout(void *data)
2395 {
2396 srb_t *sp = data;
2397 fc_port_t *fcport = sp->fcport;
2398 struct scsi_qla_host *vha = sp->vha;
2399 struct srb_iocb *lio = &sp->u.iocb_cmd;
2400
2401 ql_dbg(ql_dbg_io, vha, 0x3069,
2402 "%s Timeout, hdl=%x, portid=%02x%02x%02x\n",
2403 sp->name, sp->handle, fcport->d_id.b.domain, fcport->d_id.b.area,
2404 fcport->d_id.b.al_pa);
2405
2406 complete(&lio->u.els_logo.comp);
2407 }
2408
2409 static void
2410 qla2x00_els_dcmd_sp_done(void *ptr, int res)
2411 {
2412 srb_t *sp = ptr;
2413 fc_port_t *fcport = sp->fcport;
2414 struct srb_iocb *lio = &sp->u.iocb_cmd;
2415 struct scsi_qla_host *vha = sp->vha;
2416
2417 ql_dbg(ql_dbg_io, vha, 0x3072,
2418 "%s hdl=%x, portid=%02x%02x%02x done\n",
2419 sp->name, sp->handle, fcport->d_id.b.domain,
2420 fcport->d_id.b.area, fcport->d_id.b.al_pa);
2421
2422 complete(&lio->u.els_logo.comp);
2423 }
2424
2425 int
2426 qla24xx_els_dcmd_iocb(scsi_qla_host_t *vha, int els_opcode,
2427 port_id_t remote_did)
2428 {
2429 srb_t *sp;
2430 fc_port_t *fcport = NULL;
2431 struct srb_iocb *elsio = NULL;
2432 struct qla_hw_data *ha = vha->hw;
2433 struct els_logo_payload logo_pyld;
2434 int rval = QLA_SUCCESS;
2435
2436 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2437 if (!fcport) {
2438 ql_log(ql_log_info, vha, 0x70e5, "fcport allocation failed\n");
2439 return -ENOMEM;
2440 }
2441
2442 /* Alloc SRB structure */
2443 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2444 if (!sp) {
2445 kfree(fcport);
2446 ql_log(ql_log_info, vha, 0x70e6,
2447 "SRB allocation failed\n");
2448 return -ENOMEM;
2449 }
2450
2451 elsio = &sp->u.iocb_cmd;
2452 fcport->loop_id = 0xFFFF;
2453 fcport->d_id.b.domain = remote_did.b.domain;
2454 fcport->d_id.b.area = remote_did.b.area;
2455 fcport->d_id.b.al_pa = remote_did.b.al_pa;
2456
2457 ql_dbg(ql_dbg_io, vha, 0x3073, "portid=%02x%02x%02x done\n",
2458 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
2459
2460 sp->type = SRB_ELS_DCMD;
2461 sp->name = "ELS_DCMD";
2462 sp->fcport = fcport;
2463 qla2x00_init_timer(sp, ELS_DCMD_TIMEOUT);
2464 elsio->timeout = qla2x00_els_dcmd_iocb_timeout;
2465 sp->done = qla2x00_els_dcmd_sp_done;
2466 sp->free = qla2x00_els_dcmd_sp_free;
2467
2468 elsio->u.els_logo.els_logo_pyld = dma_alloc_coherent(&ha->pdev->dev,
2469 DMA_POOL_SIZE, &elsio->u.els_logo.els_logo_pyld_dma,
2470 GFP_KERNEL);
2471
2472 if (!elsio->u.els_logo.els_logo_pyld) {
2473 sp->free(sp);
2474 return QLA_FUNCTION_FAILED;
2475 }
2476
2477 memset(&logo_pyld, 0, sizeof(struct els_logo_payload));
2478
2479 elsio->u.els_logo.els_cmd = els_opcode;
2480 logo_pyld.opcode = els_opcode;
2481 logo_pyld.s_id[0] = vha->d_id.b.al_pa;
2482 logo_pyld.s_id[1] = vha->d_id.b.area;
2483 logo_pyld.s_id[2] = vha->d_id.b.domain;
2484 host_to_fcp_swap(logo_pyld.s_id, sizeof(uint32_t));
2485 memcpy(&logo_pyld.wwpn, vha->port_name, WWN_SIZE);
2486
2487 memcpy(elsio->u.els_logo.els_logo_pyld, &logo_pyld,
2488 sizeof(struct els_logo_payload));
2489
2490 rval = qla2x00_start_sp(sp);
2491 if (rval != QLA_SUCCESS) {
2492 sp->free(sp);
2493 return QLA_FUNCTION_FAILED;
2494 }
2495
2496 ql_dbg(ql_dbg_io, vha, 0x3074,
2497 "%s LOGO sent, hdl=%x, loopid=%x, portid=%02x%02x%02x.\n",
2498 sp->name, sp->handle, fcport->loop_id, fcport->d_id.b.domain,
2499 fcport->d_id.b.area, fcport->d_id.b.al_pa);
2500
2501 wait_for_completion(&elsio->u.els_logo.comp);
2502
2503 sp->free(sp);
2504 return rval;
2505 }
2506
2507 static void
2508 qla24xx_els_logo_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2509 {
2510 scsi_qla_host_t *vha = sp->vha;
2511 struct srb_iocb *elsio = &sp->u.iocb_cmd;
2512 uint32_t dsd_len = 24;
2513
2514 els_iocb->entry_type = ELS_IOCB_TYPE;
2515 els_iocb->entry_count = 1;
2516 els_iocb->sys_define = 0;
2517 els_iocb->entry_status = 0;
2518 els_iocb->handle = sp->handle;
2519 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2520 els_iocb->tx_dsd_count = 1;
2521 els_iocb->vp_index = vha->vp_idx;
2522 els_iocb->sof_type = EST_SOFI3;
2523 els_iocb->rx_dsd_count = 0;
2524 els_iocb->opcode = elsio->u.els_logo.els_cmd;
2525
2526 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2527 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2528 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2529 els_iocb->s_id[0] = vha->d_id.b.al_pa;
2530 els_iocb->s_id[1] = vha->d_id.b.area;
2531 els_iocb->s_id[2] = vha->d_id.b.domain;
2532 els_iocb->control_flags = 0;
2533
2534 if (elsio->u.els_logo.els_cmd == ELS_DCMD_PLOGI) {
2535 els_iocb->tx_byte_count = sizeof(struct els_plogi_payload);
2536 els_iocb->tx_address[0] =
2537 cpu_to_le32(LSD(elsio->u.els_plogi.els_plogi_pyld_dma));
2538 els_iocb->tx_address[1] =
2539 cpu_to_le32(MSD(elsio->u.els_plogi.els_plogi_pyld_dma));
2540 els_iocb->tx_len = dsd_len;
2541
2542 els_iocb->rx_dsd_count = 1;
2543 els_iocb->rx_byte_count = sizeof(struct els_plogi_payload);
2544 els_iocb->rx_address[0] =
2545 cpu_to_le32(LSD(elsio->u.els_plogi.els_resp_pyld_dma));
2546 els_iocb->rx_address[1] =
2547 cpu_to_le32(MSD(elsio->u.els_plogi.els_resp_pyld_dma));
2548 els_iocb->rx_len = dsd_len;
2549 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3073,
2550 "PLOGI ELS IOCB:\n");
2551 ql_dump_buffer(ql_log_info, vha, 0x0109,
2552 (uint8_t *)els_iocb, 0x70);
2553 } else {
2554 els_iocb->tx_byte_count = sizeof(struct els_logo_payload);
2555 els_iocb->tx_address[0] =
2556 cpu_to_le32(LSD(elsio->u.els_logo.els_logo_pyld_dma));
2557 els_iocb->tx_address[1] =
2558 cpu_to_le32(MSD(elsio->u.els_logo.els_logo_pyld_dma));
2559 els_iocb->tx_len = cpu_to_le32(sizeof(struct els_logo_payload));
2560
2561 els_iocb->rx_byte_count = 0;
2562 els_iocb->rx_address[0] = 0;
2563 els_iocb->rx_address[1] = 0;
2564 els_iocb->rx_len = 0;
2565 }
2566
2567 sp->vha->qla_stats.control_requests++;
2568 }
2569
2570 static void
2571 qla2x00_els_dcmd2_sp_free(void *data)
2572 {
2573 srb_t *sp = data;
2574 struct srb_iocb *elsio = &sp->u.iocb_cmd;
2575
2576 if (elsio->u.els_plogi.els_plogi_pyld)
2577 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE,
2578 elsio->u.els_plogi.els_plogi_pyld,
2579 elsio->u.els_plogi.els_plogi_pyld_dma);
2580
2581 if (elsio->u.els_plogi.els_resp_pyld)
2582 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE,
2583 elsio->u.els_plogi.els_resp_pyld,
2584 elsio->u.els_plogi.els_resp_pyld_dma);
2585
2586 del_timer(&elsio->timer);
2587 qla2x00_rel_sp(sp);
2588 }
2589
2590 static void
2591 qla2x00_els_dcmd2_iocb_timeout(void *data)
2592 {
2593 srb_t *sp = data;
2594 fc_port_t *fcport = sp->fcport;
2595 struct scsi_qla_host *vha = sp->vha;
2596 struct qla_hw_data *ha = vha->hw;
2597 struct srb_iocb *lio = &sp->u.iocb_cmd;
2598 unsigned long flags = 0;
2599 int res;
2600
2601 ql_dbg(ql_dbg_io + ql_dbg_disc, vha, 0x3069,
2602 "%s hdl=%x ELS Timeout, %8phC portid=%06x\n",
2603 sp->name, sp->handle, fcport->port_name, fcport->d_id.b24);
2604
2605 /* Abort the exchange */
2606 spin_lock_irqsave(&ha->hardware_lock, flags);
2607 res = ha->isp_ops->abort_command(sp);
2608 ql_dbg(ql_dbg_io, vha, 0x3070,
2609 "mbx abort_command %s\n",
2610 (res == QLA_SUCCESS) ? "successful" : "failed");
2611 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2612
2613 complete(&lio->u.els_plogi.comp);
2614 }
2615
2616 static void
2617 qla2x00_els_dcmd2_sp_done(void *ptr, int res)
2618 {
2619 srb_t *sp = ptr;
2620 fc_port_t *fcport = sp->fcport;
2621 struct srb_iocb *lio = &sp->u.iocb_cmd;
2622 struct scsi_qla_host *vha = sp->vha;
2623
2624 ql_dbg(ql_dbg_io + ql_dbg_disc, vha, 0x3072,
2625 "%s ELS hdl=%x, portid=%06x done %8phC\n",
2626 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
2627
2628 complete(&lio->u.els_plogi.comp);
2629 }
2630
2631 int
2632 qla24xx_els_dcmd2_iocb(scsi_qla_host_t *vha, int els_opcode,
2633 fc_port_t *fcport, port_id_t remote_did)
2634 {
2635 srb_t *sp;
2636 struct srb_iocb *elsio = NULL;
2637 struct qla_hw_data *ha = vha->hw;
2638 int rval = QLA_SUCCESS;
2639 void *ptr, *resp_ptr;
2640 dma_addr_t ptr_dma;
2641
2642 /* Alloc SRB structure */
2643 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2644 if (!sp) {
2645 ql_log(ql_log_info, vha, 0x70e6,
2646 "SRB allocation failed\n");
2647 return -ENOMEM;
2648 }
2649
2650 elsio = &sp->u.iocb_cmd;
2651 fcport->d_id.b.domain = remote_did.b.domain;
2652 fcport->d_id.b.area = remote_did.b.area;
2653 fcport->d_id.b.al_pa = remote_did.b.al_pa;
2654
2655 ql_dbg(ql_dbg_io, vha, 0x3073,
2656 "Enter: PLOGI portid=%06x\n", fcport->d_id.b24);
2657
2658 sp->type = SRB_ELS_DCMD;
2659 sp->name = "ELS_DCMD";
2660 sp->fcport = fcport;
2661 qla2x00_init_timer(sp, ELS_DCMD_TIMEOUT);
2662 elsio->timeout = qla2x00_els_dcmd2_iocb_timeout;
2663 sp->done = qla2x00_els_dcmd2_sp_done;
2664 sp->free = qla2x00_els_dcmd2_sp_free;
2665
2666 ptr = elsio->u.els_plogi.els_plogi_pyld =
2667 dma_alloc_coherent(&ha->pdev->dev, DMA_POOL_SIZE,
2668 &elsio->u.els_plogi.els_plogi_pyld_dma, GFP_KERNEL);
2669 ptr_dma = elsio->u.els_plogi.els_plogi_pyld_dma;
2670
2671 if (!elsio->u.els_plogi.els_plogi_pyld) {
2672 rval = QLA_FUNCTION_FAILED;
2673 goto out;
2674 }
2675
2676 resp_ptr = elsio->u.els_plogi.els_resp_pyld =
2677 dma_alloc_coherent(&ha->pdev->dev, DMA_POOL_SIZE,
2678 &elsio->u.els_plogi.els_resp_pyld_dma, GFP_KERNEL);
2679
2680 if (!elsio->u.els_plogi.els_resp_pyld) {
2681 rval = QLA_FUNCTION_FAILED;
2682 goto out;
2683 }
2684
2685 ql_dbg(ql_dbg_io, vha, 0x3073, "PLOGI %p %p\n", ptr, resp_ptr);
2686
2687 memset(ptr, 0, sizeof(struct els_plogi_payload));
2688 memset(resp_ptr, 0, sizeof(struct els_plogi_payload));
2689 elsio->u.els_plogi.els_cmd = els_opcode;
2690 elsio->u.els_plogi.els_plogi_pyld->opcode = els_opcode;
2691 qla24xx_get_port_login_templ(vha, ptr_dma + 4,
2692 &elsio->u.els_plogi.els_plogi_pyld->data[0],
2693 sizeof(struct els_plogi_payload));
2694
2695 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3073, "PLOGI buffer:\n");
2696 ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x0109,
2697 (uint8_t *)elsio->u.els_plogi.els_plogi_pyld, 0x70);
2698
2699 init_completion(&elsio->u.els_plogi.comp);
2700 rval = qla2x00_start_sp(sp);
2701 if (rval != QLA_SUCCESS) {
2702 rval = QLA_FUNCTION_FAILED;
2703 goto out;
2704 }
2705
2706 ql_dbg(ql_dbg_io, vha, 0x3074,
2707 "%s PLOGI sent, hdl=%x, loopid=%x, portid=%06x\n",
2708 sp->name, sp->handle, fcport->loop_id, fcport->d_id.b24);
2709
2710 wait_for_completion(&elsio->u.els_plogi.comp);
2711
2712 if (elsio->u.els_plogi.comp_status != CS_COMPLETE)
2713 rval = QLA_FUNCTION_FAILED;
2714
2715 out:
2716 sp->free(sp);
2717 return rval;
2718 }
2719
2720 static void
2721 qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2722 {
2723 struct bsg_job *bsg_job = sp->u.bsg_job;
2724 struct fc_bsg_request *bsg_request = bsg_job->request;
2725
2726 els_iocb->entry_type = ELS_IOCB_TYPE;
2727 els_iocb->entry_count = 1;
2728 els_iocb->sys_define = 0;
2729 els_iocb->entry_status = 0;
2730 els_iocb->handle = sp->handle;
2731 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2732 els_iocb->tx_dsd_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
2733 els_iocb->vp_index = sp->vha->vp_idx;
2734 els_iocb->sof_type = EST_SOFI3;
2735 els_iocb->rx_dsd_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2736
2737 els_iocb->opcode =
2738 sp->type == SRB_ELS_CMD_RPT ?
2739 bsg_request->rqst_data.r_els.els_code :
2740 bsg_request->rqst_data.h_els.command_code;
2741 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2742 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2743 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2744 els_iocb->control_flags = 0;
2745 els_iocb->rx_byte_count =
2746 cpu_to_le32(bsg_job->reply_payload.payload_len);
2747 els_iocb->tx_byte_count =
2748 cpu_to_le32(bsg_job->request_payload.payload_len);
2749
2750 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2751 (bsg_job->request_payload.sg_list)));
2752 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2753 (bsg_job->request_payload.sg_list)));
2754 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2755 (bsg_job->request_payload.sg_list));
2756
2757 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2758 (bsg_job->reply_payload.sg_list)));
2759 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2760 (bsg_job->reply_payload.sg_list)));
2761 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2762 (bsg_job->reply_payload.sg_list));
2763
2764 sp->vha->qla_stats.control_requests++;
2765 }
2766
2767 static void
2768 qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2769 {
2770 uint16_t avail_dsds;
2771 uint32_t *cur_dsd;
2772 struct scatterlist *sg;
2773 int index;
2774 uint16_t tot_dsds;
2775 scsi_qla_host_t *vha = sp->vha;
2776 struct qla_hw_data *ha = vha->hw;
2777 struct bsg_job *bsg_job = sp->u.bsg_job;
2778 int loop_iterartion = 0;
2779 int entry_count = 1;
2780
2781 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2782 ct_iocb->entry_type = CT_IOCB_TYPE;
2783 ct_iocb->entry_status = 0;
2784 ct_iocb->handle1 = sp->handle;
2785 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2786 ct_iocb->status = cpu_to_le16(0);
2787 ct_iocb->control_flags = cpu_to_le16(0);
2788 ct_iocb->timeout = 0;
2789 ct_iocb->cmd_dsd_count =
2790 cpu_to_le16(bsg_job->request_payload.sg_cnt);
2791 ct_iocb->total_dsd_count =
2792 cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2793 ct_iocb->req_bytecount =
2794 cpu_to_le32(bsg_job->request_payload.payload_len);
2795 ct_iocb->rsp_bytecount =
2796 cpu_to_le32(bsg_job->reply_payload.payload_len);
2797
2798 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2799 (bsg_job->request_payload.sg_list)));
2800 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2801 (bsg_job->request_payload.sg_list)));
2802 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2803
2804 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2805 (bsg_job->reply_payload.sg_list)));
2806 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2807 (bsg_job->reply_payload.sg_list)));
2808 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2809
2810 avail_dsds = 1;
2811 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2812 index = 0;
2813 tot_dsds = bsg_job->reply_payload.sg_cnt;
2814
2815 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2816 dma_addr_t sle_dma;
2817 cont_a64_entry_t *cont_pkt;
2818
2819 /* Allocate additional continuation packets? */
2820 if (avail_dsds == 0) {
2821 /*
2822 * Five DSDs are available in the Cont.
2823 * Type 1 IOCB.
2824 */
2825 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2826 vha->hw->req_q_map[0]);
2827 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2828 avail_dsds = 5;
2829 entry_count++;
2830 }
2831
2832 sle_dma = sg_dma_address(sg);
2833 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2834 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2835 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2836 loop_iterartion++;
2837 avail_dsds--;
2838 }
2839 ct_iocb->entry_count = entry_count;
2840
2841 sp->vha->qla_stats.control_requests++;
2842 }
2843
2844 static void
2845 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2846 {
2847 uint16_t avail_dsds;
2848 uint32_t *cur_dsd;
2849 struct scatterlist *sg;
2850 int index;
2851 uint16_t cmd_dsds, rsp_dsds;
2852 scsi_qla_host_t *vha = sp->vha;
2853 struct qla_hw_data *ha = vha->hw;
2854 struct bsg_job *bsg_job = sp->u.bsg_job;
2855 int entry_count = 1;
2856 cont_a64_entry_t *cont_pkt = NULL;
2857
2858 ct_iocb->entry_type = CT_IOCB_TYPE;
2859 ct_iocb->entry_status = 0;
2860 ct_iocb->sys_define = 0;
2861 ct_iocb->handle = sp->handle;
2862
2863 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2864 ct_iocb->vp_index = sp->vha->vp_idx;
2865 ct_iocb->comp_status = cpu_to_le16(0);
2866
2867 cmd_dsds = bsg_job->request_payload.sg_cnt;
2868 rsp_dsds = bsg_job->reply_payload.sg_cnt;
2869
2870 ct_iocb->cmd_dsd_count = cpu_to_le16(cmd_dsds);
2871 ct_iocb->timeout = 0;
2872 ct_iocb->rsp_dsd_count = cpu_to_le16(rsp_dsds);
2873 ct_iocb->cmd_byte_count =
2874 cpu_to_le32(bsg_job->request_payload.payload_len);
2875
2876 avail_dsds = 2;
2877 cur_dsd = (uint32_t *)ct_iocb->dseg_0_address;
2878 index = 0;
2879
2880 for_each_sg(bsg_job->request_payload.sg_list, sg, cmd_dsds, index) {
2881 dma_addr_t sle_dma;
2882
2883 /* Allocate additional continuation packets? */
2884 if (avail_dsds == 0) {
2885 /*
2886 * Five DSDs are available in the Cont.
2887 * Type 1 IOCB.
2888 */
2889 cont_pkt = qla2x00_prep_cont_type1_iocb(
2890 vha, ha->req_q_map[0]);
2891 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2892 avail_dsds = 5;
2893 entry_count++;
2894 }
2895
2896 sle_dma = sg_dma_address(sg);
2897 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2898 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2899 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2900 avail_dsds--;
2901 }
2902
2903 index = 0;
2904
2905 for_each_sg(bsg_job->reply_payload.sg_list, sg, rsp_dsds, index) {
2906 dma_addr_t sle_dma;
2907
2908 /* Allocate additional continuation packets? */
2909 if (avail_dsds == 0) {
2910 /*
2911 * Five DSDs are available in the Cont.
2912 * Type 1 IOCB.
2913 */
2914 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2915 ha->req_q_map[0]);
2916 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2917 avail_dsds = 5;
2918 entry_count++;
2919 }
2920
2921 sle_dma = sg_dma_address(sg);
2922 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2923 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2924 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2925 avail_dsds--;
2926 }
2927 ct_iocb->entry_count = entry_count;
2928 }
2929
2930 /*
2931 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2932 * @sp: command to send to the ISP
2933 *
2934 * Returns non-zero if a failure occurred, else zero.
2935 */
2936 int
2937 qla82xx_start_scsi(srb_t *sp)
2938 {
2939 int nseg;
2940 unsigned long flags;
2941 struct scsi_cmnd *cmd;
2942 uint32_t *clr_ptr;
2943 uint32_t index;
2944 uint32_t handle;
2945 uint16_t cnt;
2946 uint16_t req_cnt;
2947 uint16_t tot_dsds;
2948 struct device_reg_82xx __iomem *reg;
2949 uint32_t dbval;
2950 uint32_t *fcp_dl;
2951 uint8_t additional_cdb_len;
2952 struct ct6_dsd *ctx;
2953 struct scsi_qla_host *vha = sp->vha;
2954 struct qla_hw_data *ha = vha->hw;
2955 struct req_que *req = NULL;
2956 struct rsp_que *rsp = NULL;
2957
2958 /* Setup device pointers. */
2959 reg = &ha->iobase->isp82;
2960 cmd = GET_CMD_SP(sp);
2961 req = vha->req;
2962 rsp = ha->rsp_q_map[0];
2963
2964 /* So we know we haven't pci_map'ed anything yet */
2965 tot_dsds = 0;
2966
2967 dbval = 0x04 | (ha->portnum << 5);
2968
2969 /* Send marker if required */
2970 if (vha->marker_needed != 0) {
2971 if (qla2x00_marker(vha, req,
2972 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2973 ql_log(ql_log_warn, vha, 0x300c,
2974 "qla2x00_marker failed for cmd=%p.\n", cmd);
2975 return QLA_FUNCTION_FAILED;
2976 }
2977 vha->marker_needed = 0;
2978 }
2979
2980 /* Acquire ring specific lock */
2981 spin_lock_irqsave(&ha->hardware_lock, flags);
2982
2983 /* Check for room in outstanding command list. */
2984 handle = req->current_outstanding_cmd;
2985 for (index = 1; index < req->num_outstanding_cmds; index++) {
2986 handle++;
2987 if (handle == req->num_outstanding_cmds)
2988 handle = 1;
2989 if (!req->outstanding_cmds[handle])
2990 break;
2991 }
2992 if (index == req->num_outstanding_cmds)
2993 goto queuing_error;
2994
2995 /* Map the sg table so we have an accurate count of sg entries needed */
2996 if (scsi_sg_count(cmd)) {
2997 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2998 scsi_sg_count(cmd), cmd->sc_data_direction);
2999 if (unlikely(!nseg))
3000 goto queuing_error;
3001 } else
3002 nseg = 0;
3003
3004 tot_dsds = nseg;
3005
3006 if (tot_dsds > ql2xshiftctondsd) {
3007 struct cmd_type_6 *cmd_pkt;
3008 uint16_t more_dsd_lists = 0;
3009 struct dsd_dma *dsd_ptr;
3010 uint16_t i;
3011
3012 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
3013 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
3014 ql_dbg(ql_dbg_io, vha, 0x300d,
3015 "Num of DSD list %d is than %d for cmd=%p.\n",
3016 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
3017 cmd);
3018 goto queuing_error;
3019 }
3020
3021 if (more_dsd_lists <= ha->gbl_dsd_avail)
3022 goto sufficient_dsds;
3023 else
3024 more_dsd_lists -= ha->gbl_dsd_avail;
3025
3026 for (i = 0; i < more_dsd_lists; i++) {
3027 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
3028 if (!dsd_ptr) {
3029 ql_log(ql_log_fatal, vha, 0x300e,
3030 "Failed to allocate memory for dsd_dma "
3031 "for cmd=%p.\n", cmd);
3032 goto queuing_error;
3033 }
3034
3035 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
3036 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
3037 if (!dsd_ptr->dsd_addr) {
3038 kfree(dsd_ptr);
3039 ql_log(ql_log_fatal, vha, 0x300f,
3040 "Failed to allocate memory for dsd_addr "
3041 "for cmd=%p.\n", cmd);
3042 goto queuing_error;
3043 }
3044 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
3045 ha->gbl_dsd_avail++;
3046 }
3047
3048 sufficient_dsds:
3049 req_cnt = 1;
3050
3051 if (req->cnt < (req_cnt + 2)) {
3052 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
3053 &reg->req_q_out[0]);
3054 if (req->ring_index < cnt)
3055 req->cnt = cnt - req->ring_index;
3056 else
3057 req->cnt = req->length -
3058 (req->ring_index - cnt);
3059 if (req->cnt < (req_cnt + 2))
3060 goto queuing_error;
3061 }
3062
3063 ctx = sp->u.scmd.ctx =
3064 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
3065 if (!ctx) {
3066 ql_log(ql_log_fatal, vha, 0x3010,
3067 "Failed to allocate ctx for cmd=%p.\n", cmd);
3068 goto queuing_error;
3069 }
3070
3071 memset(ctx, 0, sizeof(struct ct6_dsd));
3072 ctx->fcp_cmnd = dma_pool_zalloc(ha->fcp_cmnd_dma_pool,
3073 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
3074 if (!ctx->fcp_cmnd) {
3075 ql_log(ql_log_fatal, vha, 0x3011,
3076 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
3077 goto queuing_error;
3078 }
3079
3080 /* Initialize the DSD list and dma handle */
3081 INIT_LIST_HEAD(&ctx->dsd_list);
3082 ctx->dsd_use_cnt = 0;
3083
3084 if (cmd->cmd_len > 16) {
3085 additional_cdb_len = cmd->cmd_len - 16;
3086 if ((cmd->cmd_len % 4) != 0) {
3087 /* SCSI command bigger than 16 bytes must be
3088 * multiple of 4
3089 */
3090 ql_log(ql_log_warn, vha, 0x3012,
3091 "scsi cmd len %d not multiple of 4 "
3092 "for cmd=%p.\n", cmd->cmd_len, cmd);
3093 goto queuing_error_fcp_cmnd;
3094 }
3095 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
3096 } else {
3097 additional_cdb_len = 0;
3098 ctx->fcp_cmnd_len = 12 + 16 + 4;
3099 }
3100
3101 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
3102 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
3103
3104 /* Zero out remaining portion of packet. */
3105 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
3106 clr_ptr = (uint32_t *)cmd_pkt + 2;
3107 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3108 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3109
3110 /* Set NPORT-ID and LUN number*/
3111 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3112 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3113 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3114 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3115 cmd_pkt->vp_index = sp->vha->vp_idx;
3116
3117 /* Build IOCB segments */
3118 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
3119 goto queuing_error_fcp_cmnd;
3120
3121 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3122 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
3123
3124 /* build FCP_CMND IU */
3125 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
3126 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
3127
3128 if (cmd->sc_data_direction == DMA_TO_DEVICE)
3129 ctx->fcp_cmnd->additional_cdb_len |= 1;
3130 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
3131 ctx->fcp_cmnd->additional_cdb_len |= 2;
3132
3133 /* Populate the FCP_PRIO. */
3134 if (ha->flags.fcp_prio_enabled)
3135 ctx->fcp_cmnd->task_attribute |=
3136 sp->fcport->fcp_prio << 3;
3137
3138 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
3139
3140 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
3141 additional_cdb_len);
3142 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
3143
3144 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
3145 cmd_pkt->fcp_cmnd_dseg_address[0] =
3146 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
3147 cmd_pkt->fcp_cmnd_dseg_address[1] =
3148 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
3149
3150 sp->flags |= SRB_FCP_CMND_DMA_VALID;
3151 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3152 /* Set total data segment count. */
3153 cmd_pkt->entry_count = (uint8_t)req_cnt;
3154 /* Specify response queue number where
3155 * completion should happen
3156 */
3157 cmd_pkt->entry_status = (uint8_t) rsp->id;
3158 } else {
3159 struct cmd_type_7 *cmd_pkt;
3160 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
3161 if (req->cnt < (req_cnt + 2)) {
3162 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
3163 &reg->req_q_out[0]);
3164 if (req->ring_index < cnt)
3165 req->cnt = cnt - req->ring_index;
3166 else
3167 req->cnt = req->length -
3168 (req->ring_index - cnt);
3169 }
3170 if (req->cnt < (req_cnt + 2))
3171 goto queuing_error;
3172
3173 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
3174 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
3175
3176 /* Zero out remaining portion of packet. */
3177 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
3178 clr_ptr = (uint32_t *)cmd_pkt + 2;
3179 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3180 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3181
3182 /* Set NPORT-ID and LUN number*/
3183 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3184 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3185 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3186 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3187 cmd_pkt->vp_index = sp->vha->vp_idx;
3188
3189 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3190 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
3191 sizeof(cmd_pkt->lun));
3192
3193 /* Populate the FCP_PRIO. */
3194 if (ha->flags.fcp_prio_enabled)
3195 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
3196
3197 /* Load SCSI command packet. */
3198 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
3199 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
3200
3201 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3202
3203 /* Build IOCB segments */
3204 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
3205
3206 /* Set total data segment count. */
3207 cmd_pkt->entry_count = (uint8_t)req_cnt;
3208 /* Specify response queue number where
3209 * completion should happen.
3210 */
3211 cmd_pkt->entry_status = (uint8_t) rsp->id;
3212
3213 }
3214 /* Build command packet. */
3215 req->current_outstanding_cmd = handle;
3216 req->outstanding_cmds[handle] = sp;
3217 sp->handle = handle;
3218 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
3219 req->cnt -= req_cnt;
3220 wmb();
3221
3222 /* Adjust ring index. */
3223 req->ring_index++;
3224 if (req->ring_index == req->length) {
3225 req->ring_index = 0;
3226 req->ring_ptr = req->ring;
3227 } else
3228 req->ring_ptr++;
3229
3230 sp->flags |= SRB_DMA_VALID;
3231
3232 /* Set chip new ring index. */
3233 /* write, read and verify logic */
3234 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
3235 if (ql2xdbwr)
3236 qla82xx_wr_32(ha, (uintptr_t __force)ha->nxdb_wr_ptr, dbval);
3237 else {
3238 WRT_REG_DWORD(ha->nxdb_wr_ptr, dbval);
3239 wmb();
3240 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
3241 WRT_REG_DWORD(ha->nxdb_wr_ptr, dbval);
3242 wmb();
3243 }
3244 }
3245
3246 /* Manage unprocessed RIO/ZIO commands in response queue. */
3247 if (vha->flags.process_response_queue &&
3248 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
3249 qla24xx_process_response_queue(vha, rsp);
3250
3251 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3252 return QLA_SUCCESS;
3253
3254 queuing_error_fcp_cmnd:
3255 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
3256 queuing_error:
3257 if (tot_dsds)
3258 scsi_dma_unmap(cmd);
3259
3260 if (sp->u.scmd.ctx) {
3261 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
3262 sp->u.scmd.ctx = NULL;
3263 }
3264 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3265
3266 return QLA_FUNCTION_FAILED;
3267 }
3268
3269 static void
3270 qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb)
3271 {
3272 struct srb_iocb *aio = &sp->u.iocb_cmd;
3273 scsi_qla_host_t *vha = sp->vha;
3274 struct req_que *req = vha->req;
3275
3276 memset(abt_iocb, 0, sizeof(struct abort_entry_24xx));
3277 abt_iocb->entry_type = ABORT_IOCB_TYPE;
3278 abt_iocb->entry_count = 1;
3279 abt_iocb->handle = cpu_to_le32(MAKE_HANDLE(req->id, sp->handle));
3280 abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3281 abt_iocb->handle_to_abort =
3282 cpu_to_le32(MAKE_HANDLE(aio->u.abt.req_que_no,
3283 aio->u.abt.cmd_hndl));
3284 abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
3285 abt_iocb->port_id[1] = sp->fcport->d_id.b.area;
3286 abt_iocb->port_id[2] = sp->fcport->d_id.b.domain;
3287 abt_iocb->vp_index = vha->vp_idx;
3288 abt_iocb->req_que_no = cpu_to_le16(aio->u.abt.req_que_no);
3289 /* Send the command to the firmware */
3290 wmb();
3291 }
3292
3293 static void
3294 qla2x00_mb_iocb(srb_t *sp, struct mbx_24xx_entry *mbx)
3295 {
3296 int i, sz;
3297
3298 mbx->entry_type = MBX_IOCB_TYPE;
3299 mbx->handle = sp->handle;
3300 sz = min(ARRAY_SIZE(mbx->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.out_mb));
3301
3302 for (i = 0; i < sz; i++)
3303 mbx->mb[i] = cpu_to_le16(sp->u.iocb_cmd.u.mbx.out_mb[i]);
3304 }
3305
3306 static void
3307 qla2x00_ctpthru_cmd_iocb(srb_t *sp, struct ct_entry_24xx *ct_pkt)
3308 {
3309 sp->u.iocb_cmd.u.ctarg.iocb = ct_pkt;
3310 qla24xx_prep_ms_iocb(sp->vha, &sp->u.iocb_cmd.u.ctarg);
3311 ct_pkt->handle = sp->handle;
3312 }
3313
3314 static void qla2x00_send_notify_ack_iocb(srb_t *sp,
3315 struct nack_to_isp *nack)
3316 {
3317 struct imm_ntfy_from_isp *ntfy = sp->u.iocb_cmd.u.nack.ntfy;
3318
3319 nack->entry_type = NOTIFY_ACK_TYPE;
3320 nack->entry_count = 1;
3321 nack->ox_id = ntfy->ox_id;
3322
3323 nack->u.isp24.handle = sp->handle;
3324 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3325 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3326 nack->u.isp24.flags = ntfy->u.isp24.flags &
3327 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3328 }
3329 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3330 nack->u.isp24.status = ntfy->u.isp24.status;
3331 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3332 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3333 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3334 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3335 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3336 nack->u.isp24.srr_flags = 0;
3337 nack->u.isp24.srr_reject_code = 0;
3338 nack->u.isp24.srr_reject_code_expl = 0;
3339 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3340 }
3341
3342 /*
3343 * Build NVME LS request
3344 */
3345 static int
3346 qla_nvme_ls(srb_t *sp, struct pt_ls4_request *cmd_pkt)
3347 {
3348 struct srb_iocb *nvme;
3349 int rval = QLA_SUCCESS;
3350
3351 nvme = &sp->u.iocb_cmd;
3352 cmd_pkt->entry_type = PT_LS4_REQUEST;
3353 cmd_pkt->entry_count = 1;
3354 cmd_pkt->control_flags = CF_LS4_ORIGINATOR << CF_LS4_SHIFT;
3355
3356 cmd_pkt->timeout = cpu_to_le16(nvme->u.nvme.timeout_sec);
3357 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3358 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
3359
3360 cmd_pkt->tx_dseg_count = 1;
3361 cmd_pkt->tx_byte_count = nvme->u.nvme.cmd_len;
3362 cmd_pkt->dseg0_len = nvme->u.nvme.cmd_len;
3363 cmd_pkt->dseg0_address[0] = cpu_to_le32(LSD(nvme->u.nvme.cmd_dma));
3364 cmd_pkt->dseg0_address[1] = cpu_to_le32(MSD(nvme->u.nvme.cmd_dma));
3365
3366 cmd_pkt->rx_dseg_count = 1;
3367 cmd_pkt->rx_byte_count = nvme->u.nvme.rsp_len;
3368 cmd_pkt->dseg1_len = nvme->u.nvme.rsp_len;
3369 cmd_pkt->dseg1_address[0] = cpu_to_le32(LSD(nvme->u.nvme.rsp_dma));
3370 cmd_pkt->dseg1_address[1] = cpu_to_le32(MSD(nvme->u.nvme.rsp_dma));
3371
3372 return rval;
3373 }
3374
3375 static void
3376 qla25xx_ctrlvp_iocb(srb_t *sp, struct vp_ctrl_entry_24xx *vce)
3377 {
3378 int map, pos;
3379
3380 vce->entry_type = VP_CTRL_IOCB_TYPE;
3381 vce->handle = sp->handle;
3382 vce->entry_count = 1;
3383 vce->command = cpu_to_le16(sp->u.iocb_cmd.u.ctrlvp.cmd);
3384 vce->vp_count = cpu_to_le16(1);
3385
3386 /*
3387 * index map in firmware starts with 1; decrement index
3388 * this is ok as we never use index 0
3389 */
3390 map = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) / 8;
3391 pos = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) & 7;
3392 vce->vp_idx_map[map] |= 1 << pos;
3393 }
3394
3395 static void
3396 qla24xx_prlo_iocb(srb_t *sp, struct logio_entry_24xx *logio)
3397 {
3398 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
3399 logio->control_flags =
3400 cpu_to_le16(LCF_COMMAND_PRLO|LCF_IMPL_PRLO);
3401
3402 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3403 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
3404 logio->port_id[1] = sp->fcport->d_id.b.area;
3405 logio->port_id[2] = sp->fcport->d_id.b.domain;
3406 logio->vp_index = sp->fcport->vha->vp_idx;
3407 }
3408
3409 int
3410 qla2x00_start_sp(srb_t *sp)
3411 {
3412 int rval;
3413 scsi_qla_host_t *vha = sp->vha;
3414 struct qla_hw_data *ha = vha->hw;
3415 void *pkt;
3416 unsigned long flags;
3417
3418 rval = QLA_FUNCTION_FAILED;
3419 spin_lock_irqsave(&ha->hardware_lock, flags);
3420 pkt = qla2x00_alloc_iocbs(vha, sp);
3421 if (!pkt) {
3422 ql_log(ql_log_warn, vha, 0x700c,
3423 "qla2x00_alloc_iocbs failed.\n");
3424 goto done;
3425 }
3426
3427 rval = QLA_SUCCESS;
3428 switch (sp->type) {
3429 case SRB_LOGIN_CMD:
3430 IS_FWI2_CAPABLE(ha) ?
3431 qla24xx_login_iocb(sp, pkt) :
3432 qla2x00_login_iocb(sp, pkt);
3433 break;
3434 case SRB_PRLI_CMD:
3435 qla24xx_prli_iocb(sp, pkt);
3436 break;
3437 case SRB_LOGOUT_CMD:
3438 IS_FWI2_CAPABLE(ha) ?
3439 qla24xx_logout_iocb(sp, pkt) :
3440 qla2x00_logout_iocb(sp, pkt);
3441 break;
3442 case SRB_ELS_CMD_RPT:
3443 case SRB_ELS_CMD_HST:
3444 qla24xx_els_iocb(sp, pkt);
3445 break;
3446 case SRB_CT_CMD:
3447 IS_FWI2_CAPABLE(ha) ?
3448 qla24xx_ct_iocb(sp, pkt) :
3449 qla2x00_ct_iocb(sp, pkt);
3450 break;
3451 case SRB_ADISC_CMD:
3452 IS_FWI2_CAPABLE(ha) ?
3453 qla24xx_adisc_iocb(sp, pkt) :
3454 qla2x00_adisc_iocb(sp, pkt);
3455 break;
3456 case SRB_TM_CMD:
3457 IS_QLAFX00(ha) ?
3458 qlafx00_tm_iocb(sp, pkt) :
3459 qla24xx_tm_iocb(sp, pkt);
3460 break;
3461 case SRB_FXIOCB_DCMD:
3462 case SRB_FXIOCB_BCMD:
3463 qlafx00_fxdisc_iocb(sp, pkt);
3464 break;
3465 case SRB_NVME_LS:
3466 qla_nvme_ls(sp, pkt);
3467 break;
3468 case SRB_ABT_CMD:
3469 IS_QLAFX00(ha) ?
3470 qlafx00_abort_iocb(sp, pkt) :
3471 qla24xx_abort_iocb(sp, pkt);
3472 break;
3473 case SRB_ELS_DCMD:
3474 qla24xx_els_logo_iocb(sp, pkt);
3475 break;
3476 case SRB_CT_PTHRU_CMD:
3477 qla2x00_ctpthru_cmd_iocb(sp, pkt);
3478 break;
3479 case SRB_MB_IOCB:
3480 qla2x00_mb_iocb(sp, pkt);
3481 break;
3482 case SRB_NACK_PLOGI:
3483 case SRB_NACK_PRLI:
3484 case SRB_NACK_LOGO:
3485 qla2x00_send_notify_ack_iocb(sp, pkt);
3486 break;
3487 case SRB_CTRL_VP:
3488 qla25xx_ctrlvp_iocb(sp, pkt);
3489 break;
3490 case SRB_PRLO_CMD:
3491 qla24xx_prlo_iocb(sp, pkt);
3492 break;
3493 default:
3494 break;
3495 }
3496
3497 wmb();
3498 qla2x00_start_iocbs(vha, ha->req_q_map[0]);
3499 done:
3500 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3501 return rval;
3502 }
3503
3504 static void
3505 qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
3506 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
3507 {
3508 uint16_t avail_dsds;
3509 uint32_t *cur_dsd;
3510 uint32_t req_data_len = 0;
3511 uint32_t rsp_data_len = 0;
3512 struct scatterlist *sg;
3513 int index;
3514 int entry_count = 1;
3515 struct bsg_job *bsg_job = sp->u.bsg_job;
3516
3517 /*Update entry type to indicate bidir command */
3518 *((uint32_t *)(&cmd_pkt->entry_type)) =
3519 cpu_to_le32(COMMAND_BIDIRECTIONAL);
3520
3521 /* Set the transfer direction, in this set both flags
3522 * Also set the BD_WRAP_BACK flag, firmware will take care
3523 * assigning DID=SID for outgoing pkts.
3524 */
3525 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
3526 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
3527 cmd_pkt->control_flags = cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
3528 BD_WRAP_BACK);
3529
3530 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
3531 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
3532 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
3533 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
3534
3535 vha->bidi_stats.transfer_bytes += req_data_len;
3536 vha->bidi_stats.io_count++;
3537
3538 vha->qla_stats.output_bytes += req_data_len;
3539 vha->qla_stats.output_requests++;
3540
3541 /* Only one dsd is available for bidirectional IOCB, remaining dsds
3542 * are bundled in continuation iocb
3543 */
3544 avail_dsds = 1;
3545 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
3546
3547 index = 0;
3548
3549 for_each_sg(bsg_job->request_payload.sg_list, sg,
3550 bsg_job->request_payload.sg_cnt, index) {
3551 dma_addr_t sle_dma;
3552 cont_a64_entry_t *cont_pkt;
3553
3554 /* Allocate additional continuation packets */
3555 if (avail_dsds == 0) {
3556 /* Continuation type 1 IOCB can accomodate
3557 * 5 DSDS
3558 */
3559 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
3560 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
3561 avail_dsds = 5;
3562 entry_count++;
3563 }
3564 sle_dma = sg_dma_address(sg);
3565 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
3566 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
3567 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
3568 avail_dsds--;
3569 }
3570 /* For read request DSD will always goes to continuation IOCB
3571 * and follow the write DSD. If there is room on the current IOCB
3572 * then it is added to that IOCB else new continuation IOCB is
3573 * allocated.
3574 */
3575 for_each_sg(bsg_job->reply_payload.sg_list, sg,
3576 bsg_job->reply_payload.sg_cnt, index) {
3577 dma_addr_t sle_dma;
3578 cont_a64_entry_t *cont_pkt;
3579
3580 /* Allocate additional continuation packets */
3581 if (avail_dsds == 0) {
3582 /* Continuation type 1 IOCB can accomodate
3583 * 5 DSDS
3584 */
3585 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
3586 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
3587 avail_dsds = 5;
3588 entry_count++;
3589 }
3590 sle_dma = sg_dma_address(sg);
3591 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
3592 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
3593 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
3594 avail_dsds--;
3595 }
3596 /* This value should be same as number of IOCB required for this cmd */
3597 cmd_pkt->entry_count = entry_count;
3598 }
3599
3600 int
3601 qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
3602 {
3603
3604 struct qla_hw_data *ha = vha->hw;
3605 unsigned long flags;
3606 uint32_t handle;
3607 uint32_t index;
3608 uint16_t req_cnt;
3609 uint16_t cnt;
3610 uint32_t *clr_ptr;
3611 struct cmd_bidir *cmd_pkt = NULL;
3612 struct rsp_que *rsp;
3613 struct req_que *req;
3614 int rval = EXT_STATUS_OK;
3615
3616 rval = QLA_SUCCESS;
3617
3618 rsp = ha->rsp_q_map[0];
3619 req = vha->req;
3620
3621 /* Send marker if required */
3622 if (vha->marker_needed != 0) {
3623 if (qla2x00_marker(vha, req,
3624 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
3625 return EXT_STATUS_MAILBOX;
3626 vha->marker_needed = 0;
3627 }
3628
3629 /* Acquire ring specific lock */
3630 spin_lock_irqsave(&ha->hardware_lock, flags);
3631
3632 /* Check for room in outstanding command list. */
3633 handle = req->current_outstanding_cmd;
3634 for (index = 1; index < req->num_outstanding_cmds; index++) {
3635 handle++;
3636 if (handle == req->num_outstanding_cmds)
3637 handle = 1;
3638 if (!req->outstanding_cmds[handle])
3639 break;
3640 }
3641
3642 if (index == req->num_outstanding_cmds) {
3643 rval = EXT_STATUS_BUSY;
3644 goto queuing_error;
3645 }
3646
3647 /* Calculate number of IOCB required */
3648 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
3649
3650 /* Check for room on request queue. */
3651 if (req->cnt < req_cnt + 2) {
3652 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
3653 RD_REG_DWORD_RELAXED(req->req_q_out);
3654 if (req->ring_index < cnt)
3655 req->cnt = cnt - req->ring_index;
3656 else
3657 req->cnt = req->length -
3658 (req->ring_index - cnt);
3659 }
3660 if (req->cnt < req_cnt + 2) {
3661 rval = EXT_STATUS_BUSY;
3662 goto queuing_error;
3663 }
3664
3665 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
3666 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
3667
3668 /* Zero out remaining portion of packet. */
3669 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
3670 clr_ptr = (uint32_t *)cmd_pkt + 2;
3671 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3672
3673 /* Set NPORT-ID (of vha)*/
3674 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
3675 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
3676 cmd_pkt->port_id[1] = vha->d_id.b.area;
3677 cmd_pkt->port_id[2] = vha->d_id.b.domain;
3678
3679 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
3680 cmd_pkt->entry_status = (uint8_t) rsp->id;
3681 /* Build command packet. */
3682 req->current_outstanding_cmd = handle;
3683 req->outstanding_cmds[handle] = sp;
3684 sp->handle = handle;
3685 req->cnt -= req_cnt;
3686
3687 /* Send the command to the firmware */
3688 wmb();
3689 qla2x00_start_iocbs(vha, req);
3690 queuing_error:
3691 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3692 return rval;
3693 }