hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/scsi/esp_scsi.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* esp_scsi.c: ESP SCSI driver.
23 *
34 * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
....@@ -242,8 +243,6 @@
242243 /* Reset the ESP chip, _not_ the SCSI bus. */
243244 static void esp_reset_esp(struct esp *esp)
244245 {
245
- u8 family_code, version;
246
-
247246 /* Now reset the ESP chip */
248247 scsi_esp_cmd(esp, ESP_CMD_RC);
249248 scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
....@@ -256,14 +255,19 @@
256255 */
257256 esp->max_period = ((35 * esp->ccycle) / 1000);
258257 if (esp->rev == FAST) {
259
- version = esp_read8(ESP_UID);
260
- family_code = (version & 0xf8) >> 3;
261
- if (family_code == 0x02)
258
+ u8 family_code = ESP_FAMILY(esp_read8(ESP_UID));
259
+
260
+ if (family_code == ESP_UID_F236) {
262261 esp->rev = FAS236;
263
- else if (family_code == 0x0a)
262
+ } else if (family_code == ESP_UID_HME) {
264263 esp->rev = FASHME; /* Version is usually '5'. */
265
- else
264
+ } else if (family_code == ESP_UID_FSC) {
265
+ esp->rev = FSC;
266
+ /* Enable Active Negation */
267
+ esp_write8(ESP_CONFIG4_RADE, ESP_CFG4);
268
+ } else {
266269 esp->rev = FAS100A;
270
+ }
267271 esp->min_period = ((4 * esp->ccycle) / 1000);
268272 } else {
269273 esp->min_period = ((5 * esp->ccycle) / 1000);
....@@ -303,11 +307,11 @@
303307
304308 case FASHME:
305309 esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
306
- /* fallthrough... */
310
+ fallthrough;
307311
308312 case FAS236:
309313 case PCSCSI:
310
- /* Fast 236, AM53c974 or HME */
314
+ case FSC:
311315 esp_write8(esp->config2, ESP_CFG2);
312316 if (esp->rev == FASHME) {
313317 u8 cfg3 = esp->target[0].esp_config3;
....@@ -369,19 +373,31 @@
369373 {
370374 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
371375 struct scatterlist *sg = scsi_sglist(cmd);
372
- int dir = cmd->sc_data_direction;
373
- int total, i;
376
+ int total = 0, i;
377
+ struct scatterlist *s;
374378
375
- if (dir == DMA_NONE)
379
+ if (cmd->sc_data_direction == DMA_NONE)
376380 return;
377381
378
- spriv->u.num_sg = esp->ops->map_sg(esp, sg, scsi_sg_count(cmd), dir);
379
- spriv->cur_residue = sg_dma_len(sg);
380
- spriv->cur_sg = sg;
382
+ if (esp->flags & ESP_FLAG_NO_DMA_MAP) {
383
+ /*
384
+ * For pseudo DMA and PIO we need the virtual address instead of
385
+ * a dma address, so perform an identity mapping.
386
+ */
387
+ spriv->num_sg = scsi_sg_count(cmd);
381388
382
- total = 0;
383
- for (i = 0; i < spriv->u.num_sg; i++)
384
- total += sg_dma_len(&sg[i]);
389
+ scsi_for_each_sg(cmd, s, spriv->num_sg, i) {
390
+ s->dma_address = (uintptr_t)sg_virt(s);
391
+ total += sg_dma_len(s);
392
+ }
393
+ } else {
394
+ spriv->num_sg = scsi_dma_map(cmd);
395
+ scsi_for_each_sg(cmd, s, spriv->num_sg, i)
396
+ total += sg_dma_len(s);
397
+ }
398
+ spriv->cur_residue = sg_dma_len(sg);
399
+ spriv->prv_sg = NULL;
400
+ spriv->cur_sg = sg;
385401 spriv->tot_residue = total;
386402 }
387403
....@@ -434,20 +450,16 @@
434450 p->tot_residue = 0;
435451 }
436452 if (!p->cur_residue && p->tot_residue) {
437
- p->cur_sg++;
453
+ p->prv_sg = p->cur_sg;
454
+ p->cur_sg = sg_next(p->cur_sg);
438455 p->cur_residue = sg_dma_len(p->cur_sg);
439456 }
440457 }
441458
442459 static void esp_unmap_dma(struct esp *esp, struct scsi_cmnd *cmd)
443460 {
444
- struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
445
- int dir = cmd->sc_data_direction;
446
-
447
- if (dir == DMA_NONE)
448
- return;
449
-
450
- esp->ops->unmap_sg(esp, scsi_sglist(cmd), spriv->u.num_sg, dir);
461
+ if (!(esp->flags & ESP_FLAG_NO_DMA_MAP))
462
+ scsi_dma_unmap(cmd);
451463 }
452464
453465 static void esp_save_pointers(struct esp *esp, struct esp_cmd_entry *ent)
....@@ -460,6 +472,7 @@
460472 return;
461473 }
462474 ent->saved_cur_residue = spriv->cur_residue;
475
+ ent->saved_prv_sg = spriv->prv_sg;
463476 ent->saved_cur_sg = spriv->cur_sg;
464477 ent->saved_tot_residue = spriv->tot_residue;
465478 }
....@@ -474,19 +487,9 @@
474487 return;
475488 }
476489 spriv->cur_residue = ent->saved_cur_residue;
490
+ spriv->prv_sg = ent->saved_prv_sg;
477491 spriv->cur_sg = ent->saved_cur_sg;
478492 spriv->tot_residue = ent->saved_tot_residue;
479
-}
480
-
481
-static void esp_check_command_len(struct esp *esp, struct scsi_cmnd *cmd)
482
-{
483
- if (cmd->cmd_len == 6 ||
484
- cmd->cmd_len == 10 ||
485
- cmd->cmd_len == 12) {
486
- esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
487
- } else {
488
- esp->flags |= ESP_FLAG_DOING_SLOWCMD;
489
- }
490493 }
491494
492495 static void esp_write_tgt_config3(struct esp *esp, int tgt)
....@@ -624,6 +627,26 @@
624627 }
625628 }
626629
630
+static void esp_map_sense(struct esp *esp, struct esp_cmd_entry *ent)
631
+{
632
+ ent->sense_ptr = ent->cmd->sense_buffer;
633
+ if (esp->flags & ESP_FLAG_NO_DMA_MAP) {
634
+ ent->sense_dma = (uintptr_t)ent->sense_ptr;
635
+ return;
636
+ }
637
+
638
+ ent->sense_dma = dma_map_single(esp->dev, ent->sense_ptr,
639
+ SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
640
+}
641
+
642
+static void esp_unmap_sense(struct esp *esp, struct esp_cmd_entry *ent)
643
+{
644
+ if (!(esp->flags & ESP_FLAG_NO_DMA_MAP))
645
+ dma_unmap_single(esp->dev, ent->sense_dma,
646
+ SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
647
+ ent->sense_ptr = NULL;
648
+}
649
+
627650 /* When a contingent allegiance conditon is created, we force feed a
628651 * REQUEST_SENSE command to the device to fetch the sense data. I
629652 * tried many other schemes, relying on the scsi error handling layer
....@@ -645,12 +668,7 @@
645668 if (!ent->sense_ptr) {
646669 esp_log_autosense("Doing auto-sense for tgt[%d] lun[%d]\n",
647670 tgt, lun);
648
-
649
- ent->sense_ptr = cmd->sense_buffer;
650
- ent->sense_dma = esp->ops->map_single(esp,
651
- ent->sense_ptr,
652
- SCSI_SENSE_BUFFERSIZE,
653
- DMA_FROM_DEVICE);
671
+ esp_map_sense(esp, ent);
654672 }
655673 ent->saved_sense_ptr = ent->sense_ptr;
656674
....@@ -717,10 +735,10 @@
717735 static void esp_maybe_execute_command(struct esp *esp)
718736 {
719737 struct esp_target_data *tp;
720
- struct esp_lun_data *lp;
721738 struct scsi_device *dev;
722739 struct scsi_cmnd *cmd;
723740 struct esp_cmd_entry *ent;
741
+ bool select_and_stop = false;
724742 int tgt, lun, i;
725743 u32 val, start_cmd;
726744 u8 *p;
....@@ -743,7 +761,6 @@
743761 tgt = dev->id;
744762 lun = dev->lun;
745763 tp = &esp->target[tgt];
746
- lp = dev->hostdata;
747764
748765 list_move(&ent->list, &esp->active_cmds);
749766
....@@ -752,7 +769,8 @@
752769 esp_map_dma(esp, cmd);
753770 esp_save_pointers(esp, ent);
754771
755
- esp_check_command_len(esp, cmd);
772
+ if (!(cmd->cmd_len == 6 || cmd->cmd_len == 10 || cmd->cmd_len == 12))
773
+ select_and_stop = true;
756774
757775 p = esp->command_block;
758776
....@@ -793,42 +811,22 @@
793811 tp->flags &= ~ESP_TGT_CHECK_NEGO;
794812 }
795813
796
- /* Process it like a slow command. */
797
- if (tp->flags & (ESP_TGT_NEGO_WIDE | ESP_TGT_NEGO_SYNC))
798
- esp->flags |= ESP_FLAG_DOING_SLOWCMD;
814
+ /* If there are multiple message bytes, use Select and Stop */
815
+ if (esp->msg_out_len)
816
+ select_and_stop = true;
799817 }
800818
801819 build_identify:
802
- /* If we don't have a lun-data struct yet, we're probing
803
- * so do not disconnect. Also, do not disconnect unless
804
- * we have a tag on this command.
805
- */
806
- if (lp && (tp->flags & ESP_TGT_DISCONNECT) && ent->tag[0])
807
- *p++ = IDENTIFY(1, lun);
808
- else
809
- *p++ = IDENTIFY(0, lun);
820
+ *p++ = IDENTIFY(tp->flags & ESP_TGT_DISCONNECT, lun);
810821
811822 if (ent->tag[0] && esp->rev == ESP100) {
812823 /* ESP100 lacks select w/atn3 command, use select
813824 * and stop instead.
814825 */
815
- esp->flags |= ESP_FLAG_DOING_SLOWCMD;
826
+ select_and_stop = true;
816827 }
817828
818
- if (!(esp->flags & ESP_FLAG_DOING_SLOWCMD)) {
819
- start_cmd = ESP_CMD_SELA;
820
- if (ent->tag[0]) {
821
- *p++ = ent->tag[0];
822
- *p++ = ent->tag[1];
823
-
824
- start_cmd = ESP_CMD_SA3;
825
- }
826
-
827
- for (i = 0; i < cmd->cmd_len; i++)
828
- *p++ = cmd->cmnd[i];
829
-
830
- esp->select_state = ESP_SELECT_BASIC;
831
- } else {
829
+ if (select_and_stop) {
832830 esp->cmd_bytes_left = cmd->cmd_len;
833831 esp->cmd_bytes_ptr = &cmd->cmnd[0];
834832
....@@ -843,6 +841,19 @@
843841
844842 start_cmd = ESP_CMD_SELAS;
845843 esp->select_state = ESP_SELECT_MSGOUT;
844
+ } else {
845
+ start_cmd = ESP_CMD_SELA;
846
+ if (ent->tag[0]) {
847
+ *p++ = ent->tag[0];
848
+ *p++ = ent->tag[1];
849
+
850
+ start_cmd = ESP_CMD_SA3;
851
+ }
852
+
853
+ for (i = 0; i < cmd->cmd_len; i++)
854
+ *p++ = cmd->cmnd[i];
855
+
856
+ esp->select_state = ESP_SELECT_BASIC;
846857 }
847858 val = tgt;
848859 if (esp->rev == FASHME)
....@@ -902,9 +913,7 @@
902913 }
903914
904915 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
905
- esp->ops->unmap_single(esp, ent->sense_dma,
906
- SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
907
- ent->sense_ptr = NULL;
916
+ esp_unmap_sense(esp, ent);
908917
909918 /* Restore the message/status bytes to what we actually
910919 * saw originally. Also, report that we are providing
....@@ -965,7 +974,7 @@
965974 cmd->scsi_done = done;
966975
967976 spriv = ESP_CMD_PRIV(cmd);
968
- spriv->u.dma_addr = ~(dma_addr_t)0x0;
977
+ spriv->num_sg = 0;
969978
970979 list_add_tail(&ent->list, &esp->queued_cmds);
971980
....@@ -1032,7 +1041,7 @@
10321041
10331042 static void esp_schedule_reset(struct esp *esp)
10341043 {
1035
- esp_log_reset("esp_schedule_reset() from %pf\n",
1044
+ esp_log_reset("esp_schedule_reset() from %ps\n",
10361045 __builtin_return_address(0));
10371046 esp->flags |= ESP_FLAG_RESETTING;
10381047 esp_event(esp, ESP_EVENT_RESET);
....@@ -1252,14 +1261,10 @@
12521261 esp_unmap_dma(esp, cmd);
12531262 esp_free_lun_tag(ent, cmd->device->hostdata);
12541263 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_NEGO_WIDE);
1255
- esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
12561264 esp->cmd_bytes_ptr = NULL;
12571265 esp->cmd_bytes_left = 0;
12581266 } else {
1259
- esp->ops->unmap_single(esp, ent->sense_dma,
1260
- SCSI_SENSE_BUFFERSIZE,
1261
- DMA_FROM_DEVICE);
1262
- ent->sense_ptr = NULL;
1267
+ esp_unmap_sense(esp, ent);
12631268 }
12641269
12651270 /* Now that the state is unwound properly, put back onto
....@@ -1303,9 +1308,8 @@
13031308 esp_flush_fifo(esp);
13041309 }
13051310
1306
- /* If we are doing a slow command, negotiation, etc.
1307
- * we'll do the right thing as we transition to the
1308
- * next phase.
1311
+ /* If we are doing a Select And Stop command, negotiation, etc.
1312
+ * we'll do the right thing as we transition to the next phase.
13091313 */
13101314 esp_event(esp, ESP_EVENT_CHECK_PHASE);
13111315 return 0;
....@@ -1359,7 +1363,7 @@
13591363 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
13601364 u8 *ptr;
13611365
1362
- ptr = scsi_kmap_atomic_sg(p->cur_sg, p->u.num_sg,
1366
+ ptr = scsi_kmap_atomic_sg(p->cur_sg, p->num_sg,
13631367 &offset, &count);
13641368 if (likely(ptr)) {
13651369 *(ptr + offset) = bval;
....@@ -1652,7 +1656,7 @@
16521656 spriv = ESP_CMD_PRIV(ent->cmd);
16531657
16541658 if (spriv->cur_residue == sg_dma_len(spriv->cur_sg)) {
1655
- spriv->cur_sg--;
1659
+ spriv->cur_sg = spriv->prv_sg;
16561660 spriv->cur_residue = 1;
16571661 } else
16581662 spriv->cur_residue++;
....@@ -1737,7 +1741,7 @@
17371741
17381742 case ESP_EVENT_DATA_IN:
17391743 write = 1;
1740
- /* fallthru */
1744
+ fallthrough;
17411745
17421746 case ESP_EVENT_DATA_OUT: {
17431747 struct esp_cmd_entry *ent = esp->active_cmd;
....@@ -2040,11 +2044,8 @@
20402044 esp_free_lun_tag(ent, cmd->device->hostdata);
20412045 cmd->result = DID_RESET << 16;
20422046
2043
- if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
2044
- esp->ops->unmap_single(esp, ent->sense_dma,
2045
- SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
2046
- ent->sense_ptr = NULL;
2047
- }
2047
+ if (ent->flags & ESP_CMD_FLAG_AUTOSENSE)
2048
+ esp_unmap_sense(esp, ent);
20482049
20492050 cmd->scsi_done(cmd);
20502051 list_del(&ent->list);
....@@ -2375,15 +2376,16 @@
23752376 "ESP100A",
23762377 "ESP236",
23772378 "FAS236",
2379
+ "AM53C974",
2380
+ "53CF9x-2",
23782381 "FAS100A",
23792382 "FAST",
23802383 "FASHME",
2381
- "AM53C974",
23822384 };
23832385
23842386 static struct scsi_transport_template *esp_transport_template;
23852387
2386
-int scsi_esp_register(struct esp *esp, struct device *dev)
2388
+int scsi_esp_register(struct esp *esp)
23872389 {
23882390 static int instance;
23892391 int err;
....@@ -2403,10 +2405,10 @@
24032405
24042406 esp_bootup_reset(esp);
24052407
2406
- dev_printk(KERN_INFO, dev, "esp%u: regs[%1p:%1p] irq[%u]\n",
2408
+ dev_printk(KERN_INFO, esp->dev, "esp%u: regs[%1p:%1p] irq[%u]\n",
24072409 esp->host->unique_id, esp->regs, esp->dma_regs,
24082410 esp->host->irq);
2409
- dev_printk(KERN_INFO, dev,
2411
+ dev_printk(KERN_INFO, esp->dev,
24102412 "esp%u: is a %s, %u MHz (ccf=%u), SCSI ID %u\n",
24112413 esp->host->unique_id, esp_chip_names[esp->rev],
24122414 esp->cfreq / 1000000, esp->cfact, esp->scsi_id);
....@@ -2414,7 +2416,7 @@
24142416 /* Let the SCSI bus reset settle. */
24152417 ssleep(esp_bus_reset_settle);
24162418
2417
- err = scsi_add_host(esp->host, dev);
2419
+ err = scsi_add_host(esp->host, esp->dev);
24182420 if (err)
24192421 return err;
24202422
....@@ -2685,7 +2687,6 @@
26852687 .can_queue = 7,
26862688 .this_id = 7,
26872689 .sg_tablesize = SG_ALL,
2688
- .use_clustering = ENABLE_CLUSTERING,
26892690 .max_sectors = 0xffff,
26902691 .skip_settle_delay = 1,
26912692 };
....@@ -2791,3 +2792,131 @@
27912792
27922793 module_init(esp_init);
27932794 module_exit(esp_exit);
2795
+
2796
+#ifdef CONFIG_SCSI_ESP_PIO
2797
+static inline unsigned int esp_wait_for_fifo(struct esp *esp)
2798
+{
2799
+ int i = 500000;
2800
+
2801
+ do {
2802
+ unsigned int fbytes = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
2803
+
2804
+ if (fbytes)
2805
+ return fbytes;
2806
+
2807
+ udelay(1);
2808
+ } while (--i);
2809
+
2810
+ shost_printk(KERN_ERR, esp->host, "FIFO is empty. sreg [%02x]\n",
2811
+ esp_read8(ESP_STATUS));
2812
+ return 0;
2813
+}
2814
+
2815
+static inline int esp_wait_for_intr(struct esp *esp)
2816
+{
2817
+ int i = 500000;
2818
+
2819
+ do {
2820
+ esp->sreg = esp_read8(ESP_STATUS);
2821
+ if (esp->sreg & ESP_STAT_INTR)
2822
+ return 0;
2823
+
2824
+ udelay(1);
2825
+ } while (--i);
2826
+
2827
+ shost_printk(KERN_ERR, esp->host, "IRQ timeout. sreg [%02x]\n",
2828
+ esp->sreg);
2829
+ return 1;
2830
+}
2831
+
2832
+#define ESP_FIFO_SIZE 16
2833
+
2834
+void esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
2835
+ u32 dma_count, int write, u8 cmd)
2836
+{
2837
+ u8 phase = esp->sreg & ESP_STAT_PMASK;
2838
+
2839
+ cmd &= ~ESP_CMD_DMA;
2840
+ esp->send_cmd_error = 0;
2841
+
2842
+ if (write) {
2843
+ u8 *dst = (u8 *)addr;
2844
+ u8 mask = ~(phase == ESP_MIP ? ESP_INTR_FDONE : ESP_INTR_BSERV);
2845
+
2846
+ scsi_esp_cmd(esp, cmd);
2847
+
2848
+ while (1) {
2849
+ if (!esp_wait_for_fifo(esp))
2850
+ break;
2851
+
2852
+ *dst++ = readb(esp->fifo_reg);
2853
+ --esp_count;
2854
+
2855
+ if (!esp_count)
2856
+ break;
2857
+
2858
+ if (esp_wait_for_intr(esp)) {
2859
+ esp->send_cmd_error = 1;
2860
+ break;
2861
+ }
2862
+
2863
+ if ((esp->sreg & ESP_STAT_PMASK) != phase)
2864
+ break;
2865
+
2866
+ esp->ireg = esp_read8(ESP_INTRPT);
2867
+ if (esp->ireg & mask) {
2868
+ esp->send_cmd_error = 1;
2869
+ break;
2870
+ }
2871
+
2872
+ if (phase == ESP_MIP)
2873
+ esp_write8(ESP_CMD_MOK, ESP_CMD);
2874
+
2875
+ esp_write8(ESP_CMD_TI, ESP_CMD);
2876
+ }
2877
+ } else {
2878
+ unsigned int n = ESP_FIFO_SIZE;
2879
+ u8 *src = (u8 *)addr;
2880
+
2881
+ scsi_esp_cmd(esp, ESP_CMD_FLUSH);
2882
+
2883
+ if (n > esp_count)
2884
+ n = esp_count;
2885
+ writesb(esp->fifo_reg, src, n);
2886
+ src += n;
2887
+ esp_count -= n;
2888
+
2889
+ scsi_esp_cmd(esp, cmd);
2890
+
2891
+ while (esp_count) {
2892
+ if (esp_wait_for_intr(esp)) {
2893
+ esp->send_cmd_error = 1;
2894
+ break;
2895
+ }
2896
+
2897
+ if ((esp->sreg & ESP_STAT_PMASK) != phase)
2898
+ break;
2899
+
2900
+ esp->ireg = esp_read8(ESP_INTRPT);
2901
+ if (esp->ireg & ~ESP_INTR_BSERV) {
2902
+ esp->send_cmd_error = 1;
2903
+ break;
2904
+ }
2905
+
2906
+ n = ESP_FIFO_SIZE -
2907
+ (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES);
2908
+
2909
+ if (n > esp_count)
2910
+ n = esp_count;
2911
+ writesb(esp->fifo_reg, src, n);
2912
+ src += n;
2913
+ esp_count -= n;
2914
+
2915
+ esp_write8(ESP_CMD_TI, ESP_CMD);
2916
+ }
2917
+ }
2918
+
2919
+ esp->send_cmd_residual = esp_count;
2920
+}
2921
+EXPORT_SYMBOL(esp_send_pio_cmd);
2922
+#endif