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