]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/scsi/qla2xxx/qla_iocb.c
[SCSI] qla2xxx: T10 DIF - Handle uninitalized sectors.
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / qla2xxx / qla_iocb.c
CommitLineData
fa90c54f
AV
1/*
2 * QLogic Fibre Channel HBA Driver
07e264b7 3 * Copyright (c) 2003-2011 QLogic Corporation
1da177e4 4 *
fa90c54f
AV
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
1da177e4
LT
7#include "qla_def.h"
8
9#include <linux/blkdev.h>
10#include <linux/delay.h>
11
12#include <scsi/scsi_tcq.h>
13
73208dfd 14static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
1da177e4 15
59e0b8b0 16static void qla25xx_set_que(srb_t *, struct rsp_que **);
1da177e4
LT
17/**
18 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
19 * @cmd: SCSI command
20 *
21 * Returns the proper CF_* direction based on CDB.
22 */
23static inline uint16_t
49fd462a 24qla2x00_get_cmd_direction(srb_t *sp)
1da177e4
LT
25{
26 uint16_t cflags;
27
28 cflags = 0;
29
30 /* Set transfer direction */
49fd462a 31 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4 32 cflags = CF_WRITE;
e315cd28 33 sp->fcport->vha->hw->qla_stats.output_bytes +=
49fd462a
HZ
34 scsi_bufflen(sp->cmd);
35 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
1da177e4 36 cflags = CF_READ;
e315cd28 37 sp->fcport->vha->hw->qla_stats.input_bytes +=
49fd462a
HZ
38 scsi_bufflen(sp->cmd);
39 }
1da177e4
LT
40 return (cflags);
41}
42
43/**
44 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
45 * Continuation Type 0 IOCBs to allocate.
46 *
47 * @dsds: number of data segment decriptors needed
48 *
49 * Returns the number of IOCB entries needed to store @dsds.
50 */
51uint16_t
52qla2x00_calc_iocbs_32(uint16_t dsds)
53{
54 uint16_t iocbs;
55
56 iocbs = 1;
57 if (dsds > 3) {
58 iocbs += (dsds - 3) / 7;
59 if ((dsds - 3) % 7)
60 iocbs++;
61 }
62 return (iocbs);
63}
64
65/**
66 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
67 * Continuation Type 1 IOCBs to allocate.
68 *
69 * @dsds: number of data segment decriptors needed
70 *
71 * Returns the number of IOCB entries needed to store @dsds.
72 */
73uint16_t
74qla2x00_calc_iocbs_64(uint16_t dsds)
75{
76 uint16_t iocbs;
77
78 iocbs = 1;
79 if (dsds > 2) {
80 iocbs += (dsds - 2) / 5;
81 if ((dsds - 2) % 5)
82 iocbs++;
83 }
84 return (iocbs);
85}
86
87/**
88 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
89 * @ha: HA context
90 *
91 * Returns a pointer to the Continuation Type 0 IOCB packet.
92 */
93static inline cont_entry_t *
67c2e93a 94qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
1da177e4
LT
95{
96 cont_entry_t *cont_pkt;
67c2e93a 97 struct req_que *req = vha->req;
1da177e4 98 /* Adjust ring index. */
e315cd28
AC
99 req->ring_index++;
100 if (req->ring_index == req->length) {
101 req->ring_index = 0;
102 req->ring_ptr = req->ring;
1da177e4 103 } else {
e315cd28 104 req->ring_ptr++;
1da177e4
LT
105 }
106
e315cd28 107 cont_pkt = (cont_entry_t *)req->ring_ptr;
1da177e4
LT
108
109 /* Load packet defaults. */
110 *((uint32_t *)(&cont_pkt->entry_type)) =
111 __constant_cpu_to_le32(CONTINUE_TYPE);
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 *
67c2e93a 123qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
1da177e4
LT
124{
125 cont_a64_entry_t *cont_pkt;
126
67c2e93a 127 struct req_que *req = vha->req;
1da177e4 128 /* Adjust ring index. */
e315cd28
AC
129 req->ring_index++;
130 if (req->ring_index == req->length) {
131 req->ring_index = 0;
132 req->ring_ptr = req->ring;
1da177e4 133 } else {
e315cd28 134 req->ring_ptr++;
1da177e4
LT
135 }
136
e315cd28 137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
1da177e4
LT
138
139 /* Load packet defaults. */
140 *((uint32_t *)(&cont_pkt->entry_type)) =
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
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{
149 uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
150
151 /* We only support T10 DIF right now */
152 if (guard != SHOST_DIX_GUARD_CRC) {
7c3df132
SK
153 ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3007,
154 "Unsupported guard: %d for cmd=%p.\n", guard, sp->cmd);
bad75002
AE
155 return 0;
156 }
157
158 /* We always use DIFF Bundling for best performance */
159 *fw_prot_opts = 0;
160
161 /* Translate SCSI opcode to a protection opcode */
162 switch (scsi_get_prot_op(sp->cmd)) {
163 case SCSI_PROT_READ_STRIP:
164 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
165 break;
166 case SCSI_PROT_WRITE_INSERT:
167 *fw_prot_opts |= PO_MODE_DIF_INSERT;
168 break;
169 case SCSI_PROT_READ_INSERT:
170 *fw_prot_opts |= PO_MODE_DIF_INSERT;
171 break;
172 case SCSI_PROT_WRITE_STRIP:
173 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
174 break;
175 case SCSI_PROT_READ_PASS:
176 *fw_prot_opts |= PO_MODE_DIF_PASS;
177 break;
178 case SCSI_PROT_WRITE_PASS:
179 *fw_prot_opts |= PO_MODE_DIF_PASS;
180 break;
181 default: /* Normal Request */
182 *fw_prot_opts |= PO_MODE_DIF_PASS;
183 break;
184 }
185
186 return scsi_prot_sg_count(sp->cmd);
187}
188
189/*
1da177e4
LT
190 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
191 * capable IOCB types.
192 *
193 * @sp: SRB command to process
194 * @cmd_pkt: Command type 2 IOCB
195 * @tot_dsds: Total number of segments to transfer
196 */
197void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
198 uint16_t tot_dsds)
199{
200 uint16_t avail_dsds;
201 uint32_t *cur_dsd;
e315cd28 202 scsi_qla_host_t *vha;
1da177e4 203 struct scsi_cmnd *cmd;
385d70b4
FT
204 struct scatterlist *sg;
205 int i;
1da177e4
LT
206
207 cmd = sp->cmd;
208
209 /* Update entry type to indicate Command Type 2 IOCB */
210 *((uint32_t *)(&cmd_pkt->entry_type)) =
211 __constant_cpu_to_le32(COMMAND_TYPE);
212
213 /* No data transfer */
385d70b4 214 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
215 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
216 return;
217 }
218
444786d7 219 vha = sp->fcport->vha;
49fd462a 220 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
1da177e4
LT
221
222 /* Three DSDs are available in the Command Type 2 IOCB */
223 avail_dsds = 3;
224 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
225
226 /* Load data segments */
385d70b4
FT
227 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
228 cont_entry_t *cont_pkt;
229
230 /* Allocate additional continuation packets? */
231 if (avail_dsds == 0) {
232 /*
233 * Seven DSDs are available in the Continuation
234 * Type 0 IOCB.
235 */
67c2e93a 236 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
385d70b4
FT
237 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
238 avail_dsds = 7;
1da177e4 239 }
385d70b4
FT
240
241 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
242 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
243 avail_dsds--;
1da177e4
LT
244 }
245}
246
247/**
248 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
249 * capable IOCB types.
250 *
251 * @sp: SRB command to process
252 * @cmd_pkt: Command type 3 IOCB
253 * @tot_dsds: Total number of segments to transfer
254 */
255void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
256 uint16_t tot_dsds)
257{
258 uint16_t avail_dsds;
259 uint32_t *cur_dsd;
e315cd28 260 scsi_qla_host_t *vha;
1da177e4 261 struct scsi_cmnd *cmd;
385d70b4
FT
262 struct scatterlist *sg;
263 int i;
1da177e4
LT
264
265 cmd = sp->cmd;
266
267 /* Update entry type to indicate Command Type 3 IOCB */
268 *((uint32_t *)(&cmd_pkt->entry_type)) =
269 __constant_cpu_to_le32(COMMAND_A64_TYPE);
270
271 /* No data transfer */
385d70b4 272 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
273 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
274 return;
275 }
276
444786d7 277 vha = sp->fcport->vha;
49fd462a 278 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
1da177e4
LT
279
280 /* Two DSDs are available in the Command Type 3 IOCB */
281 avail_dsds = 2;
282 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
283
284 /* Load data segments */
385d70b4
FT
285 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
286 dma_addr_t sle_dma;
287 cont_a64_entry_t *cont_pkt;
288
289 /* Allocate additional continuation packets? */
290 if (avail_dsds == 0) {
291 /*
292 * Five DSDs are available in the Continuation
293 * Type 1 IOCB.
294 */
67c2e93a 295 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
385d70b4
FT
296 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
297 avail_dsds = 5;
1da177e4 298 }
385d70b4
FT
299
300 sle_dma = sg_dma_address(sg);
301 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
302 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
303 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
304 avail_dsds--;
1da177e4
LT
305 }
306}
307
308/**
309 * qla2x00_start_scsi() - Send a SCSI command to the ISP
310 * @sp: command to send to the ISP
311 *
cc3ef7bc 312 * Returns non-zero if a failure occurred, else zero.
1da177e4
LT
313 */
314int
315qla2x00_start_scsi(srb_t *sp)
316{
385d70b4 317 int ret, nseg;
1da177e4 318 unsigned long flags;
e315cd28 319 scsi_qla_host_t *vha;
1da177e4
LT
320 struct scsi_cmnd *cmd;
321 uint32_t *clr_ptr;
322 uint32_t index;
323 uint32_t handle;
324 cmd_entry_t *cmd_pkt;
1da177e4
LT
325 uint16_t cnt;
326 uint16_t req_cnt;
327 uint16_t tot_dsds;
3d71644c 328 struct device_reg_2xxx __iomem *reg;
e315cd28
AC
329 struct qla_hw_data *ha;
330 struct req_que *req;
73208dfd 331 struct rsp_que *rsp;
ff2fc42e 332 char tag[2];
1da177e4
LT
333
334 /* Setup device pointers. */
335 ret = 0;
444786d7 336 vha = sp->fcport->vha;
e315cd28 337 ha = vha->hw;
3d71644c 338 reg = &ha->iobase->isp;
1da177e4 339 cmd = sp->cmd;
73208dfd
AC
340 req = ha->req_q_map[0];
341 rsp = ha->rsp_q_map[0];
83021920
AV
342 /* So we know we haven't pci_map'ed anything yet */
343 tot_dsds = 0;
1da177e4
LT
344
345 /* Send marker if required */
e315cd28 346 if (vha->marker_needed != 0) {
7c3df132
SK
347 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
348 QLA_SUCCESS) {
1da177e4 349 return (QLA_FUNCTION_FAILED);
7c3df132 350 }
e315cd28 351 vha->marker_needed = 0;
1da177e4
LT
352 }
353
354 /* Acquire ring specific lock */
c9c5ced9 355 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4
LT
356
357 /* Check for room in outstanding command list. */
e315cd28 358 handle = req->current_outstanding_cmd;
1da177e4
LT
359 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
360 handle++;
361 if (handle == MAX_OUTSTANDING_COMMANDS)
362 handle = 1;
e315cd28 363 if (!req->outstanding_cmds[handle])
1da177e4
LT
364 break;
365 }
366 if (index == MAX_OUTSTANDING_COMMANDS)
367 goto queuing_error;
368
83021920 369 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
370 if (scsi_sg_count(cmd)) {
371 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
372 scsi_sg_count(cmd), cmd->sc_data_direction);
373 if (unlikely(!nseg))
374 goto queuing_error;
375 } else
376 nseg = 0;
377
385d70b4 378 tot_dsds = nseg;
83021920 379
1da177e4 380 /* Calculate the number of request entries needed. */
fd34f556 381 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
e315cd28 382 if (req->cnt < (req_cnt + 2)) {
1da177e4 383 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
e315cd28
AC
384 if (req->ring_index < cnt)
385 req->cnt = cnt - req->ring_index;
1da177e4 386 else
e315cd28
AC
387 req->cnt = req->length -
388 (req->ring_index - cnt);
1da177e4 389 }
e315cd28 390 if (req->cnt < (req_cnt + 2))
1da177e4
LT
391 goto queuing_error;
392
1da177e4 393 /* Build command packet */
e315cd28
AC
394 req->current_outstanding_cmd = handle;
395 req->outstanding_cmds[handle] = sp;
cf53b069 396 sp->handle = handle;
1da177e4 397 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
e315cd28 398 req->cnt -= req_cnt;
1da177e4 399
e315cd28 400 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
1da177e4
LT
401 cmd_pkt->handle = handle;
402 /* Zero out remaining portion of packet. */
403 clr_ptr = (uint32_t *)cmd_pkt + 2;
404 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
405 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
406
bdf79621
AV
407 /* Set target ID and LUN number*/
408 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
409 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
1da177e4
LT
410
411 /* Update tagged queuing modifier */
ff2fc42e
AV
412 if (scsi_populate_tag_msg(cmd, tag)) {
413 switch (tag[0]) {
414 case HEAD_OF_QUEUE_TAG:
415 cmd_pkt->control_flags =
416 __constant_cpu_to_le16(CF_HEAD_TAG);
417 break;
418 case ORDERED_QUEUE_TAG:
419 cmd_pkt->control_flags =
420 __constant_cpu_to_le16(CF_ORDERED_TAG);
421 break;
422 default:
423 cmd_pkt->control_flags =
424 __constant_cpu_to_le16(CF_SIMPLE_TAG);
425 break;
426 }
427 }
1da177e4 428
1da177e4
LT
429 /* Load SCSI command packet. */
430 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
385d70b4 431 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1da177e4
LT
432
433 /* Build IOCB segments */
fd34f556 434 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
1da177e4
LT
435
436 /* Set total data segment count. */
437 cmd_pkt->entry_count = (uint8_t)req_cnt;
438 wmb();
439
440 /* Adjust ring index. */
e315cd28
AC
441 req->ring_index++;
442 if (req->ring_index == req->length) {
443 req->ring_index = 0;
444 req->ring_ptr = req->ring;
1da177e4 445 } else
e315cd28 446 req->ring_ptr++;
1da177e4 447
1da177e4 448 sp->flags |= SRB_DMA_VALID;
1da177e4
LT
449
450 /* Set chip new ring index. */
e315cd28 451 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
1da177e4
LT
452 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
453
4fdfefe5 454 /* Manage unprocessed RIO/ZIO commands in response queue. */
e315cd28 455 if (vha->flags.process_response_queue &&
73208dfd
AC
456 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
457 qla2x00_process_response_queue(rsp);
4fdfefe5 458
c9c5ced9 459 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4
LT
460 return (QLA_SUCCESS);
461
462queuing_error:
385d70b4
FT
463 if (tot_dsds)
464 scsi_dma_unmap(cmd);
465
c9c5ced9 466 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4
LT
467
468 return (QLA_FUNCTION_FAILED);
469}
470
471/**
472 * qla2x00_marker() - Send a marker IOCB to the firmware.
473 * @ha: HA context
474 * @loop_id: loop ID
475 * @lun: LUN
476 * @type: marker modifier
477 *
478 * Can be called from both normal and interrupt context.
479 *
cc3ef7bc 480 * Returns non-zero if a failure occurred, else zero.
1da177e4 481 */
3dbe756a 482static int
73208dfd
AC
483__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
484 struct rsp_que *rsp, uint16_t loop_id,
485 uint16_t lun, uint8_t type)
1da177e4 486{
2b6c0cee
AV
487 mrk_entry_t *mrk;
488 struct mrk_entry_24xx *mrk24;
e315cd28
AC
489 struct qla_hw_data *ha = vha->hw;
490 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 491
2b6c0cee 492 mrk24 = NULL;
d94d10e7 493 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
2b6c0cee 494 if (mrk == NULL) {
7c3df132
SK
495 ql_log(ql_log_warn, base_vha, 0x3026,
496 "Failed to allocate Marker IOCB.\n");
1da177e4
LT
497
498 return (QLA_FUNCTION_FAILED);
499 }
500
2b6c0cee
AV
501 mrk->entry_type = MARKER_TYPE;
502 mrk->modifier = type;
1da177e4 503 if (type != MK_SYNC_ALL) {
e428924c 504 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
505 mrk24 = (struct mrk_entry_24xx *) mrk;
506 mrk24->nport_handle = cpu_to_le16(loop_id);
507 mrk24->lun[1] = LSB(lun);
508 mrk24->lun[2] = MSB(lun);
b797b6de 509 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
e315cd28 510 mrk24->vp_index = vha->vp_idx;
2afa19a9 511 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
2b6c0cee
AV
512 } else {
513 SET_TARGET_ID(ha, mrk->target, loop_id);
514 mrk->lun = cpu_to_le16(lun);
515 }
1da177e4
LT
516 }
517 wmb();
518
73208dfd 519 qla2x00_isp_cmd(vha, req);
1da177e4
LT
520
521 return (QLA_SUCCESS);
522}
523
fa2a1ce5 524int
73208dfd
AC
525qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
526 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
527 uint8_t type)
1da177e4
LT
528{
529 int ret;
530 unsigned long flags = 0;
531
73208dfd
AC
532 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
533 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
534 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
1da177e4
LT
535
536 return (ret);
537}
538
1da177e4
LT
539/**
540 * qla2x00_isp_cmd() - Modify the request ring pointer.
541 * @ha: HA context
542 *
543 * Note: The caller must hold the hardware lock before calling this routine.
544 */
413975a0 545static void
73208dfd 546qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
1da177e4 547{
e315cd28 548 struct qla_hw_data *ha = vha->hw;
73208dfd 549 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
17d98630 550 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1da177e4 551
7c3df132
SK
552 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x302d,
553 "IOCB data:\n");
554 ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302e,
555 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE);
1da177e4
LT
556
557 /* Adjust ring index. */
e315cd28
AC
558 req->ring_index++;
559 if (req->ring_index == req->length) {
560 req->ring_index = 0;
561 req->ring_ptr = req->ring;
1da177e4 562 } else
e315cd28 563 req->ring_ptr++;
1da177e4
LT
564
565 /* Set chip new ring index. */
a9083016
GM
566 if (IS_QLA82XX(ha)) {
567 uint32_t dbval = 0x04 | (ha->portnum << 5);
568
569 /* write, read and verify logic */
570 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
571 if (ql2xdbwr)
572 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
573 else {
574 WRT_REG_DWORD(
575 (unsigned long __iomem *)ha->nxdb_wr_ptr,
576 dbval);
577 wmb();
578 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
579 WRT_REG_DWORD((unsigned long __iomem *)
580 ha->nxdb_wr_ptr, dbval);
581 wmb();
582 }
583 }
584 } else if (ha->mqenable) {
585 /* Set chip new ring index. */
17d98630
AC
586 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
587 RD_REG_DWORD(&ioreg->hccr);
a9083016 588 } else {
73208dfd
AC
589 if (IS_FWI2_CAPABLE(ha)) {
590 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
591 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
592 } else {
593 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
594 req->ring_index);
595 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
596 }
2b6c0cee
AV
597 }
598
599}
600
601/**
602 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
603 * Continuation Type 1 IOCBs to allocate.
604 *
605 * @dsds: number of data segment decriptors needed
606 *
607 * Returns the number of IOCB entries needed to store @dsds.
608 */
a9083016 609inline uint16_t
7c3df132 610qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
2b6c0cee
AV
611{
612 uint16_t iocbs;
613
614 iocbs = 1;
615 if (dsds > 1) {
616 iocbs += (dsds - 1) / 5;
617 if ((dsds - 1) % 5)
618 iocbs++;
619 }
620 return iocbs;
621}
622
623/**
624 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
625 * IOCB types.
626 *
627 * @sp: SRB command to process
628 * @cmd_pkt: Command type 3 IOCB
629 * @tot_dsds: Total number of segments to transfer
630 */
a9083016 631inline void
2b6c0cee
AV
632qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
633 uint16_t tot_dsds)
634{
635 uint16_t avail_dsds;
636 uint32_t *cur_dsd;
e315cd28 637 scsi_qla_host_t *vha;
2b6c0cee 638 struct scsi_cmnd *cmd;
385d70b4
FT
639 struct scatterlist *sg;
640 int i;
73208dfd 641 struct req_que *req;
2b6c0cee
AV
642
643 cmd = sp->cmd;
644
645 /* Update entry type to indicate Command Type 3 IOCB */
646 *((uint32_t *)(&cmd_pkt->entry_type)) =
647 __constant_cpu_to_le32(COMMAND_TYPE_7);
648
649 /* No data transfer */
385d70b4 650 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
2b6c0cee
AV
651 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
652 return;
653 }
654
444786d7 655 vha = sp->fcport->vha;
67c2e93a 656 req = vha->req;
2b6c0cee
AV
657
658 /* Set transfer direction */
49fd462a 659 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2b6c0cee
AV
660 cmd_pkt->task_mgmt_flags =
661 __constant_cpu_to_le16(TMF_WRITE_DATA);
e315cd28 662 sp->fcport->vha->hw->qla_stats.output_bytes +=
49fd462a
HZ
663 scsi_bufflen(sp->cmd);
664 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
2b6c0cee
AV
665 cmd_pkt->task_mgmt_flags =
666 __constant_cpu_to_le16(TMF_READ_DATA);
e315cd28 667 sp->fcport->vha->hw->qla_stats.input_bytes +=
49fd462a
HZ
668 scsi_bufflen(sp->cmd);
669 }
2b6c0cee
AV
670
671 /* One DSD is available in the Command Type 3 IOCB */
672 avail_dsds = 1;
673 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
674
675 /* Load data segments */
385d70b4
FT
676
677 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
678 dma_addr_t sle_dma;
679 cont_a64_entry_t *cont_pkt;
680
681 /* Allocate additional continuation packets? */
682 if (avail_dsds == 0) {
683 /*
684 * Five DSDs are available in the Continuation
685 * Type 1 IOCB.
686 */
67c2e93a 687 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
385d70b4
FT
688 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
689 avail_dsds = 5;
2b6c0cee 690 }
385d70b4
FT
691
692 sle_dma = sg_dma_address(sg);
693 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
694 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
695 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
696 avail_dsds--;
2b6c0cee
AV
697 }
698}
699
bad75002
AE
700struct fw_dif_context {
701 uint32_t ref_tag;
702 uint16_t app_tag;
703 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
704 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
705};
706
707/*
708 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
709 *
710 */
711static inline void
712qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
713 unsigned int protcnt)
714{
715 struct sd_dif_tuple *spt;
7c3df132 716 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bad75002
AE
717 unsigned char op = scsi_get_prot_op(cmd);
718
719 switch (scsi_get_prot_type(cmd)) {
bad75002 720 case SCSI_PROT_DIF_TYPE0:
8cb2049c
AE
721 /*
722 * No check for ql2xenablehba_err_chk, as it would be an
723 * I/O error if hba tag generation is not done.
724 */
725 pkt->ref_tag = cpu_to_le32((uint32_t)
726 (0xffffffff & scsi_get_lba(cmd)));
727 pkt->ref_tag_mask[0] = 0xff;
728 pkt->ref_tag_mask[1] = 0xff;
729 pkt->ref_tag_mask[2] = 0xff;
730 pkt->ref_tag_mask[3] = 0xff;
bad75002
AE
731 break;
732
733 /*
734 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
735 * match LBA in CDB + N
736 */
737 case SCSI_PROT_DIF_TYPE2:
8cb2049c 738 if (!qla2x00_hba_err_chk_enabled(op))
0c470874
AE
739 break;
740
741 if (scsi_prot_sg_count(cmd)) {
742 spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
743 scsi_prot_sglist(cmd)[0].offset;
744 pkt->app_tag = swab32(spt->app_tag);
745 pkt->app_tag_mask[0] = 0xff;
746 pkt->app_tag_mask[1] = 0xff;
747 }
748
749 pkt->ref_tag = cpu_to_le32((uint32_t)
750 (0xffffffff & scsi_get_lba(cmd)));
751
752 /* enable ALL bytes of the ref tag */
753 pkt->ref_tag_mask[0] = 0xff;
754 pkt->ref_tag_mask[1] = 0xff;
755 pkt->ref_tag_mask[2] = 0xff;
756 pkt->ref_tag_mask[3] = 0xff;
bad75002
AE
757 break;
758
759 /* For Type 3 protection: 16 bit GUARD only */
760 case SCSI_PROT_DIF_TYPE3:
761 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
762 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
763 0x00;
764 break;
765
766 /*
767 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
768 * 16 bit app tag.
769 */
770 case SCSI_PROT_DIF_TYPE1:
8cb2049c 771 if (!qla2x00_hba_err_chk_enabled(op))
bad75002
AE
772 break;
773
774 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
775 op == SCSI_PROT_WRITE_PASS)) {
776 spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
777 scsi_prot_sglist(cmd)[0].offset;
7c3df132
SK
778 ql_dbg(ql_dbg_io, vha, 0x3008,
779 "LBA from user %p, lba = 0x%x for cmd=%p.\n",
780 spt, (int)spt->ref_tag, cmd);
bad75002
AE
781 pkt->ref_tag = swab32(spt->ref_tag);
782 pkt->app_tag_mask[0] = 0x0;
783 pkt->app_tag_mask[1] = 0x0;
784 } else {
785 pkt->ref_tag = cpu_to_le32((uint32_t)
786 (0xffffffff & scsi_get_lba(cmd)));
787 pkt->app_tag = __constant_cpu_to_le16(0);
788 pkt->app_tag_mask[0] = 0x0;
789 pkt->app_tag_mask[1] = 0x0;
790 }
791 /* enable ALL bytes of the ref tag */
792 pkt->ref_tag_mask[0] = 0xff;
793 pkt->ref_tag_mask[1] = 0xff;
794 pkt->ref_tag_mask[2] = 0xff;
795 pkt->ref_tag_mask[3] = 0xff;
796 break;
797 }
798
7c3df132
SK
799 ql_dbg(ql_dbg_io, vha, 0x3009,
800 "Setting protection Tags: (BIG) ref tag = 0x%x, app tag = 0x%x, "
801 "prot SG count %d, cmd lba 0x%x, prot_type=%u cmd=%p.\n",
802 pkt->ref_tag, pkt->app_tag, protcnt, (int)scsi_get_lba(cmd),
803 scsi_get_prot_type(cmd), cmd);
bad75002
AE
804}
805
8cb2049c
AE
806struct qla2_sgx {
807 dma_addr_t dma_addr; /* OUT */
808 uint32_t dma_len; /* OUT */
809
810 uint32_t tot_bytes; /* IN */
811 struct scatterlist *cur_sg; /* IN */
812
813 /* for book keeping, bzero on initial invocation */
814 uint32_t bytes_consumed;
815 uint32_t num_bytes;
816 uint32_t tot_partial;
817
818 /* for debugging */
819 uint32_t num_sg;
820 srb_t *sp;
821};
822
823static int
824qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
825 uint32_t *partial)
826{
827 struct scatterlist *sg;
828 uint32_t cumulative_partial, sg_len;
829 dma_addr_t sg_dma_addr;
830
831 if (sgx->num_bytes == sgx->tot_bytes)
832 return 0;
833
834 sg = sgx->cur_sg;
835 cumulative_partial = sgx->tot_partial;
836
837 sg_dma_addr = sg_dma_address(sg);
838 sg_len = sg_dma_len(sg);
839
840 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
841
842 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
843 sgx->dma_len = (blk_sz - cumulative_partial);
844 sgx->tot_partial = 0;
845 sgx->num_bytes += blk_sz;
846 *partial = 0;
847 } else {
848 sgx->dma_len = sg_len - sgx->bytes_consumed;
849 sgx->tot_partial += sgx->dma_len;
850 *partial = 1;
851 }
852
853 sgx->bytes_consumed += sgx->dma_len;
854
855 if (sg_len == sgx->bytes_consumed) {
856 sg = sg_next(sg);
857 sgx->num_sg++;
858 sgx->cur_sg = sg;
859 sgx->bytes_consumed = 0;
860 }
861
862 return 1;
863}
864
865static int
866qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
867 uint32_t *dsd, uint16_t tot_dsds)
868{
869 void *next_dsd;
870 uint8_t avail_dsds = 0;
871 uint32_t dsd_list_len;
872 struct dsd_dma *dsd_ptr;
873 struct scatterlist *sg_prot;
874 uint32_t *cur_dsd = dsd;
875 uint16_t used_dsds = tot_dsds;
876
877 uint32_t prot_int;
878 uint32_t partial;
879 struct qla2_sgx sgx;
880 dma_addr_t sle_dma;
881 uint32_t sle_dma_len, tot_prot_dma_len = 0;
882 struct scsi_cmnd *cmd = sp->cmd;
883
884 prot_int = cmd->device->sector_size;
885
886 memset(&sgx, 0, sizeof(struct qla2_sgx));
887 sgx.tot_bytes = scsi_bufflen(sp->cmd);
888 sgx.cur_sg = scsi_sglist(sp->cmd);
889 sgx.sp = sp;
890
891 sg_prot = scsi_prot_sglist(sp->cmd);
892
893 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
894
895 sle_dma = sgx.dma_addr;
896 sle_dma_len = sgx.dma_len;
897alloc_and_fill:
898 /* Allocate additional continuation packets? */
899 if (avail_dsds == 0) {
900 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
901 QLA_DSDS_PER_IOCB : used_dsds;
902 dsd_list_len = (avail_dsds + 1) * 12;
903 used_dsds -= avail_dsds;
904
905 /* allocate tracking DS */
906 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
907 if (!dsd_ptr)
908 return 1;
909
910 /* allocate new list */
911 dsd_ptr->dsd_addr = next_dsd =
912 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
913 &dsd_ptr->dsd_list_dma);
914
915 if (!next_dsd) {
916 /*
917 * Need to cleanup only this dsd_ptr, rest
918 * will be done by sp_free_dma()
919 */
920 kfree(dsd_ptr);
921 return 1;
922 }
923
924 list_add_tail(&dsd_ptr->list,
925 &((struct crc_context *)sp->ctx)->dsd_list);
926
927 sp->flags |= SRB_CRC_CTX_DSD_VALID;
928
929 /* add new list to cmd iocb or last list */
930 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
931 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
932 *cur_dsd++ = dsd_list_len;
933 cur_dsd = (uint32_t *)next_dsd;
934 }
935 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
936 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
937 *cur_dsd++ = cpu_to_le32(sle_dma_len);
938 avail_dsds--;
939
940 if (partial == 0) {
941 /* Got a full protection interval */
942 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
943 sle_dma_len = 8;
bad75002 944
8cb2049c
AE
945 tot_prot_dma_len += sle_dma_len;
946 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
947 tot_prot_dma_len = 0;
948 sg_prot = sg_next(sg_prot);
949 }
950
951 partial = 1; /* So as to not re-enter this block */
952 goto alloc_and_fill;
953 }
954 }
955 /* Null termination */
956 *cur_dsd++ = 0;
957 *cur_dsd++ = 0;
958 *cur_dsd++ = 0;
959 return 0;
960}
bad75002
AE
961static int
962qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
963 uint16_t tot_dsds)
964{
965 void *next_dsd;
966 uint8_t avail_dsds = 0;
967 uint32_t dsd_list_len;
968 struct dsd_dma *dsd_ptr;
969 struct scatterlist *sg;
970 uint32_t *cur_dsd = dsd;
971 int i;
972 uint16_t used_dsds = tot_dsds;
7c3df132 973 scsi_qla_host_t *vha = shost_priv(sp->cmd->device->host);
bad75002
AE
974
975 uint8_t *cp;
976
977 scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
978 dma_addr_t sle_dma;
979
980 /* Allocate additional continuation packets? */
981 if (avail_dsds == 0) {
982 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
983 QLA_DSDS_PER_IOCB : used_dsds;
984 dsd_list_len = (avail_dsds + 1) * 12;
985 used_dsds -= avail_dsds;
986
987 /* allocate tracking DS */
988 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
989 if (!dsd_ptr)
990 return 1;
991
992 /* allocate new list */
993 dsd_ptr->dsd_addr = next_dsd =
994 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
995 &dsd_ptr->dsd_list_dma);
996
997 if (!next_dsd) {
998 /*
999 * Need to cleanup only this dsd_ptr, rest
1000 * will be done by sp_free_dma()
1001 */
1002 kfree(dsd_ptr);
1003 return 1;
1004 }
1005
1006 list_add_tail(&dsd_ptr->list,
1007 &((struct crc_context *)sp->ctx)->dsd_list);
1008
1009 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1010
1011 /* add new list to cmd iocb or last list */
1012 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1013 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1014 *cur_dsd++ = dsd_list_len;
1015 cur_dsd = (uint32_t *)next_dsd;
1016 }
1017 sle_dma = sg_dma_address(sg);
7c3df132
SK
1018 ql_dbg(ql_dbg_io, vha, 0x300a,
1019 "sg entry %d - addr=0x%x 0x%x, " "len=%d for cmd=%p.\n",
1020 cur_dsd, i, LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg),
1021 sp->cmd);
bad75002
AE
1022 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1023 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1024 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1025 avail_dsds--;
1026
1027 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
1028 cp = page_address(sg_page(sg)) + sg->offset;
7c3df132
SK
1029 ql_dbg(ql_dbg_io, vha, 0x300b,
1030 "User data buffer=%p for cmd=%p.\n", cp, sp->cmd);
bad75002
AE
1031 }
1032 }
1033 /* Null termination */
1034 *cur_dsd++ = 0;
1035 *cur_dsd++ = 0;
1036 *cur_dsd++ = 0;
1037 return 0;
1038}
1039
1040static int
1041qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1042 uint32_t *dsd,
1043 uint16_t tot_dsds)
1044{
1045 void *next_dsd;
1046 uint8_t avail_dsds = 0;
1047 uint32_t dsd_list_len;
1048 struct dsd_dma *dsd_ptr;
1049 struct scatterlist *sg;
1050 int i;
1051 struct scsi_cmnd *cmd;
1052 uint32_t *cur_dsd = dsd;
1053 uint16_t used_dsds = tot_dsds;
7c3df132 1054 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
bad75002
AE
1055 uint8_t *cp;
1056
1057
1058 cmd = sp->cmd;
1059 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1060 dma_addr_t sle_dma;
1061
1062 /* Allocate additional continuation packets? */
1063 if (avail_dsds == 0) {
1064 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1065 QLA_DSDS_PER_IOCB : used_dsds;
1066 dsd_list_len = (avail_dsds + 1) * 12;
1067 used_dsds -= avail_dsds;
1068
1069 /* allocate tracking DS */
1070 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1071 if (!dsd_ptr)
1072 return 1;
1073
1074 /* allocate new list */
1075 dsd_ptr->dsd_addr = next_dsd =
1076 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1077 &dsd_ptr->dsd_list_dma);
1078
1079 if (!next_dsd) {
1080 /*
1081 * Need to cleanup only this dsd_ptr, rest
1082 * will be done by sp_free_dma()
1083 */
1084 kfree(dsd_ptr);
1085 return 1;
1086 }
1087
1088 list_add_tail(&dsd_ptr->list,
1089 &((struct crc_context *)sp->ctx)->dsd_list);
1090
1091 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1092
1093 /* add new list to cmd iocb or last list */
1094 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1095 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1096 *cur_dsd++ = dsd_list_len;
1097 cur_dsd = (uint32_t *)next_dsd;
1098 }
1099 sle_dma = sg_dma_address(sg);
1100 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
7c3df132
SK
1101 ql_dbg(ql_dbg_io, vha, 0x3027,
1102 "%s(): %p, sg_entry %d - "
1103 "addr=0x%x0x%x, len=%d.\n",
1104 __func__, cur_dsd, i,
1105 LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg));
bad75002
AE
1106 }
1107 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1108 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1109 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1110
1111 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
1112 cp = page_address(sg_page(sg)) + sg->offset;
7c3df132
SK
1113 ql_dbg(ql_dbg_io, vha, 0x3028,
1114 "%s(): Protection Data buffer = %p.\n", __func__,
1115 cp);
bad75002
AE
1116 }
1117 avail_dsds--;
1118 }
1119 /* Null termination */
1120 *cur_dsd++ = 0;
1121 *cur_dsd++ = 0;
1122 *cur_dsd++ = 0;
1123 return 0;
1124}
1125
1126/**
1127 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1128 * Type 6 IOCB types.
1129 *
1130 * @sp: SRB command to process
1131 * @cmd_pkt: Command type 3 IOCB
1132 * @tot_dsds: Total number of segments to transfer
1133 */
1134static inline int
1135qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1136 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1137{
1138 uint32_t *cur_dsd, *fcp_dl;
1139 scsi_qla_host_t *vha;
1140 struct scsi_cmnd *cmd;
1141 struct scatterlist *cur_seg;
1142 int sgc;
8cb2049c 1143 uint32_t total_bytes = 0;
bad75002
AE
1144 uint32_t data_bytes;
1145 uint32_t dif_bytes;
1146 uint8_t bundling = 1;
1147 uint16_t blk_size;
1148 uint8_t *clr_ptr;
1149 struct crc_context *crc_ctx_pkt = NULL;
1150 struct qla_hw_data *ha;
1151 uint8_t additional_fcpcdb_len;
1152 uint16_t fcp_cmnd_len;
1153 struct fcp_cmnd *fcp_cmnd;
1154 dma_addr_t crc_ctx_dma;
ff2fc42e 1155 char tag[2];
bad75002
AE
1156
1157 cmd = sp->cmd;
1158
1159 sgc = 0;
1160 /* Update entry type to indicate Command Type CRC_2 IOCB */
1161 *((uint32_t *)(&cmd_pkt->entry_type)) =
1162 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1163
7c3df132
SK
1164 vha = sp->fcport->vha;
1165 ha = vha->hw;
1166
bad75002
AE
1167 /* No data transfer */
1168 data_bytes = scsi_bufflen(cmd);
1169 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
bad75002
AE
1170 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1171 return QLA_SUCCESS;
1172 }
1173
bad75002
AE
1174 cmd_pkt->vp_index = sp->fcport->vp_idx;
1175
1176 /* Set transfer direction */
1177 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1178 cmd_pkt->control_flags =
1179 __constant_cpu_to_le16(CF_WRITE_DATA);
1180 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1181 cmd_pkt->control_flags =
1182 __constant_cpu_to_le16(CF_READ_DATA);
1183 }
1184
8cb2049c
AE
1185 if ((scsi_get_prot_op(sp->cmd) == SCSI_PROT_READ_INSERT) ||
1186 (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_STRIP) ||
1187 (scsi_get_prot_op(sp->cmd) == SCSI_PROT_READ_STRIP) ||
1188 (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_INSERT))
bad75002
AE
1189 bundling = 0;
1190
1191 /* Allocate CRC context from global pool */
1192 crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
1193 GFP_ATOMIC, &crc_ctx_dma);
1194
1195 if (!crc_ctx_pkt)
1196 goto crc_queuing_error;
1197
1198 /* Zero out CTX area. */
1199 clr_ptr = (uint8_t *)crc_ctx_pkt;
1200 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1201
1202 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1203
1204 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1205
1206 /* Set handle */
1207 crc_ctx_pkt->handle = cmd_pkt->handle;
1208
1209 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1210
1211 qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
1212 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1213
1214 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1215 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1216 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1217
1218 /* Determine SCSI command length -- align to 4 byte boundary */
1219 if (cmd->cmd_len > 16) {
bad75002
AE
1220 additional_fcpcdb_len = cmd->cmd_len - 16;
1221 if ((cmd->cmd_len % 4) != 0) {
1222 /* SCSI cmd > 16 bytes must be multiple of 4 */
1223 goto crc_queuing_error;
1224 }
1225 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1226 } else {
1227 additional_fcpcdb_len = 0;
1228 fcp_cmnd_len = 12 + 16 + 4;
1229 }
1230
1231 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1232
1233 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1234 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1235 fcp_cmnd->additional_cdb_len |= 1;
1236 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1237 fcp_cmnd->additional_cdb_len |= 2;
1238
1239 int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
85727e1f 1240 host_to_fcp_swap((uint8_t *)&fcp_cmnd->lun, sizeof(fcp_cmnd->lun));
bad75002
AE
1241 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1242 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1243 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1244 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1245 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1246 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
65155b37 1247 fcp_cmnd->task_management = 0;
bad75002 1248
ff2fc42e
AV
1249 /*
1250 * Update tagged queuing modifier if using command tag queuing
1251 */
1252 if (scsi_populate_tag_msg(cmd, tag)) {
1253 switch (tag[0]) {
1254 case HEAD_OF_QUEUE_TAG:
1255 fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1256 break;
1257 case ORDERED_QUEUE_TAG:
1258 fcp_cmnd->task_attribute = TSK_ORDERED;
1259 break;
1260 default:
1261 fcp_cmnd->task_attribute = 0;
1262 break;
1263 }
1264 } else {
1265 fcp_cmnd->task_attribute = 0;
1266 }
1267
bad75002
AE
1268 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1269
bad75002 1270 /* Compute dif len and adjust data len to incude protection */
bad75002
AE
1271 dif_bytes = 0;
1272 blk_size = cmd->device->sector_size;
8cb2049c
AE
1273 dif_bytes = (data_bytes / blk_size) * 8;
1274
1275 switch (scsi_get_prot_op(sp->cmd)) {
1276 case SCSI_PROT_READ_INSERT:
1277 case SCSI_PROT_WRITE_STRIP:
1278 total_bytes = data_bytes;
1279 data_bytes += dif_bytes;
1280 break;
1281
1282 case SCSI_PROT_READ_STRIP:
1283 case SCSI_PROT_WRITE_INSERT:
1284 case SCSI_PROT_READ_PASS:
1285 case SCSI_PROT_WRITE_PASS:
1286 total_bytes = data_bytes + dif_bytes;
1287 break;
1288 default:
1289 BUG();
bad75002
AE
1290 }
1291
8cb2049c 1292 if (!qla2x00_hba_err_chk_enabled(scsi_get_prot_op(cmd)))
bad75002
AE
1293 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1294
1295 if (!bundling) {
1296 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1297 } else {
1298 /*
1299 * Configure Bundling if we need to fetch interlaving
1300 * protection PCI accesses
1301 */
1302 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1303 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1304 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1305 tot_prot_dsds);
1306 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1307 }
1308
1309 /* Finish the common fields of CRC pkt */
1310 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1311 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1312 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1313 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1314 /* Fibre channel byte count */
1315 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1316 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1317 additional_fcpcdb_len);
1318 *fcp_dl = htonl(total_bytes);
1319
0c470874 1320 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
0c470874
AE
1321 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1322 return QLA_SUCCESS;
1323 }
bad75002
AE
1324 /* Walks data segments */
1325
1326 cmd_pkt->control_flags |=
1327 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
8cb2049c
AE
1328
1329 if (!bundling && tot_prot_dsds) {
1330 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1331 cur_dsd, tot_dsds))
1332 goto crc_queuing_error;
1333 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
bad75002
AE
1334 (tot_dsds - tot_prot_dsds)))
1335 goto crc_queuing_error;
1336
1337 if (bundling && tot_prot_dsds) {
1338 /* Walks dif segments */
1339 cur_seg = scsi_prot_sglist(cmd);
1340 cmd_pkt->control_flags |=
1341 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1342 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1343 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1344 tot_prot_dsds))
1345 goto crc_queuing_error;
1346 }
1347 return QLA_SUCCESS;
1348
1349crc_queuing_error:
bad75002
AE
1350 /* Cleanup will be performed by the caller */
1351
1352 return QLA_FUNCTION_FAILED;
1353}
2b6c0cee
AV
1354
1355/**
1356 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1357 * @sp: command to send to the ISP
1358 *
cc3ef7bc 1359 * Returns non-zero if a failure occurred, else zero.
2b6c0cee
AV
1360 */
1361int
1362qla24xx_start_scsi(srb_t *sp)
1363{
385d70b4 1364 int ret, nseg;
2b6c0cee 1365 unsigned long flags;
2b6c0cee
AV
1366 uint32_t *clr_ptr;
1367 uint32_t index;
1368 uint32_t handle;
1369 struct cmd_type_7 *cmd_pkt;
2b6c0cee
AV
1370 uint16_t cnt;
1371 uint16_t req_cnt;
1372 uint16_t tot_dsds;
73208dfd
AC
1373 struct req_que *req = NULL;
1374 struct rsp_que *rsp = NULL;
1375 struct scsi_cmnd *cmd = sp->cmd;
444786d7 1376 struct scsi_qla_host *vha = sp->fcport->vha;
73208dfd 1377 struct qla_hw_data *ha = vha->hw;
ff2fc42e 1378 char tag[2];
2b6c0cee
AV
1379
1380 /* Setup device pointers. */
1381 ret = 0;
73208dfd 1382
59e0b8b0
AC
1383 qla25xx_set_que(sp, &rsp);
1384 req = vha->req;
73208dfd 1385
2b6c0cee
AV
1386 /* So we know we haven't pci_map'ed anything yet */
1387 tot_dsds = 0;
1388
1389 /* Send marker if required */
e315cd28 1390 if (vha->marker_needed != 0) {
7c3df132
SK
1391 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1392 QLA_SUCCESS)
2b6c0cee 1393 return QLA_FUNCTION_FAILED;
e315cd28 1394 vha->marker_needed = 0;
2b6c0cee
AV
1395 }
1396
1397 /* Acquire ring specific lock */
e315cd28 1398 spin_lock_irqsave(&ha->hardware_lock, flags);
2b6c0cee
AV
1399
1400 /* Check for room in outstanding command list. */
e315cd28 1401 handle = req->current_outstanding_cmd;
2b6c0cee
AV
1402 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1403 handle++;
1404 if (handle == MAX_OUTSTANDING_COMMANDS)
1405 handle = 1;
e315cd28 1406 if (!req->outstanding_cmds[handle])
2b6c0cee
AV
1407 break;
1408 }
7c3df132 1409 if (index == MAX_OUTSTANDING_COMMANDS) {
2b6c0cee 1410 goto queuing_error;
7c3df132 1411 }
2b6c0cee
AV
1412
1413 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
1414 if (scsi_sg_count(cmd)) {
1415 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1416 scsi_sg_count(cmd), cmd->sc_data_direction);
1417 if (unlikely(!nseg))
2b6c0cee 1418 goto queuing_error;
2c3dfe3f
SJ
1419 } else
1420 nseg = 0;
1421
385d70b4 1422 tot_dsds = nseg;
7c3df132 1423 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
e315cd28 1424 if (req->cnt < (req_cnt + 2)) {
08029990 1425 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
73208dfd 1426
e315cd28
AC
1427 if (req->ring_index < cnt)
1428 req->cnt = cnt - req->ring_index;
2b6c0cee 1429 else
e315cd28
AC
1430 req->cnt = req->length -
1431 (req->ring_index - cnt);
2b6c0cee 1432 }
e315cd28 1433 if (req->cnt < (req_cnt + 2))
2b6c0cee 1434 goto queuing_error;
2b6c0cee
AV
1435
1436 /* Build command packet. */
e315cd28
AC
1437 req->current_outstanding_cmd = handle;
1438 req->outstanding_cmds[handle] = sp;
cf53b069 1439 sp->handle = handle;
2b6c0cee 1440 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
e315cd28 1441 req->cnt -= req_cnt;
2b6c0cee 1442
e315cd28 1443 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2afa19a9 1444 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2b6c0cee
AV
1445
1446 /* Zero out remaining portion of packet. */
72df8325 1447 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2b6c0cee
AV
1448 clr_ptr = (uint32_t *)cmd_pkt + 2;
1449 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1450 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1451
1452 /* Set NPORT-ID and LUN number*/
1453 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1454 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1455 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1456 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2c3dfe3f 1457 cmd_pkt->vp_index = sp->fcport->vp_idx;
2b6c0cee 1458
661c3f6c 1459 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
0d4be124 1460 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2b6c0cee 1461
ff2fc42e
AV
1462 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
1463 if (scsi_populate_tag_msg(cmd, tag)) {
1464 switch (tag[0]) {
1465 case HEAD_OF_QUEUE_TAG:
1466 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
1467 break;
1468 case ORDERED_QUEUE_TAG:
1469 cmd_pkt->task = TSK_ORDERED;
1470 break;
1471 }
1472 }
1473
2b6c0cee
AV
1474 /* Load SCSI command packet. */
1475 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1476 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1477
385d70b4 1478 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2b6c0cee
AV
1479
1480 /* Build IOCB segments */
1481 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1482
1483 /* Set total data segment count. */
1484 cmd_pkt->entry_count = (uint8_t)req_cnt;
2afa19a9
AC
1485 /* Specify response queue number where completion should happen */
1486 cmd_pkt->entry_status = (uint8_t) rsp->id;
2b6c0cee 1487 wmb();
2b6c0cee 1488 /* Adjust ring index. */
e315cd28
AC
1489 req->ring_index++;
1490 if (req->ring_index == req->length) {
1491 req->ring_index = 0;
1492 req->ring_ptr = req->ring;
2b6c0cee 1493 } else
e315cd28 1494 req->ring_ptr++;
2b6c0cee
AV
1495
1496 sp->flags |= SRB_DMA_VALID;
2b6c0cee
AV
1497
1498 /* Set chip new ring index. */
08029990
AV
1499 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1500 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
2b6c0cee 1501
4fdfefe5 1502 /* Manage unprocessed RIO/ZIO commands in response queue. */
e315cd28 1503 if (vha->flags.process_response_queue &&
73208dfd 1504 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2afa19a9 1505 qla24xx_process_response_queue(vha, rsp);
4fdfefe5 1506
e315cd28 1507 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2b6c0cee
AV
1508 return QLA_SUCCESS;
1509
1510queuing_error:
385d70b4
FT
1511 if (tot_dsds)
1512 scsi_dma_unmap(cmd);
1513
e315cd28 1514 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2b6c0cee
AV
1515
1516 return QLA_FUNCTION_FAILED;
1da177e4 1517}
68ca949c 1518
bad75002
AE
1519
1520/**
1521 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1522 * @sp: command to send to the ISP
1523 *
1524 * Returns non-zero if a failure occurred, else zero.
1525 */
1526int
1527qla24xx_dif_start_scsi(srb_t *sp)
1528{
1529 int nseg;
1530 unsigned long flags;
1531 uint32_t *clr_ptr;
1532 uint32_t index;
1533 uint32_t handle;
1534 uint16_t cnt;
1535 uint16_t req_cnt = 0;
1536 uint16_t tot_dsds;
1537 uint16_t tot_prot_dsds;
1538 uint16_t fw_prot_opts = 0;
1539 struct req_que *req = NULL;
1540 struct rsp_que *rsp = NULL;
1541 struct scsi_cmnd *cmd = sp->cmd;
1542 struct scsi_qla_host *vha = sp->fcport->vha;
1543 struct qla_hw_data *ha = vha->hw;
1544 struct cmd_type_crc_2 *cmd_pkt;
1545 uint32_t status = 0;
1546
1547#define QDSS_GOT_Q_SPACE BIT_0
1548
0c470874
AE
1549 /* Only process protection or >16 cdb in this routine */
1550 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1551 if (cmd->cmd_len <= 16)
1552 return qla24xx_start_scsi(sp);
1553 }
bad75002
AE
1554
1555 /* Setup device pointers. */
1556
1557 qla25xx_set_que(sp, &rsp);
1558 req = vha->req;
1559
1560 /* So we know we haven't pci_map'ed anything yet */
1561 tot_dsds = 0;
1562
1563 /* Send marker if required */
1564 if (vha->marker_needed != 0) {
1565 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1566 QLA_SUCCESS)
1567 return QLA_FUNCTION_FAILED;
1568 vha->marker_needed = 0;
1569 }
1570
1571 /* Acquire ring specific lock */
1572 spin_lock_irqsave(&ha->hardware_lock, flags);
1573
1574 /* Check for room in outstanding command list. */
1575 handle = req->current_outstanding_cmd;
1576 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1577 handle++;
1578 if (handle == MAX_OUTSTANDING_COMMANDS)
1579 handle = 1;
1580 if (!req->outstanding_cmds[handle])
1581 break;
1582 }
1583
1584 if (index == MAX_OUTSTANDING_COMMANDS)
1585 goto queuing_error;
1586
1587 /* Compute number of required data segments */
1588 /* Map the sg table so we have an accurate count of sg entries needed */
1589 if (scsi_sg_count(cmd)) {
1590 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1591 scsi_sg_count(cmd), cmd->sc_data_direction);
1592 if (unlikely(!nseg))
1593 goto queuing_error;
1594 else
1595 sp->flags |= SRB_DMA_VALID;
8cb2049c
AE
1596
1597 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1598 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1599 struct qla2_sgx sgx;
1600 uint32_t partial;
1601
1602 memset(&sgx, 0, sizeof(struct qla2_sgx));
1603 sgx.tot_bytes = scsi_bufflen(cmd);
1604 sgx.cur_sg = scsi_sglist(cmd);
1605 sgx.sp = sp;
1606
1607 nseg = 0;
1608 while (qla24xx_get_one_block_sg(
1609 cmd->device->sector_size, &sgx, &partial))
1610 nseg++;
1611 }
bad75002
AE
1612 } else
1613 nseg = 0;
1614
1615 /* number of required data segments */
1616 tot_dsds = nseg;
1617
1618 /* Compute number of required protection segments */
1619 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1620 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1621 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1622 if (unlikely(!nseg))
1623 goto queuing_error;
1624 else
1625 sp->flags |= SRB_CRC_PROT_DMA_VALID;
8cb2049c
AE
1626
1627 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1628 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1629 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1630 }
bad75002
AE
1631 } else {
1632 nseg = 0;
1633 }
1634
1635 req_cnt = 1;
1636 /* Total Data and protection sg segment(s) */
1637 tot_prot_dsds = nseg;
1638 tot_dsds += nseg;
1639 if (req->cnt < (req_cnt + 2)) {
1640 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1641
1642 if (req->ring_index < cnt)
1643 req->cnt = cnt - req->ring_index;
1644 else
1645 req->cnt = req->length -
1646 (req->ring_index - cnt);
1647 }
1648
1649 if (req->cnt < (req_cnt + 2))
1650 goto queuing_error;
1651
1652 status |= QDSS_GOT_Q_SPACE;
1653
1654 /* Build header part of command packet (excluding the OPCODE). */
1655 req->current_outstanding_cmd = handle;
1656 req->outstanding_cmds[handle] = sp;
8cb2049c 1657 sp->handle = handle;
bad75002
AE
1658 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1659 req->cnt -= req_cnt;
1660
1661 /* Fill-in common area */
1662 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1663 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1664
1665 clr_ptr = (uint32_t *)cmd_pkt + 2;
1666 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1667
1668 /* Set NPORT-ID and LUN number*/
1669 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1670 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1671 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1672 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1673
1674 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
1675 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1676
1677 /* Total Data and protection segment(s) */
1678 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1679
1680 /* Build IOCB segments and adjust for data protection segments */
1681 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1682 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1683 QLA_SUCCESS)
1684 goto queuing_error;
1685
1686 cmd_pkt->entry_count = (uint8_t)req_cnt;
1687 /* Specify response queue number where completion should happen */
1688 cmd_pkt->entry_status = (uint8_t) rsp->id;
1689 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1690 wmb();
1691
1692 /* Adjust ring index. */
1693 req->ring_index++;
1694 if (req->ring_index == req->length) {
1695 req->ring_index = 0;
1696 req->ring_ptr = req->ring;
1697 } else
1698 req->ring_ptr++;
1699
1700 /* Set chip new ring index. */
1701 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1702 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1703
1704 /* Manage unprocessed RIO/ZIO commands in response queue. */
1705 if (vha->flags.process_response_queue &&
1706 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1707 qla24xx_process_response_queue(vha, rsp);
1708
1709 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1710
1711 return QLA_SUCCESS;
1712
1713queuing_error:
1714 if (status & QDSS_GOT_Q_SPACE) {
1715 req->outstanding_cmds[handle] = NULL;
1716 req->cnt += req_cnt;
1717 }
1718 /* Cleanup will be performed by the caller (queuecommand) */
1719
1720 spin_unlock_irqrestore(&ha->hardware_lock, flags);
bad75002
AE
1721 return QLA_FUNCTION_FAILED;
1722}
1723
1724
59e0b8b0 1725static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
68ca949c
AC
1726{
1727 struct scsi_cmnd *cmd = sp->cmd;
68ca949c
AC
1728 struct qla_hw_data *ha = sp->fcport->vha->hw;
1729 int affinity = cmd->request->cpu;
1730
7163ea81 1731 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
59e0b8b0 1732 affinity < ha->max_rsp_queues - 1)
68ca949c 1733 *rsp = ha->rsp_q_map[affinity + 1];
59e0b8b0 1734 else
68ca949c 1735 *rsp = ha->rsp_q_map[0];
68ca949c 1736}
ac280b67
AV
1737
1738/* Generic Control-SRB manipulation functions. */
d94d10e7
GM
1739void *
1740qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
ac280b67 1741{
ac280b67
AV
1742 struct qla_hw_data *ha = vha->hw;
1743 struct req_que *req = ha->req_q_map[0];
1744 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1745 uint32_t index, handle;
1746 request_t *pkt;
1747 uint16_t cnt, req_cnt;
1748
1749 pkt = NULL;
1750 req_cnt = 1;
d94d10e7
GM
1751 handle = 0;
1752
1753 if (!sp)
1754 goto skip_cmd_array;
ac280b67
AV
1755
1756 /* Check for room in outstanding command list. */
1757 handle = req->current_outstanding_cmd;
1758 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1759 handle++;
1760 if (handle == MAX_OUTSTANDING_COMMANDS)
1761 handle = 1;
1762 if (!req->outstanding_cmds[handle])
1763 break;
1764 }
7c3df132
SK
1765 if (index == MAX_OUTSTANDING_COMMANDS) {
1766 ql_log(ql_log_warn, vha, 0x700b,
1767 "No room on oustanding cmd array.\n");
ac280b67 1768 goto queuing_error;
7c3df132 1769 }
ac280b67 1770
d94d10e7
GM
1771 /* Prep command array. */
1772 req->current_outstanding_cmd = handle;
1773 req->outstanding_cmds[handle] = sp;
1774 sp->handle = handle;
1775
1776skip_cmd_array:
ac280b67
AV
1777 /* Check for room on request queue. */
1778 if (req->cnt < req_cnt) {
1779 if (ha->mqenable)
1780 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
d94d10e7
GM
1781 else if (IS_QLA82XX(ha))
1782 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
ac280b67
AV
1783 else if (IS_FWI2_CAPABLE(ha))
1784 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
1785 else
1786 cnt = qla2x00_debounce_register(
1787 ISP_REQ_Q_OUT(ha, &reg->isp));
1788
1789 if (req->ring_index < cnt)
1790 req->cnt = cnt - req->ring_index;
1791 else
1792 req->cnt = req->length -
1793 (req->ring_index - cnt);
1794 }
1795 if (req->cnt < req_cnt)
1796 goto queuing_error;
1797
1798 /* Prep packet */
ac280b67 1799 req->cnt -= req_cnt;
ac280b67
AV
1800 pkt = req->ring_ptr;
1801 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1802 pkt->entry_count = req_cnt;
1803 pkt->handle = handle;
ac280b67
AV
1804
1805queuing_error:
1806 return pkt;
1807}
1808
1809static void
1810qla2x00_start_iocbs(srb_t *sp)
1811{
1812 struct qla_hw_data *ha = sp->fcport->vha->hw;
1813 struct req_que *req = ha->req_q_map[0];
1814 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1815 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1816
a9083016
GM
1817 if (IS_QLA82XX(ha)) {
1818 qla82xx_start_iocbs(sp);
ac280b67 1819 } else {
a9083016
GM
1820 /* Adjust ring index. */
1821 req->ring_index++;
1822 if (req->ring_index == req->length) {
1823 req->ring_index = 0;
1824 req->ring_ptr = req->ring;
1825 } else
1826 req->ring_ptr++;
1827
1828 /* Set chip new ring index. */
1829 if (ha->mqenable) {
1830 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
1831 RD_REG_DWORD(&ioreg->hccr);
1832 } else if (IS_QLA82XX(ha)) {
1833 qla82xx_start_iocbs(sp);
1834 } else if (IS_FWI2_CAPABLE(ha)) {
1835 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
1836 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
1837 } else {
1838 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
1839 req->ring_index);
1840 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
1841 }
ac280b67
AV
1842 }
1843}
1844
1845static void
1846qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1847{
4916392b
MI
1848 struct srb_ctx *ctx = sp->ctx;
1849 struct srb_iocb *lio = ctx->u.iocb_cmd;
ac280b67
AV
1850
1851 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1852 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
4916392b 1853 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
ac280b67 1854 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
4916392b 1855 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
ac280b67
AV
1856 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1857 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1858 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1859 logio->port_id[1] = sp->fcport->d_id.b.area;
1860 logio->port_id[2] = sp->fcport->d_id.b.domain;
1861 logio->vp_index = sp->fcport->vp_idx;
1862}
1863
1864static void
1865qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1866{
1867 struct qla_hw_data *ha = sp->fcport->vha->hw;
4916392b
MI
1868 struct srb_ctx *ctx = sp->ctx;
1869 struct srb_iocb *lio = ctx->u.iocb_cmd;
ac280b67
AV
1870 uint16_t opts;
1871
b963752f 1872 mbx->entry_type = MBX_IOCB_TYPE;
ac280b67
AV
1873 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1874 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
4916392b
MI
1875 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1876 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
ac280b67
AV
1877 if (HAS_EXTENDED_IDS(ha)) {
1878 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1879 mbx->mb10 = cpu_to_le16(opts);
1880 } else {
1881 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1882 }
1883 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1884 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1885 sp->fcport->d_id.b.al_pa);
1886 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1887}
1888
1889static void
1890qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1891{
1892 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1893 logio->control_flags =
1894 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1895 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1896 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1897 logio->port_id[1] = sp->fcport->d_id.b.area;
1898 logio->port_id[2] = sp->fcport->d_id.b.domain;
1899 logio->vp_index = sp->fcport->vp_idx;
1900}
1901
1902static void
1903qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1904{
1905 struct qla_hw_data *ha = sp->fcport->vha->hw;
1906
b963752f 1907 mbx->entry_type = MBX_IOCB_TYPE;
ac280b67
AV
1908 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1909 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1910 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1911 cpu_to_le16(sp->fcport->loop_id):
1912 cpu_to_le16(sp->fcport->loop_id << 8);
1913 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1914 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1915 sp->fcport->d_id.b.al_pa);
1916 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1917 /* Implicit: mbx->mbx10 = 0. */
1918}
1919
5ff1d584
AV
1920static void
1921qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1922{
1923 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1924 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1925 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1926 logio->vp_index = sp->fcport->vp_idx;
1927}
1928
1929static void
1930qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1931{
1932 struct qla_hw_data *ha = sp->fcport->vha->hw;
1933
1934 mbx->entry_type = MBX_IOCB_TYPE;
1935 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1936 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1937 if (HAS_EXTENDED_IDS(ha)) {
1938 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1939 mbx->mb10 = cpu_to_le16(BIT_0);
1940 } else {
1941 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1942 }
1943 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1944 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1945 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1946 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
1947 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1948}
1949
3822263e
MI
1950static void
1951qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1952{
1953 uint32_t flags;
1954 unsigned int lun;
1955 struct fc_port *fcport = sp->fcport;
1956 scsi_qla_host_t *vha = fcport->vha;
1957 struct qla_hw_data *ha = vha->hw;
1958 struct srb_ctx *ctx = sp->ctx;
1959 struct srb_iocb *iocb = ctx->u.iocb_cmd;
1960 struct req_que *req = vha->req;
1961
1962 flags = iocb->u.tmf.flags;
1963 lun = iocb->u.tmf.lun;
1964
1965 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
1966 tsk->entry_count = 1;
1967 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
1968 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
1969 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
1970 tsk->control_flags = cpu_to_le32(flags);
1971 tsk->port_id[0] = fcport->d_id.b.al_pa;
1972 tsk->port_id[1] = fcport->d_id.b.area;
1973 tsk->port_id[2] = fcport->d_id.b.domain;
1974 tsk->vp_index = fcport->vp_idx;
1975
1976 if (flags == TCF_LUN_RESET) {
1977 int_to_scsilun(lun, &tsk->lun);
1978 host_to_fcp_swap((uint8_t *)&tsk->lun,
1979 sizeof(tsk->lun));
1980 }
1981}
1982
9a069e19
GM
1983static void
1984qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
1985{
4916392b 1986 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
9a069e19
GM
1987
1988 els_iocb->entry_type = ELS_IOCB_TYPE;
1989 els_iocb->entry_count = 1;
1990 els_iocb->sys_define = 0;
1991 els_iocb->entry_status = 0;
1992 els_iocb->handle = sp->handle;
1993 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1994 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1995 els_iocb->vp_index = sp->fcport->vp_idx;
1996 els_iocb->sof_type = EST_SOFI3;
1997 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1998
4916392b
MI
1999 els_iocb->opcode =
2000 (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
2001 bsg_job->request->rqst_data.r_els.els_code :
2002 bsg_job->request->rqst_data.h_els.command_code;
9a069e19
GM
2003 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2004 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2005 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2006 els_iocb->control_flags = 0;
2007 els_iocb->rx_byte_count =
2008 cpu_to_le32(bsg_job->reply_payload.payload_len);
2009 els_iocb->tx_byte_count =
2010 cpu_to_le32(bsg_job->request_payload.payload_len);
2011
2012 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2013 (bsg_job->request_payload.sg_list)));
2014 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2015 (bsg_job->request_payload.sg_list)));
2016 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2017 (bsg_job->request_payload.sg_list));
2018
2019 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2020 (bsg_job->reply_payload.sg_list)));
2021 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2022 (bsg_job->reply_payload.sg_list)));
2023 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2024 (bsg_job->reply_payload.sg_list));
2025}
2026
9bc4f4fb
HZ
2027static void
2028qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2029{
2030 uint16_t avail_dsds;
2031 uint32_t *cur_dsd;
2032 struct scatterlist *sg;
2033 int index;
2034 uint16_t tot_dsds;
2035 scsi_qla_host_t *vha = sp->fcport->vha;
2036 struct qla_hw_data *ha = vha->hw;
2037 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
2038 int loop_iterartion = 0;
2039 int cont_iocb_prsnt = 0;
2040 int entry_count = 1;
2041
2042 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2043 ct_iocb->entry_type = CT_IOCB_TYPE;
2044 ct_iocb->entry_status = 0;
2045 ct_iocb->handle1 = sp->handle;
2046 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2047 ct_iocb->status = __constant_cpu_to_le16(0);
2048 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2049 ct_iocb->timeout = 0;
2050 ct_iocb->cmd_dsd_count =
2051 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2052 ct_iocb->total_dsd_count =
2053 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2054 ct_iocb->req_bytecount =
2055 cpu_to_le32(bsg_job->request_payload.payload_len);
2056 ct_iocb->rsp_bytecount =
2057 cpu_to_le32(bsg_job->reply_payload.payload_len);
2058
2059 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2060 (bsg_job->request_payload.sg_list)));
2061 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2062 (bsg_job->request_payload.sg_list)));
2063 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2064
2065 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2066 (bsg_job->reply_payload.sg_list)));
2067 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2068 (bsg_job->reply_payload.sg_list)));
2069 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2070
2071 avail_dsds = 1;
2072 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2073 index = 0;
2074 tot_dsds = bsg_job->reply_payload.sg_cnt;
2075
2076 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2077 dma_addr_t sle_dma;
2078 cont_a64_entry_t *cont_pkt;
2079
2080 /* Allocate additional continuation packets? */
2081 if (avail_dsds == 0) {
2082 /*
2083 * Five DSDs are available in the Cont.
2084 * Type 1 IOCB.
2085 */
2086 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
2087 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2088 avail_dsds = 5;
2089 cont_iocb_prsnt = 1;
2090 entry_count++;
2091 }
2092
2093 sle_dma = sg_dma_address(sg);
2094 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2095 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2096 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2097 loop_iterartion++;
2098 avail_dsds--;
2099 }
2100 ct_iocb->entry_count = entry_count;
2101}
2102
9a069e19
GM
2103static void
2104qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2105{
2106 uint16_t avail_dsds;
2107 uint32_t *cur_dsd;
2108 struct scatterlist *sg;
2109 int index;
2110 uint16_t tot_dsds;
2111 scsi_qla_host_t *vha = sp->fcport->vha;
4916392b 2112 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
9a069e19
GM
2113 int loop_iterartion = 0;
2114 int cont_iocb_prsnt = 0;
2115 int entry_count = 1;
2116
2117 ct_iocb->entry_type = CT_IOCB_TYPE;
2118 ct_iocb->entry_status = 0;
2119 ct_iocb->sys_define = 0;
2120 ct_iocb->handle = sp->handle;
2121
2122 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2123 ct_iocb->vp_index = sp->fcport->vp_idx;
2124 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2125
2126 ct_iocb->cmd_dsd_count =
2127 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2128 ct_iocb->timeout = 0;
2129 ct_iocb->rsp_dsd_count =
2130 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2131 ct_iocb->rsp_byte_count =
2132 cpu_to_le32(bsg_job->reply_payload.payload_len);
2133 ct_iocb->cmd_byte_count =
2134 cpu_to_le32(bsg_job->request_payload.payload_len);
2135 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2136 (bsg_job->request_payload.sg_list)));
2137 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2138 (bsg_job->request_payload.sg_list)));
2139 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2140 (bsg_job->request_payload.sg_list));
2141
2142 avail_dsds = 1;
2143 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2144 index = 0;
2145 tot_dsds = bsg_job->reply_payload.sg_cnt;
2146
2147 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2148 dma_addr_t sle_dma;
2149 cont_a64_entry_t *cont_pkt;
2150
2151 /* Allocate additional continuation packets? */
2152 if (avail_dsds == 0) {
2153 /*
2154 * Five DSDs are available in the Cont.
2155 * Type 1 IOCB.
2156 */
2157 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
2158 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2159 avail_dsds = 5;
2160 cont_iocb_prsnt = 1;
2161 entry_count++;
2162 }
2163
2164 sle_dma = sg_dma_address(sg);
2165 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2166 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2167 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2168 loop_iterartion++;
2169 avail_dsds--;
2170 }
2171 ct_iocb->entry_count = entry_count;
2172}
2173
ac280b67
AV
2174int
2175qla2x00_start_sp(srb_t *sp)
2176{
2177 int rval;
2178 struct qla_hw_data *ha = sp->fcport->vha->hw;
2179 void *pkt;
2180 struct srb_ctx *ctx = sp->ctx;
2181 unsigned long flags;
2182
2183 rval = QLA_FUNCTION_FAILED;
2184 spin_lock_irqsave(&ha->hardware_lock, flags);
d94d10e7 2185 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
7c3df132
SK
2186 if (!pkt) {
2187 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2188 "qla2x00_alloc_iocbs failed.\n");
ac280b67 2189 goto done;
7c3df132 2190 }
ac280b67
AV
2191
2192 rval = QLA_SUCCESS;
2193 switch (ctx->type) {
2194 case SRB_LOGIN_CMD:
2195 IS_FWI2_CAPABLE(ha) ?
5ff1d584 2196 qla24xx_login_iocb(sp, pkt) :
ac280b67
AV
2197 qla2x00_login_iocb(sp, pkt);
2198 break;
2199 case SRB_LOGOUT_CMD:
2200 IS_FWI2_CAPABLE(ha) ?
5ff1d584 2201 qla24xx_logout_iocb(sp, pkt) :
ac280b67
AV
2202 qla2x00_logout_iocb(sp, pkt);
2203 break;
9a069e19
GM
2204 case SRB_ELS_CMD_RPT:
2205 case SRB_ELS_CMD_HST:
2206 qla24xx_els_iocb(sp, pkt);
2207 break;
2208 case SRB_CT_CMD:
9bc4f4fb
HZ
2209 IS_FWI2_CAPABLE(ha) ?
2210 qla24xx_ct_iocb(sp, pkt) :
2211 qla2x00_ct_iocb(sp, pkt);
9a069e19 2212 break;
5ff1d584
AV
2213 case SRB_ADISC_CMD:
2214 IS_FWI2_CAPABLE(ha) ?
2215 qla24xx_adisc_iocb(sp, pkt) :
2216 qla2x00_adisc_iocb(sp, pkt);
2217 break;
3822263e
MI
2218 case SRB_TM_CMD:
2219 qla24xx_tm_iocb(sp, pkt);
2220 break;
ac280b67
AV
2221 default:
2222 break;
2223 }
2224
2225 wmb();
2226 qla2x00_start_iocbs(sp);
2227done:
2228 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2229 return rval;
2230}