Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[deliverable/linux.git] / drivers / scsi / qla2xxx / qla_iocb.c
CommitLineData
fa90c54f
AV
1/*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
1da177e4 4 *
fa90c54f
AV
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
1da177e4
LT
7#include "qla_def.h"
8
9#include <linux/blkdev.h>
10#include <linux/delay.h>
11
12#include <scsi/scsi_tcq.h>
13
14static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
15static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
16static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
17static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
413975a0 18static void qla2x00_isp_cmd(scsi_qla_host_t *ha);
1da177e4
LT
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 */
26static inline uint16_t
27qla2x00_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 */
49uint16_t
50qla2x00_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 */
71uint16_t
72qla2x00_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 */
91static inline cont_entry_t *
92qla2x00_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 */
120static inline cont_a64_entry_t *
121qla2x00_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 */
151void 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;
385d70b4
FT
158 struct scatterlist *sg;
159 int i;
1da177e4
LT
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 */
385d70b4 168 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
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 */
385d70b4
FT
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;
1da177e4 194 }
385d70b4
FT
195
196 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
197 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
198 avail_dsds--;
1da177e4
LT
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 */
210void 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;
385d70b4
FT
217 struct scatterlist *sg;
218 int i;
1da177e4
LT
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 */
385d70b4 227 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
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 */
385d70b4
FT
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;
1da177e4 254 }
385d70b4
FT
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--;
1da177e4
LT
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 */
270int
271qla2x00_start_scsi(srb_t *sp)
272{
385d70b4 273 int ret, nseg;
1da177e4
LT
274 unsigned long flags;
275 scsi_qla_host_t *ha;
1da177e4
LT
276 struct scsi_cmnd *cmd;
277 uint32_t *clr_ptr;
278 uint32_t index;
279 uint32_t handle;
280 cmd_entry_t *cmd_pkt;
1da177e4
LT
281 uint16_t cnt;
282 uint16_t req_cnt;
283 uint16_t tot_dsds;
3d71644c 284 struct device_reg_2xxx __iomem *reg;
1da177e4
LT
285
286 /* Setup device pointers. */
287 ret = 0;
bdf79621 288 ha = sp->ha;
3d71644c 289 reg = &ha->iobase->isp;
1da177e4 290 cmd = sp->cmd;
83021920 291 /* So we know we haven't pci_map'ed anything yet */
292 tot_dsds = 0;
1da177e4
LT
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;
700ca0e7 311 if (!ha->outstanding_cmds[handle])
1da177e4
LT
312 break;
313 }
314 if (index == MAX_OUTSTANDING_COMMANDS)
315 goto queuing_error;
316
83021920 317 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
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
385d70b4 326 tot_dsds = nseg;
83021920 327
1da177e4 328 /* Calculate the number of request entries needed. */
fd34f556 329 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
1da177e4
LT
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
1da177e4
LT
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
bdf79621 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);
1da177e4
LT
358
359 /* Update tagged queuing modifier */
360 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
1da177e4 361
1da177e4
LT
362 /* Load SCSI command packet. */
363 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
385d70b4 364 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1da177e4
LT
365
366 /* Build IOCB segments */
fd34f556 367 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
1da177e4
LT
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
1da177e4 381 sp->flags |= SRB_DMA_VALID;
1da177e4
LT
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
4fdfefe5
AV
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
1da177e4
LT
392 spin_unlock_irqrestore(&ha->hardware_lock, flags);
393 return (QLA_SUCCESS);
394
395queuing_error:
385d70b4
FT
396 if (tot_dsds)
397 scsi_dma_unmap(cmd);
398
1da177e4
LT
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 */
fa2a1ce5 415int
1da177e4
LT
416__qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
417 uint8_t type)
418{
2b6c0cee
AV
419 mrk_entry_t *mrk;
420 struct mrk_entry_24xx *mrk24;
2c3dfe3f 421 scsi_qla_host_t *pha = to_qla_parent(ha);
1da177e4 422
2b6c0cee 423 mrk24 = NULL;
2c3dfe3f 424 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
2b6c0cee
AV
425 if (mrk == NULL) {
426 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
427 __func__, ha->host_no));
1da177e4
LT
428
429 return (QLA_FUNCTION_FAILED);
430 }
431
2b6c0cee
AV
432 mrk->entry_type = MARKER_TYPE;
433 mrk->modifier = type;
1da177e4 434 if (type != MK_SYNC_ALL) {
e428924c 435 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
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);
b797b6de 440 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
2c3dfe3f 441 mrk24->vp_index = ha->vp_idx;
2b6c0cee
AV
442 } else {
443 SET_TARGET_ID(ha, mrk->target, loop_id);
444 mrk->lun = cpu_to_le16(lun);
445 }
1da177e4
LT
446 }
447 wmb();
448
2c3dfe3f 449 qla2x00_isp_cmd(pha);
1da177e4
LT
450
451 return (QLA_SUCCESS);
452}
453
fa2a1ce5 454int
1da177e4
LT
455qla2x00_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 */
476static request_t *
477qla2x00_req_pkt(scsi_qla_host_t *ha)
478{
2b6c0cee 479 device_reg_t __iomem *reg = ha->iobase;
1da177e4
LT
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. */
e428924c 490 if (IS_FWI2_CAPABLE(ha))
2b6c0cee
AV
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));
1da177e4
LT
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 */
413975a0 546static void
1da177e4
LT
547qla2x00_isp_cmd(scsi_qla_host_t *ha)
548{
2b6c0cee 549 device_reg_t __iomem *reg = ha->iobase;
1da177e4
LT
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. */
e428924c 564 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
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 */
582static inline uint16_t
583qla24xx_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 */
604static inline void
605qla24xx_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;
385d70b4
FT
612 struct scatterlist *sg;
613 int i;
2b6c0cee
AV
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 */
385d70b4 622 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
2b6c0cee
AV
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 */
385d70b4
FT
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;
2b6c0cee 656 }
385d70b4
FT
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--;
2b6c0cee
AV
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 */
673int
674qla24xx_start_scsi(srb_t *sp)
675{
385d70b4 676 int ret, nseg;
2b6c0cee
AV
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;
2b6c0cee
AV
684 uint16_t cnt;
685 uint16_t req_cnt;
686 uint16_t tot_dsds;
db776a14 687 struct device_reg_24xx __iomem *reg;
2b6c0cee
AV
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;
700ca0e7 714 if (!ha->outstanding_cmds[handle])
2b6c0cee
AV
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 */
2c3dfe3f
SJ
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))
2b6c0cee 725 goto queuing_error;
2c3dfe3f
SJ
726 } else
727 nseg = 0;
728
385d70b4 729 tot_dsds = nseg;
2b6c0cee
AV
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 }
131736d3 740 if (ha->req_q_cnt < (req_cnt + 2))
2b6c0cee 741 goto queuing_error;
2b6c0cee
AV
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. */
72df8325 754 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2b6c0cee
AV
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;
2c3dfe3f 764 cmd_pkt->vp_index = sp->fcport->vp_idx;
2b6c0cee 765
661c3f6c 766 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
0d4be124 767 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2b6c0cee 768
2b6c0cee
AV
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
385d70b4 773 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2b6c0cee
AV
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;
2b6c0cee
AV
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
4fdfefe5
AV
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
2b6c0cee
AV
801 spin_unlock_irqrestore(&ha->hardware_lock, flags);
802 return QLA_SUCCESS;
803
804queuing_error:
385d70b4
FT
805 if (tot_dsds)
806 scsi_dma_unmap(cmd);
807
2b6c0cee
AV
808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
809
810 return QLA_FUNCTION_FAILED;
1da177e4 811}
This page took 0.259005 seconds and 5 git commands to generate.