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