vserver 1.9.3
[linux-2.6.git] / drivers / scsi / qla2xxx / qla_iocb.c
1 /******************************************************************************
2  *                  QLOGIC LINUX SOFTWARE
3  *
4  * QLogic ISP2x00 device driver for Linux 2.6.x
5  * Copyright (C) 2003-2004 QLogic Corporation
6  * (www.qlogic.com)
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  ******************************************************************************/
19
20 #include "qla_def.h"
21
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24
25 #include <scsi/scsi_tcq.h>
26
27 static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
28 static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
29 static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
30
31 /**
32  * qla2x00_get_cmd_direction() - Determine control_flag data direction.
33  * @cmd: SCSI command
34  *
35  * Returns the proper CF_* direction based on CDB.
36  */
37 static inline uint16_t
38 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
39 {
40         uint16_t cflags;
41
42         cflags = 0;
43
44         /* Set transfer direction */
45         if (cmd->sc_data_direction == DMA_TO_DEVICE)
46                 cflags = CF_WRITE;
47         else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
48                 cflags = CF_READ;
49         return (cflags);
50 }
51
52 /**
53  * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
54  * Continuation Type 0 IOCBs to allocate.
55  *
56  * @dsds: number of data segment decriptors needed
57  *
58  * Returns the number of IOCB entries needed to store @dsds.
59  */
60 uint16_t
61 qla2x00_calc_iocbs_32(uint16_t dsds)
62 {
63         uint16_t iocbs;
64
65         iocbs = 1;
66         if (dsds > 3) {
67                 iocbs += (dsds - 3) / 7;
68                 if ((dsds - 3) % 7)
69                         iocbs++;
70         }
71         return (iocbs);
72 }
73
74 /**
75  * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
76  * Continuation Type 1 IOCBs to allocate.
77  *
78  * @dsds: number of data segment decriptors needed
79  *
80  * Returns the number of IOCB entries needed to store @dsds.
81  */
82 uint16_t
83 qla2x00_calc_iocbs_64(uint16_t dsds)
84 {
85         uint16_t iocbs;
86
87         iocbs = 1;
88         if (dsds > 2) {
89                 iocbs += (dsds - 2) / 5;
90                 if ((dsds - 2) % 5)
91                         iocbs++;
92         }
93         return (iocbs);
94 }
95
96 /**
97  * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
98  * @ha: HA context
99  *
100  * Returns a pointer to the Continuation Type 0 IOCB packet.
101  */
102 static inline cont_entry_t *
103 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
104 {
105         cont_entry_t *cont_pkt;
106
107         /* Adjust ring index. */
108         ha->req_ring_index++;
109         if (ha->req_ring_index == ha->request_q_length) {
110                 ha->req_ring_index = 0;
111                 ha->request_ring_ptr = ha->request_ring;
112         } else {
113                 ha->request_ring_ptr++;
114         }
115
116         cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
117
118         /* Load packet defaults. */
119         *((uint32_t *)(&cont_pkt->entry_type)) =
120             __constant_cpu_to_le32(CONTINUE_TYPE);
121
122         return (cont_pkt);
123 }
124
125 /**
126  * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
127  * @ha: HA context
128  *
129  * Returns a pointer to the continuation type 1 IOCB packet.
130  */
131 static inline cont_a64_entry_t *
132 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
133 {
134         cont_a64_entry_t *cont_pkt;
135
136         /* Adjust ring index. */
137         ha->req_ring_index++;
138         if (ha->req_ring_index == ha->request_q_length) {
139                 ha->req_ring_index = 0;
140                 ha->request_ring_ptr = ha->request_ring;
141         } else {
142                 ha->request_ring_ptr++;
143         }
144
145         cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
146
147         /* Load packet defaults. */
148         *((uint32_t *)(&cont_pkt->entry_type)) =
149             __constant_cpu_to_le32(CONTINUE_A64_TYPE);
150
151         return (cont_pkt);
152 }
153
154 /**
155  * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
156  * capable IOCB types.
157  *
158  * @sp: SRB command to process
159  * @cmd_pkt: Command type 2 IOCB
160  * @tot_dsds: Total number of segments to transfer
161  */
162 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
163     uint16_t tot_dsds)
164 {
165         uint16_t        avail_dsds;
166         uint32_t        *cur_dsd;
167         scsi_qla_host_t *ha;
168         struct scsi_cmnd *cmd;
169
170         cmd = sp->cmd;
171
172         /* Update entry type to indicate Command Type 2 IOCB */
173         *((uint32_t *)(&cmd_pkt->entry_type)) =
174             __constant_cpu_to_le32(COMMAND_TYPE);
175
176         /* No data transfer */
177         if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
178                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
179                 return;
180         }
181
182         ha = sp->ha;
183
184         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
185
186         /* Three DSDs are available in the Command Type 2 IOCB */
187         avail_dsds = 3;
188         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
189
190         /* Load data segments */
191         if (cmd->use_sg != 0) {
192                 struct  scatterlist *cur_seg;
193                 struct  scatterlist *end_seg;
194
195                 cur_seg = (struct scatterlist *)cmd->request_buffer;
196                 end_seg = cur_seg + tot_dsds;
197                 while (cur_seg < end_seg) {
198                         cont_entry_t    *cont_pkt;
199
200                         /* Allocate additional continuation packets? */
201                         if (avail_dsds == 0) {
202                                 /*
203                                  * Seven DSDs are available in the Continuation
204                                  * Type 0 IOCB.
205                                  */
206                                 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
207                                 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
208                                 avail_dsds = 7;
209                         }
210
211                         *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
212                         *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
213                         avail_dsds--;
214
215                         cur_seg++;
216                 }
217         } else {
218                 dma_addr_t      req_dma;
219                 struct page     *page;
220                 unsigned long   offset;
221
222                 page = virt_to_page(cmd->request_buffer);
223                 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
224                 req_dma = pci_map_page(ha->pdev, page, offset,
225                     cmd->request_bufflen, cmd->sc_data_direction);
226
227                 sp->dma_handle = req_dma;
228
229                 *cur_dsd++ = cpu_to_le32(req_dma);
230                 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
231         }
232 }
233
234 /**
235  * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
236  * capable IOCB types.
237  *
238  * @sp: SRB command to process
239  * @cmd_pkt: Command type 3 IOCB
240  * @tot_dsds: Total number of segments to transfer
241  */
242 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
243     uint16_t tot_dsds)
244 {
245         uint16_t        avail_dsds;
246         uint32_t        *cur_dsd;
247         scsi_qla_host_t *ha;
248         struct scsi_cmnd *cmd;
249
250         cmd = sp->cmd;
251
252         /* Update entry type to indicate Command Type 3 IOCB */
253         *((uint32_t *)(&cmd_pkt->entry_type)) =
254             __constant_cpu_to_le32(COMMAND_A64_TYPE);
255
256         /* No data transfer */
257         if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
258                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
259                 return;
260         }
261
262         ha = sp->ha;
263
264         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
265
266         /* Two DSDs are available in the Command Type 3 IOCB */
267         avail_dsds = 2;
268         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
269
270         /* Load data segments */
271         if (cmd->use_sg != 0) {
272                 struct  scatterlist *cur_seg;
273                 struct  scatterlist *end_seg;
274
275                 cur_seg = (struct scatterlist *)cmd->request_buffer;
276                 end_seg = cur_seg + tot_dsds;
277                 while (cur_seg < end_seg) {
278                         dma_addr_t      sle_dma;
279                         cont_a64_entry_t *cont_pkt;
280
281                         /* Allocate additional continuation packets? */
282                         if (avail_dsds == 0) {
283                                 /*
284                                  * Five DSDs are available in the Continuation
285                                  * Type 1 IOCB.
286                                  */
287                                 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
288                                 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
289                                 avail_dsds = 5;
290                         }
291
292                         sle_dma = sg_dma_address(cur_seg);
293                         *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
294                         *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
295                         *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
296                         avail_dsds--;
297
298                         cur_seg++;
299                 }
300         } else {
301                 dma_addr_t      req_dma;
302                 struct page     *page;
303                 unsigned long   offset;
304
305                 page = virt_to_page(cmd->request_buffer);
306                 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
307                 req_dma = pci_map_page(ha->pdev, page, offset,
308                     cmd->request_bufflen, cmd->sc_data_direction);
309
310                 sp->dma_handle = req_dma;
311
312                 *cur_dsd++ = cpu_to_le32(LSD(req_dma));
313                 *cur_dsd++ = cpu_to_le32(MSD(req_dma));
314                 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
315         }
316 }
317
318 /**
319  * qla2x00_start_scsi() - Send a SCSI command to the ISP
320  * @sp: command to send to the ISP
321  *
322  * Returns non-zero if a failure occured, else zero.
323  */
324 int
325 qla2x00_start_scsi(srb_t *sp)
326 {
327         int             ret;
328         unsigned long   flags;
329         scsi_qla_host_t *ha;
330         fc_lun_t        *fclun;
331         struct scsi_cmnd *cmd;
332         uint32_t        *clr_ptr;
333         uint32_t        index;
334         uint32_t        handle;
335         cmd_entry_t     *cmd_pkt;
336         uint32_t        timeout;
337         struct scatterlist *sg;
338         uint16_t        cnt;
339         uint16_t        req_cnt;
340         uint16_t        tot_dsds;
341         device_reg_t    *reg;
342         char            tag[2];
343
344         /* Setup device pointers. */
345         ret = 0;
346         fclun = sp->lun_queue->fclun;
347         ha = fclun->fcport->ha;
348         cmd = sp->cmd;
349         reg = ha->iobase;
350
351         /* Send marker if required */
352         if (ha->marker_needed != 0) {
353                 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
354                         return (QLA_FUNCTION_FAILED);
355                 }
356                 ha->marker_needed = 0;
357         }
358
359         /* Acquire ring specific lock */
360         spin_lock_irqsave(&ha->hardware_lock, flags);
361
362         /* Check for room in outstanding command list. */
363         handle = ha->current_outstanding_cmd;
364         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
365                 handle++;
366                 if (handle == MAX_OUTSTANDING_COMMANDS)
367                         handle = 1;
368                 if (ha->outstanding_cmds[handle] == 0)
369                         break;
370         }
371         if (index == MAX_OUTSTANDING_COMMANDS)
372                 goto queuing_error;
373
374         /* Calculate the number of request entries needed. */
375         req_cnt = (ha->calc_request_entries)(cmd->request->nr_hw_segments);
376         if (ha->req_q_cnt < (req_cnt + 2)) {
377                 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
378                 if (ha->req_ring_index < cnt)
379                         ha->req_q_cnt = cnt - ha->req_ring_index;
380                 else
381                         ha->req_q_cnt = ha->request_q_length -
382                             (ha->req_ring_index - cnt);
383         }
384         if (ha->req_q_cnt < (req_cnt + 2))
385                 goto queuing_error;
386
387         /* Finally, we have enough space, now perform mappings. */
388         tot_dsds = 0;
389         if (cmd->use_sg) {
390                 sg = (struct scatterlist *) cmd->request_buffer;
391                 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
392                     cmd->sc_data_direction);
393                 if (tot_dsds == 0)
394                         goto queuing_error;
395         } else if (cmd->request_bufflen) {
396             tot_dsds++;
397         }
398         req_cnt = (ha->calc_request_entries)(tot_dsds);
399
400         /* Build command packet */
401         ha->current_outstanding_cmd = handle;
402         ha->outstanding_cmds[handle] = sp;
403         sp->ha = ha;
404         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
405         ha->req_q_cnt -= req_cnt;
406
407         cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
408         cmd_pkt->handle = handle;
409         /* Zero out remaining portion of packet. */
410         clr_ptr = (uint32_t *)cmd_pkt + 2;
411         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
412         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
413
414         /* Set target ID */
415         SET_TARGET_ID(ha, cmd_pkt->target, fclun->fcport->loop_id);
416
417         /* Set LUN number*/
418         cmd_pkt->lun = cpu_to_le16(fclun->lun);
419
420         /* Update tagged queuing modifier */
421         if (scsi_populate_tag_msg(cmd, tag)) {
422                 switch (tag[0]) {
423                 case MSG_SIMPLE_TAG:
424                         cmd_pkt->control_flags =
425                             __constant_cpu_to_le16(CF_SIMPLE_TAG);
426                         break;
427                 case MSG_HEAD_TAG:
428                         cmd_pkt->control_flags =
429                             __constant_cpu_to_le16(CF_HEAD_TAG);
430                         break;
431                 case MSG_ORDERED_TAG:
432                         cmd_pkt->control_flags =
433                             __constant_cpu_to_le16(CF_ORDERED_TAG);
434                         break;
435                 }
436         }
437
438         /*
439          * Allocate at least 5 (+ QLA_CMD_TIMER_DELTA) seconds for RISC timeout.
440          */
441         timeout = (uint32_t)(cmd->timeout_per_command / HZ);
442         if (timeout > 65535)
443                 cmd_pkt->timeout = __constant_cpu_to_le16(0);
444         else if (timeout > 25)
445                 cmd_pkt->timeout = cpu_to_le16((uint16_t)timeout -
446                     (5 + QLA_CMD_TIMER_DELTA));
447         else
448                 cmd_pkt->timeout = cpu_to_le16((uint16_t)timeout);
449
450         /* Load SCSI command packet. */
451         memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
452         cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
453
454         /* Build IOCB segments */
455         (ha->build_scsi_iocbs)(sp, cmd_pkt, tot_dsds);
456
457         /* Set total data segment count. */
458         cmd_pkt->entry_count = (uint8_t)req_cnt;
459         wmb();
460
461         /* Adjust ring index. */
462         ha->req_ring_index++;
463         if (ha->req_ring_index == ha->request_q_length) {
464                 ha->req_ring_index = 0;
465                 ha->request_ring_ptr = ha->request_ring;
466         } else
467                 ha->request_ring_ptr++;
468
469         ha->actthreads++;
470         ha->total_ios++;
471         sp->lun_queue->out_cnt++;
472         sp->flags |= SRB_DMA_VALID;
473         sp->state = SRB_ACTIVE_STATE;
474         sp->u_start = jiffies;
475
476         /* Set chip new ring index. */
477         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
478         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
479
480         spin_unlock_irqrestore(&ha->hardware_lock, flags);
481         return (QLA_SUCCESS);
482
483 queuing_error:
484         spin_unlock_irqrestore(&ha->hardware_lock, flags);
485
486         return (QLA_FUNCTION_FAILED);
487 }
488
489 /**
490  * qla2x00_marker() - Send a marker IOCB to the firmware.
491  * @ha: HA context
492  * @loop_id: loop ID
493  * @lun: LUN
494  * @type: marker modifier
495  *
496  * Can be called from both normal and interrupt context.
497  *
498  * Returns non-zero if a failure occured, else zero.
499  */
500 int
501 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
502     uint8_t type)
503 {
504         mrk_entry_t     *pkt;
505
506         pkt = (mrk_entry_t *)qla2x00_req_pkt(ha);
507         if (pkt == NULL) {
508                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
509
510                 return (QLA_FUNCTION_FAILED);
511         }
512
513         pkt->entry_type = MARKER_TYPE;
514         pkt->modifier = type;
515
516         if (type != MK_SYNC_ALL) {
517                 pkt->lun = cpu_to_le16(lun);
518                 SET_TARGET_ID(ha, pkt->target, loop_id);
519         }
520         wmb();
521
522         /* Issue command to ISP */
523         qla2x00_isp_cmd(ha);
524
525         return (QLA_SUCCESS);
526 }
527
528 int 
529 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
530     uint8_t type)
531 {
532         int ret;
533         unsigned long flags = 0;
534
535         spin_lock_irqsave(&ha->hardware_lock, flags);
536         ret = __qla2x00_marker(ha, loop_id, lun, type);
537         spin_unlock_irqrestore(&ha->hardware_lock, flags);
538
539         return (ret);
540 }
541
542 /**
543  * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
544  * @ha: HA context
545  *
546  * Note: The caller must hold the hardware lock before calling this routine.
547  *
548  * Returns NULL if function failed, else, a pointer to the request packet.
549  */
550 request_t *
551 qla2x00_req_pkt(scsi_qla_host_t *ha)
552 {
553         device_reg_t    *reg = ha->iobase;
554         request_t       *pkt = NULL;
555         uint16_t        cnt;
556         uint32_t        *dword_ptr;
557         uint32_t        timer;
558         uint16_t        req_cnt = 1;
559
560         /* Wait 1 second for slot. */
561         for (timer = HZ; timer; timer--) {
562                 if ((req_cnt + 2) >= ha->req_q_cnt) {
563                         /* Calculate number of free request entries. */
564                         cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
565                         if  (ha->req_ring_index < cnt)
566                                 ha->req_q_cnt = cnt - ha->req_ring_index;
567                         else
568                                 ha->req_q_cnt = ha->request_q_length -
569                                     (ha->req_ring_index - cnt);
570                 }
571                 /* If room for request in request ring. */
572                 if ((req_cnt + 2) < ha->req_q_cnt) {
573                         ha->req_q_cnt--;
574                         pkt = ha->request_ring_ptr;
575
576                         /* Zero out packet. */
577                         dword_ptr = (uint32_t *)pkt;
578                         for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
579                                 *dword_ptr++ = 0;
580
581                         /* Set system defined field. */
582                         pkt->sys_define = (uint8_t)ha->req_ring_index;
583
584                         /* Set entry count. */
585                         pkt->entry_count = 1;
586
587                         break;
588                 }
589
590                 /* Release ring specific lock */
591                 spin_unlock(&ha->hardware_lock);
592
593                 udelay(2);   /* 2 us */
594
595                 /* Check for pending interrupts. */
596                 /* During init we issue marker directly */
597                 if (!ha->marker_needed)
598                         qla2x00_poll(ha);
599
600                 spin_lock_irq(&ha->hardware_lock);
601         }
602         if (!pkt) {
603                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
604         }
605
606         return (pkt);
607 }
608
609 /**
610  * qla2x00_ms_req_pkt() - Retrieve a Management Server request packet from
611  *                              the request ring.
612  * @ha: HA context
613  * @sp: pointer to handle post function call
614  *
615  * Note: The caller must hold the hardware lock before calling this routine.
616  *
617  * Returns NULL if function failed, else, a pointer to the request packet.
618  */
619 request_t *
620 qla2x00_ms_req_pkt(scsi_qla_host_t *ha, srb_t  *sp)
621 {
622         device_reg_t    *reg = ha->iobase;
623         request_t       *pkt = NULL;
624         uint16_t        cnt, i, index;
625         uint32_t        *dword_ptr;
626         uint32_t        timer;
627         uint8_t         found = 0;
628         uint16_t        req_cnt = 1;
629
630         /* Wait 1 second for slot. */
631         for (timer = HZ; timer; timer--) {
632                 if ((req_cnt + 2) >= ha->req_q_cnt) {
633                         /* Calculate number of free request entries. */
634                         cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
635                         if (ha->req_ring_index < cnt) {
636                                 ha->req_q_cnt = cnt - ha->req_ring_index;
637                         } else {
638                                 ha->req_q_cnt = ha->request_q_length -
639                                     (ha->req_ring_index - cnt);
640                         }
641                 }
642
643                 /* Check for room in outstanding command list. */
644                 cnt = ha->current_outstanding_cmd;
645                 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
646                         cnt++;
647                         if (cnt == MAX_OUTSTANDING_COMMANDS)
648                                 cnt = 1;
649
650                         if (ha->outstanding_cmds[cnt] == 0) {
651                                 found = 1;
652                                 ha->current_outstanding_cmd = cnt;
653                                 break;
654                         }
655                 }
656
657                 /* If room for request in request ring. */
658                 if (found && (req_cnt + 2) < ha->req_q_cnt) {
659                         pkt = ha->request_ring_ptr;
660
661                         /* Zero out packet. */
662                         dword_ptr = (uint32_t *)pkt;
663                         for (i = 0; i < REQUEST_ENTRY_SIZE / 4; i++ )
664                                 *dword_ptr++ = 0;
665
666                         DEBUG5(printk("%s(): putting sp=%p in "
667                             "outstanding_cmds[%x]\n",
668                             __func__,
669                             sp, cnt));
670
671                         ha->outstanding_cmds[cnt] = sp;
672
673                         /* save the handle */
674                         sp->cmd->host_scribble = (unsigned char *) (u_long) cnt;
675                         CMD_SP(sp->cmd) = (void *)sp;
676
677                         ha->req_q_cnt--;
678                         pkt->handle = (uint32_t)cnt;
679
680                         /* Set system defined field. */
681                         pkt->sys_define = (uint8_t)ha->req_ring_index;
682                         pkt->entry_status = 0;
683
684                         break;
685                 }
686
687                 /* Release ring specific lock */
688                 spin_unlock(&ha->hardware_lock);
689                 udelay(20);
690
691                 /* Check for pending interrupts. */
692                 qla2x00_poll(ha);
693
694                 spin_lock_irq(&ha->hardware_lock);
695         }
696         if (!pkt) {
697                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
698         }
699
700         return (pkt);
701 }
702
703 /**
704  * qla2x00_isp_cmd() - Modify the request ring pointer.
705  * @ha: HA context
706  *
707  * Note: The caller must hold the hardware lock before calling this routine.
708  */
709 void
710 qla2x00_isp_cmd(scsi_qla_host_t *ha)
711 {
712         device_reg_t *reg = ha->iobase;
713
714         DEBUG5(printk("%s(): IOCB data:\n", __func__));
715         DEBUG5(qla2x00_dump_buffer(
716             (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
717
718         /* Adjust ring index. */
719         ha->req_ring_index++;
720         if (ha->req_ring_index == ha->request_q_length) {
721                 ha->req_ring_index = 0;
722                 ha->request_ring_ptr = ha->request_ring;
723         } else
724                 ha->request_ring_ptr++;
725
726         /* Set chip new ring index. */
727         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
728         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
729 }