pandora: defconfig: update
[pandora-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, 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         *((uint32_t *)(&cont_pkt->entry_type)) =
140             __constant_cpu_to_le32(CONTINUE_A64_TYPE);
141
142         return (cont_pkt);
143 }
144
145 static inline int
146 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
147 {
148         uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
149
150         /* We only support T10 DIF right now */
151         if (guard != SHOST_DIX_GUARD_CRC) {
152                 ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3007,
153                     "Unsupported guard: %d for cmd=%p.\n", guard, sp->cmd);
154                 return 0;
155         }
156
157         /* We always use DIFF Bundling for best performance */
158         *fw_prot_opts = 0;
159
160         /* Translate SCSI opcode to a protection opcode */
161         switch (scsi_get_prot_op(sp->cmd)) {
162         case SCSI_PROT_READ_STRIP:
163                 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
164                 break;
165         case SCSI_PROT_WRITE_INSERT:
166                 *fw_prot_opts |= PO_MODE_DIF_INSERT;
167                 break;
168         case SCSI_PROT_READ_INSERT:
169                 *fw_prot_opts |= PO_MODE_DIF_INSERT;
170                 break;
171         case SCSI_PROT_WRITE_STRIP:
172                 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
173                 break;
174         case SCSI_PROT_READ_PASS:
175                 *fw_prot_opts |= PO_MODE_DIF_PASS;
176                 break;
177         case SCSI_PROT_WRITE_PASS:
178                 *fw_prot_opts |= PO_MODE_DIF_PASS;
179                 break;
180         default:        /* Normal Request */
181                 *fw_prot_opts |= PO_MODE_DIF_PASS;
182                 break;
183         }
184
185         return scsi_prot_sg_count(sp->cmd);
186 }
187
188 /*
189  * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
190  * capable IOCB types.
191  *
192  * @sp: SRB command to process
193  * @cmd_pkt: Command type 2 IOCB
194  * @tot_dsds: Total number of segments to transfer
195  */
196 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
197     uint16_t tot_dsds)
198 {
199         uint16_t        avail_dsds;
200         uint32_t        *cur_dsd;
201         scsi_qla_host_t *vha;
202         struct scsi_cmnd *cmd;
203         struct scatterlist *sg;
204         int i;
205
206         cmd = sp->cmd;
207
208         /* Update entry type to indicate Command Type 2 IOCB */
209         *((uint32_t *)(&cmd_pkt->entry_type)) =
210             __constant_cpu_to_le32(COMMAND_TYPE);
211
212         /* No data transfer */
213         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
214                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
215                 return;
216         }
217
218         vha = sp->fcport->vha;
219         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
220
221         /* Three DSDs are available in the Command Type 2 IOCB */
222         avail_dsds = 3;
223         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
224
225         /* Load data segments */
226         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
227                 cont_entry_t *cont_pkt;
228
229                 /* Allocate additional continuation packets? */
230                 if (avail_dsds == 0) {
231                         /*
232                          * Seven DSDs are available in the Continuation
233                          * Type 0 IOCB.
234                          */
235                         cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
236                         cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
237                         avail_dsds = 7;
238                 }
239
240                 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
241                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
242                 avail_dsds--;
243         }
244 }
245
246 /**
247  * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
248  * capable IOCB types.
249  *
250  * @sp: SRB command to process
251  * @cmd_pkt: Command type 3 IOCB
252  * @tot_dsds: Total number of segments to transfer
253  */
254 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
255     uint16_t tot_dsds)
256 {
257         uint16_t        avail_dsds;
258         uint32_t        *cur_dsd;
259         scsi_qla_host_t *vha;
260         struct scsi_cmnd *cmd;
261         struct scatterlist *sg;
262         int i;
263
264         cmd = sp->cmd;
265
266         /* Update entry type to indicate Command Type 3 IOCB */
267         *((uint32_t *)(&cmd_pkt->entry_type)) =
268             __constant_cpu_to_le32(COMMAND_A64_TYPE);
269
270         /* No data transfer */
271         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
272                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
273                 return;
274         }
275
276         vha = sp->fcport->vha;
277         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
278
279         /* Two DSDs are available in the Command Type 3 IOCB */
280         avail_dsds = 2;
281         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
282
283         /* Load data segments */
284         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
285                 dma_addr_t      sle_dma;
286                 cont_a64_entry_t *cont_pkt;
287
288                 /* Allocate additional continuation packets? */
289                 if (avail_dsds == 0) {
290                         /*
291                          * Five DSDs are available in the Continuation
292                          * Type 1 IOCB.
293                          */
294                         cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
295                         cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
296                         avail_dsds = 5;
297                 }
298
299                 sle_dma = sg_dma_address(sg);
300                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
301                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
302                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
303                 avail_dsds--;
304         }
305 }
306
307 /**
308  * qla2x00_start_scsi() - Send a SCSI command to the ISP
309  * @sp: command to send to the ISP
310  *
311  * Returns non-zero if a failure occurred, else zero.
312  */
313 int
314 qla2x00_start_scsi(srb_t *sp)
315 {
316         int             ret, nseg;
317         unsigned long   flags;
318         scsi_qla_host_t *vha;
319         struct scsi_cmnd *cmd;
320         uint32_t        *clr_ptr;
321         uint32_t        index;
322         uint32_t        handle;
323         cmd_entry_t     *cmd_pkt;
324         uint16_t        cnt;
325         uint16_t        req_cnt;
326         uint16_t        tot_dsds;
327         struct device_reg_2xxx __iomem *reg;
328         struct qla_hw_data *ha;
329         struct req_que *req;
330         struct rsp_que *rsp;
331         char            tag[2];
332
333         /* Setup device pointers. */
334         ret = 0;
335         vha = sp->fcport->vha;
336         ha = vha->hw;
337         reg = &ha->iobase->isp;
338         cmd = sp->cmd;
339         req = ha->req_q_map[0];
340         rsp = ha->rsp_q_map[0];
341         /* So we know we haven't pci_map'ed anything yet */
342         tot_dsds = 0;
343
344         /* Send marker if required */
345         if (vha->marker_needed != 0) {
346                 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
347                     QLA_SUCCESS) {
348                         return (QLA_FUNCTION_FAILED);
349                 }
350                 vha->marker_needed = 0;
351         }
352
353         /* Acquire ring specific lock */
354         spin_lock_irqsave(&ha->hardware_lock, flags);
355
356         /* Check for room in outstanding command list. */
357         handle = req->current_outstanding_cmd;
358         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
359                 handle++;
360                 if (handle == MAX_OUTSTANDING_COMMANDS)
361                         handle = 1;
362                 if (!req->outstanding_cmds[handle])
363                         break;
364         }
365         if (index == MAX_OUTSTANDING_COMMANDS)
366                 goto queuing_error;
367
368         /* Map the sg table so we have an accurate count of sg entries needed */
369         if (scsi_sg_count(cmd)) {
370                 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
371                     scsi_sg_count(cmd), cmd->sc_data_direction);
372                 if (unlikely(!nseg))
373                         goto queuing_error;
374         } else
375                 nseg = 0;
376
377         tot_dsds = nseg;
378
379         /* Calculate the number of request entries needed. */
380         req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
381         if (req->cnt < (req_cnt + 2)) {
382                 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
383                 if (req->ring_index < cnt)
384                         req->cnt = cnt - req->ring_index;
385                 else
386                         req->cnt = req->length -
387                             (req->ring_index - cnt);
388         }
389         if (req->cnt < (req_cnt + 2))
390                 goto queuing_error;
391
392         /* Build command packet */
393         req->current_outstanding_cmd = handle;
394         req->outstanding_cmds[handle] = sp;
395         sp->handle = handle;
396         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
397         req->cnt -= req_cnt;
398
399         cmd_pkt = (cmd_entry_t *)req->ring_ptr;
400         cmd_pkt->handle = handle;
401         /* Zero out remaining portion of packet. */
402         clr_ptr = (uint32_t *)cmd_pkt + 2;
403         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
404         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
405
406         /* Set target ID and LUN number*/
407         SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
408         cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
409
410         /* Update tagged queuing modifier */
411         if (scsi_populate_tag_msg(cmd, tag)) {
412                 switch (tag[0]) {
413                 case HEAD_OF_QUEUE_TAG:
414                         cmd_pkt->control_flags =
415                             __constant_cpu_to_le16(CF_HEAD_TAG);
416                         break;
417                 case ORDERED_QUEUE_TAG:
418                         cmd_pkt->control_flags =
419                             __constant_cpu_to_le16(CF_ORDERED_TAG);
420                         break;
421                 default:
422                         cmd_pkt->control_flags =
423                             __constant_cpu_to_le16(CF_SIMPLE_TAG);
424                         break;
425                 }
426         } else {
427                 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
428         }
429
430         /* Load SCSI command packet. */
431         memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
432         cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
433
434         /* Build IOCB segments */
435         ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
436
437         /* Set total data segment count. */
438         cmd_pkt->entry_count = (uint8_t)req_cnt;
439         wmb();
440
441         /* Adjust ring index. */
442         req->ring_index++;
443         if (req->ring_index == req->length) {
444                 req->ring_index = 0;
445                 req->ring_ptr = req->ring;
446         } else
447                 req->ring_ptr++;
448
449         sp->flags |= SRB_DMA_VALID;
450
451         /* Set chip new ring index. */
452         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
453         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
454
455         /* Manage unprocessed RIO/ZIO commands in response queue. */
456         if (vha->flags.process_response_queue &&
457             rsp->ring_ptr->signature != RESPONSE_PROCESSED)
458                 qla2x00_process_response_queue(rsp);
459
460         spin_unlock_irqrestore(&ha->hardware_lock, flags);
461         return (QLA_SUCCESS);
462
463 queuing_error:
464         if (tot_dsds)
465                 scsi_dma_unmap(cmd);
466
467         spin_unlock_irqrestore(&ha->hardware_lock, flags);
468
469         return (QLA_FUNCTION_FAILED);
470 }
471
472 /**
473  * qla2x00_marker() - Send a marker IOCB to the firmware.
474  * @ha: HA context
475  * @loop_id: loop ID
476  * @lun: LUN
477  * @type: marker modifier
478  *
479  * Can be called from both normal and interrupt context.
480  *
481  * Returns non-zero if a failure occurred, else zero.
482  */
483 static int
484 __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
485                         struct rsp_que *rsp, uint16_t loop_id,
486                         uint16_t lun, uint8_t type)
487 {
488         mrk_entry_t *mrk;
489         struct mrk_entry_24xx *mrk24;
490         struct qla_hw_data *ha = vha->hw;
491         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
492
493         mrk24 = NULL;
494         mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
495         if (mrk == NULL) {
496                 ql_log(ql_log_warn, base_vha, 0x3026,
497                     "Failed to allocate Marker IOCB.\n");
498
499                 return (QLA_FUNCTION_FAILED);
500         }
501
502         mrk->entry_type = MARKER_TYPE;
503         mrk->modifier = type;
504         if (type != MK_SYNC_ALL) {
505                 if (IS_FWI2_CAPABLE(ha)) {
506                         mrk24 = (struct mrk_entry_24xx *) mrk;
507                         mrk24->nport_handle = cpu_to_le16(loop_id);
508                         mrk24->lun[1] = LSB(lun);
509                         mrk24->lun[2] = MSB(lun);
510                         host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
511                         mrk24->vp_index = vha->vp_idx;
512                         mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
513                 } else {
514                         SET_TARGET_ID(ha, mrk->target, loop_id);
515                         mrk->lun = cpu_to_le16(lun);
516                 }
517         }
518         wmb();
519
520         qla2x00_isp_cmd(vha, req);
521
522         return (QLA_SUCCESS);
523 }
524
525 int
526 qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
527                 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
528                 uint8_t type)
529 {
530         int ret;
531         unsigned long flags = 0;
532
533         spin_lock_irqsave(&vha->hw->hardware_lock, flags);
534         ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
535         spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
536
537         return (ret);
538 }
539
540 /**
541  * qla2x00_isp_cmd() - Modify the request ring pointer.
542  * @ha: HA context
543  *
544  * Note: The caller must hold the hardware lock before calling this routine.
545  */
546 static void
547 qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
548 {
549         struct qla_hw_data *ha = vha->hw;
550         device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
551         struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
552
553         ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x302d,
554             "IOCB data:\n");
555         ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302e,
556             (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE);
557
558         /* Adjust ring index. */
559         req->ring_index++;
560         if (req->ring_index == req->length) {
561                 req->ring_index = 0;
562                 req->ring_ptr = req->ring;
563         } else
564                 req->ring_ptr++;
565
566         /* Set chip new ring index. */
567         if (IS_QLA82XX(ha)) {
568                 uint32_t dbval = 0x04 | (ha->portnum << 5);
569
570                 /* write, read and verify logic */
571                 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
572                 if (ql2xdbwr)
573                         qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
574                 else {
575                         WRT_REG_DWORD(
576                                 (unsigned long __iomem *)ha->nxdb_wr_ptr,
577                                 dbval);
578                         wmb();
579                         while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
580                                 WRT_REG_DWORD((unsigned long __iomem *)
581                                         ha->nxdb_wr_ptr, dbval);
582                                 wmb();
583                         }
584                 }
585         } else if (ha->mqenable) {
586                 /* Set chip new ring index. */
587                 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
588                 RD_REG_DWORD(&ioreg->hccr);
589         } else {
590                 if (IS_FWI2_CAPABLE(ha)) {
591                         WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
592                         RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
593                 } else {
594                         WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
595                                 req->ring_index);
596                         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
597                 }
598         }
599
600 }
601
602 /**
603  * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
604  * Continuation Type 1 IOCBs to allocate.
605  *
606  * @dsds: number of data segment decriptors needed
607  *
608  * Returns the number of IOCB entries needed to store @dsds.
609  */
610 inline uint16_t
611 qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
612 {
613         uint16_t iocbs;
614
615         iocbs = 1;
616         if (dsds > 1) {
617                 iocbs += (dsds - 1) / 5;
618                 if ((dsds - 1) % 5)
619                         iocbs++;
620         }
621         return iocbs;
622 }
623
624 /**
625  * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
626  * IOCB types.
627  *
628  * @sp: SRB command to process
629  * @cmd_pkt: Command type 3 IOCB
630  * @tot_dsds: Total number of segments to transfer
631  */
632 inline void
633 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
634     uint16_t tot_dsds)
635 {
636         uint16_t        avail_dsds;
637         uint32_t        *cur_dsd;
638         scsi_qla_host_t *vha;
639         struct scsi_cmnd *cmd;
640         struct scatterlist *sg;
641         int i;
642         struct req_que *req;
643
644         cmd = sp->cmd;
645
646         /* Update entry type to indicate Command Type 3 IOCB */
647         *((uint32_t *)(&cmd_pkt->entry_type)) =
648             __constant_cpu_to_le32(COMMAND_TYPE_7);
649
650         /* No data transfer */
651         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
652                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
653                 return;
654         }
655
656         vha = sp->fcport->vha;
657         req = vha->req;
658
659         /* Set transfer direction */
660         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
661                 cmd_pkt->task_mgmt_flags =
662                     __constant_cpu_to_le16(TMF_WRITE_DATA);
663                 sp->fcport->vha->hw->qla_stats.output_bytes +=
664                     scsi_bufflen(sp->cmd);
665         } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
666                 cmd_pkt->task_mgmt_flags =
667                     __constant_cpu_to_le16(TMF_READ_DATA);
668                 sp->fcport->vha->hw->qla_stats.input_bytes +=
669                     scsi_bufflen(sp->cmd);
670         }
671
672         /* One DSD is available in the Command Type 3 IOCB */
673         avail_dsds = 1;
674         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
675
676         /* Load data segments */
677
678         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
679                 dma_addr_t      sle_dma;
680                 cont_a64_entry_t *cont_pkt;
681
682                 /* Allocate additional continuation packets? */
683                 if (avail_dsds == 0) {
684                         /*
685                          * Five DSDs are available in the Continuation
686                          * Type 1 IOCB.
687                          */
688                         cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
689                         cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
690                         avail_dsds = 5;
691                 }
692
693                 sle_dma = sg_dma_address(sg);
694                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
695                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
696                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
697                 avail_dsds--;
698         }
699 }
700
701 struct fw_dif_context {
702         uint32_t ref_tag;
703         uint16_t app_tag;
704         uint8_t ref_tag_mask[4];        /* Validation/Replacement Mask*/
705         uint8_t app_tag_mask[2];        /* Validation/Replacement Mask*/
706 };
707
708 /*
709  * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
710  *
711  */
712 static inline void
713 qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
714     unsigned int protcnt)
715 {
716         struct scsi_cmnd *cmd = sp->cmd;
717         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
718
719         switch (scsi_get_prot_type(cmd)) {
720         case SCSI_PROT_DIF_TYPE0:
721                 /*
722                  * No check for ql2xenablehba_err_chk, as it would be an
723                  * I/O error if hba tag generation is not done.
724                  */
725                 pkt->ref_tag = cpu_to_le32((uint32_t)
726                     (0xffffffff & scsi_get_lba(cmd)));
727
728                 if (!qla2x00_hba_err_chk_enabled(sp))
729                         break;
730
731                 pkt->ref_tag_mask[0] = 0xff;
732                 pkt->ref_tag_mask[1] = 0xff;
733                 pkt->ref_tag_mask[2] = 0xff;
734                 pkt->ref_tag_mask[3] = 0xff;
735                 break;
736
737         /*
738          * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
739          * match LBA in CDB + N
740          */
741         case SCSI_PROT_DIF_TYPE2:
742                 pkt->app_tag = __constant_cpu_to_le16(0);
743                 pkt->app_tag_mask[0] = 0x0;
744                 pkt->app_tag_mask[1] = 0x0;
745
746                 pkt->ref_tag = cpu_to_le32((uint32_t)
747                     (0xffffffff & scsi_get_lba(cmd)));
748
749                 if (!qla2x00_hba_err_chk_enabled(sp))
750                         break;
751
752                 /* enable ALL bytes of the ref tag */
753                 pkt->ref_tag_mask[0] = 0xff;
754                 pkt->ref_tag_mask[1] = 0xff;
755                 pkt->ref_tag_mask[2] = 0xff;
756                 pkt->ref_tag_mask[3] = 0xff;
757                 break;
758
759         /* For Type 3 protection: 16 bit GUARD only */
760         case SCSI_PROT_DIF_TYPE3:
761                 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
762                         pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
763                                                                 0x00;
764                 break;
765
766         /*
767          * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
768          * 16 bit app tag.
769          */
770         case SCSI_PROT_DIF_TYPE1:
771                 pkt->ref_tag = cpu_to_le32((uint32_t)
772                     (0xffffffff & scsi_get_lba(cmd)));
773                 pkt->app_tag = __constant_cpu_to_le16(0);
774                 pkt->app_tag_mask[0] = 0x0;
775                 pkt->app_tag_mask[1] = 0x0;
776
777                 if (!qla2x00_hba_err_chk_enabled(sp))
778                         break;
779
780                 /* enable ALL bytes of the ref tag */
781                 pkt->ref_tag_mask[0] = 0xff;
782                 pkt->ref_tag_mask[1] = 0xff;
783                 pkt->ref_tag_mask[2] = 0xff;
784                 pkt->ref_tag_mask[3] = 0xff;
785                 break;
786         }
787
788         ql_dbg(ql_dbg_io, vha, 0x3009,
789             "Setting protection Tags: (BIG) ref tag = 0x%x, app tag = 0x%x, "
790             "prot SG count %d, cmd lba 0x%x, prot_type=%u cmd=%p.\n",
791             pkt->ref_tag, pkt->app_tag, protcnt, (int)scsi_get_lba(cmd),
792             scsi_get_prot_type(cmd), cmd);
793 }
794
795 struct qla2_sgx {
796         dma_addr_t              dma_addr;       /* OUT */
797         uint32_t                dma_len;        /* OUT */
798
799         uint32_t                tot_bytes;      /* IN */
800         struct scatterlist      *cur_sg;        /* IN */
801
802         /* for book keeping, bzero on initial invocation */
803         uint32_t                bytes_consumed;
804         uint32_t                num_bytes;
805         uint32_t                tot_partial;
806
807         /* for debugging */
808         uint32_t                num_sg;
809         srb_t                   *sp;
810 };
811
812 static int
813 qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
814         uint32_t *partial)
815 {
816         struct scatterlist *sg;
817         uint32_t cumulative_partial, sg_len;
818         dma_addr_t sg_dma_addr;
819
820         if (sgx->num_bytes == sgx->tot_bytes)
821                 return 0;
822
823         sg = sgx->cur_sg;
824         cumulative_partial = sgx->tot_partial;
825
826         sg_dma_addr = sg_dma_address(sg);
827         sg_len = sg_dma_len(sg);
828
829         sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
830
831         if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
832                 sgx->dma_len = (blk_sz - cumulative_partial);
833                 sgx->tot_partial = 0;
834                 sgx->num_bytes += blk_sz;
835                 *partial = 0;
836         } else {
837                 sgx->dma_len = sg_len - sgx->bytes_consumed;
838                 sgx->tot_partial += sgx->dma_len;
839                 *partial = 1;
840         }
841
842         sgx->bytes_consumed += sgx->dma_len;
843
844         if (sg_len == sgx->bytes_consumed) {
845                 sg = sg_next(sg);
846                 sgx->num_sg++;
847                 sgx->cur_sg = sg;
848                 sgx->bytes_consumed = 0;
849         }
850
851         return 1;
852 }
853
854 static int
855 qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
856         uint32_t *dsd, uint16_t tot_dsds)
857 {
858         void *next_dsd;
859         uint8_t avail_dsds = 0;
860         uint32_t dsd_list_len;
861         struct dsd_dma *dsd_ptr;
862         struct scatterlist *sg_prot;
863         uint32_t *cur_dsd = dsd;
864         uint16_t        used_dsds = tot_dsds;
865
866         uint32_t        prot_int;
867         uint32_t        partial;
868         struct qla2_sgx sgx;
869         dma_addr_t      sle_dma;
870         uint32_t        sle_dma_len, tot_prot_dma_len = 0;
871         struct scsi_cmnd *cmd = sp->cmd;
872
873         prot_int = cmd->device->sector_size;
874
875         memset(&sgx, 0, sizeof(struct qla2_sgx));
876         sgx.tot_bytes = scsi_bufflen(sp->cmd);
877         sgx.cur_sg = scsi_sglist(sp->cmd);
878         sgx.sp = sp;
879
880         sg_prot = scsi_prot_sglist(sp->cmd);
881
882         while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
883
884                 sle_dma = sgx.dma_addr;
885                 sle_dma_len = sgx.dma_len;
886 alloc_and_fill:
887                 /* Allocate additional continuation packets? */
888                 if (avail_dsds == 0) {
889                         avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
890                                         QLA_DSDS_PER_IOCB : used_dsds;
891                         dsd_list_len = (avail_dsds + 1) * 12;
892                         used_dsds -= avail_dsds;
893
894                         /* allocate tracking DS */
895                         dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
896                         if (!dsd_ptr)
897                                 return 1;
898
899                         /* allocate new list */
900                         dsd_ptr->dsd_addr = next_dsd =
901                             dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
902                                 &dsd_ptr->dsd_list_dma);
903
904                         if (!next_dsd) {
905                                 /*
906                                  * Need to cleanup only this dsd_ptr, rest
907                                  * will be done by sp_free_dma()
908                                  */
909                                 kfree(dsd_ptr);
910                                 return 1;
911                         }
912
913                         list_add_tail(&dsd_ptr->list,
914                             &((struct crc_context *)sp->ctx)->dsd_list);
915
916                         sp->flags |= SRB_CRC_CTX_DSD_VALID;
917
918                         /* add new list to cmd iocb or last list */
919                         *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
920                         *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
921                         *cur_dsd++ = dsd_list_len;
922                         cur_dsd = (uint32_t *)next_dsd;
923                 }
924                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
925                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
926                 *cur_dsd++ = cpu_to_le32(sle_dma_len);
927                 avail_dsds--;
928
929                 if (partial == 0) {
930                         /* Got a full protection interval */
931                         sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
932                         sle_dma_len = 8;
933
934                         tot_prot_dma_len += sle_dma_len;
935                         if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
936                                 tot_prot_dma_len = 0;
937                                 sg_prot = sg_next(sg_prot);
938                         }
939
940                         partial = 1; /* So as to not re-enter this block */
941                         goto alloc_and_fill;
942                 }
943         }
944         /* Null termination */
945         *cur_dsd++ = 0;
946         *cur_dsd++ = 0;
947         *cur_dsd++ = 0;
948         return 0;
949 }
950 static int
951 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
952         uint16_t tot_dsds)
953 {
954         void *next_dsd;
955         uint8_t avail_dsds = 0;
956         uint32_t dsd_list_len;
957         struct dsd_dma *dsd_ptr;
958         struct scatterlist *sg;
959         uint32_t *cur_dsd = dsd;
960         int     i;
961         uint16_t        used_dsds = tot_dsds;
962         scsi_qla_host_t *vha = shost_priv(sp->cmd->device->host);
963
964         uint8_t         *cp;
965
966         scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
967                 dma_addr_t      sle_dma;
968
969                 /* Allocate additional continuation packets? */
970                 if (avail_dsds == 0) {
971                         avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
972                                         QLA_DSDS_PER_IOCB : used_dsds;
973                         dsd_list_len = (avail_dsds + 1) * 12;
974                         used_dsds -= avail_dsds;
975
976                         /* allocate tracking DS */
977                         dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
978                         if (!dsd_ptr)
979                                 return 1;
980
981                         /* allocate new list */
982                         dsd_ptr->dsd_addr = next_dsd =
983                             dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
984                                 &dsd_ptr->dsd_list_dma);
985
986                         if (!next_dsd) {
987                                 /*
988                                  * Need to cleanup only this dsd_ptr, rest
989                                  * will be done by sp_free_dma()
990                                  */
991                                 kfree(dsd_ptr);
992                                 return 1;
993                         }
994
995                         list_add_tail(&dsd_ptr->list,
996                             &((struct crc_context *)sp->ctx)->dsd_list);
997
998                         sp->flags |= SRB_CRC_CTX_DSD_VALID;
999
1000                         /* add new list to cmd iocb or last list */
1001                         *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1002                         *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1003                         *cur_dsd++ = dsd_list_len;
1004                         cur_dsd = (uint32_t *)next_dsd;
1005                 }
1006                 sle_dma = sg_dma_address(sg);
1007                 ql_dbg(ql_dbg_io, vha, 0x300a,
1008                     "sg entry %d - addr=0x%x 0x%x, " "len=%d for cmd=%p.\n",
1009                     cur_dsd, i, LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg),
1010                     sp->cmd);
1011                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1012                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1013                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1014                 avail_dsds--;
1015
1016                 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
1017                         cp = page_address(sg_page(sg)) + sg->offset;
1018                         ql_dbg(ql_dbg_io, vha, 0x300b,
1019                             "User data buffer=%p for cmd=%p.\n", cp, sp->cmd);
1020                 }
1021         }
1022         /* Null termination */
1023         *cur_dsd++ = 0;
1024         *cur_dsd++ = 0;
1025         *cur_dsd++ = 0;
1026         return 0;
1027 }
1028
1029 static int
1030 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1031                                                         uint32_t *dsd,
1032         uint16_t tot_dsds)
1033 {
1034         void *next_dsd;
1035         uint8_t avail_dsds = 0;
1036         uint32_t dsd_list_len;
1037         struct dsd_dma *dsd_ptr;
1038         struct scatterlist *sg;
1039         int     i;
1040         struct scsi_cmnd *cmd;
1041         uint32_t *cur_dsd = dsd;
1042         uint16_t        used_dsds = tot_dsds;
1043         scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1044         uint8_t         *cp;
1045
1046
1047         cmd = sp->cmd;
1048         scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1049                 dma_addr_t      sle_dma;
1050
1051                 /* Allocate additional continuation packets? */
1052                 if (avail_dsds == 0) {
1053                         avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1054                                                 QLA_DSDS_PER_IOCB : used_dsds;
1055                         dsd_list_len = (avail_dsds + 1) * 12;
1056                         used_dsds -= avail_dsds;
1057
1058                         /* allocate tracking DS */
1059                         dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1060                         if (!dsd_ptr)
1061                                 return 1;
1062
1063                         /* allocate new list */
1064                         dsd_ptr->dsd_addr = next_dsd =
1065                             dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1066                                 &dsd_ptr->dsd_list_dma);
1067
1068                         if (!next_dsd) {
1069                                 /*
1070                                  * Need to cleanup only this dsd_ptr, rest
1071                                  * will be done by sp_free_dma()
1072                                  */
1073                                 kfree(dsd_ptr);
1074                                 return 1;
1075                         }
1076
1077                         list_add_tail(&dsd_ptr->list,
1078                             &((struct crc_context *)sp->ctx)->dsd_list);
1079
1080                         sp->flags |= SRB_CRC_CTX_DSD_VALID;
1081
1082                         /* add new list to cmd iocb or last list */
1083                         *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1084                         *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1085                         *cur_dsd++ = dsd_list_len;
1086                         cur_dsd = (uint32_t *)next_dsd;
1087                 }
1088                 sle_dma = sg_dma_address(sg);
1089                 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
1090                         ql_dbg(ql_dbg_io, vha, 0x3027,
1091                             "%s(): %p, sg_entry %d - "
1092                             "addr=0x%x0x%x, len=%d.\n",
1093                             __func__, cur_dsd, i,
1094                             LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg));
1095                 }
1096                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1097                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1098                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1099
1100                 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
1101                         cp = page_address(sg_page(sg)) + sg->offset;
1102                         ql_dbg(ql_dbg_io, vha, 0x3028,
1103                             "%s(): Protection Data buffer = %p.\n", __func__,
1104                             cp);
1105                 }
1106                 avail_dsds--;
1107         }
1108         /* Null termination */
1109         *cur_dsd++ = 0;
1110         *cur_dsd++ = 0;
1111         *cur_dsd++ = 0;
1112         return 0;
1113 }
1114
1115 /**
1116  * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1117  *                                                      Type 6 IOCB types.
1118  *
1119  * @sp: SRB command to process
1120  * @cmd_pkt: Command type 3 IOCB
1121  * @tot_dsds: Total number of segments to transfer
1122  */
1123 static inline int
1124 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1125     uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1126 {
1127         uint32_t                *cur_dsd, *fcp_dl;
1128         scsi_qla_host_t         *vha;
1129         struct scsi_cmnd        *cmd;
1130         struct scatterlist      *cur_seg;
1131         int                     sgc;
1132         uint32_t                total_bytes = 0;
1133         uint32_t                data_bytes;
1134         uint32_t                dif_bytes;
1135         uint8_t                 bundling = 1;
1136         uint16_t                blk_size;
1137         uint8_t                 *clr_ptr;
1138         struct crc_context      *crc_ctx_pkt = NULL;
1139         struct qla_hw_data      *ha;
1140         uint8_t                 additional_fcpcdb_len;
1141         uint16_t                fcp_cmnd_len;
1142         struct fcp_cmnd         *fcp_cmnd;
1143         dma_addr_t              crc_ctx_dma;
1144         char                    tag[2];
1145
1146         cmd = sp->cmd;
1147
1148         sgc = 0;
1149         /* Update entry type to indicate Command Type CRC_2 IOCB */
1150         *((uint32_t *)(&cmd_pkt->entry_type)) =
1151             __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1152
1153         vha = sp->fcport->vha;
1154         ha = vha->hw;
1155
1156         /* No data transfer */
1157         data_bytes = scsi_bufflen(cmd);
1158         if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1159                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1160                 return QLA_SUCCESS;
1161         }
1162
1163         cmd_pkt->vp_index = sp->fcport->vp_idx;
1164
1165         /* Set transfer direction */
1166         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1167                 cmd_pkt->control_flags =
1168                     __constant_cpu_to_le16(CF_WRITE_DATA);
1169         } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1170                 cmd_pkt->control_flags =
1171                     __constant_cpu_to_le16(CF_READ_DATA);
1172         }
1173
1174         if ((scsi_get_prot_op(sp->cmd) == SCSI_PROT_READ_INSERT) ||
1175             (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_STRIP) ||
1176             (scsi_get_prot_op(sp->cmd) == SCSI_PROT_READ_STRIP) ||
1177             (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_INSERT))
1178                 bundling = 0;
1179
1180         /* Allocate CRC context from global pool */
1181         crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
1182             GFP_ATOMIC, &crc_ctx_dma);
1183
1184         if (!crc_ctx_pkt)
1185                 goto crc_queuing_error;
1186
1187         /* Zero out CTX area. */
1188         clr_ptr = (uint8_t *)crc_ctx_pkt;
1189         memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1190
1191         crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1192
1193         sp->flags |= SRB_CRC_CTX_DMA_VALID;
1194
1195         /* Set handle */
1196         crc_ctx_pkt->handle = cmd_pkt->handle;
1197
1198         INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1199
1200         qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
1201             &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1202
1203         cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1204         cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1205         cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1206
1207         /* Determine SCSI command length -- align to 4 byte boundary */
1208         if (cmd->cmd_len > 16) {
1209                 additional_fcpcdb_len = cmd->cmd_len - 16;
1210                 if ((cmd->cmd_len % 4) != 0) {
1211                         /* SCSI cmd > 16 bytes must be multiple of 4 */
1212                         goto crc_queuing_error;
1213                 }
1214                 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1215         } else {
1216                 additional_fcpcdb_len = 0;
1217                 fcp_cmnd_len = 12 + 16 + 4;
1218         }
1219
1220         fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1221
1222         fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1223         if (cmd->sc_data_direction == DMA_TO_DEVICE)
1224                 fcp_cmnd->additional_cdb_len |= 1;
1225         else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1226                 fcp_cmnd->additional_cdb_len |= 2;
1227
1228         int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
1229         memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1230         cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1231         cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1232             LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1233         cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1234             MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1235         fcp_cmnd->task_management = 0;
1236
1237         /*
1238          * Update tagged queuing modifier if using command tag queuing
1239          */
1240         if (scsi_populate_tag_msg(cmd, tag)) {
1241                 switch (tag[0]) {
1242                 case HEAD_OF_QUEUE_TAG:
1243                     fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1244                     break;
1245                 case ORDERED_QUEUE_TAG:
1246                     fcp_cmnd->task_attribute = TSK_ORDERED;
1247                     break;
1248                 default:
1249                     fcp_cmnd->task_attribute = TSK_SIMPLE;
1250                     break;
1251                 }
1252         } else {
1253                 fcp_cmnd->task_attribute = TSK_SIMPLE;
1254         }
1255
1256         cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1257
1258         /* Compute dif len and adjust data len to incude protection */
1259         dif_bytes = 0;
1260         blk_size = cmd->device->sector_size;
1261         dif_bytes = (data_bytes / blk_size) * 8;
1262
1263         switch (scsi_get_prot_op(sp->cmd)) {
1264         case SCSI_PROT_READ_INSERT:
1265         case SCSI_PROT_WRITE_STRIP:
1266             total_bytes = data_bytes;
1267             data_bytes += dif_bytes;
1268             break;
1269
1270         case SCSI_PROT_READ_STRIP:
1271         case SCSI_PROT_WRITE_INSERT:
1272         case SCSI_PROT_READ_PASS:
1273         case SCSI_PROT_WRITE_PASS:
1274             total_bytes = data_bytes + dif_bytes;
1275             break;
1276         default:
1277             BUG();
1278         }
1279
1280         if (!qla2x00_hba_err_chk_enabled(sp))
1281                 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1282
1283         if (!bundling) {
1284                 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1285         } else {
1286                 /*
1287                  * Configure Bundling if we need to fetch interlaving
1288                  * protection PCI accesses
1289                  */
1290                 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1291                 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1292                 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1293                                                         tot_prot_dsds);
1294                 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1295         }
1296
1297         /* Finish the common fields of CRC pkt */
1298         crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1299         crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1300         crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1301         crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1302         /* Fibre channel byte count */
1303         cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1304         fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1305             additional_fcpcdb_len);
1306         *fcp_dl = htonl(total_bytes);
1307
1308         if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1309                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1310                 return QLA_SUCCESS;
1311         }
1312         /* Walks data segments */
1313
1314         cmd_pkt->control_flags |=
1315             __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1316
1317         if (!bundling && tot_prot_dsds) {
1318                 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1319                     cur_dsd, tot_dsds))
1320                         goto crc_queuing_error;
1321         } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1322             (tot_dsds - tot_prot_dsds)))
1323                 goto crc_queuing_error;
1324
1325         if (bundling && tot_prot_dsds) {
1326                 /* Walks dif segments */
1327                 cur_seg = scsi_prot_sglist(cmd);
1328                 cmd_pkt->control_flags |=
1329                         __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1330                 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1331                 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1332                     tot_prot_dsds))
1333                         goto crc_queuing_error;
1334         }
1335         return QLA_SUCCESS;
1336
1337 crc_queuing_error:
1338         /* Cleanup will be performed by the caller */
1339
1340         return QLA_FUNCTION_FAILED;
1341 }
1342
1343 /**
1344  * qla24xx_start_scsi() - Send a SCSI command to the ISP
1345  * @sp: command to send to the ISP
1346  *
1347  * Returns non-zero if a failure occurred, else zero.
1348  */
1349 int
1350 qla24xx_start_scsi(srb_t *sp)
1351 {
1352         int             ret, nseg;
1353         unsigned long   flags;
1354         uint32_t        *clr_ptr;
1355         uint32_t        index;
1356         uint32_t        handle;
1357         struct cmd_type_7 *cmd_pkt;
1358         uint16_t        cnt;
1359         uint16_t        req_cnt;
1360         uint16_t        tot_dsds;
1361         struct req_que *req = NULL;
1362         struct rsp_que *rsp = NULL;
1363         struct scsi_cmnd *cmd = sp->cmd;
1364         struct scsi_qla_host *vha = sp->fcport->vha;
1365         struct qla_hw_data *ha = vha->hw;
1366         char            tag[2];
1367
1368         /* Setup device pointers. */
1369         ret = 0;
1370
1371         qla25xx_set_que(sp, &rsp);
1372         req = vha->req;
1373
1374         /* So we know we haven't pci_map'ed anything yet */
1375         tot_dsds = 0;
1376
1377         /* Send marker if required */
1378         if (vha->marker_needed != 0) {
1379                 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1380                     QLA_SUCCESS)
1381                         return QLA_FUNCTION_FAILED;
1382                 vha->marker_needed = 0;
1383         }
1384
1385         /* Acquire ring specific lock */
1386         spin_lock_irqsave(&ha->hardware_lock, flags);
1387
1388         /* Check for room in outstanding command list. */
1389         handle = req->current_outstanding_cmd;
1390         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1391                 handle++;
1392                 if (handle == MAX_OUTSTANDING_COMMANDS)
1393                         handle = 1;
1394                 if (!req->outstanding_cmds[handle])
1395                         break;
1396         }
1397         if (index == MAX_OUTSTANDING_COMMANDS) {
1398                 goto queuing_error;
1399         }
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 < MAX_OUTSTANDING_COMMANDS; index++) {
1570                 handle++;
1571                 if (handle == MAX_OUTSTANDING_COMMANDS)
1572                         handle = 1;
1573                 if (!req->outstanding_cmds[handle])
1574                         break;
1575         }
1576
1577         if (index == MAX_OUTSTANDING_COMMANDS)
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 < MAX_OUTSTANDING_COMMANDS; index++) {
1752                 handle++;
1753                 if (handle == MAX_OUTSTANDING_COMMANDS)
1754                         handle = 1;
1755                 if (!req->outstanding_cmds[handle])
1756                         break;
1757         }
1758         if (index == MAX_OUTSTANDING_COMMANDS) {
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                             vha->hw->req_q_map[0]);
2081                         cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2082                         avail_dsds = 5;
2083                         cont_iocb_prsnt = 1;
2084                         entry_count++;
2085                 }
2086
2087                 sle_dma = sg_dma_address(sg);
2088                 *cur_dsd++   = cpu_to_le32(LSD(sle_dma));
2089                 *cur_dsd++   = cpu_to_le32(MSD(sle_dma));
2090                 *cur_dsd++   = cpu_to_le32(sg_dma_len(sg));
2091                 loop_iterartion++;
2092                 avail_dsds--;
2093         }
2094         ct_iocb->entry_count = entry_count;
2095 }
2096
2097 static void
2098 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2099 {
2100         uint16_t        avail_dsds;
2101         uint32_t        *cur_dsd;
2102         struct scatterlist *sg;
2103         int index;
2104         uint16_t tot_dsds;
2105         scsi_qla_host_t *vha = sp->fcport->vha;
2106         struct qla_hw_data *ha = vha->hw;
2107         struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
2108         int loop_iterartion = 0;
2109         int cont_iocb_prsnt = 0;
2110         int entry_count = 1;
2111
2112         ct_iocb->entry_type = CT_IOCB_TYPE;
2113         ct_iocb->entry_status = 0;
2114         ct_iocb->sys_define = 0;
2115         ct_iocb->handle = sp->handle;
2116
2117         ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2118         ct_iocb->vp_index = sp->fcport->vp_idx;
2119         ct_iocb->comp_status = __constant_cpu_to_le16(0);
2120
2121         ct_iocb->cmd_dsd_count =
2122             __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2123         ct_iocb->timeout = 0;
2124         ct_iocb->rsp_dsd_count =
2125             __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2126         ct_iocb->rsp_byte_count =
2127             cpu_to_le32(bsg_job->reply_payload.payload_len);
2128         ct_iocb->cmd_byte_count =
2129             cpu_to_le32(bsg_job->request_payload.payload_len);
2130         ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2131             (bsg_job->request_payload.sg_list)));
2132         ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2133            (bsg_job->request_payload.sg_list)));
2134         ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2135             (bsg_job->request_payload.sg_list));
2136
2137         avail_dsds = 1;
2138         cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2139         index = 0;
2140         tot_dsds = bsg_job->reply_payload.sg_cnt;
2141
2142         for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2143                 dma_addr_t       sle_dma;
2144                 cont_a64_entry_t *cont_pkt;
2145
2146                 /* Allocate additional continuation packets? */
2147                 if (avail_dsds == 0) {
2148                         /*
2149                         * Five DSDs are available in the Cont.
2150                         * Type 1 IOCB.
2151                                */
2152                         cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2153                             ha->req_q_map[0]);
2154                         cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2155                         avail_dsds = 5;
2156                         cont_iocb_prsnt = 1;
2157                         entry_count++;
2158                 }
2159
2160                 sle_dma = sg_dma_address(sg);
2161                 *cur_dsd++   = cpu_to_le32(LSD(sle_dma));
2162                 *cur_dsd++   = cpu_to_le32(MSD(sle_dma));
2163                 *cur_dsd++   = cpu_to_le32(sg_dma_len(sg));
2164                 loop_iterartion++;
2165                 avail_dsds--;
2166         }
2167         ct_iocb->entry_count = entry_count;
2168 }
2169
2170 int
2171 qla2x00_start_sp(srb_t *sp)
2172 {
2173         int rval;
2174         struct qla_hw_data *ha = sp->fcport->vha->hw;
2175         void *pkt;
2176         struct srb_ctx *ctx = sp->ctx;
2177         unsigned long flags;
2178
2179         rval = QLA_FUNCTION_FAILED;
2180         spin_lock_irqsave(&ha->hardware_lock, flags);
2181         pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
2182         if (!pkt) {
2183                 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2184                     "qla2x00_alloc_iocbs failed.\n");
2185                 goto done;
2186         }
2187
2188         rval = QLA_SUCCESS;
2189         switch (ctx->type) {
2190         case SRB_LOGIN_CMD:
2191                 IS_FWI2_CAPABLE(ha) ?
2192                     qla24xx_login_iocb(sp, pkt) :
2193                     qla2x00_login_iocb(sp, pkt);
2194                 break;
2195         case SRB_LOGOUT_CMD:
2196                 IS_FWI2_CAPABLE(ha) ?
2197                     qla24xx_logout_iocb(sp, pkt) :
2198                     qla2x00_logout_iocb(sp, pkt);
2199                 break;
2200         case SRB_ELS_CMD_RPT:
2201         case SRB_ELS_CMD_HST:
2202                 qla24xx_els_iocb(sp, pkt);
2203                 break;
2204         case SRB_CT_CMD:
2205                 IS_FWI2_CAPABLE(ha) ?
2206                 qla24xx_ct_iocb(sp, pkt) :
2207                 qla2x00_ct_iocb(sp, pkt);
2208                 break;
2209         case SRB_ADISC_CMD:
2210                 IS_FWI2_CAPABLE(ha) ?
2211                     qla24xx_adisc_iocb(sp, pkt) :
2212                     qla2x00_adisc_iocb(sp, pkt);
2213                 break;
2214         case SRB_TM_CMD:
2215                 qla24xx_tm_iocb(sp, pkt);
2216                 break;
2217         default:
2218                 break;
2219         }
2220
2221         wmb();
2222         qla2x00_start_iocbs(sp);
2223 done:
2224         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2225         return rval;
2226 }