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