rt2x00: Restrict DMA to 32-bit addresses.
[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;
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         reg = &ha->iobase->isp;
287         cmd = sp->cmd;
288         /* So we know we haven't pci_map'ed anything yet */
289         tot_dsds = 0;
290
291         /* Send marker if required */
292         if (ha->marker_needed != 0) {
293                 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
294                         return (QLA_FUNCTION_FAILED);
295                 }
296                 ha->marker_needed = 0;
297         }
298
299         /* Acquire ring specific lock */
300         spin_lock_irqsave(&ha->hardware_lock, flags);
301
302         /* Check for room in outstanding command list. */
303         handle = ha->current_outstanding_cmd;
304         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
305                 handle++;
306                 if (handle == MAX_OUTSTANDING_COMMANDS)
307                         handle = 1;
308                 if (!ha->outstanding_cmds[handle])
309                         break;
310         }
311         if (index == MAX_OUTSTANDING_COMMANDS)
312                 goto queuing_error;
313
314         /* Map the sg table so we have an accurate count of sg entries needed */
315         if (scsi_sg_count(cmd)) {
316                 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
317                     scsi_sg_count(cmd), cmd->sc_data_direction);
318                 if (unlikely(!nseg))
319                         goto queuing_error;
320         } else
321                 nseg = 0;
322
323         tot_dsds = nseg;
324
325         /* Calculate the number of request entries needed. */
326         req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
327         if (ha->req_q_cnt < (req_cnt + 2)) {
328                 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
329                 if (ha->req_ring_index < cnt)
330                         ha->req_q_cnt = cnt - ha->req_ring_index;
331                 else
332                         ha->req_q_cnt = ha->request_q_length -
333                             (ha->req_ring_index - cnt);
334         }
335         if (ha->req_q_cnt < (req_cnt + 2))
336                 goto queuing_error;
337
338         /* Build command packet */
339         ha->current_outstanding_cmd = handle;
340         ha->outstanding_cmds[handle] = sp;
341         sp->ha = ha;
342         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
343         ha->req_q_cnt -= req_cnt;
344
345         cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
346         cmd_pkt->handle = handle;
347         /* Zero out remaining portion of packet. */
348         clr_ptr = (uint32_t *)cmd_pkt + 2;
349         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
350         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
351
352         /* Set target ID and LUN number*/
353         SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
354         cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
355
356         /* Update tagged queuing modifier */
357         cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
358
359         /* Load SCSI command packet. */
360         memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
361         cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
362
363         /* Build IOCB segments */
364         ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
365
366         /* Set total data segment count. */
367         cmd_pkt->entry_count = (uint8_t)req_cnt;
368         wmb();
369
370         /* Adjust ring index. */
371         ha->req_ring_index++;
372         if (ha->req_ring_index == ha->request_q_length) {
373                 ha->req_ring_index = 0;
374                 ha->request_ring_ptr = ha->request_ring;
375         } else
376                 ha->request_ring_ptr++;
377
378         sp->flags |= SRB_DMA_VALID;
379
380         /* Set chip new ring index. */
381         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
382         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
383
384         /* Manage unprocessed RIO/ZIO commands in response queue. */
385         if (ha->flags.process_response_queue &&
386             ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
387                 qla2x00_process_response_queue(ha);
388
389         spin_unlock_irqrestore(&ha->hardware_lock, flags);
390         return (QLA_SUCCESS);
391
392 queuing_error:
393         if (tot_dsds)
394                 scsi_dma_unmap(cmd);
395
396         spin_unlock_irqrestore(&ha->hardware_lock, flags);
397
398         return (QLA_FUNCTION_FAILED);
399 }
400
401 /**
402  * qla2x00_marker() - Send a marker IOCB to the firmware.
403  * @ha: HA context
404  * @loop_id: loop ID
405  * @lun: LUN
406  * @type: marker modifier
407  *
408  * Can be called from both normal and interrupt context.
409  *
410  * Returns non-zero if a failure occured, else zero.
411  */
412 int
413 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
414     uint8_t type)
415 {
416         mrk_entry_t *mrk;
417         struct mrk_entry_24xx *mrk24;
418         scsi_qla_host_t *pha = to_qla_parent(ha);
419
420         mrk24 = NULL;
421         mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
422         if (mrk == NULL) {
423                 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
424                     __func__, ha->host_no));
425
426                 return (QLA_FUNCTION_FAILED);
427         }
428
429         mrk->entry_type = MARKER_TYPE;
430         mrk->modifier = type;
431         if (type != MK_SYNC_ALL) {
432                 if (IS_FWI2_CAPABLE(ha)) {
433                         mrk24 = (struct mrk_entry_24xx *) mrk;
434                         mrk24->nport_handle = cpu_to_le16(loop_id);
435                         mrk24->lun[1] = LSB(lun);
436                         mrk24->lun[2] = MSB(lun);
437                         host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
438                         mrk24->vp_index = ha->vp_idx;
439                 } else {
440                         SET_TARGET_ID(ha, mrk->target, loop_id);
441                         mrk->lun = cpu_to_le16(lun);
442                 }
443         }
444         wmb();
445
446         qla2x00_isp_cmd(pha);
447
448         return (QLA_SUCCESS);
449 }
450
451 int
452 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
453     uint8_t type)
454 {
455         int ret;
456         unsigned long flags = 0;
457
458         spin_lock_irqsave(&ha->hardware_lock, flags);
459         ret = __qla2x00_marker(ha, loop_id, lun, type);
460         spin_unlock_irqrestore(&ha->hardware_lock, flags);
461
462         return (ret);
463 }
464
465 /**
466  * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
467  * @ha: HA context
468  *
469  * Note: The caller must hold the hardware lock before calling this routine.
470  *
471  * Returns NULL if function failed, else, a pointer to the request packet.
472  */
473 static request_t *
474 qla2x00_req_pkt(scsi_qla_host_t *ha)
475 {
476         device_reg_t __iomem *reg = ha->iobase;
477         request_t       *pkt = NULL;
478         uint16_t        cnt;
479         uint32_t        *dword_ptr;
480         uint32_t        timer;
481         uint16_t        req_cnt = 1;
482
483         /* Wait 1 second for slot. */
484         for (timer = HZ; timer; timer--) {
485                 if ((req_cnt + 2) >= ha->req_q_cnt) {
486                         /* Calculate number of free request entries. */
487                         if (IS_FWI2_CAPABLE(ha))
488                                 cnt = (uint16_t)RD_REG_DWORD(
489                                     &reg->isp24.req_q_out);
490                         else
491                                 cnt = qla2x00_debounce_register(
492                                     ISP_REQ_Q_OUT(ha, &reg->isp));
493                         if  (ha->req_ring_index < cnt)
494                                 ha->req_q_cnt = cnt - ha->req_ring_index;
495                         else
496                                 ha->req_q_cnt = ha->request_q_length -
497                                     (ha->req_ring_index - cnt);
498                 }
499                 /* If room for request in request ring. */
500                 if ((req_cnt + 2) < ha->req_q_cnt) {
501                         ha->req_q_cnt--;
502                         pkt = ha->request_ring_ptr;
503
504                         /* Zero out packet. */
505                         dword_ptr = (uint32_t *)pkt;
506                         for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
507                                 *dword_ptr++ = 0;
508
509                         /* Set system defined field. */
510                         pkt->sys_define = (uint8_t)ha->req_ring_index;
511
512                         /* Set entry count. */
513                         pkt->entry_count = 1;
514
515                         break;
516                 }
517
518                 /* Release ring specific lock */
519                 spin_unlock(&ha->hardware_lock);
520
521                 udelay(2);   /* 2 us */
522
523                 /* Check for pending interrupts. */
524                 /* During init we issue marker directly */
525                 if (!ha->marker_needed && !ha->flags.init_done)
526                         qla2x00_poll(ha);
527
528                 spin_lock_irq(&ha->hardware_lock);
529         }
530         if (!pkt) {
531                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
532         }
533
534         return (pkt);
535 }
536
537 /**
538  * qla2x00_isp_cmd() - Modify the request ring pointer.
539  * @ha: HA context
540  *
541  * Note: The caller must hold the hardware lock before calling this routine.
542  */
543 static void
544 qla2x00_isp_cmd(scsi_qla_host_t *ha)
545 {
546         device_reg_t __iomem *reg = ha->iobase;
547
548         DEBUG5(printk("%s(): IOCB data:\n", __func__));
549         DEBUG5(qla2x00_dump_buffer(
550             (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
551
552         /* Adjust ring index. */
553         ha->req_ring_index++;
554         if (ha->req_ring_index == ha->request_q_length) {
555                 ha->req_ring_index = 0;
556                 ha->request_ring_ptr = ha->request_ring;
557         } else
558                 ha->request_ring_ptr++;
559
560         /* Set chip new ring index. */
561         if (IS_FWI2_CAPABLE(ha)) {
562                 WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
563                 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
564         } else {
565                 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
566                 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
567         }
568
569 }
570
571 /**
572  * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
573  * Continuation Type 1 IOCBs to allocate.
574  *
575  * @dsds: number of data segment decriptors needed
576  *
577  * Returns the number of IOCB entries needed to store @dsds.
578  */
579 static inline uint16_t
580 qla24xx_calc_iocbs(uint16_t dsds)
581 {
582         uint16_t iocbs;
583
584         iocbs = 1;
585         if (dsds > 1) {
586                 iocbs += (dsds - 1) / 5;
587                 if ((dsds - 1) % 5)
588                         iocbs++;
589         }
590         return iocbs;
591 }
592
593 /**
594  * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
595  * IOCB types.
596  *
597  * @sp: SRB command to process
598  * @cmd_pkt: Command type 3 IOCB
599  * @tot_dsds: Total number of segments to transfer
600  */
601 static inline void
602 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
603     uint16_t tot_dsds)
604 {
605         uint16_t        avail_dsds;
606         uint32_t        *cur_dsd;
607         scsi_qla_host_t *ha;
608         struct scsi_cmnd *cmd;
609         struct scatterlist *sg;
610         int i;
611
612         cmd = sp->cmd;
613
614         /* Update entry type to indicate Command Type 3 IOCB */
615         *((uint32_t *)(&cmd_pkt->entry_type)) =
616             __constant_cpu_to_le32(COMMAND_TYPE_7);
617
618         /* No data transfer */
619         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
620                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
621                 return;
622         }
623
624         ha = sp->ha;
625
626         /* Set transfer direction */
627         if (cmd->sc_data_direction == DMA_TO_DEVICE)
628                 cmd_pkt->task_mgmt_flags =
629                     __constant_cpu_to_le16(TMF_WRITE_DATA);
630         else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
631                 cmd_pkt->task_mgmt_flags =
632                     __constant_cpu_to_le16(TMF_READ_DATA);
633
634         /* One DSD is available in the Command Type 3 IOCB */
635         avail_dsds = 1;
636         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
637
638         /* Load data segments */
639
640         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
641                 dma_addr_t      sle_dma;
642                 cont_a64_entry_t *cont_pkt;
643
644                 /* Allocate additional continuation packets? */
645                 if (avail_dsds == 0) {
646                         /*
647                          * Five DSDs are available in the Continuation
648                          * Type 1 IOCB.
649                          */
650                         cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
651                         cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
652                         avail_dsds = 5;
653                 }
654
655                 sle_dma = sg_dma_address(sg);
656                 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
657                 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
658                 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
659                 avail_dsds--;
660         }
661 }
662
663
664 /**
665  * qla24xx_start_scsi() - Send a SCSI command to the ISP
666  * @sp: command to send to the ISP
667  *
668  * Returns non-zero if a failure occured, else zero.
669  */
670 int
671 qla24xx_start_scsi(srb_t *sp)
672 {
673         int             ret, nseg;
674         unsigned long   flags;
675         scsi_qla_host_t *ha;
676         struct scsi_cmnd *cmd;
677         uint32_t        *clr_ptr;
678         uint32_t        index;
679         uint32_t        handle;
680         struct cmd_type_7 *cmd_pkt;
681         uint16_t        cnt;
682         uint16_t        req_cnt;
683         uint16_t        tot_dsds;
684         struct device_reg_24xx __iomem *reg;
685
686         /* Setup device pointers. */
687         ret = 0;
688         ha = sp->ha;
689         reg = &ha->iobase->isp24;
690         cmd = sp->cmd;
691         /* So we know we haven't pci_map'ed anything yet */
692         tot_dsds = 0;
693
694         /* Send marker if required */
695         if (ha->marker_needed != 0) {
696                 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
697                         return QLA_FUNCTION_FAILED;
698                 }
699                 ha->marker_needed = 0;
700         }
701
702         /* Acquire ring specific lock */
703         spin_lock_irqsave(&ha->hardware_lock, flags);
704
705         /* Check for room in outstanding command list. */
706         handle = ha->current_outstanding_cmd;
707         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
708                 handle++;
709                 if (handle == MAX_OUTSTANDING_COMMANDS)
710                         handle = 1;
711                 if (!ha->outstanding_cmds[handle])
712                         break;
713         }
714         if (index == MAX_OUTSTANDING_COMMANDS)
715                 goto queuing_error;
716
717         /* Map the sg table so we have an accurate count of sg entries needed */
718         if (scsi_sg_count(cmd)) {
719                 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
720                     scsi_sg_count(cmd), cmd->sc_data_direction);
721                 if (unlikely(!nseg))
722                         goto queuing_error;
723         } else
724                 nseg = 0;
725
726         tot_dsds = nseg;
727
728         req_cnt = qla24xx_calc_iocbs(tot_dsds);
729         if (ha->req_q_cnt < (req_cnt + 2)) {
730                 cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
731                 if (ha->req_ring_index < cnt)
732                         ha->req_q_cnt = cnt - ha->req_ring_index;
733                 else
734                         ha->req_q_cnt = ha->request_q_length -
735                                 (ha->req_ring_index - cnt);
736         }
737         if (ha->req_q_cnt < (req_cnt + 2))
738                 goto queuing_error;
739
740         /* Build command packet. */
741         ha->current_outstanding_cmd = handle;
742         ha->outstanding_cmds[handle] = sp;
743         sp->ha = ha;
744         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
745         ha->req_q_cnt -= req_cnt;
746
747         cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
748         cmd_pkt->handle = handle;
749
750         /* Zero out remaining portion of packet. */
751         /*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
752         clr_ptr = (uint32_t *)cmd_pkt + 2;
753         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
754         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
755
756         /* Set NPORT-ID and LUN number*/
757         cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
758         cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
759         cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
760         cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
761         cmd_pkt->vp_index = sp->fcport->vp_idx;
762
763         int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
764         host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
765
766         /* Load SCSI command packet. */
767         memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
768         host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
769
770         cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
771
772         /* Build IOCB segments */
773         qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
774
775         /* Set total data segment count. */
776         cmd_pkt->entry_count = (uint8_t)req_cnt;
777         wmb();
778
779         /* Adjust ring index. */
780         ha->req_ring_index++;
781         if (ha->req_ring_index == ha->request_q_length) {
782                 ha->req_ring_index = 0;
783                 ha->request_ring_ptr = ha->request_ring;
784         } else
785                 ha->request_ring_ptr++;
786
787         sp->flags |= SRB_DMA_VALID;
788
789         /* Set chip new ring index. */
790         WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
791         RD_REG_DWORD_RELAXED(&reg->req_q_in);           /* PCI Posting. */
792
793         /* Manage unprocessed RIO/ZIO commands in response queue. */
794         if (ha->flags.process_response_queue &&
795             ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
796                 qla24xx_process_response_queue(ha);
797
798         spin_unlock_irqrestore(&ha->hardware_lock, flags);
799         return QLA_SUCCESS;
800
801 queuing_error:
802         if (tot_dsds)
803                 scsi_dma_unmap(cmd);
804
805         spin_unlock_irqrestore(&ha->hardware_lock, flags);
806
807         return QLA_FUNCTION_FAILED;
808 }