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