.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards |
---|
3 | 4 | * Copyright 2013 Ondrej Zary |
---|
.. | .. |
---|
107 | 108 | } |
---|
108 | 109 | |
---|
109 | 110 | if (status != WD719X_INT_NOERRORS) { |
---|
| 111 | + u8 sue = wd719x_readb(wd, WD719X_AMR_SCB_ERROR); |
---|
| 112 | + /* we get this after wd719x_dev_reset, it's not an error */ |
---|
| 113 | + if (sue == WD719X_SUE_TERM) |
---|
| 114 | + return 0; |
---|
| 115 | + /* we get this after wd719x_bus_reset, it's not an error */ |
---|
| 116 | + if (sue == WD719X_SUE_RESET) |
---|
| 117 | + return 0; |
---|
110 | 118 | dev_err(&wd->pdev->dev, "direct command failed, status 0x%02x, SUE 0x%02x\n", |
---|
111 | | - status, wd719x_readb(wd, WD719X_AMR_SCB_ERROR)); |
---|
| 119 | + status, sue); |
---|
112 | 120 | return -EIO; |
---|
113 | 121 | } |
---|
114 | 122 | |
---|
.. | .. |
---|
127 | 135 | if (wd719x_wait_ready(wd)) |
---|
128 | 136 | return -ETIMEDOUT; |
---|
129 | 137 | |
---|
130 | | - /* make sure we get NO interrupts */ |
---|
131 | | - dev |= WD719X_DISABLE_INT; |
---|
| 138 | + /* disable interrupts except for RESET/ABORT (it breaks them) */ |
---|
| 139 | + if (opcode != WD719X_CMD_BUSRESET && opcode != WD719X_CMD_ABORT && |
---|
| 140 | + opcode != WD719X_CMD_ABORT_TAG && opcode != WD719X_CMD_RESET) |
---|
| 141 | + dev |= WD719X_DISABLE_INT; |
---|
132 | 142 | wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, dev); |
---|
133 | 143 | wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_2, lun); |
---|
134 | 144 | wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_3, tag); |
---|
.. | .. |
---|
153 | 163 | |
---|
154 | 164 | static void wd719x_destroy(struct wd719x *wd) |
---|
155 | 165 | { |
---|
156 | | - struct wd719x_scb *scb; |
---|
157 | | - |
---|
158 | 166 | /* stop the RISC */ |
---|
159 | 167 | if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0, |
---|
160 | 168 | WD719X_WAIT_FOR_RISC)) |
---|
.. | .. |
---|
162 | 170 | /* disable RISC */ |
---|
163 | 171 | wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); |
---|
164 | 172 | |
---|
165 | | - /* free all SCBs */ |
---|
166 | | - list_for_each_entry(scb, &wd->active_scbs, list) |
---|
167 | | - pci_free_consistent(wd->pdev, sizeof(struct wd719x_scb), scb, |
---|
168 | | - scb->phys); |
---|
169 | | - list_for_each_entry(scb, &wd->free_scbs, list) |
---|
170 | | - pci_free_consistent(wd->pdev, sizeof(struct wd719x_scb), scb, |
---|
171 | | - scb->phys); |
---|
| 173 | + WARN_ON_ONCE(!list_empty(&wd->active_scbs)); |
---|
| 174 | + |
---|
172 | 175 | /* free internal buffers */ |
---|
173 | | - pci_free_consistent(wd->pdev, wd->fw_size, wd->fw_virt, wd->fw_phys); |
---|
| 176 | + dma_free_coherent(&wd->pdev->dev, wd->fw_size, wd->fw_virt, |
---|
| 177 | + wd->fw_phys); |
---|
174 | 178 | wd->fw_virt = NULL; |
---|
175 | | - pci_free_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, |
---|
176 | | - wd->hash_phys); |
---|
| 179 | + dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, |
---|
| 180 | + wd->hash_phys); |
---|
177 | 181 | wd->hash_virt = NULL; |
---|
178 | | - pci_free_consistent(wd->pdev, sizeof(struct wd719x_host_param), |
---|
179 | | - wd->params, wd->params_phys); |
---|
| 182 | + dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param), |
---|
| 183 | + wd->params, wd->params_phys); |
---|
180 | 184 | wd->params = NULL; |
---|
181 | 185 | free_irq(wd->pdev->irq, wd); |
---|
182 | 186 | } |
---|
183 | 187 | |
---|
184 | | -/* finish a SCSI command, mark SCB (if any) as free, unmap buffers */ |
---|
185 | | -static void wd719x_finish_cmd(struct scsi_cmnd *cmd, int result) |
---|
| 188 | +/* finish a SCSI command, unmap buffers */ |
---|
| 189 | +static void wd719x_finish_cmd(struct wd719x_scb *scb, int result) |
---|
186 | 190 | { |
---|
| 191 | + struct scsi_cmnd *cmd = scb->cmd; |
---|
187 | 192 | struct wd719x *wd = shost_priv(cmd->device->host); |
---|
188 | | - struct wd719x_scb *scb = (struct wd719x_scb *) cmd->host_scribble; |
---|
189 | 193 | |
---|
190 | | - if (scb) { |
---|
191 | | - list_move(&scb->list, &wd->free_scbs); |
---|
192 | | - dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, |
---|
193 | | - SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); |
---|
194 | | - scsi_dma_unmap(cmd); |
---|
195 | | - } |
---|
| 194 | + list_del(&scb->list); |
---|
| 195 | + |
---|
| 196 | + dma_unmap_single(&wd->pdev->dev, scb->phys, |
---|
| 197 | + sizeof(struct wd719x_scb), DMA_BIDIRECTIONAL); |
---|
| 198 | + scsi_dma_unmap(cmd); |
---|
| 199 | + dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, |
---|
| 200 | + SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); |
---|
| 201 | + |
---|
196 | 202 | cmd->result = result << 16; |
---|
197 | 203 | cmd->scsi_done(cmd); |
---|
198 | 204 | } |
---|
.. | .. |
---|
202 | 208 | { |
---|
203 | 209 | int i, count_sg; |
---|
204 | 210 | unsigned long flags; |
---|
205 | | - struct wd719x_scb *scb; |
---|
| 211 | + struct wd719x_scb *scb = scsi_cmd_priv(cmd); |
---|
206 | 212 | struct wd719x *wd = shost_priv(sh); |
---|
207 | | - dma_addr_t phys; |
---|
208 | 213 | |
---|
209 | | - cmd->host_scribble = NULL; |
---|
210 | | - |
---|
211 | | - /* get a free SCB - either from existing ones or allocate a new one */ |
---|
212 | | - spin_lock_irqsave(wd->sh->host_lock, flags); |
---|
213 | | - scb = list_first_entry_or_null(&wd->free_scbs, struct wd719x_scb, list); |
---|
214 | | - if (scb) { |
---|
215 | | - list_del(&scb->list); |
---|
216 | | - phys = scb->phys; |
---|
217 | | - } else { |
---|
218 | | - spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
219 | | - scb = pci_alloc_consistent(wd->pdev, sizeof(struct wd719x_scb), |
---|
220 | | - &phys); |
---|
221 | | - spin_lock_irqsave(wd->sh->host_lock, flags); |
---|
222 | | - if (!scb) { |
---|
223 | | - dev_err(&wd->pdev->dev, "unable to allocate SCB\n"); |
---|
224 | | - wd719x_finish_cmd(cmd, DID_ERROR); |
---|
225 | | - spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
226 | | - return 0; |
---|
227 | | - } |
---|
228 | | - } |
---|
229 | | - memset(scb, 0, sizeof(struct wd719x_scb)); |
---|
230 | | - list_add(&scb->list, &wd->active_scbs); |
---|
231 | | - |
---|
232 | | - scb->phys = phys; |
---|
233 | 214 | scb->cmd = cmd; |
---|
234 | | - cmd->host_scribble = (char *) scb; |
---|
235 | 215 | |
---|
236 | 216 | scb->CDB_tag = 0; /* Tagged queueing not supported yet */ |
---|
237 | 217 | scb->devid = cmd->device->id; |
---|
.. | .. |
---|
240 | 220 | /* copy the command */ |
---|
241 | 221 | memcpy(scb->CDB, cmd->cmnd, cmd->cmd_len); |
---|
242 | 222 | |
---|
| 223 | + /* map SCB */ |
---|
| 224 | + scb->phys = dma_map_single(&wd->pdev->dev, scb, sizeof(*scb), |
---|
| 225 | + DMA_BIDIRECTIONAL); |
---|
| 226 | + |
---|
| 227 | + if (dma_mapping_error(&wd->pdev->dev, scb->phys)) |
---|
| 228 | + goto out_error; |
---|
| 229 | + |
---|
243 | 230 | /* map sense buffer */ |
---|
244 | 231 | scb->sense_buf_length = SCSI_SENSE_BUFFERSIZE; |
---|
245 | 232 | cmd->SCp.dma_handle = dma_map_single(&wd->pdev->dev, cmd->sense_buffer, |
---|
246 | 233 | SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); |
---|
| 234 | + if (dma_mapping_error(&wd->pdev->dev, cmd->SCp.dma_handle)) |
---|
| 235 | + goto out_unmap_scb; |
---|
247 | 236 | scb->sense_buf = cpu_to_le32(cmd->SCp.dma_handle); |
---|
248 | 237 | |
---|
249 | 238 | /* request autosense */ |
---|
.. | .. |
---|
258 | 247 | |
---|
259 | 248 | /* Scather/gather */ |
---|
260 | 249 | count_sg = scsi_dma_map(cmd); |
---|
261 | | - if (count_sg < 0) { |
---|
262 | | - wd719x_finish_cmd(cmd, DID_ERROR); |
---|
263 | | - spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
264 | | - return 0; |
---|
265 | | - } |
---|
| 250 | + if (count_sg < 0) |
---|
| 251 | + goto out_unmap_sense; |
---|
266 | 252 | BUG_ON(count_sg > WD719X_SG); |
---|
267 | 253 | |
---|
268 | 254 | if (count_sg) { |
---|
.. | .. |
---|
283 | 269 | scb->data_p = 0; |
---|
284 | 270 | } |
---|
285 | 271 | |
---|
| 272 | + spin_lock_irqsave(wd->sh->host_lock, flags); |
---|
| 273 | + |
---|
286 | 274 | /* check if the Command register is free */ |
---|
287 | 275 | if (wd719x_readb(wd, WD719X_AMR_COMMAND) != WD719X_CMD_READY) { |
---|
288 | 276 | spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
289 | 277 | return SCSI_MLQUEUE_HOST_BUSY; |
---|
290 | 278 | } |
---|
| 279 | + |
---|
| 280 | + list_add(&scb->list, &wd->active_scbs); |
---|
291 | 281 | |
---|
292 | 282 | /* write pointer to the AMR */ |
---|
293 | 283 | wd719x_writel(wd, WD719X_AMR_SCB_IN, scb->phys); |
---|
.. | .. |
---|
295 | 285 | wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_PROCESS_SCB); |
---|
296 | 286 | |
---|
297 | 287 | spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
| 288 | + return 0; |
---|
298 | 289 | |
---|
| 290 | +out_unmap_sense: |
---|
| 291 | + dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, |
---|
| 292 | + SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); |
---|
| 293 | +out_unmap_scb: |
---|
| 294 | + dma_unmap_single(&wd->pdev->dev, scb->phys, sizeof(*scb), |
---|
| 295 | + DMA_BIDIRECTIONAL); |
---|
| 296 | +out_error: |
---|
| 297 | + cmd->result = DID_ERROR << 16; |
---|
| 298 | + cmd->scsi_done(cmd); |
---|
299 | 299 | return 0; |
---|
300 | 300 | } |
---|
301 | 301 | |
---|
.. | .. |
---|
327 | 327 | wd->fw_size = ALIGN(fw_wcs->size, 4) + fw_risc->size; |
---|
328 | 328 | |
---|
329 | 329 | if (!wd->fw_virt) |
---|
330 | | - wd->fw_virt = pci_alloc_consistent(wd->pdev, wd->fw_size, |
---|
331 | | - &wd->fw_phys); |
---|
| 330 | + wd->fw_virt = dma_alloc_coherent(&wd->pdev->dev, wd->fw_size, |
---|
| 331 | + &wd->fw_phys, GFP_KERNEL); |
---|
332 | 332 | if (!wd->fw_virt) { |
---|
333 | 333 | ret = -ENOMEM; |
---|
334 | 334 | goto wd719x_init_end; |
---|
.. | .. |
---|
464 | 464 | { |
---|
465 | 465 | int action, result; |
---|
466 | 466 | unsigned long flags; |
---|
467 | | - struct wd719x_scb *scb = (struct wd719x_scb *)cmd->host_scribble; |
---|
| 467 | + struct wd719x_scb *scb = scsi_cmd_priv(cmd); |
---|
468 | 468 | struct wd719x *wd = shost_priv(cmd->device->host); |
---|
469 | 469 | |
---|
470 | 470 | dev_info(&wd->pdev->dev, "abort command, tag: %x\n", cmd->tag); |
---|
.. | .. |
---|
474 | 474 | spin_lock_irqsave(wd->sh->host_lock, flags); |
---|
475 | 475 | result = wd719x_direct_cmd(wd, action, cmd->device->id, |
---|
476 | 476 | cmd->device->lun, cmd->tag, scb->phys, 0); |
---|
| 477 | + wd719x_finish_cmd(scb, DID_ABORT); |
---|
477 | 478 | spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
478 | 479 | if (result) |
---|
479 | 480 | return FAILED; |
---|
.. | .. |
---|
486 | 487 | int result; |
---|
487 | 488 | unsigned long flags; |
---|
488 | 489 | struct wd719x *wd = shost_priv(cmd->device->host); |
---|
| 490 | + struct wd719x_scb *scb, *tmp; |
---|
489 | 491 | |
---|
490 | 492 | dev_info(&wd->pdev->dev, "%s reset requested\n", |
---|
491 | 493 | (opcode == WD719X_CMD_BUSRESET) ? "bus" : "device"); |
---|
.. | .. |
---|
493 | 495 | spin_lock_irqsave(wd->sh->host_lock, flags); |
---|
494 | 496 | result = wd719x_direct_cmd(wd, opcode, device, 0, 0, 0, |
---|
495 | 497 | WD719X_WAIT_FOR_SCSI_RESET); |
---|
| 498 | + /* flush all SCBs (or all for a device if dev_reset) */ |
---|
| 499 | + list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) { |
---|
| 500 | + if (opcode == WD719X_CMD_BUSRESET || |
---|
| 501 | + scb->cmd->device->id == device) |
---|
| 502 | + wd719x_finish_cmd(scb, DID_RESET); |
---|
| 503 | + } |
---|
496 | 504 | spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
497 | 505 | if (result) |
---|
498 | 506 | return FAILED; |
---|
.. | .. |
---|
515 | 523 | struct wd719x *wd = shost_priv(cmd->device->host); |
---|
516 | 524 | struct wd719x_scb *scb, *tmp; |
---|
517 | 525 | unsigned long flags; |
---|
518 | | - int result; |
---|
519 | 526 | |
---|
520 | 527 | dev_info(&wd->pdev->dev, "host reset requested\n"); |
---|
521 | 528 | spin_lock_irqsave(wd->sh->host_lock, flags); |
---|
522 | | - /* Try to reinit the RISC */ |
---|
523 | | - if (wd719x_chip_init(wd) == 0) |
---|
524 | | - result = SUCCESS; |
---|
525 | | - else |
---|
526 | | - result = FAILED; |
---|
| 529 | + /* stop the RISC */ |
---|
| 530 | + if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0, |
---|
| 531 | + WD719X_WAIT_FOR_RISC)) |
---|
| 532 | + dev_warn(&wd->pdev->dev, "RISC sleep command failed\n"); |
---|
| 533 | + /* disable RISC */ |
---|
| 534 | + wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); |
---|
527 | 535 | |
---|
528 | 536 | /* flush all SCBs */ |
---|
529 | | - list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) { |
---|
530 | | - struct scsi_cmnd *tmp_cmd = scb->cmd; |
---|
531 | | - wd719x_finish_cmd(tmp_cmd, result); |
---|
532 | | - } |
---|
| 537 | + list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) |
---|
| 538 | + wd719x_finish_cmd(scb, DID_RESET); |
---|
533 | 539 | spin_unlock_irqrestore(wd->sh->host_lock, flags); |
---|
534 | 540 | |
---|
535 | | - return result; |
---|
| 541 | + /* Try to reinit the RISC */ |
---|
| 542 | + return wd719x_chip_init(wd) == 0 ? SUCCESS : FAILED; |
---|
536 | 543 | } |
---|
537 | 544 | |
---|
538 | 545 | static int wd719x_biosparam(struct scsi_device *sdev, struct block_device *bdev, |
---|
.. | .. |
---|
555 | 562 | union wd719x_regs regs, |
---|
556 | 563 | struct wd719x_scb *scb) |
---|
557 | 564 | { |
---|
558 | | - struct scsi_cmnd *cmd; |
---|
559 | 565 | int result; |
---|
560 | 566 | |
---|
561 | 567 | /* now have to find result from card */ |
---|
.. | .. |
---|
643 | 649 | result = DID_ERROR; |
---|
644 | 650 | break; |
---|
645 | 651 | } |
---|
646 | | - cmd = scb->cmd; |
---|
647 | 652 | |
---|
648 | | - wd719x_finish_cmd(cmd, result); |
---|
| 653 | + wd719x_finish_cmd(scb, result); |
---|
649 | 654 | } |
---|
650 | 655 | |
---|
651 | 656 | static irqreturn_t wd719x_interrupt(int irq, void *dev_id) |
---|
.. | .. |
---|
686 | 691 | else |
---|
687 | 692 | dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n"); |
---|
688 | 693 | } else |
---|
689 | | - dev_warn(&wd->pdev->dev, "direct command 0x%x completed\n", |
---|
| 694 | + dev_dbg(&wd->pdev->dev, "direct command 0x%x completed\n", |
---|
690 | 695 | regs.bytes.OPC); |
---|
691 | 696 | break; |
---|
692 | 697 | case WD719X_INT_PIOREADY: |
---|
.. | .. |
---|
809 | 814 | int ret; |
---|
810 | 815 | |
---|
811 | 816 | INIT_LIST_HEAD(&wd->active_scbs); |
---|
812 | | - INIT_LIST_HEAD(&wd->free_scbs); |
---|
813 | 817 | |
---|
814 | 818 | sh->base = pci_resource_start(wd->pdev, 0); |
---|
815 | 819 | |
---|
.. | .. |
---|
820 | 824 | wd->fw_virt = NULL; |
---|
821 | 825 | |
---|
822 | 826 | /* memory area for host (EEPROM) parameters */ |
---|
823 | | - wd->params = pci_alloc_consistent(wd->pdev, |
---|
824 | | - sizeof(struct wd719x_host_param), |
---|
825 | | - &wd->params_phys); |
---|
| 827 | + wd->params = dma_alloc_coherent(&wd->pdev->dev, |
---|
| 828 | + sizeof(struct wd719x_host_param), |
---|
| 829 | + &wd->params_phys, GFP_KERNEL); |
---|
826 | 830 | if (!wd->params) { |
---|
827 | 831 | dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n"); |
---|
828 | 832 | return -ENOMEM; |
---|
829 | 833 | } |
---|
830 | 834 | |
---|
831 | 835 | /* memory area for the RISC for hash table of outstanding requests */ |
---|
832 | | - wd->hash_virt = pci_alloc_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, |
---|
833 | | - &wd->hash_phys); |
---|
| 836 | + wd->hash_virt = dma_alloc_coherent(&wd->pdev->dev, |
---|
| 837 | + WD719X_HASH_TABLE_SIZE, |
---|
| 838 | + &wd->hash_phys, GFP_KERNEL); |
---|
834 | 839 | if (!wd->hash_virt) { |
---|
835 | 840 | dev_warn(&wd->pdev->dev, "unable to allocate hash buffer\n"); |
---|
836 | 841 | ret = -ENOMEM; |
---|
.. | .. |
---|
862 | 867 | fail_free_irq: |
---|
863 | 868 | free_irq(wd->pdev->irq, wd); |
---|
864 | 869 | fail_free_hash: |
---|
865 | | - pci_free_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, |
---|
| 870 | + dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, |
---|
866 | 871 | wd->hash_phys); |
---|
867 | 872 | fail_free_params: |
---|
868 | | - pci_free_consistent(wd->pdev, sizeof(struct wd719x_host_param), |
---|
| 873 | + dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param), |
---|
869 | 874 | wd->params, wd->params_phys); |
---|
870 | 875 | |
---|
871 | 876 | return ret; |
---|
.. | .. |
---|
874 | 879 | static struct scsi_host_template wd719x_template = { |
---|
875 | 880 | .module = THIS_MODULE, |
---|
876 | 881 | .name = "Western Digital 719x", |
---|
| 882 | + .cmd_size = sizeof(struct wd719x_scb), |
---|
877 | 883 | .queuecommand = wd719x_queuecommand, |
---|
878 | 884 | .eh_abort_handler = wd719x_abort, |
---|
879 | 885 | .eh_device_reset_handler = wd719x_dev_reset, |
---|
.. | .. |
---|
884 | 890 | .can_queue = 255, |
---|
885 | 891 | .this_id = 7, |
---|
886 | 892 | .sg_tablesize = WD719X_SG, |
---|
887 | | - .use_clustering = ENABLE_CLUSTERING, |
---|
888 | 893 | }; |
---|
889 | 894 | |
---|
890 | 895 | static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d) |
---|
.. | .. |
---|
897 | 902 | if (err) |
---|
898 | 903 | goto fail; |
---|
899 | 904 | |
---|
900 | | - if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) { |
---|
| 905 | + if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) { |
---|
901 | 906 | dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n"); |
---|
902 | 907 | goto disable_device; |
---|
903 | 908 | } |
---|