]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/qla2xxx/qla_iocb.c
[PATCH] qla2xxx: remove lun discovery codes...
[linux-2.6-omap-h63xx.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 static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
31
32 /**
33  * qla2x00_get_cmd_direction() - Determine control_flag data direction.
34  * @cmd: SCSI command
35  *
36  * Returns the proper CF_* direction based on CDB.
37  */
38 static inline uint16_t
39 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
40 {
41         uint16_t cflags;
42
43         cflags = 0;
44
45         /* Set transfer direction */
46         if (cmd->sc_data_direction == DMA_TO_DEVICE)
47                 cflags = CF_WRITE;
48         else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
49                 cflags = CF_READ;
50         return (cflags);
51 }
52
53 /**
54  * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
55  * Continuation Type 0 IOCBs to allocate.
56  *
57  * @dsds: number of data segment decriptors needed
58  *
59  * Returns the number of IOCB entries needed to store @dsds.
60  */
61 uint16_t
62 qla2x00_calc_iocbs_32(uint16_t dsds)
63 {
64         uint16_t iocbs;
65
66         iocbs = 1;
67         if (dsds > 3) {
68                 iocbs += (dsds - 3) / 7;
69                 if ((dsds - 3) % 7)
70                         iocbs++;
71         }
72         return (iocbs);
73 }
74
75 /**
76  * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
77  * Continuation Type 1 IOCBs to allocate.
78  *
79  * @dsds: number of data segment decriptors needed
80  *
81  * Returns the number of IOCB entries needed to store @dsds.
82  */
83 uint16_t
84 qla2x00_calc_iocbs_64(uint16_t dsds)
85 {
86         uint16_t iocbs;
87
88         iocbs = 1;
89         if (dsds > 2) {
90                 iocbs += (dsds - 2) / 5;
91                 if ((dsds - 2) % 5)
92                         iocbs++;
93         }
94         return (iocbs);
95 }
96
97 /**
98  * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
99  * @ha: HA context
100  *
101  * Returns a pointer to the Continuation Type 0 IOCB packet.
102  */
103 static inline cont_entry_t *
104 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
105 {
106         cont_entry_t *cont_pkt;
107
108         /* Adjust ring index. */
109         ha->req_ring_index++;
110         if (ha->req_ring_index == ha->request_q_length) {
111                 ha->req_ring_index = 0;
112                 ha->request_ring_ptr = ha->request_ring;
113         } else {
114                 ha->request_ring_ptr++;
115         }
116
117         cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
118
119         /* Load packet defaults. */
120         *((uint32_t *)(&cont_pkt->entry_type)) =
121             __constant_cpu_to_le32(CONTINUE_TYPE);
122
123         return (cont_pkt);
124 }
125
126 /**
127  * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
128  * @ha: HA context
129  *
130  * Returns a pointer to the continuation type 1 IOCB packet.
131  */
132 static inline cont_a64_entry_t *
133 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
134 {
135         cont_a64_entry_t *cont_pkt;
136
137         /* Adjust ring index. */
138         ha->req_ring_index++;
139         if (ha->req_ring_index == ha->request_q_length) {
140                 ha->req_ring_index = 0;
141                 ha->request_ring_ptr = ha->request_ring;
142         } else {
143                 ha->request_ring_ptr++;
144         }
145
146         cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
147
148         /* Load packet defaults. */
149         *((uint32_t *)(&cont_pkt->entry_type)) =
150             __constant_cpu_to_le32(CONTINUE_A64_TYPE);
151
152         return (cont_pkt);
153 }
154
155 /**
156  * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
157  * capable IOCB types.
158  *
159  * @sp: SRB command to process
160  * @cmd_pkt: Command type 2 IOCB
161  * @tot_dsds: Total number of segments to transfer
162  */
163 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
164     uint16_t tot_dsds)
165 {
166         uint16_t        avail_dsds;
167         uint32_t        *cur_dsd;
168         scsi_qla_host_t *ha;
169         struct scsi_cmnd *cmd;
170
171         cmd = sp->cmd;
172
173         /* Update entry type to indicate Command Type 2 IOCB */
174         *((uint32_t *)(&cmd_pkt->entry_type)) =
175             __constant_cpu_to_le32(COMMAND_TYPE);
176
177         /* No data transfer */
178         if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
179                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
180                 return;
181         }
182
183         ha = sp->ha;
184
185         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
186
187         /* Three DSDs are available in the Command Type 2 IOCB */
188         avail_dsds = 3;
189         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
190
191         /* Load data segments */
192         if (cmd->use_sg != 0) {
193                 struct  scatterlist *cur_seg;
194                 struct  scatterlist *end_seg;
195
196                 cur_seg = (struct scatterlist *)cmd->request_buffer;
197                 end_seg = cur_seg + tot_dsds;
198                 while (cur_seg < end_seg) {
199                         cont_entry_t    *cont_pkt;
200
201                         /* Allocate additional continuation packets? */
202                         if (avail_dsds == 0) {
203                                 /*
204                                  * Seven DSDs are available in the Continuation
205                                  * Type 0 IOCB.
206                                  */
207                                 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
208                                 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
209                                 avail_dsds = 7;
210                         }
211
212                         *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
213                         *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
214                         avail_dsds--;
215
216                         cur_seg++;
217                 }
218         } else {
219                 dma_addr_t      req_dma;
220                 struct page     *page;
221                 unsigned long   offset;
222
223                 page = virt_to_page(cmd->request_buffer);
224                 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
225                 req_dma = pci_map_page(ha->pdev, page, offset,
226                     cmd->request_bufflen, cmd->sc_data_direction);
227
228                 sp->dma_handle = req_dma;
229
230                 *cur_dsd++ = cpu_to_le32(req_dma);
231                 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
232         }
233 }
234
235 /**
236  * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
237  * capable IOCB types.
238  *
239  * @sp: SRB command to process
240  * @cmd_pkt: Command type 3 IOCB
241  * @tot_dsds: Total number of segments to transfer
242  */
243 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
244     uint16_t tot_dsds)
245 {
246         uint16_t        avail_dsds;
247         uint32_t        *cur_dsd;
248         scsi_qla_host_t *ha;
249         struct scsi_cmnd *cmd;
250
251         cmd = sp->cmd;
252
253         /* Update entry type to indicate Command Type 3 IOCB */
254         *((uint32_t *)(&cmd_pkt->entry_type)) =
255             __constant_cpu_to_le32(COMMAND_A64_TYPE);
256
257         /* No data transfer */
258         if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
259                 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
260                 return;
261         }
262
263         ha = sp->ha;
264
265         cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
266
267         /* Two DSDs are available in the Command Type 3 IOCB */
268         avail_dsds = 2;
269         cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
270
271         /* Load data segments */
272         if (cmd->use_sg != 0) {
273                 struct  scatterlist *cur_seg;
274                 struct  scatterlist *end_seg;
275
276                 cur_seg = (struct scatterlist *)cmd->request_buffer;
277                 end_seg = cur_seg + tot_dsds;
278                 while (cur_seg < end_seg) {
279                         dma_addr_t      sle_dma;
280                         cont_a64_entry_t *cont_pkt;
281
282                         /* Allocate additional continuation packets? */
283                         if (avail_dsds == 0) {
284                                 /*
285                                  * Five DSDs are available in the Continuation
286                                  * Type 1 IOCB.
287                                  */
288                                 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
289                                 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
290                                 avail_dsds = 5;
291                         }
292
293                         sle_dma = sg_dma_address(cur_seg);
294                         *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
295                         *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
296                         *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
297                         avail_dsds--;
298
299                         cur_seg++;
300                 }
301         } else {
302                 dma_addr_t      req_dma;
303                 struct page     *page;
304                 unsigned long   offset;
305
306                 page = virt_to_page(cmd->request_buffer);
307                 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
308                 req_dma = pci_map_page(ha->pdev, page, offset,
309                     cmd->request_bufflen, cmd->sc_data_direction);
310
311                 sp->dma_handle = req_dma;
312
313                 *cur_dsd++ = cpu_to_le32(LSD(req_dma));
314                 *cur_dsd++ = cpu_to_le32(MSD(req_dma));
315                 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
316         }
317 }
318
319 /**
320  * qla2x00_start_scsi() - Send a SCSI command to the ISP
321  * @sp: command to send to the ISP
322  *
323  * Returns non-zero if a failure occured, else zero.
324  */
325 int
326 qla2x00_start_scsi(srb_t *sp)
327 {
328         int             ret;
329         unsigned long   flags;
330         scsi_qla_host_t *ha;
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         struct scatterlist *sg;
337         uint16_t        cnt;
338         uint16_t        req_cnt;
339         uint16_t        tot_dsds;
340         device_reg_t __iomem *reg;
341         char            tag[2];
342
343         /* Setup device pointers. */
344         ret = 0;
345         ha = sp->ha;
346         reg = ha->iobase;
347         cmd = sp->cmd;
348
349         /* Send marker if required */
350         if (ha->marker_needed != 0) {
351                 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
352                         return (QLA_FUNCTION_FAILED);
353                 }
354                 ha->marker_needed = 0;
355         }
356
357         /* Acquire ring specific lock */
358         spin_lock_irqsave(&ha->hardware_lock, flags);
359
360         /* Check for room in outstanding command list. */
361         handle = ha->current_outstanding_cmd;
362         for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
363                 handle++;
364                 if (handle == MAX_OUTSTANDING_COMMANDS)
365                         handle = 1;
366                 if (ha->outstanding_cmds[handle] == 0)
367                         break;
368         }
369         if (index == MAX_OUTSTANDING_COMMANDS)
370                 goto queuing_error;
371
372         /* Calculate the number of request entries needed. */
373         req_cnt = (ha->calc_request_entries)(cmd->request->nr_hw_segments);
374         if (ha->req_q_cnt < (req_cnt + 2)) {
375                 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
376                 if (ha->req_ring_index < cnt)
377                         ha->req_q_cnt = cnt - ha->req_ring_index;
378                 else
379                         ha->req_q_cnt = ha->request_q_length -
380                             (ha->req_ring_index - cnt);
381         }
382         if (ha->req_q_cnt < (req_cnt + 2))
383                 goto queuing_error;
384
385         /* Finally, we have enough space, now perform mappings. */
386         tot_dsds = 0;
387         if (cmd->use_sg) {
388                 sg = (struct scatterlist *) cmd->request_buffer;
389                 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
390                     cmd->sc_data_direction);
391                 if (tot_dsds == 0)
392                         goto queuing_error;
393         } else if (cmd->request_bufflen) {
394             tot_dsds++;
395         }
396         req_cnt = (ha->calc_request_entries)(tot_dsds);
397
398         /* Build command packet */
399         ha->current_outstanding_cmd = handle;
400         ha->outstanding_cmds[handle] = sp;
401         sp->ha = ha;
402         sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
403         ha->req_q_cnt -= req_cnt;
404
405         cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
406         cmd_pkt->handle = handle;
407         /* Zero out remaining portion of packet. */
408         clr_ptr = (uint32_t *)cmd_pkt + 2;
409         memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
410         cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
411
412         /* Set target ID and LUN number*/
413         SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
414         cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
415
416         /* Update tagged queuing modifier */
417         cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
418         if (scsi_populate_tag_msg(cmd, tag)) {
419                 switch (tag[0]) {
420                 case MSG_HEAD_TAG:
421                         cmd_pkt->control_flags =
422                             __constant_cpu_to_le16(CF_HEAD_TAG);
423                         break;
424                 case MSG_ORDERED_TAG:
425                         cmd_pkt->control_flags =
426                             __constant_cpu_to_le16(CF_ORDERED_TAG);
427                         break;
428                 }
429         }
430
431         /* Load SCSI command packet. */
432         memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
433         cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
434
435         /* Build IOCB segments */
436         (ha->build_scsi_iocbs)(sp, cmd_pkt, tot_dsds);
437
438         /* Set total data segment count. */
439         cmd_pkt->entry_count = (uint8_t)req_cnt;
440         wmb();
441
442         /* Adjust ring index. */
443         ha->req_ring_index++;
444         if (ha->req_ring_index == ha->request_q_length) {
445                 ha->req_ring_index = 0;
446                 ha->request_ring_ptr = ha->request_ring;
447         } else
448                 ha->request_ring_ptr++;
449
450         ha->actthreads++;
451         ha->total_ios++;
452         sp->flags |= SRB_DMA_VALID;
453         sp->state = SRB_ACTIVE_STATE;
454         sp->u_start = jiffies;
455
456         /* Set chip new ring index. */
457         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
458         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
459
460         spin_unlock_irqrestore(&ha->hardware_lock, flags);
461         return (QLA_SUCCESS);
462
463 queuing_error:
464         spin_unlock_irqrestore(&ha->hardware_lock, flags);
465
466         return (QLA_FUNCTION_FAILED);
467 }
468
469 /**
470  * qla2x00_marker() - Send a marker IOCB to the firmware.
471  * @ha: HA context
472  * @loop_id: loop ID
473  * @lun: LUN
474  * @type: marker modifier
475  *
476  * Can be called from both normal and interrupt context.
477  *
478  * Returns non-zero if a failure occured, else zero.
479  */
480 int
481 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
482     uint8_t type)
483 {
484         mrk_entry_t     *pkt;
485
486         pkt = (mrk_entry_t *)qla2x00_req_pkt(ha);
487         if (pkt == NULL) {
488                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
489
490                 return (QLA_FUNCTION_FAILED);
491         }
492
493         pkt->entry_type = MARKER_TYPE;
494         pkt->modifier = type;
495
496         if (type != MK_SYNC_ALL) {
497                 pkt->lun = cpu_to_le16(lun);
498                 SET_TARGET_ID(ha, pkt->target, loop_id);
499         }
500         wmb();
501
502         /* Issue command to ISP */
503         qla2x00_isp_cmd(ha);
504
505         return (QLA_SUCCESS);
506 }
507
508 int 
509 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
510     uint8_t type)
511 {
512         int ret;
513         unsigned long flags = 0;
514
515         spin_lock_irqsave(&ha->hardware_lock, flags);
516         ret = __qla2x00_marker(ha, loop_id, lun, type);
517         spin_unlock_irqrestore(&ha->hardware_lock, flags);
518
519         return (ret);
520 }
521
522 /**
523  * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
524  * @ha: HA context
525  *
526  * Note: The caller must hold the hardware lock before calling this routine.
527  *
528  * Returns NULL if function failed, else, a pointer to the request packet.
529  */
530 static request_t *
531 qla2x00_req_pkt(scsi_qla_host_t *ha)
532 {
533         device_reg_t __iomem *reg = ha->iobase;
534         request_t       *pkt = NULL;
535         uint16_t        cnt;
536         uint32_t        *dword_ptr;
537         uint32_t        timer;
538         uint16_t        req_cnt = 1;
539
540         /* Wait 1 second for slot. */
541         for (timer = HZ; timer; timer--) {
542                 if ((req_cnt + 2) >= ha->req_q_cnt) {
543                         /* Calculate number of free request entries. */
544                         cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
545                         if  (ha->req_ring_index < cnt)
546                                 ha->req_q_cnt = cnt - ha->req_ring_index;
547                         else
548                                 ha->req_q_cnt = ha->request_q_length -
549                                     (ha->req_ring_index - cnt);
550                 }
551                 /* If room for request in request ring. */
552                 if ((req_cnt + 2) < ha->req_q_cnt) {
553                         ha->req_q_cnt--;
554                         pkt = ha->request_ring_ptr;
555
556                         /* Zero out packet. */
557                         dword_ptr = (uint32_t *)pkt;
558                         for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
559                                 *dword_ptr++ = 0;
560
561                         /* Set system defined field. */
562                         pkt->sys_define = (uint8_t)ha->req_ring_index;
563
564                         /* Set entry count. */
565                         pkt->entry_count = 1;
566
567                         break;
568                 }
569
570                 /* Release ring specific lock */
571                 spin_unlock(&ha->hardware_lock);
572
573                 udelay(2);   /* 2 us */
574
575                 /* Check for pending interrupts. */
576                 /* During init we issue marker directly */
577                 if (!ha->marker_needed)
578                         qla2x00_poll(ha);
579
580                 spin_lock_irq(&ha->hardware_lock);
581         }
582         if (!pkt) {
583                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
584         }
585
586         return (pkt);
587 }
588
589 /**
590  * qla2x00_isp_cmd() - Modify the request ring pointer.
591  * @ha: HA context
592  *
593  * Note: The caller must hold the hardware lock before calling this routine.
594  */
595 void
596 qla2x00_isp_cmd(scsi_qla_host_t *ha)
597 {
598         device_reg_t __iomem *reg = ha->iobase;
599
600         DEBUG5(printk("%s(): IOCB data:\n", __func__));
601         DEBUG5(qla2x00_dump_buffer(
602             (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
603
604         /* Adjust ring index. */
605         ha->req_ring_index++;
606         if (ha->req_ring_index == ha->request_q_length) {
607                 ha->req_ring_index = 0;
608                 ha->request_ring_ptr = ha->request_ring;
609         } else
610                 ha->request_ring_ptr++;
611
612         /* Set chip new ring index. */
613         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
614         RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg));     /* PCI Posting. */
615 }