]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/scsi/qla2xxx/qla_iocb.c
[SCSI] qla2xxx: add support for multi-queue adapter
[net-next-2.6.git] / drivers / scsi / qla2xxx / qla_iocb.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 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 request_t *qla2x00_req_pkt(struct scsi_qla_host *, struct req_que *,
15                                                         struct rsp_que *rsp);
16 static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
17
18 /**
19  * qla2x00_get_cmd_direction() - Determine control_flag data direction.
20  * @cmd: SCSI command
21  *
22  * Returns the proper CF_* direction based on CDB.
23  */
24 static inline uint16_t
25 qla2x00_get_cmd_direction(srb_t *sp)
26 {
27         uint16_t cflags;
28
29         cflags = 0;
30
31         /* Set transfer direction */
32         if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
33                 cflags = CF_WRITE;
34                 sp->fcport->vha->hw->qla_stats.output_bytes +=
35                     scsi_bufflen(sp->cmd);
36         } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
37                 cflags = CF_READ;
38                 sp->fcport->vha->hw->qla_stats.input_bytes +=
39                     scsi_bufflen(sp->cmd);
40         }
41         return (cflags);
42 }
43
44 /**
45  * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
46  * Continuation Type 0 IOCBs to allocate.
47  *
48  * @dsds: number of data segment decriptors needed
49  *
50  * Returns the number of IOCB entries needed to store @dsds.
51  */
52 uint16_t
53 qla2x00_calc_iocbs_32(uint16_t dsds)
54 {
55         uint16_t iocbs;
56
57         iocbs = 1;
58         if (dsds > 3) {
59                 iocbs += (dsds - 3) / 7;
60                 if ((dsds - 3) % 7)
61                         iocbs++;
62         }
63         return (iocbs);
64 }
65
66 /**
67  * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
68  * Continuation Type 1 IOCBs to allocate.
69  *
70  * @dsds: number of data segment decriptors needed
71  *
72  * Returns the number of IOCB entries needed to store @dsds.
73  */
74 uint16_t
75 qla2x00_calc_iocbs_64(uint16_t dsds)
76 {
77         uint16_t iocbs;
78
79         iocbs = 1;
80         if (dsds > 2) {
81                 iocbs += (dsds - 2) / 5;
82                 if ((dsds - 2) % 5)
83                         iocbs++;
84         }
85         return (iocbs);
86 }
87
88 /**
89  * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
90  * @ha: HA context
91  *
92  * Returns a pointer to the Continuation Type 0 IOCB packet.
93  */
94 static inline cont_entry_t *
95 qla2x00_prep_cont_type0_iocb(struct req_que *req, struct scsi_qla_host *vha)
96 {
97         cont_entry_t *cont_pkt;
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(struct req_que *req, scsi_qla_host_t *vha)
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 /**
146  * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
147  * capable IOCB types.
148  *
149  * @sp: SRB command to process
150  * @cmd_pkt: Command type 2 IOCB
151  * @tot_dsds: Total number of segments to transfer
152  */
153 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
154     uint16_t tot_dsds)
155 {
156         uint16_t        avail_dsds;
157         uint32_t        *cur_dsd;
158         scsi_qla_host_t *vha;
159         struct scsi_cmnd *cmd;
160         struct scatterlist *sg;
161         int i;
162         struct req_que *req;
163         uint16_t que_id;
164
165         cmd = sp->cmd;
166
167         /* Update entry type to indicate Command Type 2 IOCB */
168         *((uint32_t *)(&cmd_pkt->entry_type)) =
169             __constant_cpu_to_le32(COMMAND_TYPE);
170
171         /* No data transfer */
172         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
173                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
174                 return;
175         }
176
177         vha = sp->vha;
178         que_id = vha->req_ques[0];
179         req = vha->hw->req_q_map[que_id];
180
181         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
182
183         /* Three DSDs are available in the Command Type 2 IOCB */
184         avail_dsds = 3;
185         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
186
187         /* Load data segments */
188         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
189                 cont_entry_t *cont_pkt;
190
191                 /* Allocate additional continuation packets? */
192                 if (avail_dsds == 0) {
193                         /*
194                          * Seven DSDs are available in the Continuation
195                          * Type 0 IOCB.
196                          */
197                         cont_pkt = qla2x00_prep_cont_type0_iocb(req, vha);
198                         cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
199                         avail_dsds = 7;
200                 }
201
202                 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
203                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
204                 avail_dsds--;
205         }
206 }
207
208 /**
209  * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
210  * capable IOCB types.
211  *
212  * @sp: SRB command to process
213  * @cmd_pkt: Command type 3 IOCB
214  * @tot_dsds: Total number of segments to transfer
215  */
216 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
217     uint16_t tot_dsds)
218 {
219         uint16_t        avail_dsds;
220         uint32_t        *cur_dsd;
221         scsi_qla_host_t *vha;
222         struct scsi_cmnd *cmd;
223         struct scatterlist *sg;
224         int i;
225         struct req_que *req;
226         uint16_t que_id;
227
228         cmd = sp->cmd;
229
230         /* Update entry type to indicate Command Type 3 IOCB */
231         *((uint32_t *)(&cmd_pkt->entry_type)) =
232             __constant_cpu_to_le32(COMMAND_A64_TYPE);
233
234         /* No data transfer */
235         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
236                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
237                 return;
238         }
239
240         vha = sp->vha;
241         que_id = vha->req_ques[0];
242         req = vha->hw->req_q_map[que_id];
243
244         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
245
246         /* Two DSDs are available in the Command Type 3 IOCB */
247         avail_dsds = 2;
248         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
249
250         /* Load data segments */
251         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
252                 dma_addr_t      sle_dma;
253                 cont_a64_entry_t *cont_pkt;
254
255                 /* Allocate additional continuation packets? */
256                 if (avail_dsds == 0) {
257                         /*
258                          * Five DSDs are available in the Continuation
259                          * Type 1 IOCB.
260                          */
261                         cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha);
262                         cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
263                         avail_dsds = 5;
264                 }
265
266                 sle_dma = sg_dma_address(sg);
267                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
268                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
269                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
270                 avail_dsds--;
271         }
272 }
273
274 /**
275  * qla2x00_start_scsi() - Send a SCSI command to the ISP
276  * @sp: command to send to the ISP
277  *
278  * Returns non-zero if a failure occurred, else zero.
279  */
280 int
281 qla2x00_start_scsi(srb_t *sp)
282 {
283         int             ret, nseg;
284         unsigned long   flags;
285         scsi_qla_host_t *vha;
286         struct scsi_cmnd *cmd;
287         uint32_t        *clr_ptr;
288         uint32_t        index;
289         uint32_t        handle;
290         cmd_entry_t     *cmd_pkt;
291         uint16_t        cnt;
292         uint16_t        req_cnt;
293         uint16_t        tot_dsds;
294         struct device_reg_2xxx __iomem *reg;
295         struct qla_hw_data *ha;
296         struct req_que *req;
297         struct rsp_que *rsp;
298
299         /* Setup device pointers. */
300         ret = 0;
301         vha = sp->vha;
302         ha = vha->hw;
303         reg = &ha->iobase->isp;
304         cmd = sp->cmd;
305         req = ha->req_q_map[0];
306         rsp = ha->rsp_q_map[0];
307         /* So we know we haven't pci_map'ed anything yet */
308         tot_dsds = 0;
309
310         /* Send marker if required */
311         if (vha->marker_needed != 0) {
312                 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
313                                                         != QLA_SUCCESS)
314                         return (QLA_FUNCTION_FAILED);
315                 vha->marker_needed = 0;
316         }
317
318         /* Acquire ring specific lock */
319         spin_lock_irqsave(&ha->hardware_lock, flags);
320
321         /* Check for room in outstanding command list. */
322         handle = req->current_outstanding_cmd;
323         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
324                 handle++;
325                 if (handle == MAX_OUTSTANDING_COMMANDS)
326                         handle = 1;
327                 if (!req->outstanding_cmds[handle])
328                         break;
329         }
330         if (index == MAX_OUTSTANDING_COMMANDS)
331                 goto queuing_error;
332
333         /* Map the sg table so we have an accurate count of sg entries needed */
334         if (scsi_sg_count(cmd)) {
335                 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
336                     scsi_sg_count(cmd), cmd->sc_data_direction);
337                 if (unlikely(!nseg))
338                         goto queuing_error;
339         } else
340                 nseg = 0;
341
342         tot_dsds = nseg;
343
344         /* Calculate the number of request entries needed. */
345         req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
346         if (req->cnt < (req_cnt + 2)) {
347                 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
348                 if (req->ring_index < cnt)
349                         req->cnt = cnt - req->ring_index;
350                 else
351                         req->cnt = req->length -
352                             (req->ring_index - cnt);
353         }
354         if (req->cnt < (req_cnt + 2))
355                 goto queuing_error;
356
357         /* Build command packet */
358         req->current_outstanding_cmd = handle;
359         req->outstanding_cmds[handle] = sp;
360         sp->vha = vha;
361         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
362         req->cnt -= req_cnt;
363
364         cmd_pkt = (cmd_entry_t *)req->ring_ptr;
365         cmd_pkt->handle = handle;
366         /* Zero out remaining portion of packet. */
367         clr_ptr = (uint32_t *)cmd_pkt + 2;
368         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
369         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
370
371         /* Set target ID and LUN number*/
372         SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
373         cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
374
375         /* Update tagged queuing modifier */
376         cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
377
378         /* Load SCSI command packet. */
379         memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
380         cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
381
382         /* Build IOCB segments */
383         ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
384
385         /* Set total data segment count. */
386         cmd_pkt->entry_count = (uint8_t)req_cnt;
387         wmb();
388
389         /* Adjust ring index. */
390         req->ring_index++;
391         if (req->ring_index == req->length) {
392                 req->ring_index = 0;
393                 req->ring_ptr = req->ring;
394         } else
395                 req->ring_ptr++;
396
397         sp->flags |= SRB_DMA_VALID;
398
399         /* Set chip new ring index. */
400         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
401         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
402
403         /* Manage unprocessed RIO/ZIO commands in response queue. */
404         if (vha->flags.process_response_queue &&
405             rsp->ring_ptr->signature != RESPONSE_PROCESSED)
406                 qla2x00_process_response_queue(rsp);
407
408         spin_unlock_irqrestore(&ha->hardware_lock, flags);
409         return (QLA_SUCCESS);
410
411 queuing_error:
412         if (tot_dsds)
413                 scsi_dma_unmap(cmd);
414
415         spin_unlock_irqrestore(&ha->hardware_lock, flags);
416
417         return (QLA_FUNCTION_FAILED);
418 }
419
420 /**
421  * qla2x00_marker() - Send a marker IOCB to the firmware.
422  * @ha: HA context
423  * @loop_id: loop ID
424  * @lun: LUN
425  * @type: marker modifier
426  *
427  * Can be called from both normal and interrupt context.
428  *
429  * Returns non-zero if a failure occurred, else zero.
430  */
431 int
432 __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
433                         struct rsp_que *rsp, uint16_t loop_id,
434                         uint16_t lun, uint8_t type)
435 {
436         mrk_entry_t *mrk;
437         struct mrk_entry_24xx *mrk24;
438         struct qla_hw_data *ha = vha->hw;
439         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
440
441         mrk24 = NULL;
442         mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp);
443         if (mrk == NULL) {
444                 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
445                     __func__, base_vha->host_no));
446
447                 return (QLA_FUNCTION_FAILED);
448         }
449
450         mrk->entry_type = MARKER_TYPE;
451         mrk->modifier = type;
452         if (type != MK_SYNC_ALL) {
453                 if (IS_FWI2_CAPABLE(ha)) {
454                         mrk24 = (struct mrk_entry_24xx *) mrk;
455                         mrk24->nport_handle = cpu_to_le16(loop_id);
456                         mrk24->lun[1] = LSB(lun);
457                         mrk24->lun[2] = MSB(lun);
458                         host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
459                         mrk24->vp_index = vha->vp_idx;
460                 } else {
461                         SET_TARGET_ID(ha, mrk->target, loop_id);
462                         mrk->lun = cpu_to_le16(lun);
463                 }
464         }
465         wmb();
466
467         qla2x00_isp_cmd(vha, req);
468
469         return (QLA_SUCCESS);
470 }
471
472 int
473 qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
474                 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
475                 uint8_t type)
476 {
477         int ret;
478         unsigned long flags = 0;
479
480         spin_lock_irqsave(&vha->hw->hardware_lock, flags);
481         ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
482         spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
483
484         return (ret);
485 }
486
487 /**
488  * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
489  * @ha: HA context
490  *
491  * Note: The caller must hold the hardware lock before calling this routine.
492  *
493  * Returns NULL if function failed, else, a pointer to the request packet.
494  */
495 static request_t *
496 qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req,
497                 struct rsp_que *rsp)
498 {
499         struct qla_hw_data *ha = vha->hw;
500         device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
501         request_t       *pkt = NULL;
502         uint16_t        cnt;
503         uint32_t        *dword_ptr;
504         uint32_t        timer;
505         uint16_t        req_cnt = 1;
506
507         /* Wait 1 second for slot. */
508         for (timer = HZ; timer; timer--) {
509                 if ((req_cnt + 2) >= req->cnt) {
510                         /* Calculate number of free request entries. */
511                         if (ha->mqenable)
512                                 cnt = (uint16_t)
513                                         RD_REG_DWORD(&reg->isp25mq.req_q_out);
514                         else {
515                                 if (IS_FWI2_CAPABLE(ha))
516                                         cnt = (uint16_t)RD_REG_DWORD(
517                                                 &reg->isp24.req_q_out);
518                                 else
519                                         cnt = qla2x00_debounce_register(
520                                                 ISP_REQ_Q_OUT(ha, &reg->isp));
521                         }
522                         if  (req->ring_index < cnt)
523                                 req->cnt = cnt - req->ring_index;
524                         else
525                                 req->cnt = req->length -
526                                     (req->ring_index - cnt);
527                 }
528                 /* If room for request in request ring. */
529                 if ((req_cnt + 2) < req->cnt) {
530                         req->cnt--;
531                         pkt = req->ring_ptr;
532
533                         /* Zero out packet. */
534                         dword_ptr = (uint32_t *)pkt;
535                         for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
536                                 *dword_ptr++ = 0;
537
538                         /* Set system defined field. */
539                         pkt->sys_define = (uint8_t)req->ring_index;
540
541                         /* Set entry count. */
542                         pkt->entry_count = 1;
543
544                         break;
545                 }
546
547                 /* Release ring specific lock */
548                 spin_unlock_irq(&ha->hardware_lock);
549
550                 udelay(2);   /* 2 us */
551
552                 /* Check for pending interrupts. */
553                 /* During init we issue marker directly */
554                 if (!vha->marker_needed && !vha->flags.init_done)
555                         qla2x00_poll(rsp);
556                 spin_lock_irq(&ha->hardware_lock);
557         }
558         if (!pkt) {
559                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
560         }
561
562         return (pkt);
563 }
564
565 /**
566  * qla2x00_isp_cmd() - Modify the request ring pointer.
567  * @ha: HA context
568  *
569  * Note: The caller must hold the hardware lock before calling this routine.
570  */
571 static void
572 qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
573 {
574         struct qla_hw_data *ha = vha->hw;
575         device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
576
577         DEBUG5(printk("%s(): IOCB data:\n", __func__));
578         DEBUG5(qla2x00_dump_buffer(
579             (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
580
581         /* Adjust ring index. */
582         req->ring_index++;
583         if (req->ring_index == req->length) {
584                 req->ring_index = 0;
585                 req->ring_ptr = req->ring;
586         } else
587                 req->ring_ptr++;
588
589         /* Set chip new ring index. */
590         if (ha->mqenable)
591                 RD_REG_DWORD(&reg->isp25mq.req_q_out);
592         else {
593                 if (IS_FWI2_CAPABLE(ha)) {
594                         WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
595                         RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
596                 } else {
597                         WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
598                                 req->ring_index);
599                         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
600                 }
601         }
602
603 }
604
605 /**
606  * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
607  * Continuation Type 1 IOCBs to allocate.
608  *
609  * @dsds: number of data segment decriptors needed
610  *
611  * Returns the number of IOCB entries needed to store @dsds.
612  */
613 static inline uint16_t
614 qla24xx_calc_iocbs(uint16_t dsds)
615 {
616         uint16_t iocbs;
617
618         iocbs = 1;
619         if (dsds > 1) {
620                 iocbs += (dsds - 1) / 5;
621                 if ((dsds - 1) % 5)
622                         iocbs++;
623         }
624         return iocbs;
625 }
626
627 /**
628  * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
629  * IOCB types.
630  *
631  * @sp: SRB command to process
632  * @cmd_pkt: Command type 3 IOCB
633  * @tot_dsds: Total number of segments to transfer
634  */
635 static inline void
636 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
637     uint16_t tot_dsds)
638 {
639         uint16_t        avail_dsds;
640         uint32_t        *cur_dsd;
641         scsi_qla_host_t *vha;
642         struct scsi_cmnd *cmd;
643         struct scatterlist *sg;
644         int i;
645         uint16_t que_id;
646         struct req_que *req;
647
648         cmd = sp->cmd;
649
650         /* Update entry type to indicate Command Type 3 IOCB */
651         *((uint32_t *)(&cmd_pkt->entry_type)) =
652             __constant_cpu_to_le32(COMMAND_TYPE_7);
653
654         /* No data transfer */
655         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
656                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
657                 return;
658         }
659
660         vha = sp->vha;
661         que_id = vha->req_ques[0];
662         req = vha->hw->req_q_map[que_id];
663
664         /* Set transfer direction */
665         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
666                 cmd_pkt->task_mgmt_flags =
667                     __constant_cpu_to_le16(TMF_WRITE_DATA);
668                 sp->fcport->vha->hw->qla_stats.output_bytes +=
669                     scsi_bufflen(sp->cmd);
670         } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
671                 cmd_pkt->task_mgmt_flags =
672                     __constant_cpu_to_le16(TMF_READ_DATA);
673                 sp->fcport->vha->hw->qla_stats.input_bytes +=
674                     scsi_bufflen(sp->cmd);
675         }
676
677         /* One DSD is available in the Command Type 3 IOCB */
678         avail_dsds = 1;
679         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
680
681         /* Load data segments */
682
683         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
684                 dma_addr_t      sle_dma;
685                 cont_a64_entry_t *cont_pkt;
686
687                 /* Allocate additional continuation packets? */
688                 if (avail_dsds == 0) {
689                         /*
690                          * Five DSDs are available in the Continuation
691                          * Type 1 IOCB.
692                          */
693                         cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha);
694                         cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
695                         avail_dsds = 5;
696                 }
697
698                 sle_dma = sg_dma_address(sg);
699                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
700                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
701                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
702                 avail_dsds--;
703         }
704 }
705
706
707 /**
708  * qla24xx_start_scsi() - Send a SCSI command to the ISP
709  * @sp: command to send to the ISP
710  *
711  * Returns non-zero if a failure occurred, else zero.
712  */
713 int
714 qla24xx_start_scsi(srb_t *sp)
715 {
716         int             ret, nseg;
717         unsigned long   flags;
718         uint32_t        *clr_ptr;
719         uint32_t        index;
720         uint32_t        handle;
721         struct cmd_type_7 *cmd_pkt;
722         uint16_t        cnt;
723         uint16_t        req_cnt;
724         uint16_t        tot_dsds;
725         struct req_que *req = NULL;
726         struct rsp_que *rsp = NULL;
727         struct scsi_cmnd *cmd = sp->cmd;
728         struct scsi_qla_host *vha = sp->vha;
729         struct qla_hw_data *ha = vha->hw;
730         device_reg_t __iomem *reg;
731         uint16_t que_id;
732
733         /* Setup device pointers. */
734         ret = 0;
735         que_id = vha->req_ques[0];
736
737         req = ha->req_q_map[que_id];
738         reg = ISP_QUE_REG(ha, req->id);
739
740         if (req->rsp)
741                 rsp = req->rsp;
742         else
743                 rsp = ha->rsp_q_map[que_id];
744         /* So we know we haven't pci_map'ed anything yet */
745         tot_dsds = 0;
746
747         /* Send marker if required */
748         if (vha->marker_needed != 0) {
749                 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
750                                                         != QLA_SUCCESS)
751                         return QLA_FUNCTION_FAILED;
752                 vha->marker_needed = 0;
753         }
754
755         /* Acquire ring specific lock */
756         spin_lock_irqsave(&ha->hardware_lock, flags);
757
758         /* Check for room in outstanding command list. */
759         handle = req->current_outstanding_cmd;
760         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
761                 handle++;
762                 if (handle == MAX_OUTSTANDING_COMMANDS)
763                         handle = 1;
764                 if (!req->outstanding_cmds[handle])
765                         break;
766         }
767         if (index == MAX_OUTSTANDING_COMMANDS)
768                 goto queuing_error;
769
770         /* Map the sg table so we have an accurate count of sg entries needed */
771         if (scsi_sg_count(cmd)) {
772                 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
773                     scsi_sg_count(cmd), cmd->sc_data_direction);
774                 if (unlikely(!nseg))
775                         goto queuing_error;
776         } else
777                 nseg = 0;
778
779         tot_dsds = nseg;
780
781         req_cnt = qla24xx_calc_iocbs(tot_dsds);
782         if (req->cnt < (req_cnt + 2)) {
783                 if (ha->mqenable)
784                         cnt = (uint16_t)
785                                 RD_REG_DWORD_RELAXED(&reg->isp25mq.req_q_out);
786                 else
787                         cnt = (uint16_t)
788                                 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_out);
789
790                 if (req->ring_index < cnt)
791                         req->cnt = cnt - req->ring_index;
792                 else
793                         req->cnt = req->length -
794                                 (req->ring_index - cnt);
795         }
796         if (req->cnt < (req_cnt + 2))
797                 goto queuing_error;
798
799         /* Build command packet. */
800         req->current_outstanding_cmd = handle;
801         req->outstanding_cmds[handle] = sp;
802         sp->vha = vha;
803         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
804         req->cnt -= req_cnt;
805
806         cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
807         cmd_pkt->handle = handle;
808
809         /* Zero out remaining portion of packet. */
810         /*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
811         clr_ptr = (uint32_t *)cmd_pkt + 2;
812         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
813         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
814
815         /* Set NPORT-ID and LUN number*/
816         cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
817         cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
818         cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
819         cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
820         cmd_pkt->vp_index = sp->fcport->vp_idx;
821
822         int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
823         host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
824
825         /* Load SCSI command packet. */
826         memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
827         host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
828
829         cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
830
831         /* Build IOCB segments */
832         qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
833
834         /* Set total data segment count. */
835         cmd_pkt->entry_count = (uint8_t)req_cnt;
836         wmb();
837
838         /* Adjust ring index. */
839         req->ring_index++;
840         if (req->ring_index == req->length) {
841                 req->ring_index = 0;
842                 req->ring_ptr = req->ring;
843         } else
844                 req->ring_ptr++;
845
846         sp->flags |= SRB_DMA_VALID;
847
848         /* Set chip new ring index. */
849         if (ha->mqenable)
850                 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
851         else {
852                 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
853                 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
854         }
855
856         /* Manage unprocessed RIO/ZIO commands in response queue. */
857         if (vha->flags.process_response_queue &&
858                 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
859                 qla24xx_process_response_queue(rsp);
860
861         spin_unlock_irqrestore(&ha->hardware_lock, flags);
862         return QLA_SUCCESS;
863
864 queuing_error:
865         if (tot_dsds)
866                 scsi_dma_unmap(cmd);
867
868         spin_unlock_irqrestore(&ha->hardware_lock, flags);
869
870         return QLA_FUNCTION_FAILED;
871 }
872