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