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