qlge: bugfix: Move netif_napi_del() to common call point.
[safe/jmp/linux-2.6] / drivers / scsi / qla4xxx / ql4_iocb.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2006 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7
8 #include "ql4_def.h"
9 #include "ql4_glbl.h"
10 #include "ql4_dbg.h"
11 #include "ql4_inline.h"
12
13
14 #include <scsi/scsi_tcq.h>
15
16 /**
17  * qla4xxx_get_req_pkt - returns a valid entry in request queue.
18  * @ha: Pointer to host adapter structure.
19  * @queue_entry: Pointer to pointer to queue entry structure
20  *
21  * This routine performs the following tasks:
22  *      - returns the current request_in pointer (if queue not full)
23  *      - advances the request_in pointer
24  *      - checks for queue full
25  **/
26 static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha,
27                                struct queue_entry **queue_entry)
28 {
29         uint16_t request_in;
30         uint8_t status = QLA_SUCCESS;
31
32         *queue_entry = ha->request_ptr;
33
34         /* get the latest request_in and request_out index */
35         request_in = ha->request_in;
36         ha->request_out = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out);
37
38         /* Advance request queue pointer and check for queue full */
39         if (request_in == (REQUEST_QUEUE_DEPTH - 1)) {
40                 request_in = 0;
41                 ha->request_ptr = ha->request_ring;
42         } else {
43                 request_in++;
44                 ha->request_ptr++;
45         }
46
47         /* request queue is full, try again later */
48         if ((ha->iocb_cnt + 1) >= ha->iocb_hiwat) {
49                 /* restore request pointer */
50                 ha->request_ptr = *queue_entry;
51                 status = QLA_ERROR;
52         } else {
53                 ha->request_in = request_in;
54                 memset(*queue_entry, 0, sizeof(**queue_entry));
55         }
56
57         return status;
58 }
59
60 /**
61  * qla4xxx_send_marker_iocb - issues marker iocb to HBA
62  * @ha: Pointer to host adapter structure.
63  * @ddb_entry: Pointer to device database entry
64  * @lun: SCSI LUN
65  * @marker_type: marker identifier
66  *
67  * This routine issues a marker IOCB.
68  **/
69 int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha,
70         struct ddb_entry *ddb_entry, int lun, uint16_t mrkr_mod)
71 {
72         struct qla4_marker_entry *marker_entry;
73         unsigned long flags = 0;
74         uint8_t status = QLA_SUCCESS;
75
76         /* Acquire hardware specific lock */
77         spin_lock_irqsave(&ha->hardware_lock, flags);
78
79         /* Get pointer to the queue entry for the marker */
80         if (qla4xxx_get_req_pkt(ha, (struct queue_entry **) &marker_entry) !=
81             QLA_SUCCESS) {
82                 status = QLA_ERROR;
83                 goto exit_send_marker;
84         }
85
86         /* Put the marker in the request queue */
87         marker_entry->hdr.entryType = ET_MARKER;
88         marker_entry->hdr.entryCount = 1;
89         marker_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
90         marker_entry->modifier = cpu_to_le16(mrkr_mod);
91         int_to_scsilun(lun, &marker_entry->lun);
92         wmb();
93
94         /* Tell ISP it's got a new I/O request */
95         writel(ha->request_in, &ha->reg->req_q_in);
96         readl(&ha->reg->req_q_in);
97
98 exit_send_marker:
99         spin_unlock_irqrestore(&ha->hardware_lock, flags);
100         return status;
101 }
102
103 static struct continuation_t1_entry* qla4xxx_alloc_cont_entry(
104         struct scsi_qla_host *ha)
105 {
106         struct continuation_t1_entry *cont_entry;
107
108         cont_entry = (struct continuation_t1_entry *)ha->request_ptr;
109
110         /* Advance request queue pointer */
111         if (ha->request_in == (REQUEST_QUEUE_DEPTH - 1)) {
112                 ha->request_in = 0;
113                 ha->request_ptr = ha->request_ring;
114         } else {
115                 ha->request_in++;
116                 ha->request_ptr++;
117         }
118
119         /* Load packet defaults */
120         cont_entry->hdr.entryType = ET_CONTINUE;
121         cont_entry->hdr.entryCount = 1;
122         cont_entry->hdr.systemDefined = (uint8_t) cpu_to_le16(ha->request_in);
123
124         return cont_entry;
125 }
126
127 static uint16_t qla4xxx_calc_request_entries(uint16_t dsds)
128 {
129         uint16_t iocbs;
130
131         iocbs = 1;
132         if (dsds > COMMAND_SEG) {
133                 iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG;
134                 if ((dsds - COMMAND_SEG) % CONTINUE_SEG)
135                         iocbs++;
136         }
137         return iocbs;
138 }
139
140 static void qla4xxx_build_scsi_iocbs(struct srb *srb,
141                                      struct command_t3_entry *cmd_entry,
142                                      uint16_t tot_dsds)
143 {
144         struct scsi_qla_host *ha;
145         uint16_t avail_dsds;
146         struct data_seg_a64 *cur_dsd;
147         struct scsi_cmnd *cmd;
148         struct scatterlist *sg;
149         int i;
150
151         cmd = srb->cmd;
152         ha = srb->ha;
153
154         if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
155                 /* No data being transferred */
156                 cmd_entry->ttlByteCnt = __constant_cpu_to_le32(0);
157                 return;
158         }
159
160         avail_dsds = COMMAND_SEG;
161         cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]);
162
163         scsi_for_each_sg(cmd, sg, tot_dsds, i) {
164                 dma_addr_t sle_dma;
165
166                 /* Allocate additional continuation packets? */
167                 if (avail_dsds == 0) {
168                         struct continuation_t1_entry *cont_entry;
169
170                         cont_entry = qla4xxx_alloc_cont_entry(ha);
171                         cur_dsd =
172                                 (struct data_seg_a64 *)
173                                 &cont_entry->dataseg[0];
174                         avail_dsds = CONTINUE_SEG;
175                 }
176
177                 sle_dma = sg_dma_address(sg);
178                 cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma));
179                 cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma));
180                 cur_dsd->count = cpu_to_le32(sg_dma_len(sg));
181                 avail_dsds--;
182
183                 cur_dsd++;
184         }
185 }
186
187 /**
188  * qla4xxx_send_command_to_isp - issues command to HBA
189  * @ha: pointer to host adapter structure.
190  * @srb: pointer to SCSI Request Block to be sent to ISP
191  *
192  * This routine is called by qla4xxx_queuecommand to build an ISP
193  * command and pass it to the ISP for execution.
194  **/
195 int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb)
196 {
197         struct scsi_cmnd *cmd = srb->cmd;
198         struct ddb_entry *ddb_entry;
199         struct command_t3_entry *cmd_entry;
200
201         int nseg;
202         uint16_t tot_dsds;
203         uint16_t req_cnt;
204
205         unsigned long flags;
206         uint16_t cnt;
207         uint32_t index;
208         char tag[2];
209
210         /* Get real lun and adapter */
211         ddb_entry = srb->ddb;
212
213         tot_dsds = 0;
214
215         /* Acquire hardware specific lock */
216         spin_lock_irqsave(&ha->hardware_lock, flags);
217
218         index = (uint32_t)cmd->request->tag;
219
220         /* Calculate the number of request entries needed. */
221         nseg = scsi_dma_map(cmd);
222         if (nseg < 0)
223                 goto queuing_error;
224         tot_dsds = nseg;
225
226         req_cnt = qla4xxx_calc_request_entries(tot_dsds);
227
228         if (ha->req_q_count < (req_cnt + 2)) {
229                 cnt = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out);
230                 if (ha->request_in < cnt)
231                         ha->req_q_count = cnt - ha->request_in;
232                 else
233                         ha->req_q_count = REQUEST_QUEUE_DEPTH -
234                                 (ha->request_in - cnt);
235         }
236
237         if (ha->req_q_count < (req_cnt + 2))
238                 goto queuing_error;
239
240         /* total iocbs active */
241         if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH)
242                 goto queuing_error;
243
244         /* Build command packet */
245         cmd_entry = (struct command_t3_entry *) ha->request_ptr;
246         memset(cmd_entry, 0, sizeof(struct command_t3_entry));
247         cmd_entry->hdr.entryType = ET_COMMAND;
248         cmd_entry->handle = cpu_to_le32(index);
249         cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
250         cmd_entry->connection_id = cpu_to_le16(ddb_entry->connection_id);
251
252         int_to_scsilun(cmd->device->lun, &cmd_entry->lun);
253         cmd_entry->cmdSeqNum = cpu_to_le32(ddb_entry->CmdSn);
254         cmd_entry->ttlByteCnt = cpu_to_le32(scsi_bufflen(cmd));
255         memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len);
256         cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds);
257         cmd_entry->hdr.entryCount = req_cnt;
258
259         /* Set data transfer direction control flags
260          * NOTE: Look at data_direction bits iff there is data to be
261          *       transferred, as the data direction bit is sometimed filled
262          *       in when there is no data to be transferred */
263         cmd_entry->control_flags = CF_NO_DATA;
264         if (scsi_bufflen(cmd)) {
265                 if (cmd->sc_data_direction == DMA_TO_DEVICE)
266                         cmd_entry->control_flags = CF_WRITE;
267                 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
268                         cmd_entry->control_flags = CF_READ;
269
270                 ha->bytes_xfered += scsi_bufflen(cmd);
271                 if (ha->bytes_xfered & ~0xFFFFF){
272                         ha->total_mbytes_xferred += ha->bytes_xfered >> 20;
273                         ha->bytes_xfered &= 0xFFFFF;
274                 }
275         }
276
277         /* Set tagged queueing control flags */
278         cmd_entry->control_flags |= CF_SIMPLE_TAG;
279         if (scsi_populate_tag_msg(cmd, tag))
280                 switch (tag[0]) {
281                 case MSG_HEAD_TAG:
282                         cmd_entry->control_flags |= CF_HEAD_TAG;
283                         break;
284                 case MSG_ORDERED_TAG:
285                         cmd_entry->control_flags |= CF_ORDERED_TAG;
286                         break;
287                 }
288
289
290         /* Advance request queue pointer */
291         ha->request_in++;
292         if (ha->request_in == REQUEST_QUEUE_DEPTH) {
293                 ha->request_in = 0;
294                 ha->request_ptr = ha->request_ring;
295         } else
296                 ha->request_ptr++;
297
298
299         qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds);
300         wmb();
301
302         /*
303          * Check to see if adapter is online before placing request on
304          * request queue.  If a reset occurs and a request is in the queue,
305          * the firmware will still attempt to process the request, retrieving
306          * garbage for pointers.
307          */
308         if (!test_bit(AF_ONLINE, &ha->flags)) {
309                 DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! "
310                               "Do not issue command.\n",
311                               ha->host_no, __func__));
312                 goto queuing_error;
313         }
314
315         srb->cmd->host_scribble = (unsigned char *)srb;
316
317         /* update counters */
318         srb->state = SRB_ACTIVE_STATE;
319         srb->flags |= SRB_DMA_VALID;
320
321         /* Track IOCB used */
322         ha->iocb_cnt += req_cnt;
323         srb->iocb_cnt = req_cnt;
324         ha->req_q_count -= req_cnt;
325
326         /* Debug print statements */
327         writel(ha->request_in, &ha->reg->req_q_in);
328         readl(&ha->reg->req_q_in);
329         spin_unlock_irqrestore(&ha->hardware_lock, flags);
330
331         return QLA_SUCCESS;
332
333 queuing_error:
334         if (tot_dsds)
335                 scsi_dma_unmap(cmd);
336
337         spin_unlock_irqrestore(&ha->hardware_lock, flags);
338
339         return QLA_ERROR;
340 }
341