Commit | Line | Data |
---|---|---|
fa90c54f AV |
1 | /* |
2 | * QLogic Fibre Channel HBA Driver | |
01e58d8e | 3 | * Copyright (c) 2003-2008 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 | ||
73208dfd AC |
14 | static request_t *qla2x00_req_pkt(struct scsi_qla_host *, struct req_que *, |
15 | struct rsp_que *rsp); | |
16 | static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *); | |
1da177e4 LT |
17 | |
18 | /** | |
19 | * qla2x00_get_cmd_direction() - Determine control_flag data direction. | |
20 | * @cmd: SCSI command | |
21 | * | |
22 | * Returns the proper CF_* direction based on CDB. | |
23 | */ | |
24 | static inline uint16_t | |
49fd462a | 25 | qla2x00_get_cmd_direction(srb_t *sp) |
1da177e4 LT |
26 | { |
27 | uint16_t cflags; | |
28 | ||
29 | cflags = 0; | |
30 | ||
31 | /* Set transfer direction */ | |
49fd462a | 32 | if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) { |
1da177e4 | 33 | cflags = CF_WRITE; |
e315cd28 | 34 | sp->fcport->vha->hw->qla_stats.output_bytes += |
49fd462a HZ |
35 | scsi_bufflen(sp->cmd); |
36 | } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) { | |
1da177e4 | 37 | cflags = CF_READ; |
e315cd28 | 38 | sp->fcport->vha->hw->qla_stats.input_bytes += |
49fd462a HZ |
39 | scsi_bufflen(sp->cmd); |
40 | } | |
1da177e4 LT |
41 | return (cflags); |
42 | } | |
43 | ||
44 | /** | |
45 | * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and | |
46 | * Continuation Type 0 IOCBs to allocate. | |
47 | * | |
48 | * @dsds: number of data segment decriptors needed | |
49 | * | |
50 | * Returns the number of IOCB entries needed to store @dsds. | |
51 | */ | |
52 | uint16_t | |
53 | qla2x00_calc_iocbs_32(uint16_t dsds) | |
54 | { | |
55 | uint16_t iocbs; | |
56 | ||
57 | iocbs = 1; | |
58 | if (dsds > 3) { | |
59 | iocbs += (dsds - 3) / 7; | |
60 | if ((dsds - 3) % 7) | |
61 | iocbs++; | |
62 | } | |
63 | return (iocbs); | |
64 | } | |
65 | ||
66 | /** | |
67 | * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and | |
68 | * Continuation Type 1 IOCBs to allocate. | |
69 | * | |
70 | * @dsds: number of data segment decriptors needed | |
71 | * | |
72 | * Returns the number of IOCB entries needed to store @dsds. | |
73 | */ | |
74 | uint16_t | |
75 | qla2x00_calc_iocbs_64(uint16_t dsds) | |
76 | { | |
77 | uint16_t iocbs; | |
78 | ||
79 | iocbs = 1; | |
80 | if (dsds > 2) { | |
81 | iocbs += (dsds - 2) / 5; | |
82 | if ((dsds - 2) % 5) | |
83 | iocbs++; | |
84 | } | |
85 | return (iocbs); | |
86 | } | |
87 | ||
88 | /** | |
89 | * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB. | |
90 | * @ha: HA context | |
91 | * | |
92 | * Returns a pointer to the Continuation Type 0 IOCB packet. | |
93 | */ | |
94 | static inline cont_entry_t * | |
73208dfd | 95 | qla2x00_prep_cont_type0_iocb(struct req_que *req, struct scsi_qla_host *vha) |
1da177e4 LT |
96 | { |
97 | cont_entry_t *cont_pkt; | |
1da177e4 | 98 | /* Adjust ring index. */ |
e315cd28 AC |
99 | req->ring_index++; |
100 | if (req->ring_index == req->length) { | |
101 | req->ring_index = 0; | |
102 | req->ring_ptr = req->ring; | |
1da177e4 | 103 | } else { |
e315cd28 | 104 | req->ring_ptr++; |
1da177e4 LT |
105 | } |
106 | ||
e315cd28 | 107 | cont_pkt = (cont_entry_t *)req->ring_ptr; |
1da177e4 LT |
108 | |
109 | /* Load packet defaults. */ | |
110 | *((uint32_t *)(&cont_pkt->entry_type)) = | |
111 | __constant_cpu_to_le32(CONTINUE_TYPE); | |
112 | ||
113 | return (cont_pkt); | |
114 | } | |
115 | ||
116 | /** | |
117 | * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB. | |
118 | * @ha: HA context | |
119 | * | |
120 | * Returns a pointer to the continuation type 1 IOCB packet. | |
121 | */ | |
122 | static inline cont_a64_entry_t * | |
73208dfd | 123 | qla2x00_prep_cont_type1_iocb(struct req_que *req, scsi_qla_host_t *vha) |
1da177e4 LT |
124 | { |
125 | cont_a64_entry_t *cont_pkt; | |
126 | ||
127 | /* Adjust ring index. */ | |
e315cd28 AC |
128 | req->ring_index++; |
129 | if (req->ring_index == req->length) { | |
130 | req->ring_index = 0; | |
131 | req->ring_ptr = req->ring; | |
1da177e4 | 132 | } else { |
e315cd28 | 133 | req->ring_ptr++; |
1da177e4 LT |
134 | } |
135 | ||
e315cd28 | 136 | cont_pkt = (cont_a64_entry_t *)req->ring_ptr; |
1da177e4 LT |
137 | |
138 | /* Load packet defaults. */ | |
139 | *((uint32_t *)(&cont_pkt->entry_type)) = | |
140 | __constant_cpu_to_le32(CONTINUE_A64_TYPE); | |
141 | ||
142 | return (cont_pkt); | |
143 | } | |
144 | ||
145 | /** | |
146 | * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit | |
147 | * capable IOCB types. | |
148 | * | |
149 | * @sp: SRB command to process | |
150 | * @cmd_pkt: Command type 2 IOCB | |
151 | * @tot_dsds: Total number of segments to transfer | |
152 | */ | |
153 | void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt, | |
154 | uint16_t tot_dsds) | |
155 | { | |
156 | uint16_t avail_dsds; | |
157 | uint32_t *cur_dsd; | |
e315cd28 | 158 | scsi_qla_host_t *vha; |
1da177e4 | 159 | struct scsi_cmnd *cmd; |
385d70b4 FT |
160 | struct scatterlist *sg; |
161 | int i; | |
73208dfd | 162 | struct req_que *req; |
1da177e4 LT |
163 | |
164 | cmd = sp->cmd; | |
165 | ||
166 | /* Update entry type to indicate Command Type 2 IOCB */ | |
167 | *((uint32_t *)(&cmd_pkt->entry_type)) = | |
168 | __constant_cpu_to_le32(COMMAND_TYPE); | |
169 | ||
170 | /* No data transfer */ | |
385d70b4 | 171 | if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { |
1da177e4 LT |
172 | cmd_pkt->byte_count = __constant_cpu_to_le32(0); |
173 | return; | |
174 | } | |
175 | ||
444786d7 | 176 | vha = sp->fcport->vha; |
17d98630 | 177 | req = sp->que; |
1da177e4 | 178 | |
49fd462a | 179 | cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp)); |
1da177e4 LT |
180 | |
181 | /* Three DSDs are available in the Command Type 2 IOCB */ | |
182 | avail_dsds = 3; | |
183 | cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; | |
184 | ||
185 | /* Load data segments */ | |
385d70b4 FT |
186 | scsi_for_each_sg(cmd, sg, tot_dsds, i) { |
187 | cont_entry_t *cont_pkt; | |
188 | ||
189 | /* Allocate additional continuation packets? */ | |
190 | if (avail_dsds == 0) { | |
191 | /* | |
192 | * Seven DSDs are available in the Continuation | |
193 | * Type 0 IOCB. | |
194 | */ | |
73208dfd | 195 | cont_pkt = qla2x00_prep_cont_type0_iocb(req, vha); |
385d70b4 FT |
196 | cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address; |
197 | avail_dsds = 7; | |
1da177e4 | 198 | } |
385d70b4 FT |
199 | |
200 | *cur_dsd++ = cpu_to_le32(sg_dma_address(sg)); | |
201 | *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); | |
202 | avail_dsds--; | |
1da177e4 LT |
203 | } |
204 | } | |
205 | ||
206 | /** | |
207 | * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit | |
208 | * capable IOCB types. | |
209 | * | |
210 | * @sp: SRB command to process | |
211 | * @cmd_pkt: Command type 3 IOCB | |
212 | * @tot_dsds: Total number of segments to transfer | |
213 | */ | |
214 | void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt, | |
215 | uint16_t tot_dsds) | |
216 | { | |
217 | uint16_t avail_dsds; | |
218 | uint32_t *cur_dsd; | |
e315cd28 | 219 | scsi_qla_host_t *vha; |
1da177e4 | 220 | struct scsi_cmnd *cmd; |
385d70b4 FT |
221 | struct scatterlist *sg; |
222 | int i; | |
73208dfd | 223 | struct req_que *req; |
1da177e4 LT |
224 | |
225 | cmd = sp->cmd; | |
226 | ||
227 | /* Update entry type to indicate Command Type 3 IOCB */ | |
228 | *((uint32_t *)(&cmd_pkt->entry_type)) = | |
229 | __constant_cpu_to_le32(COMMAND_A64_TYPE); | |
230 | ||
231 | /* No data transfer */ | |
385d70b4 | 232 | if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { |
1da177e4 LT |
233 | cmd_pkt->byte_count = __constant_cpu_to_le32(0); |
234 | return; | |
235 | } | |
236 | ||
444786d7 | 237 | vha = sp->fcport->vha; |
17d98630 | 238 | req = sp->que; |
1da177e4 | 239 | |
49fd462a | 240 | cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp)); |
1da177e4 LT |
241 | |
242 | /* Two DSDs are available in the Command Type 3 IOCB */ | |
243 | avail_dsds = 2; | |
244 | cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; | |
245 | ||
246 | /* Load data segments */ | |
385d70b4 FT |
247 | scsi_for_each_sg(cmd, sg, tot_dsds, i) { |
248 | dma_addr_t sle_dma; | |
249 | cont_a64_entry_t *cont_pkt; | |
250 | ||
251 | /* Allocate additional continuation packets? */ | |
252 | if (avail_dsds == 0) { | |
253 | /* | |
254 | * Five DSDs are available in the Continuation | |
255 | * Type 1 IOCB. | |
256 | */ | |
73208dfd | 257 | cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha); |
385d70b4 FT |
258 | cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; |
259 | avail_dsds = 5; | |
1da177e4 | 260 | } |
385d70b4 FT |
261 | |
262 | sle_dma = sg_dma_address(sg); | |
263 | *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); | |
264 | *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); | |
265 | *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); | |
266 | avail_dsds--; | |
1da177e4 LT |
267 | } |
268 | } | |
269 | ||
270 | /** | |
271 | * qla2x00_start_scsi() - Send a SCSI command to the ISP | |
272 | * @sp: command to send to the ISP | |
273 | * | |
cc3ef7bc | 274 | * Returns non-zero if a failure occurred, else zero. |
1da177e4 LT |
275 | */ |
276 | int | |
277 | qla2x00_start_scsi(srb_t *sp) | |
278 | { | |
385d70b4 | 279 | int ret, nseg; |
1da177e4 | 280 | unsigned long flags; |
e315cd28 | 281 | scsi_qla_host_t *vha; |
1da177e4 LT |
282 | struct scsi_cmnd *cmd; |
283 | uint32_t *clr_ptr; | |
284 | uint32_t index; | |
285 | uint32_t handle; | |
286 | cmd_entry_t *cmd_pkt; | |
1da177e4 LT |
287 | uint16_t cnt; |
288 | uint16_t req_cnt; | |
289 | uint16_t tot_dsds; | |
3d71644c | 290 | struct device_reg_2xxx __iomem *reg; |
e315cd28 AC |
291 | struct qla_hw_data *ha; |
292 | struct req_que *req; | |
73208dfd | 293 | struct rsp_que *rsp; |
1da177e4 LT |
294 | |
295 | /* Setup device pointers. */ | |
296 | ret = 0; | |
444786d7 | 297 | vha = sp->fcport->vha; |
e315cd28 | 298 | ha = vha->hw; |
3d71644c | 299 | reg = &ha->iobase->isp; |
1da177e4 | 300 | cmd = sp->cmd; |
73208dfd AC |
301 | req = ha->req_q_map[0]; |
302 | rsp = ha->rsp_q_map[0]; | |
83021920 | 303 | /* So we know we haven't pci_map'ed anything yet */ |
304 | tot_dsds = 0; | |
1da177e4 LT |
305 | |
306 | /* Send marker if required */ | |
e315cd28 | 307 | if (vha->marker_needed != 0) { |
73208dfd AC |
308 | if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) |
309 | != QLA_SUCCESS) | |
1da177e4 | 310 | return (QLA_FUNCTION_FAILED); |
e315cd28 | 311 | vha->marker_needed = 0; |
1da177e4 LT |
312 | } |
313 | ||
314 | /* Acquire ring specific lock */ | |
c9c5ced9 | 315 | spin_lock_irqsave(&ha->hardware_lock, flags); |
1da177e4 LT |
316 | |
317 | /* Check for room in outstanding command list. */ | |
e315cd28 | 318 | handle = req->current_outstanding_cmd; |
1da177e4 LT |
319 | for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) { |
320 | handle++; | |
321 | if (handle == MAX_OUTSTANDING_COMMANDS) | |
322 | handle = 1; | |
e315cd28 | 323 | if (!req->outstanding_cmds[handle]) |
1da177e4 LT |
324 | break; |
325 | } | |
326 | if (index == MAX_OUTSTANDING_COMMANDS) | |
327 | goto queuing_error; | |
328 | ||
83021920 | 329 | /* Map the sg table so we have an accurate count of sg entries needed */ |
2c3dfe3f SJ |
330 | if (scsi_sg_count(cmd)) { |
331 | nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), | |
332 | scsi_sg_count(cmd), cmd->sc_data_direction); | |
333 | if (unlikely(!nseg)) | |
334 | goto queuing_error; | |
335 | } else | |
336 | nseg = 0; | |
337 | ||
385d70b4 | 338 | tot_dsds = nseg; |
83021920 | 339 | |
1da177e4 | 340 | /* Calculate the number of request entries needed. */ |
fd34f556 | 341 | req_cnt = ha->isp_ops->calc_req_entries(tot_dsds); |
e315cd28 | 342 | if (req->cnt < (req_cnt + 2)) { |
1da177e4 | 343 | cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg)); |
e315cd28 AC |
344 | if (req->ring_index < cnt) |
345 | req->cnt = cnt - req->ring_index; | |
1da177e4 | 346 | else |
e315cd28 AC |
347 | req->cnt = req->length - |
348 | (req->ring_index - cnt); | |
1da177e4 | 349 | } |
e315cd28 | 350 | if (req->cnt < (req_cnt + 2)) |
1da177e4 LT |
351 | goto queuing_error; |
352 | ||
1da177e4 | 353 | /* Build command packet */ |
e315cd28 AC |
354 | req->current_outstanding_cmd = handle; |
355 | req->outstanding_cmds[handle] = sp; | |
17d98630 | 356 | sp->que = req; |
1da177e4 | 357 | sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle; |
e315cd28 | 358 | req->cnt -= req_cnt; |
1da177e4 | 359 | |
e315cd28 | 360 | cmd_pkt = (cmd_entry_t *)req->ring_ptr; |
1da177e4 LT |
361 | cmd_pkt->handle = handle; |
362 | /* Zero out remaining portion of packet. */ | |
363 | clr_ptr = (uint32_t *)cmd_pkt + 2; | |
364 | memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); | |
365 | cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); | |
366 | ||
bdf79621 | 367 | /* Set target ID and LUN number*/ |
368 | SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id); | |
369 | cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun); | |
1da177e4 LT |
370 | |
371 | /* Update tagged queuing modifier */ | |
372 | cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG); | |
1da177e4 | 373 | |
1da177e4 LT |
374 | /* Load SCSI command packet. */ |
375 | memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len); | |
385d70b4 | 376 | cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); |
1da177e4 LT |
377 | |
378 | /* Build IOCB segments */ | |
fd34f556 | 379 | ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds); |
1da177e4 LT |
380 | |
381 | /* Set total data segment count. */ | |
382 | cmd_pkt->entry_count = (uint8_t)req_cnt; | |
383 | wmb(); | |
384 | ||
385 | /* Adjust ring index. */ | |
e315cd28 AC |
386 | req->ring_index++; |
387 | if (req->ring_index == req->length) { | |
388 | req->ring_index = 0; | |
389 | req->ring_ptr = req->ring; | |
1da177e4 | 390 | } else |
e315cd28 | 391 | req->ring_ptr++; |
1da177e4 | 392 | |
1da177e4 | 393 | sp->flags |= SRB_DMA_VALID; |
1da177e4 LT |
394 | |
395 | /* Set chip new ring index. */ | |
e315cd28 | 396 | WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index); |
1da177e4 LT |
397 | RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */ |
398 | ||
4fdfefe5 | 399 | /* Manage unprocessed RIO/ZIO commands in response queue. */ |
e315cd28 | 400 | if (vha->flags.process_response_queue && |
73208dfd AC |
401 | rsp->ring_ptr->signature != RESPONSE_PROCESSED) |
402 | qla2x00_process_response_queue(rsp); | |
4fdfefe5 | 403 | |
c9c5ced9 | 404 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
1da177e4 LT |
405 | return (QLA_SUCCESS); |
406 | ||
407 | queuing_error: | |
385d70b4 FT |
408 | if (tot_dsds) |
409 | scsi_dma_unmap(cmd); | |
410 | ||
c9c5ced9 | 411 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
1da177e4 LT |
412 | |
413 | return (QLA_FUNCTION_FAILED); | |
414 | } | |
415 | ||
416 | /** | |
417 | * qla2x00_marker() - Send a marker IOCB to the firmware. | |
418 | * @ha: HA context | |
419 | * @loop_id: loop ID | |
420 | * @lun: LUN | |
421 | * @type: marker modifier | |
422 | * | |
423 | * Can be called from both normal and interrupt context. | |
424 | * | |
cc3ef7bc | 425 | * Returns non-zero if a failure occurred, else zero. |
1da177e4 | 426 | */ |
fa2a1ce5 | 427 | int |
73208dfd AC |
428 | __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req, |
429 | struct rsp_que *rsp, uint16_t loop_id, | |
430 | uint16_t lun, uint8_t type) | |
1da177e4 | 431 | { |
2b6c0cee AV |
432 | mrk_entry_t *mrk; |
433 | struct mrk_entry_24xx *mrk24; | |
e315cd28 AC |
434 | struct qla_hw_data *ha = vha->hw; |
435 | scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev); | |
1da177e4 | 436 | |
2b6c0cee | 437 | mrk24 = NULL; |
73208dfd | 438 | mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp); |
2b6c0cee AV |
439 | if (mrk == NULL) { |
440 | DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n", | |
e315cd28 | 441 | __func__, base_vha->host_no)); |
1da177e4 LT |
442 | |
443 | return (QLA_FUNCTION_FAILED); | |
444 | } | |
445 | ||
2b6c0cee AV |
446 | mrk->entry_type = MARKER_TYPE; |
447 | mrk->modifier = type; | |
1da177e4 | 448 | if (type != MK_SYNC_ALL) { |
e428924c | 449 | if (IS_FWI2_CAPABLE(ha)) { |
2b6c0cee AV |
450 | mrk24 = (struct mrk_entry_24xx *) mrk; |
451 | mrk24->nport_handle = cpu_to_le16(loop_id); | |
452 | mrk24->lun[1] = LSB(lun); | |
453 | mrk24->lun[2] = MSB(lun); | |
b797b6de | 454 | host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun)); |
e315cd28 | 455 | mrk24->vp_index = vha->vp_idx; |
2b6c0cee AV |
456 | } else { |
457 | SET_TARGET_ID(ha, mrk->target, loop_id); | |
458 | mrk->lun = cpu_to_le16(lun); | |
459 | } | |
1da177e4 LT |
460 | } |
461 | wmb(); | |
462 | ||
73208dfd | 463 | qla2x00_isp_cmd(vha, req); |
1da177e4 LT |
464 | |
465 | return (QLA_SUCCESS); | |
466 | } | |
467 | ||
fa2a1ce5 | 468 | int |
73208dfd AC |
469 | qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req, |
470 | struct rsp_que *rsp, uint16_t loop_id, uint16_t lun, | |
471 | uint8_t type) | |
1da177e4 LT |
472 | { |
473 | int ret; | |
474 | unsigned long flags = 0; | |
475 | ||
73208dfd AC |
476 | spin_lock_irqsave(&vha->hw->hardware_lock, flags); |
477 | ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type); | |
478 | spin_unlock_irqrestore(&vha->hw->hardware_lock, flags); | |
1da177e4 LT |
479 | |
480 | return (ret); | |
481 | } | |
482 | ||
483 | /** | |
484 | * qla2x00_req_pkt() - Retrieve a request packet from the request ring. | |
485 | * @ha: HA context | |
486 | * | |
487 | * Note: The caller must hold the hardware lock before calling this routine. | |
488 | * | |
489 | * Returns NULL if function failed, else, a pointer to the request packet. | |
490 | */ | |
491 | static request_t * | |
73208dfd AC |
492 | qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req, |
493 | struct rsp_que *rsp) | |
1da177e4 | 494 | { |
e315cd28 | 495 | struct qla_hw_data *ha = vha->hw; |
73208dfd | 496 | device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id); |
1da177e4 LT |
497 | request_t *pkt = NULL; |
498 | uint16_t cnt; | |
499 | uint32_t *dword_ptr; | |
500 | uint32_t timer; | |
501 | uint16_t req_cnt = 1; | |
502 | ||
503 | /* Wait 1 second for slot. */ | |
504 | for (timer = HZ; timer; timer--) { | |
e315cd28 | 505 | if ((req_cnt + 2) >= req->cnt) { |
1da177e4 | 506 | /* Calculate number of free request entries. */ |
73208dfd AC |
507 | if (ha->mqenable) |
508 | cnt = (uint16_t) | |
509 | RD_REG_DWORD(®->isp25mq.req_q_out); | |
510 | else { | |
511 | if (IS_FWI2_CAPABLE(ha)) | |
512 | cnt = (uint16_t)RD_REG_DWORD( | |
513 | ®->isp24.req_q_out); | |
514 | else | |
515 | cnt = qla2x00_debounce_register( | |
516 | ISP_REQ_Q_OUT(ha, ®->isp)); | |
517 | } | |
e315cd28 AC |
518 | if (req->ring_index < cnt) |
519 | req->cnt = cnt - req->ring_index; | |
1da177e4 | 520 | else |
e315cd28 AC |
521 | req->cnt = req->length - |
522 | (req->ring_index - cnt); | |
1da177e4 LT |
523 | } |
524 | /* If room for request in request ring. */ | |
e315cd28 AC |
525 | if ((req_cnt + 2) < req->cnt) { |
526 | req->cnt--; | |
527 | pkt = req->ring_ptr; | |
1da177e4 LT |
528 | |
529 | /* Zero out packet. */ | |
530 | dword_ptr = (uint32_t *)pkt; | |
531 | for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++) | |
532 | *dword_ptr++ = 0; | |
533 | ||
534 | /* Set system defined field. */ | |
e315cd28 | 535 | pkt->sys_define = (uint8_t)req->ring_index; |
1da177e4 LT |
536 | |
537 | /* Set entry count. */ | |
538 | pkt->entry_count = 1; | |
539 | ||
540 | break; | |
541 | } | |
542 | ||
543 | /* Release ring specific lock */ | |
e315cd28 | 544 | spin_unlock_irq(&ha->hardware_lock); |
1da177e4 LT |
545 | |
546 | udelay(2); /* 2 us */ | |
547 | ||
548 | /* Check for pending interrupts. */ | |
549 | /* During init we issue marker directly */ | |
e315cd28 | 550 | if (!vha->marker_needed && !vha->flags.init_done) |
73208dfd | 551 | qla2x00_poll(rsp); |
1da177e4 LT |
552 | spin_lock_irq(&ha->hardware_lock); |
553 | } | |
554 | if (!pkt) { | |
555 | DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__)); | |
556 | } | |
557 | ||
558 | return (pkt); | |
559 | } | |
560 | ||
561 | /** | |
562 | * qla2x00_isp_cmd() - Modify the request ring pointer. | |
563 | * @ha: HA context | |
564 | * | |
565 | * Note: The caller must hold the hardware lock before calling this routine. | |
566 | */ | |
413975a0 | 567 | static void |
73208dfd | 568 | qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req) |
1da177e4 | 569 | { |
e315cd28 | 570 | struct qla_hw_data *ha = vha->hw; |
73208dfd | 571 | device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id); |
17d98630 | 572 | struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp; |
1da177e4 LT |
573 | |
574 | DEBUG5(printk("%s(): IOCB data:\n", __func__)); | |
575 | DEBUG5(qla2x00_dump_buffer( | |
e315cd28 | 576 | (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE)); |
1da177e4 LT |
577 | |
578 | /* Adjust ring index. */ | |
e315cd28 AC |
579 | req->ring_index++; |
580 | if (req->ring_index == req->length) { | |
581 | req->ring_index = 0; | |
582 | req->ring_ptr = req->ring; | |
1da177e4 | 583 | } else |
e315cd28 | 584 | req->ring_ptr++; |
1da177e4 LT |
585 | |
586 | /* Set chip new ring index. */ | |
17d98630 AC |
587 | if (ha->mqenable) { |
588 | WRT_REG_DWORD(®->isp25mq.req_q_in, req->ring_index); | |
589 | RD_REG_DWORD(&ioreg->hccr); | |
590 | } | |
73208dfd AC |
591 | else { |
592 | if (IS_FWI2_CAPABLE(ha)) { | |
593 | WRT_REG_DWORD(®->isp24.req_q_in, req->ring_index); | |
594 | RD_REG_DWORD_RELAXED(®->isp24.req_q_in); | |
595 | } else { | |
596 | WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), | |
597 | req->ring_index); | |
598 | RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp)); | |
599 | } | |
2b6c0cee AV |
600 | } |
601 | ||
602 | } | |
603 | ||
604 | /** | |
605 | * qla24xx_calc_iocbs() - Determine number of Command Type 3 and | |
606 | * Continuation Type 1 IOCBs to allocate. | |
607 | * | |
608 | * @dsds: number of data segment decriptors needed | |
609 | * | |
610 | * Returns the number of IOCB entries needed to store @dsds. | |
611 | */ | |
612 | static inline uint16_t | |
613 | qla24xx_calc_iocbs(uint16_t dsds) | |
614 | { | |
615 | uint16_t iocbs; | |
616 | ||
617 | iocbs = 1; | |
618 | if (dsds > 1) { | |
619 | iocbs += (dsds - 1) / 5; | |
620 | if ((dsds - 1) % 5) | |
621 | iocbs++; | |
622 | } | |
623 | return iocbs; | |
624 | } | |
625 | ||
626 | /** | |
627 | * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7 | |
628 | * IOCB types. | |
629 | * | |
630 | * @sp: SRB command to process | |
631 | * @cmd_pkt: Command type 3 IOCB | |
632 | * @tot_dsds: Total number of segments to transfer | |
633 | */ | |
634 | static inline void | |
635 | qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt, | |
636 | uint16_t tot_dsds) | |
637 | { | |
638 | uint16_t avail_dsds; | |
639 | uint32_t *cur_dsd; | |
e315cd28 | 640 | scsi_qla_host_t *vha; |
2b6c0cee | 641 | struct scsi_cmnd *cmd; |
385d70b4 FT |
642 | struct scatterlist *sg; |
643 | int i; | |
73208dfd | 644 | struct req_que *req; |
2b6c0cee AV |
645 | |
646 | cmd = sp->cmd; | |
647 | ||
648 | /* Update entry type to indicate Command Type 3 IOCB */ | |
649 | *((uint32_t *)(&cmd_pkt->entry_type)) = | |
650 | __constant_cpu_to_le32(COMMAND_TYPE_7); | |
651 | ||
652 | /* No data transfer */ | |
385d70b4 | 653 | if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { |
2b6c0cee AV |
654 | cmd_pkt->byte_count = __constant_cpu_to_le32(0); |
655 | return; | |
656 | } | |
657 | ||
444786d7 | 658 | vha = sp->fcport->vha; |
17d98630 | 659 | req = sp->que; |
2b6c0cee AV |
660 | |
661 | /* Set transfer direction */ | |
49fd462a | 662 | if (cmd->sc_data_direction == DMA_TO_DEVICE) { |
2b6c0cee AV |
663 | cmd_pkt->task_mgmt_flags = |
664 | __constant_cpu_to_le16(TMF_WRITE_DATA); | |
e315cd28 | 665 | sp->fcport->vha->hw->qla_stats.output_bytes += |
49fd462a HZ |
666 | scsi_bufflen(sp->cmd); |
667 | } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) { | |
2b6c0cee AV |
668 | cmd_pkt->task_mgmt_flags = |
669 | __constant_cpu_to_le16(TMF_READ_DATA); | |
e315cd28 | 670 | sp->fcport->vha->hw->qla_stats.input_bytes += |
49fd462a HZ |
671 | scsi_bufflen(sp->cmd); |
672 | } | |
2b6c0cee AV |
673 | |
674 | /* One DSD is available in the Command Type 3 IOCB */ | |
675 | avail_dsds = 1; | |
676 | cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; | |
677 | ||
678 | /* Load data segments */ | |
385d70b4 FT |
679 | |
680 | scsi_for_each_sg(cmd, sg, tot_dsds, i) { | |
681 | dma_addr_t sle_dma; | |
682 | cont_a64_entry_t *cont_pkt; | |
683 | ||
684 | /* Allocate additional continuation packets? */ | |
685 | if (avail_dsds == 0) { | |
686 | /* | |
687 | * Five DSDs are available in the Continuation | |
688 | * Type 1 IOCB. | |
689 | */ | |
73208dfd | 690 | cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha); |
385d70b4 FT |
691 | cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; |
692 | avail_dsds = 5; | |
2b6c0cee | 693 | } |
385d70b4 FT |
694 | |
695 | sle_dma = sg_dma_address(sg); | |
696 | *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); | |
697 | *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); | |
698 | *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); | |
699 | avail_dsds--; | |
2b6c0cee AV |
700 | } |
701 | } | |
702 | ||
703 | ||
704 | /** | |
705 | * qla24xx_start_scsi() - Send a SCSI command to the ISP | |
706 | * @sp: command to send to the ISP | |
707 | * | |
cc3ef7bc | 708 | * Returns non-zero if a failure occurred, else zero. |
2b6c0cee AV |
709 | */ |
710 | int | |
711 | qla24xx_start_scsi(srb_t *sp) | |
712 | { | |
385d70b4 | 713 | int ret, nseg; |
2b6c0cee | 714 | unsigned long flags; |
2b6c0cee AV |
715 | uint32_t *clr_ptr; |
716 | uint32_t index; | |
717 | uint32_t handle; | |
718 | struct cmd_type_7 *cmd_pkt; | |
2b6c0cee AV |
719 | uint16_t cnt; |
720 | uint16_t req_cnt; | |
721 | uint16_t tot_dsds; | |
73208dfd AC |
722 | struct req_que *req = NULL; |
723 | struct rsp_que *rsp = NULL; | |
724 | struct scsi_cmnd *cmd = sp->cmd; | |
444786d7 | 725 | struct scsi_qla_host *vha = sp->fcport->vha; |
73208dfd | 726 | struct qla_hw_data *ha = vha->hw; |
73208dfd | 727 | uint16_t que_id; |
2b6c0cee AV |
728 | |
729 | /* Setup device pointers. */ | |
730 | ret = 0; | |
73208dfd AC |
731 | que_id = vha->req_ques[0]; |
732 | ||
733 | req = ha->req_q_map[que_id]; | |
17d98630 | 734 | sp->que = req; |
73208dfd AC |
735 | |
736 | if (req->rsp) | |
737 | rsp = req->rsp; | |
738 | else | |
739 | rsp = ha->rsp_q_map[que_id]; | |
2b6c0cee AV |
740 | /* So we know we haven't pci_map'ed anything yet */ |
741 | tot_dsds = 0; | |
742 | ||
743 | /* Send marker if required */ | |
e315cd28 | 744 | if (vha->marker_needed != 0) { |
73208dfd AC |
745 | if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) |
746 | != QLA_SUCCESS) | |
2b6c0cee | 747 | return QLA_FUNCTION_FAILED; |
e315cd28 | 748 | vha->marker_needed = 0; |
2b6c0cee AV |
749 | } |
750 | ||
751 | /* Acquire ring specific lock */ | |
e315cd28 | 752 | spin_lock_irqsave(&ha->hardware_lock, flags); |
2b6c0cee AV |
753 | |
754 | /* Check for room in outstanding command list. */ | |
e315cd28 | 755 | handle = req->current_outstanding_cmd; |
2b6c0cee AV |
756 | for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) { |
757 | handle++; | |
758 | if (handle == MAX_OUTSTANDING_COMMANDS) | |
759 | handle = 1; | |
e315cd28 | 760 | if (!req->outstanding_cmds[handle]) |
2b6c0cee AV |
761 | break; |
762 | } | |
763 | if (index == MAX_OUTSTANDING_COMMANDS) | |
764 | goto queuing_error; | |
765 | ||
766 | /* Map the sg table so we have an accurate count of sg entries needed */ | |
2c3dfe3f SJ |
767 | if (scsi_sg_count(cmd)) { |
768 | nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), | |
769 | scsi_sg_count(cmd), cmd->sc_data_direction); | |
770 | if (unlikely(!nseg)) | |
2b6c0cee | 771 | goto queuing_error; |
2c3dfe3f SJ |
772 | } else |
773 | nseg = 0; | |
774 | ||
385d70b4 | 775 | tot_dsds = nseg; |
2b6c0cee AV |
776 | |
777 | req_cnt = qla24xx_calc_iocbs(tot_dsds); | |
e315cd28 | 778 | if (req->cnt < (req_cnt + 2)) { |
17d98630 | 779 | cnt = ha->isp_ops->rd_req_reg(ha, req->id); |
73208dfd | 780 | |
e315cd28 AC |
781 | if (req->ring_index < cnt) |
782 | req->cnt = cnt - req->ring_index; | |
2b6c0cee | 783 | else |
e315cd28 AC |
784 | req->cnt = req->length - |
785 | (req->ring_index - cnt); | |
2b6c0cee | 786 | } |
e315cd28 | 787 | if (req->cnt < (req_cnt + 2)) |
2b6c0cee | 788 | goto queuing_error; |
2b6c0cee AV |
789 | |
790 | /* Build command packet. */ | |
e315cd28 AC |
791 | req->current_outstanding_cmd = handle; |
792 | req->outstanding_cmds[handle] = sp; | |
2b6c0cee | 793 | sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle; |
e315cd28 | 794 | req->cnt -= req_cnt; |
2b6c0cee | 795 | |
e315cd28 | 796 | cmd_pkt = (struct cmd_type_7 *)req->ring_ptr; |
2b6c0cee AV |
797 | cmd_pkt->handle = handle; |
798 | ||
799 | /* Zero out remaining portion of packet. */ | |
72df8325 | 800 | /* tagged queuing modifier -- default is TSK_SIMPLE (0). */ |
2b6c0cee AV |
801 | clr_ptr = (uint32_t *)cmd_pkt + 2; |
802 | memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); | |
803 | cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); | |
804 | ||
805 | /* Set NPORT-ID and LUN number*/ | |
806 | cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); | |
807 | cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; | |
808 | cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; | |
809 | cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; | |
2c3dfe3f | 810 | cmd_pkt->vp_index = sp->fcport->vp_idx; |
2b6c0cee | 811 | |
661c3f6c | 812 | int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun); |
0d4be124 | 813 | host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); |
2b6c0cee | 814 | |
2b6c0cee AV |
815 | /* Load SCSI command packet. */ |
816 | memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len); | |
817 | host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb)); | |
818 | ||
385d70b4 | 819 | cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); |
2b6c0cee AV |
820 | |
821 | /* Build IOCB segments */ | |
822 | qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds); | |
823 | ||
824 | /* Set total data segment count. */ | |
825 | cmd_pkt->entry_count = (uint8_t)req_cnt; | |
826 | wmb(); | |
827 | ||
828 | /* Adjust ring index. */ | |
e315cd28 AC |
829 | req->ring_index++; |
830 | if (req->ring_index == req->length) { | |
831 | req->ring_index = 0; | |
832 | req->ring_ptr = req->ring; | |
2b6c0cee | 833 | } else |
e315cd28 | 834 | req->ring_ptr++; |
2b6c0cee AV |
835 | |
836 | sp->flags |= SRB_DMA_VALID; | |
2b6c0cee AV |
837 | |
838 | /* Set chip new ring index. */ | |
17d98630 | 839 | ha->isp_ops->wrt_req_reg(ha, req->id, req->ring_index); |
2b6c0cee | 840 | |
4fdfefe5 | 841 | /* Manage unprocessed RIO/ZIO commands in response queue. */ |
e315cd28 | 842 | if (vha->flags.process_response_queue && |
73208dfd AC |
843 | rsp->ring_ptr->signature != RESPONSE_PROCESSED) |
844 | qla24xx_process_response_queue(rsp); | |
4fdfefe5 | 845 | |
e315cd28 | 846 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2b6c0cee AV |
847 | return QLA_SUCCESS; |
848 | ||
849 | queuing_error: | |
385d70b4 FT |
850 | if (tot_dsds) |
851 | scsi_dma_unmap(cmd); | |
852 | ||
e315cd28 | 853 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2b6c0cee AV |
854 | |
855 | return QLA_FUNCTION_FAILED; | |
1da177e4 | 856 | } |
73208dfd | 857 | |
17d98630 AC |
858 | uint16_t |
859 | qla24xx_rd_req_reg(struct qla_hw_data *ha, uint16_t id) | |
860 | { | |
861 | device_reg_t __iomem *reg = (void *) ha->iobase; | |
862 | return RD_REG_DWORD_RELAXED(®->isp24.req_q_out); | |
863 | } | |
864 | ||
865 | uint16_t | |
866 | qla25xx_rd_req_reg(struct qla_hw_data *ha, uint16_t id) | |
867 | { | |
868 | device_reg_t __iomem *reg = (void *) ha->mqiobase + QLA_QUE_PAGE * id; | |
869 | return RD_REG_DWORD_RELAXED(®->isp25mq.req_q_out); | |
870 | } | |
871 | ||
872 | void | |
873 | qla24xx_wrt_req_reg(struct qla_hw_data *ha, uint16_t id, uint16_t index) | |
874 | { | |
875 | device_reg_t __iomem *reg = (void *) ha->iobase; | |
876 | WRT_REG_DWORD(®->isp24.req_q_in, index); | |
877 | RD_REG_DWORD_RELAXED(®->isp24.req_q_in); | |
878 | } | |
879 | ||
880 | void | |
881 | qla25xx_wrt_req_reg(struct qla_hw_data *ha, uint16_t id, uint16_t index) | |
882 | { | |
883 | device_reg_t __iomem *reg = (void *) ha->mqiobase + QLA_QUE_PAGE * id; | |
884 | struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp; | |
885 | WRT_REG_DWORD(®->isp25mq.req_q_in, index); | |
886 | RD_REG_DWORD(&ioreg->hccr); /* PCI posting */ | |
887 | } | |
888 |