qla2xxx: Remove mapped vp index iterator macro dead code.
[linux-2.6-block.git] / drivers / scsi / qla2xxx / qla_iocb.c
CommitLineData
fa90c54f
AV
1/*
2 * QLogic Fibre Channel HBA Driver
bd21eaf9 3 * Copyright (c) 2003-2014 QLogic Corporation
1da177e4 4 *
fa90c54f
AV
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
1da177e4 7#include "qla_def.h"
2d70c103 8#include "qla_target.h"
1da177e4
LT
9
10#include <linux/blkdev.h>
11#include <linux/delay.h>
12
13#include <scsi/scsi_tcq.h>
14
59e0b8b0 15static void qla25xx_set_que(srb_t *, struct rsp_que **);
1da177e4
LT
16/**
17 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
18 * @cmd: SCSI command
19 *
20 * Returns the proper CF_* direction based on CDB.
21 */
22static inline uint16_t
49fd462a 23qla2x00_get_cmd_direction(srb_t *sp)
1da177e4
LT
24{
25 uint16_t cflags;
9ba56b95 26 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
2be21fa2 27 struct scsi_qla_host *vha = sp->fcport->vha;
1da177e4
LT
28
29 cflags = 0;
30
31 /* Set transfer direction */
9ba56b95 32 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4 33 cflags = CF_WRITE;
2be21fa2 34 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
fabbb8df 35 vha->qla_stats.output_requests++;
9ba56b95 36 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1da177e4 37 cflags = CF_READ;
2be21fa2 38 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
fabbb8df 39 vha->qla_stats.input_requests++;
49fd462a 40 }
1da177e4
LT
41 return (cflags);
42}
43
44/**
45 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
46 * Continuation Type 0 IOCBs to allocate.
47 *
48 * @dsds: number of data segment decriptors needed
49 *
50 * Returns the number of IOCB entries needed to store @dsds.
51 */
52uint16_t
53qla2x00_calc_iocbs_32(uint16_t dsds)
54{
55 uint16_t iocbs;
56
57 iocbs = 1;
58 if (dsds > 3) {
59 iocbs += (dsds - 3) / 7;
60 if ((dsds - 3) % 7)
61 iocbs++;
62 }
63 return (iocbs);
64}
65
66/**
67 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
68 * Continuation Type 1 IOCBs to allocate.
69 *
70 * @dsds: number of data segment decriptors needed
71 *
72 * Returns the number of IOCB entries needed to store @dsds.
73 */
74uint16_t
75qla2x00_calc_iocbs_64(uint16_t dsds)
76{
77 uint16_t iocbs;
78
79 iocbs = 1;
80 if (dsds > 2) {
81 iocbs += (dsds - 2) / 5;
82 if ((dsds - 2) % 5)
83 iocbs++;
84 }
85 return (iocbs);
86}
87
88/**
89 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
90 * @ha: HA context
91 *
92 * Returns a pointer to the Continuation Type 0 IOCB packet.
93 */
94static inline cont_entry_t *
67c2e93a 95qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
1da177e4
LT
96{
97 cont_entry_t *cont_pkt;
67c2e93a 98 struct req_que *req = vha->req;
1da177e4 99 /* Adjust ring index. */
e315cd28
AC
100 req->ring_index++;
101 if (req->ring_index == req->length) {
102 req->ring_index = 0;
103 req->ring_ptr = req->ring;
1da177e4 104 } else {
e315cd28 105 req->ring_ptr++;
1da177e4
LT
106 }
107
e315cd28 108 cont_pkt = (cont_entry_t *)req->ring_ptr;
1da177e4
LT
109
110 /* Load packet defaults. */
111 *((uint32_t *)(&cont_pkt->entry_type)) =
112 __constant_cpu_to_le32(CONTINUE_TYPE);
113
114 return (cont_pkt);
115}
116
117/**
118 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
119 * @ha: HA context
120 *
121 * Returns a pointer to the continuation type 1 IOCB packet.
122 */
123static inline cont_a64_entry_t *
0d2aa38e 124qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
1da177e4
LT
125{
126 cont_a64_entry_t *cont_pkt;
127
128 /* Adjust ring index. */
e315cd28
AC
129 req->ring_index++;
130 if (req->ring_index == req->length) {
131 req->ring_index = 0;
132 req->ring_ptr = req->ring;
1da177e4 133 } else {
e315cd28 134 req->ring_ptr++;
1da177e4
LT
135 }
136
e315cd28 137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
1da177e4
LT
138
139 /* Load packet defaults. */
8ae6d9c7
GM
140 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ?
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE_FX00) :
1da177e4
LT
142 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
143
144 return (cont_pkt);
145}
146
bad75002
AE
147static inline int
148qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
149{
9ba56b95
GM
150 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
151 uint8_t guard = scsi_host_get_guard(cmd->device->host);
bad75002 152
bad75002
AE
153 /* We always use DIFF Bundling for best performance */
154 *fw_prot_opts = 0;
155
156 /* Translate SCSI opcode to a protection opcode */
9ba56b95 157 switch (scsi_get_prot_op(cmd)) {
bad75002
AE
158 case SCSI_PROT_READ_STRIP:
159 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
160 break;
161 case SCSI_PROT_WRITE_INSERT:
162 *fw_prot_opts |= PO_MODE_DIF_INSERT;
163 break;
164 case SCSI_PROT_READ_INSERT:
165 *fw_prot_opts |= PO_MODE_DIF_INSERT;
166 break;
167 case SCSI_PROT_WRITE_STRIP:
168 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
169 break;
170 case SCSI_PROT_READ_PASS:
bad75002 171 case SCSI_PROT_WRITE_PASS:
9e522cd8
AE
172 if (guard & SHOST_DIX_GUARD_IP)
173 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
174 else
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
bad75002
AE
176 break;
177 default: /* Normal Request */
178 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 break;
180 }
181
9ba56b95 182 return scsi_prot_sg_count(cmd);
bad75002
AE
183}
184
185/*
1da177e4
LT
186 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
187 * capable IOCB types.
188 *
189 * @sp: SRB command to process
190 * @cmd_pkt: Command type 2 IOCB
191 * @tot_dsds: Total number of segments to transfer
192 */
193void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
194 uint16_t tot_dsds)
195{
196 uint16_t avail_dsds;
197 uint32_t *cur_dsd;
e315cd28 198 scsi_qla_host_t *vha;
1da177e4 199 struct scsi_cmnd *cmd;
385d70b4
FT
200 struct scatterlist *sg;
201 int i;
1da177e4 202
9ba56b95 203 cmd = GET_CMD_SP(sp);
1da177e4
LT
204
205 /* Update entry type to indicate Command Type 2 IOCB */
206 *((uint32_t *)(&cmd_pkt->entry_type)) =
207 __constant_cpu_to_le32(COMMAND_TYPE);
208
209 /* No data transfer */
385d70b4 210 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
211 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
212 return;
213 }
214
444786d7 215 vha = sp->fcport->vha;
49fd462a 216 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
1da177e4
LT
217
218 /* Three DSDs are available in the Command Type 2 IOCB */
219 avail_dsds = 3;
220 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
221
222 /* Load data segments */
385d70b4
FT
223 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
224 cont_entry_t *cont_pkt;
225
226 /* Allocate additional continuation packets? */
227 if (avail_dsds == 0) {
228 /*
229 * Seven DSDs are available in the Continuation
230 * Type 0 IOCB.
231 */
67c2e93a 232 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
385d70b4
FT
233 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
234 avail_dsds = 7;
1da177e4 235 }
385d70b4
FT
236
237 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
238 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
239 avail_dsds--;
1da177e4
LT
240 }
241}
242
243/**
244 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
245 * capable IOCB types.
246 *
247 * @sp: SRB command to process
248 * @cmd_pkt: Command type 3 IOCB
249 * @tot_dsds: Total number of segments to transfer
250 */
251void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
252 uint16_t tot_dsds)
253{
254 uint16_t avail_dsds;
255 uint32_t *cur_dsd;
e315cd28 256 scsi_qla_host_t *vha;
1da177e4 257 struct scsi_cmnd *cmd;
385d70b4
FT
258 struct scatterlist *sg;
259 int i;
1da177e4 260
9ba56b95 261 cmd = GET_CMD_SP(sp);
1da177e4
LT
262
263 /* Update entry type to indicate Command Type 3 IOCB */
264 *((uint32_t *)(&cmd_pkt->entry_type)) =
265 __constant_cpu_to_le32(COMMAND_A64_TYPE);
266
267 /* No data transfer */
385d70b4 268 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
269 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
270 return;
271 }
272
444786d7 273 vha = sp->fcport->vha;
49fd462a 274 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
1da177e4
LT
275
276 /* Two DSDs are available in the Command Type 3 IOCB */
277 avail_dsds = 2;
278 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
279
280 /* Load data segments */
385d70b4
FT
281 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
282 dma_addr_t sle_dma;
283 cont_a64_entry_t *cont_pkt;
284
285 /* Allocate additional continuation packets? */
286 if (avail_dsds == 0) {
287 /*
288 * Five DSDs are available in the Continuation
289 * Type 1 IOCB.
290 */
0d2aa38e 291 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
385d70b4
FT
292 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
293 avail_dsds = 5;
1da177e4 294 }
385d70b4
FT
295
296 sle_dma = sg_dma_address(sg);
297 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
298 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
299 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
300 avail_dsds--;
1da177e4
LT
301 }
302}
303
304/**
305 * qla2x00_start_scsi() - Send a SCSI command to the ISP
306 * @sp: command to send to the ISP
307 *
cc3ef7bc 308 * Returns non-zero if a failure occurred, else zero.
1da177e4
LT
309 */
310int
311qla2x00_start_scsi(srb_t *sp)
312{
385d70b4 313 int ret, nseg;
1da177e4 314 unsigned long flags;
e315cd28 315 scsi_qla_host_t *vha;
1da177e4
LT
316 struct scsi_cmnd *cmd;
317 uint32_t *clr_ptr;
318 uint32_t index;
319 uint32_t handle;
320 cmd_entry_t *cmd_pkt;
1da177e4
LT
321 uint16_t cnt;
322 uint16_t req_cnt;
323 uint16_t tot_dsds;
3d71644c 324 struct device_reg_2xxx __iomem *reg;
e315cd28
AC
325 struct qla_hw_data *ha;
326 struct req_que *req;
73208dfd 327 struct rsp_que *rsp;
ff2fc42e 328 char tag[2];
1da177e4
LT
329
330 /* Setup device pointers. */
331 ret = 0;
444786d7 332 vha = sp->fcport->vha;
e315cd28 333 ha = vha->hw;
3d71644c 334 reg = &ha->iobase->isp;
9ba56b95 335 cmd = GET_CMD_SP(sp);
73208dfd
AC
336 req = ha->req_q_map[0];
337 rsp = ha->rsp_q_map[0];
83021920 338 /* So we know we haven't pci_map'ed anything yet */
339 tot_dsds = 0;
1da177e4
LT
340
341 /* Send marker if required */
e315cd28 342 if (vha->marker_needed != 0) {
7c3df132
SK
343 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
344 QLA_SUCCESS) {
1da177e4 345 return (QLA_FUNCTION_FAILED);
7c3df132 346 }
e315cd28 347 vha->marker_needed = 0;
1da177e4
LT
348 }
349
350 /* Acquire ring specific lock */
c9c5ced9 351 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4
LT
352
353 /* Check for room in outstanding command list. */
e315cd28 354 handle = req->current_outstanding_cmd;
8d93f550 355 for (index = 1; index < req->num_outstanding_cmds; index++) {
1da177e4 356 handle++;
8d93f550 357 if (handle == req->num_outstanding_cmds)
1da177e4 358 handle = 1;
e315cd28 359 if (!req->outstanding_cmds[handle])
1da177e4
LT
360 break;
361 }
8d93f550 362 if (index == req->num_outstanding_cmds)
1da177e4
LT
363 goto queuing_error;
364
83021920 365 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
366 if (scsi_sg_count(cmd)) {
367 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
368 scsi_sg_count(cmd), cmd->sc_data_direction);
369 if (unlikely(!nseg))
370 goto queuing_error;
371 } else
372 nseg = 0;
373
385d70b4 374 tot_dsds = nseg;
83021920 375
1da177e4 376 /* Calculate the number of request entries needed. */
fd34f556 377 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
e315cd28 378 if (req->cnt < (req_cnt + 2)) {
1da177e4 379 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
e315cd28
AC
380 if (req->ring_index < cnt)
381 req->cnt = cnt - req->ring_index;
1da177e4 382 else
e315cd28
AC
383 req->cnt = req->length -
384 (req->ring_index - cnt);
a6eb3c9f
CL
385 /* If still no head room then bail out */
386 if (req->cnt < (req_cnt + 2))
387 goto queuing_error;
1da177e4 388 }
1da177e4 389
1da177e4 390 /* Build command packet */
e315cd28
AC
391 req->current_outstanding_cmd = handle;
392 req->outstanding_cmds[handle] = sp;
cf53b069 393 sp->handle = handle;
9ba56b95 394 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
e315cd28 395 req->cnt -= req_cnt;
1da177e4 396
e315cd28 397 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
1da177e4
LT
398 cmd_pkt->handle = handle;
399 /* Zero out remaining portion of packet. */
400 clr_ptr = (uint32_t *)cmd_pkt + 2;
401 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
402 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
403
bdf79621 404 /* Set target ID and LUN number*/
405 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
9ba56b95 406 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
1da177e4
LT
407
408 /* Update tagged queuing modifier */
ff2fc42e
AV
409 if (scsi_populate_tag_msg(cmd, tag)) {
410 switch (tag[0]) {
411 case HEAD_OF_QUEUE_TAG:
412 cmd_pkt->control_flags =
413 __constant_cpu_to_le16(CF_HEAD_TAG);
414 break;
415 case ORDERED_QUEUE_TAG:
416 cmd_pkt->control_flags =
417 __constant_cpu_to_le16(CF_ORDERED_TAG);
418 break;
419 default:
420 cmd_pkt->control_flags =
421 __constant_cpu_to_le16(CF_SIMPLE_TAG);
422 break;
423 }
c3ccb1d7
SK
424 } else {
425 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
ff2fc42e 426 }
1da177e4 427
1da177e4
LT
428 /* Load SCSI command packet. */
429 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
385d70b4 430 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1da177e4
LT
431
432 /* Build IOCB segments */
fd34f556 433 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
1da177e4
LT
434
435 /* Set total data segment count. */
436 cmd_pkt->entry_count = (uint8_t)req_cnt;
437 wmb();
438
439 /* Adjust ring index. */
e315cd28
AC
440 req->ring_index++;
441 if (req->ring_index == req->length) {
442 req->ring_index = 0;
443 req->ring_ptr = req->ring;
1da177e4 444 } else
e315cd28 445 req->ring_ptr++;
1da177e4 446
1da177e4 447 sp->flags |= SRB_DMA_VALID;
1da177e4
LT
448
449 /* Set chip new ring index. */
e315cd28 450 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
1da177e4
LT
451 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
452
4fdfefe5 453 /* Manage unprocessed RIO/ZIO commands in response queue. */
e315cd28 454 if (vha->flags.process_response_queue &&
73208dfd
AC
455 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
456 qla2x00_process_response_queue(rsp);
4fdfefe5 457
c9c5ced9 458 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4
LT
459 return (QLA_SUCCESS);
460
461queuing_error:
385d70b4
FT
462 if (tot_dsds)
463 scsi_dma_unmap(cmd);
464
c9c5ced9 465 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4
LT
466
467 return (QLA_FUNCTION_FAILED);
468}
469
5162cf0c
GM
470/**
471 * qla2x00_start_iocbs() - Execute the IOCB command
472 */
2d70c103 473void
5162cf0c
GM
474qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
475{
476 struct qla_hw_data *ha = vha->hw;
477 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
5162cf0c 478
7ec0effd 479 if (IS_P3P_TYPE(ha)) {
5162cf0c
GM
480 qla82xx_start_iocbs(vha);
481 } else {
482 /* Adjust ring index. */
483 req->ring_index++;
484 if (req->ring_index == req->length) {
485 req->ring_index = 0;
486 req->ring_ptr = req->ring;
487 } else
488 req->ring_ptr++;
489
490 /* Set chip new ring index. */
f73cb695 491 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6246b8a1 492 WRT_REG_DWORD(req->req_q_in, req->ring_index);
98878a16 493 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
8ae6d9c7
GM
494 } else if (IS_QLAFX00(ha)) {
495 WRT_REG_DWORD(&reg->ispfx00.req_q_in, req->ring_index);
496 RD_REG_DWORD_RELAXED(&reg->ispfx00.req_q_in);
497 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
5162cf0c
GM
498 } else if (IS_FWI2_CAPABLE(ha)) {
499 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
500 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
501 } else {
502 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
503 req->ring_index);
504 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
505 }
506 }
507}
508
1da177e4
LT
509/**
510 * qla2x00_marker() - Send a marker IOCB to the firmware.
511 * @ha: HA context
512 * @loop_id: loop ID
513 * @lun: LUN
514 * @type: marker modifier
515 *
516 * Can be called from both normal and interrupt context.
517 *
cc3ef7bc 518 * Returns non-zero if a failure occurred, else zero.
1da177e4 519 */
3dbe756a 520static int
73208dfd
AC
521__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
522 struct rsp_que *rsp, uint16_t loop_id,
523 uint16_t lun, uint8_t type)
1da177e4 524{
2b6c0cee 525 mrk_entry_t *mrk;
8ae6d9c7 526 struct mrk_entry_24xx *mrk24 = NULL;
8ae6d9c7 527
e315cd28
AC
528 struct qla_hw_data *ha = vha->hw;
529 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 530
99b8212c 531 req = ha->req_q_map[0];
fa492630 532 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
2b6c0cee 533 if (mrk == NULL) {
7c3df132
SK
534 ql_log(ql_log_warn, base_vha, 0x3026,
535 "Failed to allocate Marker IOCB.\n");
1da177e4
LT
536
537 return (QLA_FUNCTION_FAILED);
538 }
539
2b6c0cee
AV
540 mrk->entry_type = MARKER_TYPE;
541 mrk->modifier = type;
1da177e4 542 if (type != MK_SYNC_ALL) {
bfd7334e 543 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
544 mrk24 = (struct mrk_entry_24xx *) mrk;
545 mrk24->nport_handle = cpu_to_le16(loop_id);
546 mrk24->lun[1] = LSB(lun);
547 mrk24->lun[2] = MSB(lun);
b797b6de 548 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
e315cd28 549 mrk24->vp_index = vha->vp_idx;
2afa19a9 550 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
2b6c0cee
AV
551 } else {
552 SET_TARGET_ID(ha, mrk->target, loop_id);
553 mrk->lun = cpu_to_le16(lun);
554 }
1da177e4
LT
555 }
556 wmb();
557
5162cf0c 558 qla2x00_start_iocbs(vha, req);
1da177e4
LT
559
560 return (QLA_SUCCESS);
561}
562
fa2a1ce5 563int
73208dfd
AC
564qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
565 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
566 uint8_t type)
1da177e4
LT
567{
568 int ret;
569 unsigned long flags = 0;
570
73208dfd
AC
571 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
572 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
573 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
1da177e4
LT
574
575 return (ret);
576}
577
2d70c103
NB
578/*
579 * qla2x00_issue_marker
580 *
581 * Issue marker
582 * Caller CAN have hardware lock held as specified by ha_locked parameter.
583 * Might release it, then reaquire.
584 */
585int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
586{
587 if (ha_locked) {
588 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
589 MK_SYNC_ALL) != QLA_SUCCESS)
590 return QLA_FUNCTION_FAILED;
591 } else {
592 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
593 MK_SYNC_ALL) != QLA_SUCCESS)
594 return QLA_FUNCTION_FAILED;
595 }
596 vha->marker_needed = 0;
597
598 return QLA_SUCCESS;
599}
600
5162cf0c
GM
601static inline int
602qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
603 uint16_t tot_dsds)
604{
605 uint32_t *cur_dsd = NULL;
606 scsi_qla_host_t *vha;
607 struct qla_hw_data *ha;
608 struct scsi_cmnd *cmd;
609 struct scatterlist *cur_seg;
610 uint32_t *dsd_seg;
611 void *next_dsd;
612 uint8_t avail_dsds;
613 uint8_t first_iocb = 1;
614 uint32_t dsd_list_len;
615 struct dsd_dma *dsd_ptr;
616 struct ct6_dsd *ctx;
1da177e4 617
9ba56b95 618 cmd = GET_CMD_SP(sp);
a9083016 619
5162cf0c
GM
620 /* Update entry type to indicate Command Type 3 IOCB */
621 *((uint32_t *)(&cmd_pkt->entry_type)) =
622 __constant_cpu_to_le32(COMMAND_TYPE_6);
623
624 /* No data transfer */
625 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
626 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
627 return 0;
628 }
629
630 vha = sp->fcport->vha;
631 ha = vha->hw;
632
633 /* Set transfer direction */
634 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
635 cmd_pkt->control_flags =
636 __constant_cpu_to_le16(CF_WRITE_DATA);
2be21fa2 637 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
fabbb8df 638 vha->qla_stats.output_requests++;
5162cf0c
GM
639 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
640 cmd_pkt->control_flags =
641 __constant_cpu_to_le16(CF_READ_DATA);
2be21fa2 642 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
fabbb8df 643 vha->qla_stats.input_requests++;
5162cf0c
GM
644 }
645
646 cur_seg = scsi_sglist(cmd);
9ba56b95 647 ctx = GET_CMD_CTX_SP(sp);
5162cf0c
GM
648
649 while (tot_dsds) {
650 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
651 QLA_DSDS_PER_IOCB : tot_dsds;
652 tot_dsds -= avail_dsds;
653 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
654
655 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
656 struct dsd_dma, list);
657 next_dsd = dsd_ptr->dsd_addr;
658 list_del(&dsd_ptr->list);
659 ha->gbl_dsd_avail--;
660 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
661 ctx->dsd_use_cnt++;
662 ha->gbl_dsd_inuse++;
663
664 if (first_iocb) {
665 first_iocb = 0;
666 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
667 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
668 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
669 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
73208dfd 670 } else {
5162cf0c
GM
671 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
672 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
673 *cur_dsd++ = cpu_to_le32(dsd_list_len);
674 }
675 cur_dsd = (uint32_t *)next_dsd;
676 while (avail_dsds) {
677 dma_addr_t sle_dma;
678
679 sle_dma = sg_dma_address(cur_seg);
680 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
681 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
682 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
683 cur_seg = sg_next(cur_seg);
684 avail_dsds--;
73208dfd 685 }
2b6c0cee
AV
686 }
687
5162cf0c
GM
688 /* Null termination */
689 *cur_dsd++ = 0;
690 *cur_dsd++ = 0;
691 *cur_dsd++ = 0;
692 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
693 return 0;
2b6c0cee
AV
694}
695
5162cf0c
GM
696/*
697 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
698 * for Command Type 6.
2b6c0cee
AV
699 *
700 * @dsds: number of data segment decriptors needed
701 *
5162cf0c 702 * Returns the number of dsd list needed to store @dsds.
2b6c0cee 703 */
a9083016 704inline uint16_t
5162cf0c 705qla24xx_calc_dsd_lists(uint16_t dsds)
2b6c0cee 706{
5162cf0c 707 uint16_t dsd_lists = 0;
2b6c0cee 708
5162cf0c
GM
709 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
710 if (dsds % QLA_DSDS_PER_IOCB)
711 dsd_lists++;
712 return dsd_lists;
2b6c0cee
AV
713}
714
5162cf0c 715
2b6c0cee
AV
716/**
717 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
718 * IOCB types.
719 *
720 * @sp: SRB command to process
721 * @cmd_pkt: Command type 3 IOCB
722 * @tot_dsds: Total number of segments to transfer
723 */
a9083016 724inline void
2b6c0cee
AV
725qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
726 uint16_t tot_dsds)
727{
728 uint16_t avail_dsds;
729 uint32_t *cur_dsd;
e315cd28 730 scsi_qla_host_t *vha;
2b6c0cee 731 struct scsi_cmnd *cmd;
385d70b4
FT
732 struct scatterlist *sg;
733 int i;
73208dfd 734 struct req_que *req;
2b6c0cee 735
9ba56b95 736 cmd = GET_CMD_SP(sp);
2b6c0cee
AV
737
738 /* Update entry type to indicate Command Type 3 IOCB */
739 *((uint32_t *)(&cmd_pkt->entry_type)) =
740 __constant_cpu_to_le32(COMMAND_TYPE_7);
741
742 /* No data transfer */
385d70b4 743 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
2b6c0cee
AV
744 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
745 return;
746 }
747
444786d7 748 vha = sp->fcport->vha;
67c2e93a 749 req = vha->req;
2b6c0cee
AV
750
751 /* Set transfer direction */
49fd462a 752 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2b6c0cee
AV
753 cmd_pkt->task_mgmt_flags =
754 __constant_cpu_to_le16(TMF_WRITE_DATA);
2be21fa2 755 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
fabbb8df 756 vha->qla_stats.output_requests++;
49fd462a 757 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
2b6c0cee
AV
758 cmd_pkt->task_mgmt_flags =
759 __constant_cpu_to_le16(TMF_READ_DATA);
2be21fa2 760 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
fabbb8df 761 vha->qla_stats.input_requests++;
49fd462a 762 }
2b6c0cee
AV
763
764 /* One DSD is available in the Command Type 3 IOCB */
765 avail_dsds = 1;
766 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
767
768 /* Load data segments */
385d70b4
FT
769
770 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
771 dma_addr_t sle_dma;
772 cont_a64_entry_t *cont_pkt;
773
774 /* Allocate additional continuation packets? */
775 if (avail_dsds == 0) {
776 /*
777 * Five DSDs are available in the Continuation
778 * Type 1 IOCB.
779 */
0d2aa38e 780 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
385d70b4
FT
781 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
782 avail_dsds = 5;
2b6c0cee 783 }
385d70b4
FT
784
785 sle_dma = sg_dma_address(sg);
786 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
787 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
788 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
789 avail_dsds--;
2b6c0cee
AV
790 }
791}
792
bad75002
AE
793struct fw_dif_context {
794 uint32_t ref_tag;
795 uint16_t app_tag;
796 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
797 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
798};
799
800/*
801 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
802 *
803 */
804static inline void
e02587d7 805qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
bad75002
AE
806 unsigned int protcnt)
807{
9ba56b95 808 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
bad75002
AE
809
810 switch (scsi_get_prot_type(cmd)) {
bad75002 811 case SCSI_PROT_DIF_TYPE0:
8cb2049c
AE
812 /*
813 * No check for ql2xenablehba_err_chk, as it would be an
814 * I/O error if hba tag generation is not done.
815 */
816 pkt->ref_tag = cpu_to_le32((uint32_t)
817 (0xffffffff & scsi_get_lba(cmd)));
e02587d7
AE
818
819 if (!qla2x00_hba_err_chk_enabled(sp))
820 break;
821
8cb2049c
AE
822 pkt->ref_tag_mask[0] = 0xff;
823 pkt->ref_tag_mask[1] = 0xff;
824 pkt->ref_tag_mask[2] = 0xff;
825 pkt->ref_tag_mask[3] = 0xff;
bad75002
AE
826 break;
827
828 /*
829 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
830 * match LBA in CDB + N
831 */
832 case SCSI_PROT_DIF_TYPE2:
e02587d7
AE
833 pkt->app_tag = __constant_cpu_to_le16(0);
834 pkt->app_tag_mask[0] = 0x0;
835 pkt->app_tag_mask[1] = 0x0;
0c470874
AE
836
837 pkt->ref_tag = cpu_to_le32((uint32_t)
838 (0xffffffff & scsi_get_lba(cmd)));
839
e02587d7
AE
840 if (!qla2x00_hba_err_chk_enabled(sp))
841 break;
842
0c470874
AE
843 /* enable ALL bytes of the ref tag */
844 pkt->ref_tag_mask[0] = 0xff;
845 pkt->ref_tag_mask[1] = 0xff;
846 pkt->ref_tag_mask[2] = 0xff;
847 pkt->ref_tag_mask[3] = 0xff;
bad75002
AE
848 break;
849
850 /* For Type 3 protection: 16 bit GUARD only */
851 case SCSI_PROT_DIF_TYPE3:
852 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
853 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
854 0x00;
855 break;
856
857 /*
858 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
859 * 16 bit app tag.
860 */
861 case SCSI_PROT_DIF_TYPE1:
e02587d7
AE
862 pkt->ref_tag = cpu_to_le32((uint32_t)
863 (0xffffffff & scsi_get_lba(cmd)));
864 pkt->app_tag = __constant_cpu_to_le16(0);
865 pkt->app_tag_mask[0] = 0x0;
866 pkt->app_tag_mask[1] = 0x0;
867
868 if (!qla2x00_hba_err_chk_enabled(sp))
bad75002
AE
869 break;
870
bad75002
AE
871 /* enable ALL bytes of the ref tag */
872 pkt->ref_tag_mask[0] = 0xff;
873 pkt->ref_tag_mask[1] = 0xff;
874 pkt->ref_tag_mask[2] = 0xff;
875 pkt->ref_tag_mask[3] = 0xff;
876 break;
877 }
bad75002
AE
878}
879
8cb2049c
AE
880struct qla2_sgx {
881 dma_addr_t dma_addr; /* OUT */
882 uint32_t dma_len; /* OUT */
883
884 uint32_t tot_bytes; /* IN */
885 struct scatterlist *cur_sg; /* IN */
886
887 /* for book keeping, bzero on initial invocation */
888 uint32_t bytes_consumed;
889 uint32_t num_bytes;
890 uint32_t tot_partial;
891
892 /* for debugging */
893 uint32_t num_sg;
894 srb_t *sp;
895};
896
897static int
898qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
899 uint32_t *partial)
900{
901 struct scatterlist *sg;
902 uint32_t cumulative_partial, sg_len;
903 dma_addr_t sg_dma_addr;
904
905 if (sgx->num_bytes == sgx->tot_bytes)
906 return 0;
907
908 sg = sgx->cur_sg;
909 cumulative_partial = sgx->tot_partial;
910
911 sg_dma_addr = sg_dma_address(sg);
912 sg_len = sg_dma_len(sg);
913
914 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
915
916 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
917 sgx->dma_len = (blk_sz - cumulative_partial);
918 sgx->tot_partial = 0;
919 sgx->num_bytes += blk_sz;
920 *partial = 0;
921 } else {
922 sgx->dma_len = sg_len - sgx->bytes_consumed;
923 sgx->tot_partial += sgx->dma_len;
924 *partial = 1;
925 }
926
927 sgx->bytes_consumed += sgx->dma_len;
928
929 if (sg_len == sgx->bytes_consumed) {
930 sg = sg_next(sg);
931 sgx->num_sg++;
932 sgx->cur_sg = sg;
933 sgx->bytes_consumed = 0;
934 }
935
936 return 1;
937}
938
939static int
940qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
941 uint32_t *dsd, uint16_t tot_dsds)
942{
943 void *next_dsd;
944 uint8_t avail_dsds = 0;
945 uint32_t dsd_list_len;
946 struct dsd_dma *dsd_ptr;
947 struct scatterlist *sg_prot;
948 uint32_t *cur_dsd = dsd;
949 uint16_t used_dsds = tot_dsds;
950
951 uint32_t prot_int;
952 uint32_t partial;
953 struct qla2_sgx sgx;
954 dma_addr_t sle_dma;
955 uint32_t sle_dma_len, tot_prot_dma_len = 0;
9ba56b95 956 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
8cb2049c
AE
957
958 prot_int = cmd->device->sector_size;
959
960 memset(&sgx, 0, sizeof(struct qla2_sgx));
9ba56b95
GM
961 sgx.tot_bytes = scsi_bufflen(cmd);
962 sgx.cur_sg = scsi_sglist(cmd);
8cb2049c
AE
963 sgx.sp = sp;
964
9ba56b95 965 sg_prot = scsi_prot_sglist(cmd);
8cb2049c
AE
966
967 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
968
969 sle_dma = sgx.dma_addr;
970 sle_dma_len = sgx.dma_len;
971alloc_and_fill:
972 /* Allocate additional continuation packets? */
973 if (avail_dsds == 0) {
974 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
975 QLA_DSDS_PER_IOCB : used_dsds;
976 dsd_list_len = (avail_dsds + 1) * 12;
977 used_dsds -= avail_dsds;
978
979 /* allocate tracking DS */
980 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
981 if (!dsd_ptr)
982 return 1;
983
984 /* allocate new list */
985 dsd_ptr->dsd_addr = next_dsd =
986 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
987 &dsd_ptr->dsd_list_dma);
988
989 if (!next_dsd) {
990 /*
991 * Need to cleanup only this dsd_ptr, rest
992 * will be done by sp_free_dma()
993 */
994 kfree(dsd_ptr);
995 return 1;
996 }
997
998 list_add_tail(&dsd_ptr->list,
9ba56b95 999 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
8cb2049c
AE
1000
1001 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1002
1003 /* add new list to cmd iocb or last list */
1004 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1005 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1006 *cur_dsd++ = dsd_list_len;
1007 cur_dsd = (uint32_t *)next_dsd;
1008 }
1009 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1010 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1011 *cur_dsd++ = cpu_to_le32(sle_dma_len);
1012 avail_dsds--;
1013
1014 if (partial == 0) {
1015 /* Got a full protection interval */
1016 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
1017 sle_dma_len = 8;
bad75002 1018
8cb2049c
AE
1019 tot_prot_dma_len += sle_dma_len;
1020 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
1021 tot_prot_dma_len = 0;
1022 sg_prot = sg_next(sg_prot);
1023 }
1024
1025 partial = 1; /* So as to not re-enter this block */
1026 goto alloc_and_fill;
1027 }
1028 }
1029 /* Null termination */
1030 *cur_dsd++ = 0;
1031 *cur_dsd++ = 0;
1032 *cur_dsd++ = 0;
1033 return 0;
1034}
5162cf0c 1035
bad75002
AE
1036static int
1037qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
1038 uint16_t tot_dsds)
1039{
1040 void *next_dsd;
1041 uint8_t avail_dsds = 0;
1042 uint32_t dsd_list_len;
1043 struct dsd_dma *dsd_ptr;
1044 struct scatterlist *sg;
1045 uint32_t *cur_dsd = dsd;
1046 int i;
1047 uint16_t used_dsds = tot_dsds;
9ba56b95 1048 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
bad75002 1049
9ba56b95 1050 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
bad75002
AE
1051 dma_addr_t sle_dma;
1052
1053 /* Allocate additional continuation packets? */
1054 if (avail_dsds == 0) {
1055 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1056 QLA_DSDS_PER_IOCB : used_dsds;
1057 dsd_list_len = (avail_dsds + 1) * 12;
1058 used_dsds -= avail_dsds;
1059
1060 /* allocate tracking DS */
1061 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1062 if (!dsd_ptr)
1063 return 1;
1064
1065 /* allocate new list */
1066 dsd_ptr->dsd_addr = next_dsd =
1067 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1068 &dsd_ptr->dsd_list_dma);
1069
1070 if (!next_dsd) {
1071 /*
1072 * Need to cleanup only this dsd_ptr, rest
1073 * will be done by sp_free_dma()
1074 */
1075 kfree(dsd_ptr);
1076 return 1;
1077 }
1078
1079 list_add_tail(&dsd_ptr->list,
9ba56b95 1080 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
bad75002
AE
1081
1082 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1083
1084 /* add new list to cmd iocb or last list */
1085 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1086 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1087 *cur_dsd++ = dsd_list_len;
1088 cur_dsd = (uint32_t *)next_dsd;
1089 }
1090 sle_dma = sg_dma_address(sg);
9e522cd8 1091
bad75002
AE
1092 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1093 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1094 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1095 avail_dsds--;
1096
bad75002
AE
1097 }
1098 /* Null termination */
1099 *cur_dsd++ = 0;
1100 *cur_dsd++ = 0;
1101 *cur_dsd++ = 0;
1102 return 0;
1103}
1104
1105static int
1106qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1107 uint32_t *dsd,
1108 uint16_t tot_dsds)
1109{
1110 void *next_dsd;
1111 uint8_t avail_dsds = 0;
1112 uint32_t dsd_list_len;
1113 struct dsd_dma *dsd_ptr;
1114 struct scatterlist *sg;
1115 int i;
1116 struct scsi_cmnd *cmd;
1117 uint32_t *cur_dsd = dsd;
1118 uint16_t used_dsds = tot_dsds;
bad75002 1119
9ba56b95 1120 cmd = GET_CMD_SP(sp);
bad75002
AE
1121 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1122 dma_addr_t sle_dma;
1123
1124 /* Allocate additional continuation packets? */
1125 if (avail_dsds == 0) {
1126 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1127 QLA_DSDS_PER_IOCB : used_dsds;
1128 dsd_list_len = (avail_dsds + 1) * 12;
1129 used_dsds -= avail_dsds;
1130
1131 /* allocate tracking DS */
1132 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1133 if (!dsd_ptr)
1134 return 1;
1135
1136 /* allocate new list */
1137 dsd_ptr->dsd_addr = next_dsd =
1138 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1139 &dsd_ptr->dsd_list_dma);
1140
1141 if (!next_dsd) {
1142 /*
1143 * Need to cleanup only this dsd_ptr, rest
1144 * will be done by sp_free_dma()
1145 */
1146 kfree(dsd_ptr);
1147 return 1;
1148 }
1149
1150 list_add_tail(&dsd_ptr->list,
9ba56b95 1151 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
bad75002
AE
1152
1153 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1154
1155 /* add new list to cmd iocb or last list */
1156 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1157 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1158 *cur_dsd++ = dsd_list_len;
1159 cur_dsd = (uint32_t *)next_dsd;
1160 }
1161 sle_dma = sg_dma_address(sg);
9e522cd8 1162
bad75002
AE
1163 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1164 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1165 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1166
bad75002
AE
1167 avail_dsds--;
1168 }
1169 /* Null termination */
1170 *cur_dsd++ = 0;
1171 *cur_dsd++ = 0;
1172 *cur_dsd++ = 0;
1173 return 0;
1174}
1175
1176/**
1177 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1178 * Type 6 IOCB types.
1179 *
1180 * @sp: SRB command to process
1181 * @cmd_pkt: Command type 3 IOCB
1182 * @tot_dsds: Total number of segments to transfer
1183 */
1184static inline int
1185qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1186 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1187{
1188 uint32_t *cur_dsd, *fcp_dl;
1189 scsi_qla_host_t *vha;
1190 struct scsi_cmnd *cmd;
bad75002 1191 int sgc;
8cb2049c 1192 uint32_t total_bytes = 0;
bad75002
AE
1193 uint32_t data_bytes;
1194 uint32_t dif_bytes;
1195 uint8_t bundling = 1;
1196 uint16_t blk_size;
1197 uint8_t *clr_ptr;
1198 struct crc_context *crc_ctx_pkt = NULL;
1199 struct qla_hw_data *ha;
1200 uint8_t additional_fcpcdb_len;
1201 uint16_t fcp_cmnd_len;
1202 struct fcp_cmnd *fcp_cmnd;
1203 dma_addr_t crc_ctx_dma;
ff2fc42e 1204 char tag[2];
bad75002 1205
9ba56b95 1206 cmd = GET_CMD_SP(sp);
bad75002
AE
1207
1208 sgc = 0;
1209 /* Update entry type to indicate Command Type CRC_2 IOCB */
1210 *((uint32_t *)(&cmd_pkt->entry_type)) =
1211 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1212
7c3df132
SK
1213 vha = sp->fcport->vha;
1214 ha = vha->hw;
1215
bad75002
AE
1216 /* No data transfer */
1217 data_bytes = scsi_bufflen(cmd);
1218 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
bad75002
AE
1219 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1220 return QLA_SUCCESS;
1221 }
1222
c6d39e23 1223 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
bad75002
AE
1224
1225 /* Set transfer direction */
1226 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1227 cmd_pkt->control_flags =
1228 __constant_cpu_to_le16(CF_WRITE_DATA);
1229 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1230 cmd_pkt->control_flags =
1231 __constant_cpu_to_le16(CF_READ_DATA);
1232 }
1233
9ba56b95
GM
1234 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1235 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1236 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1237 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
bad75002
AE
1238 bundling = 0;
1239
1240 /* Allocate CRC context from global pool */
9ba56b95
GM
1241 crc_ctx_pkt = sp->u.scmd.ctx =
1242 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
bad75002
AE
1243
1244 if (!crc_ctx_pkt)
1245 goto crc_queuing_error;
1246
1247 /* Zero out CTX area. */
1248 clr_ptr = (uint8_t *)crc_ctx_pkt;
1249 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1250
1251 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1252
1253 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1254
1255 /* Set handle */
1256 crc_ctx_pkt->handle = cmd_pkt->handle;
1257
1258 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1259
e02587d7 1260 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
bad75002
AE
1261 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1262
1263 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1264 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1265 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1266
1267 /* Determine SCSI command length -- align to 4 byte boundary */
1268 if (cmd->cmd_len > 16) {
bad75002
AE
1269 additional_fcpcdb_len = cmd->cmd_len - 16;
1270 if ((cmd->cmd_len % 4) != 0) {
1271 /* SCSI cmd > 16 bytes must be multiple of 4 */
1272 goto crc_queuing_error;
1273 }
1274 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1275 } else {
1276 additional_fcpcdb_len = 0;
1277 fcp_cmnd_len = 12 + 16 + 4;
1278 }
1279
1280 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1281
1282 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1283 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1284 fcp_cmnd->additional_cdb_len |= 1;
1285 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1286 fcp_cmnd->additional_cdb_len |= 2;
1287
9ba56b95 1288 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
bad75002
AE
1289 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1290 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1291 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1292 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1293 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1294 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
65155b37 1295 fcp_cmnd->task_management = 0;
bad75002 1296
ff2fc42e
AV
1297 /*
1298 * Update tagged queuing modifier if using command tag queuing
1299 */
1300 if (scsi_populate_tag_msg(cmd, tag)) {
1301 switch (tag[0]) {
1302 case HEAD_OF_QUEUE_TAG:
1303 fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1304 break;
1305 case ORDERED_QUEUE_TAG:
1306 fcp_cmnd->task_attribute = TSK_ORDERED;
1307 break;
1308 default:
c3ccb1d7 1309 fcp_cmnd->task_attribute = TSK_SIMPLE;
ff2fc42e
AV
1310 break;
1311 }
1312 } else {
c3ccb1d7 1313 fcp_cmnd->task_attribute = TSK_SIMPLE;
ff2fc42e
AV
1314 }
1315
bad75002
AE
1316 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1317
bad75002 1318 /* Compute dif len and adjust data len to incude protection */
bad75002
AE
1319 dif_bytes = 0;
1320 blk_size = cmd->device->sector_size;
8cb2049c
AE
1321 dif_bytes = (data_bytes / blk_size) * 8;
1322
9ba56b95 1323 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
8cb2049c
AE
1324 case SCSI_PROT_READ_INSERT:
1325 case SCSI_PROT_WRITE_STRIP:
1326 total_bytes = data_bytes;
1327 data_bytes += dif_bytes;
1328 break;
1329
1330 case SCSI_PROT_READ_STRIP:
1331 case SCSI_PROT_WRITE_INSERT:
1332 case SCSI_PROT_READ_PASS:
1333 case SCSI_PROT_WRITE_PASS:
1334 total_bytes = data_bytes + dif_bytes;
1335 break;
1336 default:
1337 BUG();
bad75002
AE
1338 }
1339
e02587d7 1340 if (!qla2x00_hba_err_chk_enabled(sp))
bad75002 1341 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
9e522cd8
AE
1342 /* HBA error checking enabled */
1343 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1344 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1345 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1346 SCSI_PROT_DIF_TYPE2))
1347 fw_prot_opts |= BIT_10;
1348 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1349 SCSI_PROT_DIF_TYPE3)
1350 fw_prot_opts |= BIT_11;
1351 }
bad75002
AE
1352
1353 if (!bundling) {
1354 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1355 } else {
1356 /*
1357 * Configure Bundling if we need to fetch interlaving
1358 * protection PCI accesses
1359 */
1360 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1361 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1362 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1363 tot_prot_dsds);
1364 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1365 }
1366
1367 /* Finish the common fields of CRC pkt */
1368 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1369 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1370 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1371 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1372 /* Fibre channel byte count */
1373 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1374 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1375 additional_fcpcdb_len);
1376 *fcp_dl = htonl(total_bytes);
1377
0c470874 1378 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
0c470874
AE
1379 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1380 return QLA_SUCCESS;
1381 }
bad75002
AE
1382 /* Walks data segments */
1383
1384 cmd_pkt->control_flags |=
1385 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
8cb2049c
AE
1386
1387 if (!bundling && tot_prot_dsds) {
1388 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1389 cur_dsd, tot_dsds))
1390 goto crc_queuing_error;
1391 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
bad75002
AE
1392 (tot_dsds - tot_prot_dsds)))
1393 goto crc_queuing_error;
1394
1395 if (bundling && tot_prot_dsds) {
1396 /* Walks dif segments */
bad75002
AE
1397 cmd_pkt->control_flags |=
1398 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1399 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1400 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1401 tot_prot_dsds))
1402 goto crc_queuing_error;
1403 }
1404 return QLA_SUCCESS;
1405
1406crc_queuing_error:
bad75002
AE
1407 /* Cleanup will be performed by the caller */
1408
1409 return QLA_FUNCTION_FAILED;
1410}
2b6c0cee
AV
1411
1412/**
1413 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1414 * @sp: command to send to the ISP
1415 *
cc3ef7bc 1416 * Returns non-zero if a failure occurred, else zero.
2b6c0cee
AV
1417 */
1418int
1419qla24xx_start_scsi(srb_t *sp)
1420{
385d70b4 1421 int ret, nseg;
2b6c0cee 1422 unsigned long flags;
2b6c0cee
AV
1423 uint32_t *clr_ptr;
1424 uint32_t index;
1425 uint32_t handle;
1426 struct cmd_type_7 *cmd_pkt;
2b6c0cee
AV
1427 uint16_t cnt;
1428 uint16_t req_cnt;
1429 uint16_t tot_dsds;
73208dfd
AC
1430 struct req_que *req = NULL;
1431 struct rsp_que *rsp = NULL;
9ba56b95 1432 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
444786d7 1433 struct scsi_qla_host *vha = sp->fcport->vha;
73208dfd 1434 struct qla_hw_data *ha = vha->hw;
ff2fc42e 1435 char tag[2];
2b6c0cee
AV
1436
1437 /* Setup device pointers. */
1438 ret = 0;
73208dfd 1439
59e0b8b0
AC
1440 qla25xx_set_que(sp, &rsp);
1441 req = vha->req;
73208dfd 1442
2b6c0cee
AV
1443 /* So we know we haven't pci_map'ed anything yet */
1444 tot_dsds = 0;
1445
1446 /* Send marker if required */
e315cd28 1447 if (vha->marker_needed != 0) {
7c3df132
SK
1448 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1449 QLA_SUCCESS)
2b6c0cee 1450 return QLA_FUNCTION_FAILED;
e315cd28 1451 vha->marker_needed = 0;
2b6c0cee
AV
1452 }
1453
1454 /* Acquire ring specific lock */
e315cd28 1455 spin_lock_irqsave(&ha->hardware_lock, flags);
2b6c0cee
AV
1456
1457 /* Check for room in outstanding command list. */
e315cd28 1458 handle = req->current_outstanding_cmd;
8d93f550 1459 for (index = 1; index < req->num_outstanding_cmds; index++) {
2b6c0cee 1460 handle++;
8d93f550 1461 if (handle == req->num_outstanding_cmds)
2b6c0cee 1462 handle = 1;
e315cd28 1463 if (!req->outstanding_cmds[handle])
2b6c0cee
AV
1464 break;
1465 }
8d93f550 1466 if (index == req->num_outstanding_cmds)
2b6c0cee
AV
1467 goto queuing_error;
1468
1469 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
1470 if (scsi_sg_count(cmd)) {
1471 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1472 scsi_sg_count(cmd), cmd->sc_data_direction);
1473 if (unlikely(!nseg))
2b6c0cee 1474 goto queuing_error;
2c3dfe3f
SJ
1475 } else
1476 nseg = 0;
1477
385d70b4 1478 tot_dsds = nseg;
7c3df132 1479 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
e315cd28 1480 if (req->cnt < (req_cnt + 2)) {
7c6300e3
JC
1481 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1482 RD_REG_DWORD_RELAXED(req->req_q_out);
e315cd28
AC
1483 if (req->ring_index < cnt)
1484 req->cnt = cnt - req->ring_index;
2b6c0cee 1485 else
e315cd28
AC
1486 req->cnt = req->length -
1487 (req->ring_index - cnt);
a6eb3c9f
CL
1488 if (req->cnt < (req_cnt + 2))
1489 goto queuing_error;
2b6c0cee 1490 }
2b6c0cee
AV
1491
1492 /* Build command packet. */
e315cd28
AC
1493 req->current_outstanding_cmd = handle;
1494 req->outstanding_cmds[handle] = sp;
cf53b069 1495 sp->handle = handle;
9ba56b95 1496 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
e315cd28 1497 req->cnt -= req_cnt;
2b6c0cee 1498
e315cd28 1499 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2afa19a9 1500 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2b6c0cee
AV
1501
1502 /* Zero out remaining portion of packet. */
72df8325 1503 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2b6c0cee
AV
1504 clr_ptr = (uint32_t *)cmd_pkt + 2;
1505 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1506 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1507
1508 /* Set NPORT-ID and LUN number*/
1509 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1510 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1511 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1512 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 1513 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
2b6c0cee 1514
9ba56b95 1515 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
0d4be124 1516 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2b6c0cee 1517
ff2fc42e
AV
1518 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
1519 if (scsi_populate_tag_msg(cmd, tag)) {
1520 switch (tag[0]) {
1521 case HEAD_OF_QUEUE_TAG:
1522 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
1523 break;
1524 case ORDERED_QUEUE_TAG:
1525 cmd_pkt->task = TSK_ORDERED;
1526 break;
c3ccb1d7
SK
1527 default:
1528 cmd_pkt->task = TSK_SIMPLE;
1529 break;
ff2fc42e 1530 }
c3ccb1d7
SK
1531 } else {
1532 cmd_pkt->task = TSK_SIMPLE;
ff2fc42e
AV
1533 }
1534
2b6c0cee
AV
1535 /* Load SCSI command packet. */
1536 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1537 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1538
385d70b4 1539 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2b6c0cee
AV
1540
1541 /* Build IOCB segments */
1542 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1543
1544 /* Set total data segment count. */
1545 cmd_pkt->entry_count = (uint8_t)req_cnt;
2afa19a9
AC
1546 /* Specify response queue number where completion should happen */
1547 cmd_pkt->entry_status = (uint8_t) rsp->id;
2b6c0cee 1548 wmb();
2b6c0cee 1549 /* Adjust ring index. */
e315cd28
AC
1550 req->ring_index++;
1551 if (req->ring_index == req->length) {
1552 req->ring_index = 0;
1553 req->ring_ptr = req->ring;
2b6c0cee 1554 } else
e315cd28 1555 req->ring_ptr++;
2b6c0cee
AV
1556
1557 sp->flags |= SRB_DMA_VALID;
2b6c0cee
AV
1558
1559 /* Set chip new ring index. */
08029990
AV
1560 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1561 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
2b6c0cee 1562
4fdfefe5 1563 /* Manage unprocessed RIO/ZIO commands in response queue. */
e315cd28 1564 if (vha->flags.process_response_queue &&
73208dfd 1565 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2afa19a9 1566 qla24xx_process_response_queue(vha, rsp);
4fdfefe5 1567
e315cd28 1568 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2b6c0cee
AV
1569 return QLA_SUCCESS;
1570
1571queuing_error:
385d70b4
FT
1572 if (tot_dsds)
1573 scsi_dma_unmap(cmd);
1574
e315cd28 1575 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2b6c0cee
AV
1576
1577 return QLA_FUNCTION_FAILED;
1da177e4 1578}
68ca949c 1579
bad75002
AE
1580/**
1581 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1582 * @sp: command to send to the ISP
1583 *
1584 * Returns non-zero if a failure occurred, else zero.
1585 */
1586int
1587qla24xx_dif_start_scsi(srb_t *sp)
1588{
1589 int nseg;
1590 unsigned long flags;
1591 uint32_t *clr_ptr;
1592 uint32_t index;
1593 uint32_t handle;
1594 uint16_t cnt;
1595 uint16_t req_cnt = 0;
1596 uint16_t tot_dsds;
1597 uint16_t tot_prot_dsds;
1598 uint16_t fw_prot_opts = 0;
1599 struct req_que *req = NULL;
1600 struct rsp_que *rsp = NULL;
9ba56b95 1601 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
bad75002
AE
1602 struct scsi_qla_host *vha = sp->fcport->vha;
1603 struct qla_hw_data *ha = vha->hw;
1604 struct cmd_type_crc_2 *cmd_pkt;
1605 uint32_t status = 0;
1606
1607#define QDSS_GOT_Q_SPACE BIT_0
1608
0c470874
AE
1609 /* Only process protection or >16 cdb in this routine */
1610 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1611 if (cmd->cmd_len <= 16)
1612 return qla24xx_start_scsi(sp);
1613 }
bad75002
AE
1614
1615 /* Setup device pointers. */
1616
1617 qla25xx_set_que(sp, &rsp);
1618 req = vha->req;
1619
1620 /* So we know we haven't pci_map'ed anything yet */
1621 tot_dsds = 0;
1622
1623 /* Send marker if required */
1624 if (vha->marker_needed != 0) {
1625 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1626 QLA_SUCCESS)
1627 return QLA_FUNCTION_FAILED;
1628 vha->marker_needed = 0;
1629 }
1630
1631 /* Acquire ring specific lock */
1632 spin_lock_irqsave(&ha->hardware_lock, flags);
1633
1634 /* Check for room in outstanding command list. */
1635 handle = req->current_outstanding_cmd;
8d93f550 1636 for (index = 1; index < req->num_outstanding_cmds; index++) {
bad75002 1637 handle++;
8d93f550 1638 if (handle == req->num_outstanding_cmds)
bad75002
AE
1639 handle = 1;
1640 if (!req->outstanding_cmds[handle])
1641 break;
1642 }
1643
8d93f550 1644 if (index == req->num_outstanding_cmds)
bad75002
AE
1645 goto queuing_error;
1646
1647 /* Compute number of required data segments */
1648 /* Map the sg table so we have an accurate count of sg entries needed */
1649 if (scsi_sg_count(cmd)) {
1650 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1651 scsi_sg_count(cmd), cmd->sc_data_direction);
1652 if (unlikely(!nseg))
1653 goto queuing_error;
1654 else
1655 sp->flags |= SRB_DMA_VALID;
8cb2049c
AE
1656
1657 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1658 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1659 struct qla2_sgx sgx;
1660 uint32_t partial;
1661
1662 memset(&sgx, 0, sizeof(struct qla2_sgx));
1663 sgx.tot_bytes = scsi_bufflen(cmd);
1664 sgx.cur_sg = scsi_sglist(cmd);
1665 sgx.sp = sp;
1666
1667 nseg = 0;
1668 while (qla24xx_get_one_block_sg(
1669 cmd->device->sector_size, &sgx, &partial))
1670 nseg++;
1671 }
bad75002
AE
1672 } else
1673 nseg = 0;
1674
1675 /* number of required data segments */
1676 tot_dsds = nseg;
1677
1678 /* Compute number of required protection segments */
1679 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1680 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1681 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1682 if (unlikely(!nseg))
1683 goto queuing_error;
1684 else
1685 sp->flags |= SRB_CRC_PROT_DMA_VALID;
8cb2049c
AE
1686
1687 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1688 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1689 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1690 }
bad75002
AE
1691 } else {
1692 nseg = 0;
1693 }
1694
1695 req_cnt = 1;
1696 /* Total Data and protection sg segment(s) */
1697 tot_prot_dsds = nseg;
1698 tot_dsds += nseg;
1699 if (req->cnt < (req_cnt + 2)) {
7c6300e3
JC
1700 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1701 RD_REG_DWORD_RELAXED(req->req_q_out);
bad75002
AE
1702 if (req->ring_index < cnt)
1703 req->cnt = cnt - req->ring_index;
1704 else
1705 req->cnt = req->length -
1706 (req->ring_index - cnt);
a6eb3c9f
CL
1707 if (req->cnt < (req_cnt + 2))
1708 goto queuing_error;
bad75002
AE
1709 }
1710
bad75002
AE
1711 status |= QDSS_GOT_Q_SPACE;
1712
1713 /* Build header part of command packet (excluding the OPCODE). */
1714 req->current_outstanding_cmd = handle;
1715 req->outstanding_cmds[handle] = sp;
8cb2049c 1716 sp->handle = handle;
9ba56b95 1717 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
bad75002
AE
1718 req->cnt -= req_cnt;
1719
1720 /* Fill-in common area */
1721 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1722 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1723
1724 clr_ptr = (uint32_t *)cmd_pkt + 2;
1725 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1726
1727 /* Set NPORT-ID and LUN number*/
1728 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1729 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1730 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1731 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1732
9ba56b95 1733 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
bad75002
AE
1734 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1735
1736 /* Total Data and protection segment(s) */
1737 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1738
1739 /* Build IOCB segments and adjust for data protection segments */
1740 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1741 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1742 QLA_SUCCESS)
1743 goto queuing_error;
1744
1745 cmd_pkt->entry_count = (uint8_t)req_cnt;
1746 /* Specify response queue number where completion should happen */
1747 cmd_pkt->entry_status = (uint8_t) rsp->id;
1748 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1749 wmb();
1750
1751 /* Adjust ring index. */
1752 req->ring_index++;
1753 if (req->ring_index == req->length) {
1754 req->ring_index = 0;
1755 req->ring_ptr = req->ring;
1756 } else
1757 req->ring_ptr++;
1758
1759 /* Set chip new ring index. */
1760 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1761 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1762
1763 /* Manage unprocessed RIO/ZIO commands in response queue. */
1764 if (vha->flags.process_response_queue &&
1765 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1766 qla24xx_process_response_queue(vha, rsp);
1767
1768 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1769
1770 return QLA_SUCCESS;
1771
1772queuing_error:
1773 if (status & QDSS_GOT_Q_SPACE) {
1774 req->outstanding_cmds[handle] = NULL;
1775 req->cnt += req_cnt;
1776 }
1777 /* Cleanup will be performed by the caller (queuecommand) */
1778
1779 spin_unlock_irqrestore(&ha->hardware_lock, flags);
bad75002
AE
1780 return QLA_FUNCTION_FAILED;
1781}
1782
1783
59e0b8b0 1784static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
68ca949c 1785{
9ba56b95 1786 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
68ca949c
AC
1787 struct qla_hw_data *ha = sp->fcport->vha->hw;
1788 int affinity = cmd->request->cpu;
1789
7163ea81 1790 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
59e0b8b0 1791 affinity < ha->max_rsp_queues - 1)
68ca949c 1792 *rsp = ha->rsp_q_map[affinity + 1];
59e0b8b0 1793 else
68ca949c 1794 *rsp = ha->rsp_q_map[0];
68ca949c 1795}
ac280b67
AV
1796
1797/* Generic Control-SRB manipulation functions. */
d94d10e7
GM
1798void *
1799qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
ac280b67 1800{
ac280b67
AV
1801 struct qla_hw_data *ha = vha->hw;
1802 struct req_que *req = ha->req_q_map[0];
1803 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1804 uint32_t index, handle;
1805 request_t *pkt;
1806 uint16_t cnt, req_cnt;
1807
1808 pkt = NULL;
1809 req_cnt = 1;
d94d10e7
GM
1810 handle = 0;
1811
1812 if (!sp)
1813 goto skip_cmd_array;
ac280b67
AV
1814
1815 /* Check for room in outstanding command list. */
1816 handle = req->current_outstanding_cmd;
4b4f30cc 1817 for (index = 1; index < req->num_outstanding_cmds; index++) {
ac280b67 1818 handle++;
8d93f550 1819 if (handle == req->num_outstanding_cmds)
ac280b67
AV
1820 handle = 1;
1821 if (!req->outstanding_cmds[handle])
1822 break;
1823 }
8d93f550 1824 if (index == req->num_outstanding_cmds) {
7c3df132 1825 ql_log(ql_log_warn, vha, 0x700b,
d6a03581 1826 "No room on outstanding cmd array.\n");
ac280b67 1827 goto queuing_error;
7c3df132 1828 }
ac280b67 1829
d94d10e7
GM
1830 /* Prep command array. */
1831 req->current_outstanding_cmd = handle;
1832 req->outstanding_cmds[handle] = sp;
1833 sp->handle = handle;
1834
5780790e 1835 /* Adjust entry-counts as needed. */
9ba56b95
GM
1836 if (sp->type != SRB_SCSI_CMD)
1837 req_cnt = sp->iocbs;
5780790e 1838
d94d10e7 1839skip_cmd_array:
ac280b67
AV
1840 /* Check for room on request queue. */
1841 if (req->cnt < req_cnt) {
f73cb695 1842 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha))
ac280b67 1843 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
7ec0effd 1844 else if (IS_P3P_TYPE(ha))
d94d10e7 1845 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
ac280b67
AV
1846 else if (IS_FWI2_CAPABLE(ha))
1847 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
8ae6d9c7
GM
1848 else if (IS_QLAFX00(ha))
1849 cnt = RD_REG_DWORD(&reg->ispfx00.req_q_out);
ac280b67
AV
1850 else
1851 cnt = qla2x00_debounce_register(
1852 ISP_REQ_Q_OUT(ha, &reg->isp));
1853
1854 if (req->ring_index < cnt)
1855 req->cnt = cnt - req->ring_index;
1856 else
1857 req->cnt = req->length -
1858 (req->ring_index - cnt);
1859 }
1860 if (req->cnt < req_cnt)
1861 goto queuing_error;
1862
1863 /* Prep packet */
ac280b67 1864 req->cnt -= req_cnt;
ac280b67
AV
1865 pkt = req->ring_ptr;
1866 memset(pkt, 0, REQUEST_ENTRY_SIZE);
8ae6d9c7 1867 if (IS_QLAFX00(ha)) {
1f8deefe
SK
1868 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt);
1869 WRT_REG_WORD((void __iomem *)&pkt->handle, handle);
8ae6d9c7
GM
1870 } else {
1871 pkt->entry_count = req_cnt;
1872 pkt->handle = handle;
1873 }
ac280b67
AV
1874
1875queuing_error:
1876 return pkt;
1877}
1878
ac280b67
AV
1879static void
1880qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1881{
9ba56b95 1882 struct srb_iocb *lio = &sp->u.iocb_cmd;
ac280b67
AV
1883
1884 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1885 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
4916392b 1886 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
ac280b67 1887 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
4916392b 1888 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
ac280b67
AV
1889 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1890 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1891 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1892 logio->port_id[1] = sp->fcport->d_id.b.area;
1893 logio->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 1894 logio->vp_index = sp->fcport->vha->vp_idx;
ac280b67
AV
1895}
1896
1897static void
1898qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1899{
1900 struct qla_hw_data *ha = sp->fcport->vha->hw;
9ba56b95 1901 struct srb_iocb *lio = &sp->u.iocb_cmd;
ac280b67
AV
1902 uint16_t opts;
1903
b963752f 1904 mbx->entry_type = MBX_IOCB_TYPE;
ac280b67
AV
1905 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1906 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
4916392b
MI
1907 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1908 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
ac280b67
AV
1909 if (HAS_EXTENDED_IDS(ha)) {
1910 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1911 mbx->mb10 = cpu_to_le16(opts);
1912 } else {
1913 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1914 }
1915 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1916 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1917 sp->fcport->d_id.b.al_pa);
c6d39e23 1918 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
ac280b67
AV
1919}
1920
1921static void
1922qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1923{
1924 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1925 logio->control_flags =
1926 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1927 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1928 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1929 logio->port_id[1] = sp->fcport->d_id.b.area;
1930 logio->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 1931 logio->vp_index = sp->fcport->vha->vp_idx;
ac280b67
AV
1932}
1933
1934static void
1935qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1936{
1937 struct qla_hw_data *ha = sp->fcport->vha->hw;
1938
b963752f 1939 mbx->entry_type = MBX_IOCB_TYPE;
ac280b67
AV
1940 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1941 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1942 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1943 cpu_to_le16(sp->fcport->loop_id):
1944 cpu_to_le16(sp->fcport->loop_id << 8);
1945 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1946 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1947 sp->fcport->d_id.b.al_pa);
c6d39e23 1948 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
ac280b67
AV
1949 /* Implicit: mbx->mbx10 = 0. */
1950}
1951
5ff1d584
AV
1952static void
1953qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1954{
1955 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1956 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1957 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
c6d39e23 1958 logio->vp_index = sp->fcport->vha->vp_idx;
5ff1d584
AV
1959}
1960
1961static void
1962qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1963{
1964 struct qla_hw_data *ha = sp->fcport->vha->hw;
1965
1966 mbx->entry_type = MBX_IOCB_TYPE;
1967 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1968 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1969 if (HAS_EXTENDED_IDS(ha)) {
1970 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1971 mbx->mb10 = cpu_to_le16(BIT_0);
1972 } else {
1973 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1974 }
1975 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1976 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1977 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1978 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
c6d39e23 1979 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
5ff1d584
AV
1980}
1981
3822263e
MI
1982static void
1983qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1984{
1985 uint32_t flags;
1986 unsigned int lun;
1987 struct fc_port *fcport = sp->fcport;
1988 scsi_qla_host_t *vha = fcport->vha;
1989 struct qla_hw_data *ha = vha->hw;
9ba56b95 1990 struct srb_iocb *iocb = &sp->u.iocb_cmd;
3822263e
MI
1991 struct req_que *req = vha->req;
1992
1993 flags = iocb->u.tmf.flags;
1994 lun = iocb->u.tmf.lun;
1995
1996 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
1997 tsk->entry_count = 1;
1998 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
1999 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2000 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2001 tsk->control_flags = cpu_to_le32(flags);
2002 tsk->port_id[0] = fcport->d_id.b.al_pa;
2003 tsk->port_id[1] = fcport->d_id.b.area;
2004 tsk->port_id[2] = fcport->d_id.b.domain;
c6d39e23 2005 tsk->vp_index = fcport->vha->vp_idx;
3822263e
MI
2006
2007 if (flags == TCF_LUN_RESET) {
2008 int_to_scsilun(lun, &tsk->lun);
2009 host_to_fcp_swap((uint8_t *)&tsk->lun,
2010 sizeof(tsk->lun));
2011 }
2012}
2013
9a069e19
GM
2014static void
2015qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2016{
9ba56b95 2017 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
9a069e19
GM
2018
2019 els_iocb->entry_type = ELS_IOCB_TYPE;
2020 els_iocb->entry_count = 1;
2021 els_iocb->sys_define = 0;
2022 els_iocb->entry_status = 0;
2023 els_iocb->handle = sp->handle;
2024 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2025 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
c6d39e23 2026 els_iocb->vp_index = sp->fcport->vha->vp_idx;
9a069e19
GM
2027 els_iocb->sof_type = EST_SOFI3;
2028 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2029
4916392b 2030 els_iocb->opcode =
9ba56b95 2031 sp->type == SRB_ELS_CMD_RPT ?
4916392b
MI
2032 bsg_job->request->rqst_data.r_els.els_code :
2033 bsg_job->request->rqst_data.h_els.command_code;
9a069e19
GM
2034 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2035 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2036 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2037 els_iocb->control_flags = 0;
2038 els_iocb->rx_byte_count =
2039 cpu_to_le32(bsg_job->reply_payload.payload_len);
2040 els_iocb->tx_byte_count =
2041 cpu_to_le32(bsg_job->request_payload.payload_len);
2042
2043 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2044 (bsg_job->request_payload.sg_list)));
2045 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2046 (bsg_job->request_payload.sg_list)));
2047 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2048 (bsg_job->request_payload.sg_list));
2049
2050 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2051 (bsg_job->reply_payload.sg_list)));
2052 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2053 (bsg_job->reply_payload.sg_list)));
2054 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2055 (bsg_job->reply_payload.sg_list));
fabbb8df
JC
2056
2057 sp->fcport->vha->qla_stats.control_requests++;
9a069e19
GM
2058}
2059
9bc4f4fb
HZ
2060static void
2061qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2062{
2063 uint16_t avail_dsds;
2064 uint32_t *cur_dsd;
2065 struct scatterlist *sg;
2066 int index;
2067 uint16_t tot_dsds;
2068 scsi_qla_host_t *vha = sp->fcport->vha;
2069 struct qla_hw_data *ha = vha->hw;
9ba56b95 2070 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
9bc4f4fb
HZ
2071 int loop_iterartion = 0;
2072 int cont_iocb_prsnt = 0;
2073 int entry_count = 1;
2074
2075 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2076 ct_iocb->entry_type = CT_IOCB_TYPE;
2077 ct_iocb->entry_status = 0;
2078 ct_iocb->handle1 = sp->handle;
2079 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2080 ct_iocb->status = __constant_cpu_to_le16(0);
2081 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2082 ct_iocb->timeout = 0;
2083 ct_iocb->cmd_dsd_count =
2084 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2085 ct_iocb->total_dsd_count =
2086 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2087 ct_iocb->req_bytecount =
2088 cpu_to_le32(bsg_job->request_payload.payload_len);
2089 ct_iocb->rsp_bytecount =
2090 cpu_to_le32(bsg_job->reply_payload.payload_len);
2091
2092 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2093 (bsg_job->request_payload.sg_list)));
2094 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2095 (bsg_job->request_payload.sg_list)));
2096 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2097
2098 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2099 (bsg_job->reply_payload.sg_list)));
2100 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2101 (bsg_job->reply_payload.sg_list)));
2102 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2103
2104 avail_dsds = 1;
2105 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2106 index = 0;
2107 tot_dsds = bsg_job->reply_payload.sg_cnt;
2108
2109 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2110 dma_addr_t sle_dma;
2111 cont_a64_entry_t *cont_pkt;
2112
2113 /* Allocate additional continuation packets? */
2114 if (avail_dsds == 0) {
2115 /*
2116 * Five DSDs are available in the Cont.
2117 * Type 1 IOCB.
2118 */
0d2aa38e
GM
2119 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2120 vha->hw->req_q_map[0]);
9bc4f4fb
HZ
2121 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2122 avail_dsds = 5;
2123 cont_iocb_prsnt = 1;
2124 entry_count++;
2125 }
2126
2127 sle_dma = sg_dma_address(sg);
2128 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2129 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2130 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2131 loop_iterartion++;
2132 avail_dsds--;
2133 }
2134 ct_iocb->entry_count = entry_count;
fabbb8df
JC
2135
2136 sp->fcport->vha->qla_stats.control_requests++;
9bc4f4fb
HZ
2137}
2138
9a069e19
GM
2139static void
2140qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2141{
2142 uint16_t avail_dsds;
2143 uint32_t *cur_dsd;
2144 struct scatterlist *sg;
2145 int index;
2146 uint16_t tot_dsds;
2147 scsi_qla_host_t *vha = sp->fcport->vha;
0d2aa38e 2148 struct qla_hw_data *ha = vha->hw;
9ba56b95 2149 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
9a069e19
GM
2150 int loop_iterartion = 0;
2151 int cont_iocb_prsnt = 0;
2152 int entry_count = 1;
2153
2154 ct_iocb->entry_type = CT_IOCB_TYPE;
2155 ct_iocb->entry_status = 0;
2156 ct_iocb->sys_define = 0;
2157 ct_iocb->handle = sp->handle;
2158
2159 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
c6d39e23 2160 ct_iocb->vp_index = sp->fcport->vha->vp_idx;
9a069e19
GM
2161 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2162
2163 ct_iocb->cmd_dsd_count =
2164 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2165 ct_iocb->timeout = 0;
2166 ct_iocb->rsp_dsd_count =
2167 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2168 ct_iocb->rsp_byte_count =
2169 cpu_to_le32(bsg_job->reply_payload.payload_len);
2170 ct_iocb->cmd_byte_count =
2171 cpu_to_le32(bsg_job->request_payload.payload_len);
2172 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2173 (bsg_job->request_payload.sg_list)));
2174 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2175 (bsg_job->request_payload.sg_list)));
2176 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2177 (bsg_job->request_payload.sg_list));
2178
2179 avail_dsds = 1;
2180 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2181 index = 0;
2182 tot_dsds = bsg_job->reply_payload.sg_cnt;
2183
2184 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2185 dma_addr_t sle_dma;
2186 cont_a64_entry_t *cont_pkt;
2187
2188 /* Allocate additional continuation packets? */
2189 if (avail_dsds == 0) {
2190 /*
2191 * Five DSDs are available in the Cont.
2192 * Type 1 IOCB.
2193 */
0d2aa38e
GM
2194 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2195 ha->req_q_map[0]);
9a069e19
GM
2196 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2197 avail_dsds = 5;
2198 cont_iocb_prsnt = 1;
2199 entry_count++;
2200 }
2201
2202 sle_dma = sg_dma_address(sg);
2203 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2204 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2205 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2206 loop_iterartion++;
2207 avail_dsds--;
2208 }
2209 ct_iocb->entry_count = entry_count;
2210}
2211
5162cf0c
GM
2212/*
2213 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2214 * @sp: command to send to the ISP
2215 *
2216 * Returns non-zero if a failure occurred, else zero.
2217 */
2218int
2219qla82xx_start_scsi(srb_t *sp)
2220{
2221 int ret, nseg;
2222 unsigned long flags;
2223 struct scsi_cmnd *cmd;
2224 uint32_t *clr_ptr;
2225 uint32_t index;
2226 uint32_t handle;
2227 uint16_t cnt;
2228 uint16_t req_cnt;
2229 uint16_t tot_dsds;
2230 struct device_reg_82xx __iomem *reg;
2231 uint32_t dbval;
2232 uint32_t *fcp_dl;
2233 uint8_t additional_cdb_len;
2234 struct ct6_dsd *ctx;
2235 struct scsi_qla_host *vha = sp->fcport->vha;
2236 struct qla_hw_data *ha = vha->hw;
2237 struct req_que *req = NULL;
2238 struct rsp_que *rsp = NULL;
9ba56b95 2239 char tag[2];
5162cf0c
GM
2240
2241 /* Setup device pointers. */
2242 ret = 0;
2243 reg = &ha->iobase->isp82;
9ba56b95 2244 cmd = GET_CMD_SP(sp);
5162cf0c
GM
2245 req = vha->req;
2246 rsp = ha->rsp_q_map[0];
2247
2248 /* So we know we haven't pci_map'ed anything yet */
2249 tot_dsds = 0;
2250
2251 dbval = 0x04 | (ha->portnum << 5);
2252
2253 /* Send marker if required */
2254 if (vha->marker_needed != 0) {
2255 if (qla2x00_marker(vha, req,
2256 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2257 ql_log(ql_log_warn, vha, 0x300c,
2258 "qla2x00_marker failed for cmd=%p.\n", cmd);
2259 return QLA_FUNCTION_FAILED;
2260 }
2261 vha->marker_needed = 0;
2262 }
2263
2264 /* Acquire ring specific lock */
2265 spin_lock_irqsave(&ha->hardware_lock, flags);
2266
2267 /* Check for room in outstanding command list. */
2268 handle = req->current_outstanding_cmd;
8d93f550 2269 for (index = 1; index < req->num_outstanding_cmds; index++) {
5162cf0c 2270 handle++;
8d93f550 2271 if (handle == req->num_outstanding_cmds)
5162cf0c
GM
2272 handle = 1;
2273 if (!req->outstanding_cmds[handle])
2274 break;
2275 }
8d93f550 2276 if (index == req->num_outstanding_cmds)
5162cf0c
GM
2277 goto queuing_error;
2278
2279 /* Map the sg table so we have an accurate count of sg entries needed */
2280 if (scsi_sg_count(cmd)) {
2281 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2282 scsi_sg_count(cmd), cmd->sc_data_direction);
2283 if (unlikely(!nseg))
2284 goto queuing_error;
2285 } else
2286 nseg = 0;
2287
2288 tot_dsds = nseg;
2289
2290 if (tot_dsds > ql2xshiftctondsd) {
2291 struct cmd_type_6 *cmd_pkt;
2292 uint16_t more_dsd_lists = 0;
2293 struct dsd_dma *dsd_ptr;
2294 uint16_t i;
2295
2296 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2297 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2298 ql_dbg(ql_dbg_io, vha, 0x300d,
2299 "Num of DSD list %d is than %d for cmd=%p.\n",
2300 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2301 cmd);
2302 goto queuing_error;
2303 }
2304
2305 if (more_dsd_lists <= ha->gbl_dsd_avail)
2306 goto sufficient_dsds;
2307 else
2308 more_dsd_lists -= ha->gbl_dsd_avail;
2309
2310 for (i = 0; i < more_dsd_lists; i++) {
2311 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2312 if (!dsd_ptr) {
2313 ql_log(ql_log_fatal, vha, 0x300e,
2314 "Failed to allocate memory for dsd_dma "
2315 "for cmd=%p.\n", cmd);
2316 goto queuing_error;
2317 }
2318
2319 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2320 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2321 if (!dsd_ptr->dsd_addr) {
2322 kfree(dsd_ptr);
2323 ql_log(ql_log_fatal, vha, 0x300f,
2324 "Failed to allocate memory for dsd_addr "
2325 "for cmd=%p.\n", cmd);
2326 goto queuing_error;
2327 }
2328 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2329 ha->gbl_dsd_avail++;
2330 }
2331
2332sufficient_dsds:
2333 req_cnt = 1;
2334
2335 if (req->cnt < (req_cnt + 2)) {
2336 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2337 &reg->req_q_out[0]);
2338 if (req->ring_index < cnt)
2339 req->cnt = cnt - req->ring_index;
2340 else
2341 req->cnt = req->length -
2342 (req->ring_index - cnt);
a6eb3c9f
CL
2343 if (req->cnt < (req_cnt + 2))
2344 goto queuing_error;
5162cf0c
GM
2345 }
2346
9ba56b95
GM
2347 ctx = sp->u.scmd.ctx =
2348 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2349 if (!ctx) {
5162cf0c
GM
2350 ql_log(ql_log_fatal, vha, 0x3010,
2351 "Failed to allocate ctx for cmd=%p.\n", cmd);
2352 goto queuing_error;
2353 }
9ba56b95 2354
5162cf0c
GM
2355 memset(ctx, 0, sizeof(struct ct6_dsd));
2356 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2357 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2358 if (!ctx->fcp_cmnd) {
2359 ql_log(ql_log_fatal, vha, 0x3011,
2360 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
841f97bf 2361 goto queuing_error;
5162cf0c
GM
2362 }
2363
2364 /* Initialize the DSD list and dma handle */
2365 INIT_LIST_HEAD(&ctx->dsd_list);
2366 ctx->dsd_use_cnt = 0;
2367
2368 if (cmd->cmd_len > 16) {
2369 additional_cdb_len = cmd->cmd_len - 16;
2370 if ((cmd->cmd_len % 4) != 0) {
2371 /* SCSI command bigger than 16 bytes must be
2372 * multiple of 4
2373 */
2374 ql_log(ql_log_warn, vha, 0x3012,
2375 "scsi cmd len %d not multiple of 4 "
2376 "for cmd=%p.\n", cmd->cmd_len, cmd);
2377 goto queuing_error_fcp_cmnd;
2378 }
2379 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2380 } else {
2381 additional_cdb_len = 0;
2382 ctx->fcp_cmnd_len = 12 + 16 + 4;
2383 }
2384
2385 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2386 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2387
2388 /* Zero out remaining portion of packet. */
2389 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2390 clr_ptr = (uint32_t *)cmd_pkt + 2;
2391 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2392 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2393
2394 /* Set NPORT-ID and LUN number*/
2395 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2396 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2397 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2398 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 2399 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
5162cf0c
GM
2400
2401 /* Build IOCB segments */
2402 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2403 goto queuing_error_fcp_cmnd;
2404
9ba56b95 2405 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
5162cf0c
GM
2406 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2407
2408 /* build FCP_CMND IU */
2409 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
9ba56b95 2410 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
5162cf0c
GM
2411 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2412
2413 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2414 ctx->fcp_cmnd->additional_cdb_len |= 1;
2415 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2416 ctx->fcp_cmnd->additional_cdb_len |= 2;
2417
2418 /*
2419 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2420 */
2421 if (scsi_populate_tag_msg(cmd, tag)) {
2422 switch (tag[0]) {
2423 case HEAD_OF_QUEUE_TAG:
2424 ctx->fcp_cmnd->task_attribute =
2425 TSK_HEAD_OF_QUEUE;
2426 break;
2427 case ORDERED_QUEUE_TAG:
2428 ctx->fcp_cmnd->task_attribute =
2429 TSK_ORDERED;
2430 break;
2431 }
2432 }
2433
a00f6296
SK
2434 /* Populate the FCP_PRIO. */
2435 if (ha->flags.fcp_prio_enabled)
2436 ctx->fcp_cmnd->task_attribute |=
2437 sp->fcport->fcp_prio << 3;
2438
5162cf0c
GM
2439 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2440
2441 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2442 additional_cdb_len);
2443 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2444
2445 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2446 cmd_pkt->fcp_cmnd_dseg_address[0] =
2447 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2448 cmd_pkt->fcp_cmnd_dseg_address[1] =
2449 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2450
2451 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2452 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2453 /* Set total data segment count. */
2454 cmd_pkt->entry_count = (uint8_t)req_cnt;
2455 /* Specify response queue number where
2456 * completion should happen
2457 */
2458 cmd_pkt->entry_status = (uint8_t) rsp->id;
2459 } else {
2460 struct cmd_type_7 *cmd_pkt;
2461 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2462 if (req->cnt < (req_cnt + 2)) {
2463 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2464 &reg->req_q_out[0]);
2465 if (req->ring_index < cnt)
2466 req->cnt = cnt - req->ring_index;
2467 else
2468 req->cnt = req->length -
2469 (req->ring_index - cnt);
2470 }
2471 if (req->cnt < (req_cnt + 2))
2472 goto queuing_error;
2473
2474 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2475 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2476
2477 /* Zero out remaining portion of packet. */
2478 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2479 clr_ptr = (uint32_t *)cmd_pkt + 2;
2480 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2481 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2482
2483 /* Set NPORT-ID and LUN number*/
2484 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2485 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2486 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2487 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 2488 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
5162cf0c 2489
9ba56b95 2490 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
5162cf0c 2491 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
9ba56b95 2492 sizeof(cmd_pkt->lun));
5162cf0c
GM
2493
2494 /*
2495 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2496 */
2497 if (scsi_populate_tag_msg(cmd, tag)) {
2498 switch (tag[0]) {
2499 case HEAD_OF_QUEUE_TAG:
2500 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
2501 break;
2502 case ORDERED_QUEUE_TAG:
2503 cmd_pkt->task = TSK_ORDERED;
2504 break;
2505 }
2506 }
2507
a00f6296
SK
2508 /* Populate the FCP_PRIO. */
2509 if (ha->flags.fcp_prio_enabled)
2510 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2511
5162cf0c
GM
2512 /* Load SCSI command packet. */
2513 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2514 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2515
2516 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2517
2518 /* Build IOCB segments */
2519 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2520
2521 /* Set total data segment count. */
2522 cmd_pkt->entry_count = (uint8_t)req_cnt;
2523 /* Specify response queue number where
2524 * completion should happen.
2525 */
2526 cmd_pkt->entry_status = (uint8_t) rsp->id;
2527
2528 }
2529 /* Build command packet. */
2530 req->current_outstanding_cmd = handle;
2531 req->outstanding_cmds[handle] = sp;
2532 sp->handle = handle;
9ba56b95 2533 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
5162cf0c
GM
2534 req->cnt -= req_cnt;
2535 wmb();
2536
2537 /* Adjust ring index. */
2538 req->ring_index++;
2539 if (req->ring_index == req->length) {
2540 req->ring_index = 0;
2541 req->ring_ptr = req->ring;
2542 } else
2543 req->ring_ptr++;
2544
2545 sp->flags |= SRB_DMA_VALID;
2546
2547 /* Set chip new ring index. */
2548 /* write, read and verify logic */
2549 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2550 if (ql2xdbwr)
2551 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2552 else {
2553 WRT_REG_DWORD(
2554 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2555 dbval);
2556 wmb();
fa492630 2557 while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
5162cf0c
GM
2558 WRT_REG_DWORD(
2559 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2560 dbval);
2561 wmb();
2562 }
2563 }
2564
2565 /* Manage unprocessed RIO/ZIO commands in response queue. */
2566 if (vha->flags.process_response_queue &&
2567 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2568 qla24xx_process_response_queue(vha, rsp);
2569
2570 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2571 return QLA_SUCCESS;
2572
2573queuing_error_fcp_cmnd:
2574 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2575queuing_error:
2576 if (tot_dsds)
2577 scsi_dma_unmap(cmd);
2578
9ba56b95
GM
2579 if (sp->u.scmd.ctx) {
2580 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2581 sp->u.scmd.ctx = NULL;
5162cf0c
GM
2582 }
2583 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2584
2585 return QLA_FUNCTION_FAILED;
2586}
2587
4440e46d
AB
2588void
2589qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb)
2590{
2591 struct srb_iocb *aio = &sp->u.iocb_cmd;
2592 scsi_qla_host_t *vha = sp->fcport->vha;
2593 struct req_que *req = vha->req;
2594
2595 memset(abt_iocb, 0, sizeof(struct abort_entry_24xx));
2596 abt_iocb->entry_type = ABORT_IOCB_TYPE;
2597 abt_iocb->entry_count = 1;
2598 abt_iocb->handle = cpu_to_le32(MAKE_HANDLE(req->id, sp->handle));
2599 abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2600 abt_iocb->handle_to_abort =
2601 cpu_to_le32(MAKE_HANDLE(req->id, aio->u.abt.cmd_hndl));
2602 abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2603 abt_iocb->port_id[1] = sp->fcport->d_id.b.area;
2604 abt_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2605 abt_iocb->vp_index = vha->vp_idx;
2606 abt_iocb->req_que_no = cpu_to_le16(req->id);
2607 /* Send the command to the firmware */
2608 wmb();
2609}
2610
ac280b67
AV
2611int
2612qla2x00_start_sp(srb_t *sp)
2613{
2614 int rval;
2615 struct qla_hw_data *ha = sp->fcport->vha->hw;
2616 void *pkt;
ac280b67
AV
2617 unsigned long flags;
2618
2619 rval = QLA_FUNCTION_FAILED;
2620 spin_lock_irqsave(&ha->hardware_lock, flags);
d94d10e7 2621 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
7c3df132
SK
2622 if (!pkt) {
2623 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2624 "qla2x00_alloc_iocbs failed.\n");
ac280b67 2625 goto done;
7c3df132 2626 }
ac280b67
AV
2627
2628 rval = QLA_SUCCESS;
9ba56b95 2629 switch (sp->type) {
ac280b67
AV
2630 case SRB_LOGIN_CMD:
2631 IS_FWI2_CAPABLE(ha) ?
5ff1d584 2632 qla24xx_login_iocb(sp, pkt) :
ac280b67
AV
2633 qla2x00_login_iocb(sp, pkt);
2634 break;
2635 case SRB_LOGOUT_CMD:
2636 IS_FWI2_CAPABLE(ha) ?
5ff1d584 2637 qla24xx_logout_iocb(sp, pkt) :
ac280b67
AV
2638 qla2x00_logout_iocb(sp, pkt);
2639 break;
9a069e19
GM
2640 case SRB_ELS_CMD_RPT:
2641 case SRB_ELS_CMD_HST:
2642 qla24xx_els_iocb(sp, pkt);
2643 break;
2644 case SRB_CT_CMD:
9bc4f4fb 2645 IS_FWI2_CAPABLE(ha) ?
5780790e
AV
2646 qla24xx_ct_iocb(sp, pkt) :
2647 qla2x00_ct_iocb(sp, pkt);
9a069e19 2648 break;
5ff1d584
AV
2649 case SRB_ADISC_CMD:
2650 IS_FWI2_CAPABLE(ha) ?
2651 qla24xx_adisc_iocb(sp, pkt) :
2652 qla2x00_adisc_iocb(sp, pkt);
2653 break;
3822263e 2654 case SRB_TM_CMD:
8ae6d9c7
GM
2655 IS_QLAFX00(ha) ?
2656 qlafx00_tm_iocb(sp, pkt) :
2657 qla24xx_tm_iocb(sp, pkt);
2658 break;
2659 case SRB_FXIOCB_DCMD:
2660 case SRB_FXIOCB_BCMD:
2661 qlafx00_fxdisc_iocb(sp, pkt);
2662 break;
2663 case SRB_ABT_CMD:
4440e46d
AB
2664 IS_QLAFX00(ha) ?
2665 qlafx00_abort_iocb(sp, pkt) :
2666 qla24xx_abort_iocb(sp, pkt);
3822263e 2667 break;
ac280b67
AV
2668 default:
2669 break;
2670 }
2671
2672 wmb();
5162cf0c 2673 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
ac280b67
AV
2674done:
2675 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2676 return rval;
2677}
a9b6f722
SK
2678
2679static void
2680qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
2681 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
2682{
2683 uint16_t avail_dsds;
2684 uint32_t *cur_dsd;
2685 uint32_t req_data_len = 0;
2686 uint32_t rsp_data_len = 0;
2687 struct scatterlist *sg;
2688 int index;
2689 int entry_count = 1;
2690 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2691
2692 /*Update entry type to indicate bidir command */
2693 *((uint32_t *)(&cmd_pkt->entry_type)) =
2694 __constant_cpu_to_le32(COMMAND_BIDIRECTIONAL);
2695
2696 /* Set the transfer direction, in this set both flags
2697 * Also set the BD_WRAP_BACK flag, firmware will take care
2698 * assigning DID=SID for outgoing pkts.
2699 */
2700 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
2701 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2702 cmd_pkt->control_flags =
2703 __constant_cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
2704 BD_WRAP_BACK);
2705
2706 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
2707 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
2708 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
2709 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
2710
2711 vha->bidi_stats.transfer_bytes += req_data_len;
2712 vha->bidi_stats.io_count++;
2713
fabbb8df
JC
2714 vha->qla_stats.output_bytes += req_data_len;
2715 vha->qla_stats.output_requests++;
2716
a9b6f722
SK
2717 /* Only one dsd is available for bidirectional IOCB, remaining dsds
2718 * are bundled in continuation iocb
2719 */
2720 avail_dsds = 1;
2721 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
2722
2723 index = 0;
2724
2725 for_each_sg(bsg_job->request_payload.sg_list, sg,
2726 bsg_job->request_payload.sg_cnt, index) {
2727 dma_addr_t sle_dma;
2728 cont_a64_entry_t *cont_pkt;
2729
2730 /* Allocate additional continuation packets */
2731 if (avail_dsds == 0) {
2732 /* Continuation type 1 IOCB can accomodate
2733 * 5 DSDS
2734 */
2735 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2736 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2737 avail_dsds = 5;
2738 entry_count++;
2739 }
2740 sle_dma = sg_dma_address(sg);
2741 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2742 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2743 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2744 avail_dsds--;
2745 }
2746 /* For read request DSD will always goes to continuation IOCB
2747 * and follow the write DSD. If there is room on the current IOCB
2748 * then it is added to that IOCB else new continuation IOCB is
2749 * allocated.
2750 */
2751 for_each_sg(bsg_job->reply_payload.sg_list, sg,
2752 bsg_job->reply_payload.sg_cnt, index) {
2753 dma_addr_t sle_dma;
2754 cont_a64_entry_t *cont_pkt;
2755
2756 /* Allocate additional continuation packets */
2757 if (avail_dsds == 0) {
2758 /* Continuation type 1 IOCB can accomodate
2759 * 5 DSDS
2760 */
2761 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2762 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2763 avail_dsds = 5;
2764 entry_count++;
2765 }
2766 sle_dma = sg_dma_address(sg);
2767 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2768 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2769 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2770 avail_dsds--;
2771 }
2772 /* This value should be same as number of IOCB required for this cmd */
2773 cmd_pkt->entry_count = entry_count;
2774}
2775
2776int
2777qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
2778{
2779
2780 struct qla_hw_data *ha = vha->hw;
2781 unsigned long flags;
2782 uint32_t handle;
2783 uint32_t index;
2784 uint16_t req_cnt;
2785 uint16_t cnt;
2786 uint32_t *clr_ptr;
2787 struct cmd_bidir *cmd_pkt = NULL;
2788 struct rsp_que *rsp;
2789 struct req_que *req;
2790 int rval = EXT_STATUS_OK;
a9b6f722
SK
2791
2792 rval = QLA_SUCCESS;
2793
2794 rsp = ha->rsp_q_map[0];
2795 req = vha->req;
2796
2797 /* Send marker if required */
2798 if (vha->marker_needed != 0) {
2799 if (qla2x00_marker(vha, req,
2800 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
2801 return EXT_STATUS_MAILBOX;
2802 vha->marker_needed = 0;
2803 }
2804
2805 /* Acquire ring specific lock */
2806 spin_lock_irqsave(&ha->hardware_lock, flags);
2807
2808 /* Check for room in outstanding command list. */
2809 handle = req->current_outstanding_cmd;
8d93f550 2810 for (index = 1; index < req->num_outstanding_cmds; index++) {
a9b6f722 2811 handle++;
8d93f550 2812 if (handle == req->num_outstanding_cmds)
a9b6f722
SK
2813 handle = 1;
2814 if (!req->outstanding_cmds[handle])
2815 break;
2816 }
2817
8d93f550 2818 if (index == req->num_outstanding_cmds) {
a9b6f722
SK
2819 rval = EXT_STATUS_BUSY;
2820 goto queuing_error;
2821 }
2822
2823 /* Calculate number of IOCB required */
2824 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2825
2826 /* Check for room on request queue. */
2827 if (req->cnt < req_cnt + 2) {
7c6300e3
JC
2828 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
2829 RD_REG_DWORD_RELAXED(req->req_q_out);
a9b6f722
SK
2830 if (req->ring_index < cnt)
2831 req->cnt = cnt - req->ring_index;
2832 else
2833 req->cnt = req->length -
2834 (req->ring_index - cnt);
2835 }
2836 if (req->cnt < req_cnt + 2) {
2837 rval = EXT_STATUS_BUSY;
2838 goto queuing_error;
2839 }
2840
2841 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
2842 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2843
2844 /* Zero out remaining portion of packet. */
2845 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2846 clr_ptr = (uint32_t *)cmd_pkt + 2;
2847 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2848
2849 /* Set NPORT-ID (of vha)*/
2850 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
2851 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
2852 cmd_pkt->port_id[1] = vha->d_id.b.area;
2853 cmd_pkt->port_id[2] = vha->d_id.b.domain;
2854
2855 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
2856 cmd_pkt->entry_status = (uint8_t) rsp->id;
2857 /* Build command packet. */
2858 req->current_outstanding_cmd = handle;
2859 req->outstanding_cmds[handle] = sp;
2860 sp->handle = handle;
2861 req->cnt -= req_cnt;
2862
2863 /* Send the command to the firmware */
2864 wmb();
2865 qla2x00_start_iocbs(vha, req);
2866queuing_error:
2867 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2868 return rval;
2869}