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