~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to modules/linux/pvscsi/pvscsi.c

  • Committer: Evan Broder
  • Date: 2010-03-21 23:26:53 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: broder@mit.edu-20100321232653-5a57r7v7ch4o6byv
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
#include <linux/kernel.h>
28
28
#include <linux/module.h>
29
 
#include <linux/moduleparam.h>
30
 
#include <linux/types.h>
31
29
#include <linux/interrupt.h>
32
30
#include <linux/workqueue.h>
 
31
#include <linux/pci.h>
 
32
 
33
33
#include <scsi/scsi.h>
34
34
#include <scsi/scsi_host.h>
35
35
#include <scsi/scsi_cmnd.h>
65
65
#define PVSCSI_DEFAULT_NUM_PAGES_MSG_RING       1
66
66
#define PVSCSI_DEFAULT_QUEUE_DEPTH              64
67
67
 
68
 
/* MSI has horrible performance in < 2.6.13 due to needless mask frotzing */
69
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 13)
70
 
#define PVSCSI_DISABLE_MSI      0
71
 
#else
72
 
#define PVSCSI_DISABLE_MSI      1
73
 
#endif
74
 
 
75
68
/* MSI-X has horrible performance in < 2.6.19 due to needless mask frobbing */
76
69
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
77
70
#define PVSCSI_DISABLE_MSIX     0
80
73
#endif
81
74
 
82
75
struct pvscsi_sg_list {
83
 
        PVSCSISGElement sge[PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT];
 
76
        struct PVSCSISGElement sge[PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT];
84
77
};
85
78
 
86
79
struct pvscsi_ctx {
97
90
};
98
91
 
99
92
struct pvscsi_adapter {
100
 
        char                    *mmioBase;
101
 
        unsigned int            irq;
102
 
        u8                      rev;
103
 
        char                    use_msi;
104
 
        char                    use_msix;
105
 
        char                    use_msg;
106
 
 
107
 
        spinlock_t              hw_lock;
108
 
 
109
 
        struct workqueue_struct *workqueue;
110
 
        struct work_struct      work;
111
 
 
112
 
        PVSCSIRingReqDesc       *req_ring;
113
 
        unsigned                req_pages;
114
 
        unsigned                req_depth;
115
 
        dma_addr_t              reqRingPA;
116
 
 
117
 
        PVSCSIRingCmpDesc       *cmp_ring;
118
 
        unsigned                cmp_pages;
119
 
        dma_addr_t              cmpRingPA;
120
 
 
121
 
        PVSCSIRingMsgDesc       *msg_ring;
122
 
        unsigned                msg_pages;
123
 
        dma_addr_t              msgRingPA;
124
 
 
125
 
        PVSCSIRingsState        *rings_state;
126
 
        dma_addr_t              ringStatePA;
127
 
 
128
 
        struct pci_dev          *dev;
129
 
        struct Scsi_Host        *host;
130
 
 
131
 
        struct list_head        cmd_pool;
132
 
        struct pvscsi_ctx       *cmd_map;
 
93
        char                            *mmioBase;
 
94
        unsigned int                    irq;
 
95
        u8                              rev;
 
96
        char                            use_msi;
 
97
        char                            use_msix;
 
98
        char                            use_msg;
 
99
 
 
100
        spinlock_t                      hw_lock;
 
101
 
 
102
        struct workqueue_struct         *workqueue;
 
103
        struct work_struct              work;
 
104
 
 
105
        struct PVSCSIRingReqDesc        *req_ring;
 
106
        unsigned                        req_pages;
 
107
        unsigned                        req_depth;
 
108
        dma_addr_t                      reqRingPA;
 
109
 
 
110
        struct PVSCSIRingCmpDesc        *cmp_ring;
 
111
        unsigned                        cmp_pages;
 
112
        dma_addr_t                      cmpRingPA;
 
113
 
 
114
        struct PVSCSIRingMsgDesc        *msg_ring;
 
115
        unsigned                        msg_pages;
 
116
        dma_addr_t                      msgRingPA;
 
117
 
 
118
        struct PVSCSIRingsState         *rings_state;
 
119
        dma_addr_t                      ringStatePA;
 
120
 
 
121
        struct pci_dev                  *dev;
 
122
        struct Scsi_Host                *host;
 
123
 
 
124
        struct list_head                cmd_pool;
 
125
        struct pvscsi_ctx               *cmd_map;
133
126
};
134
127
 
135
128
#define HOST_ADAPTER(host) ((struct pvscsi_adapter *)(host)->hostdata)
146
139
static int pvscsi_ring_pages     = PVSCSI_DEFAULT_NUM_PAGES_PER_RING;
147
140
static int pvscsi_msg_ring_pages = PVSCSI_DEFAULT_NUM_PAGES_MSG_RING;
148
141
static int pvscsi_cmd_per_lun    = PVSCSI_DEFAULT_QUEUE_DEPTH;
149
 
static int pvscsi_disable_msi    = PVSCSI_DISABLE_MSI;
 
142
static int pvscsi_disable_msi;
150
143
static int pvscsi_disable_msix   = PVSCSI_DISABLE_MSIX;
151
144
static int pvscsi_use_msg        = TRUE;
152
145
 
168
161
                 __stringify(PVSCSI_MAX_REQ_QUEUE_DEPTH) ")");
169
162
 
170
163
module_param_named(disable_msi, pvscsi_disable_msi, bool, PVSCSI_RW);
171
 
MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default="
172
 
                 __stringify(PVSCSI_DISABLE_MSI) ")");
 
164
MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
173
165
 
174
166
module_param_named(disable_msix, pvscsi_disable_msix, bool, PVSCSI_RW);
175
167
MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default="
298
290
static void pvscsi_abort_cmd(const struct pvscsi_adapter *adapter,
299
291
                             const struct pvscsi_ctx *ctx)
300
292
{
301
 
        PVSCSICmdDescAbortCmd cmd = { 0 };
 
293
        struct PVSCSICmdDescAbortCmd cmd = { 0 };
302
294
 
303
295
        cmd.target = ctx->cmd->device->id;
304
296
        cmd.context = pvscsi_map_context(adapter, ctx);
349
341
 
350
342
static void ll_device_reset(const struct pvscsi_adapter *adapter, u32 target)
351
343
{
352
 
        PVSCSICmdDescResetDevice cmd = { 0 };
 
344
        struct PVSCSICmdDescResetDevice cmd = { 0 };
353
345
 
354
346
        LOG(0, "Reseting device: target=%u\n", target);
355
347
 
390
382
 * setup the scatter/gather list if needed.
391
383
 */
392
384
static void pvscsi_map_buffers(struct pvscsi_adapter *adapter,
393
 
                               struct pvscsi_ctx *ctx,
394
 
                               struct scsi_cmnd *cmd, PVSCSIRingReqDesc *e)
 
385
                               struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd,
 
386
                               struct PVSCSIRingReqDesc *e)
395
387
{
396
388
        unsigned count;
397
389
        unsigned bufflen = scsi_bufflen(cmd);
410
402
                        pvscsi_create_sg(ctx, sg, segs);
411
403
 
412
404
                        e->flags |= PVSCSI_FLAG_CMD_WITH_SG_LIST;
 
405
                        ctx->sglPA = pci_map_single(adapter->dev, ctx->sgl,
 
406
                                                    PAGE_SIZE, PCI_DMA_TODEVICE);
413
407
                        e->dataAddr = ctx->sglPA;
414
408
                } else
415
409
                        e->dataAddr = sg_dma_address(sg);
422
416
}
423
417
 
424
418
static void pvscsi_unmap_buffers(const struct pvscsi_adapter *adapter,
425
 
                                 const struct pvscsi_ctx *ctx)
 
419
                                 struct pvscsi_ctx *ctx)
426
420
{
427
421
        struct scsi_cmnd *cmd;
428
422
        unsigned bufflen;
433
427
        if (bufflen != 0) {
434
428
                unsigned count = scsi_sg_count(cmd);
435
429
 
436
 
                if (count != 0)
 
430
                if (count != 0) {
437
431
                        pci_unmap_sg(adapter->dev, scsi_sglist(cmd), count,
438
432
                                     cmd->sc_data_direction);
439
 
                else
 
433
                        if (ctx->sglPA) {
 
434
                                pci_unmap_single(adapter->dev, ctx->sglPA,
 
435
                                                 PAGE_SIZE, PCI_DMA_TODEVICE);
 
436
                                ctx->sglPA = 0;
 
437
                        }
 
438
                } else
440
439
                        pci_unmap_single(adapter->dev, ctx->dataPA, bufflen,
441
440
                                         cmd->sc_data_direction);
442
441
        }
491
490
 
492
491
static void pvscsi_setup_all_rings(const struct pvscsi_adapter *adapter)
493
492
{
494
 
        PVSCSICmdDescSetupRings cmd = { 0 };
 
493
        struct PVSCSICmdDescSetupRings cmd = { 0 };
495
494
        dma_addr_t base;
496
495
        unsigned i;
497
496
 
519
518
                              &cmd, sizeof cmd);
520
519
 
521
520
        if (adapter->use_msg) {
522
 
                PVSCSICmdDescSetupMsgRing cmd_msg = { 0 };
 
521
                struct PVSCSICmdDescSetupMsgRing cmd_msg = { 0 };
523
522
 
524
523
                cmd_msg.numPages = adapter->msg_pages;
525
524
 
540
539
 * to the SCSI mid layer.
541
540
 */
542
541
static void pvscsi_complete_request(struct pvscsi_adapter *adapter,
543
 
                                    const PVSCSIRingCmpDesc *e)
 
542
                                    const struct PVSCSIRingCmpDesc *e)
544
543
{
545
544
        struct pvscsi_ctx *ctx;
546
545
        struct scsi_cmnd *cmd;
632
631
 */
633
632
static void pvscsi_process_completion_ring(struct pvscsi_adapter *adapter)
634
633
{
635
 
        PVSCSIRingsState *s = adapter->rings_state;
636
 
        PVSCSIRingCmpDesc *ring = adapter->cmp_ring;
 
634
        struct PVSCSIRingsState *s = adapter->rings_state;
 
635
        struct PVSCSIRingCmpDesc *ring = adapter->cmp_ring;
637
636
        u32 cmp_entries = s->cmpNumEntriesLog2;
638
637
 
639
638
        while (s->cmpConsIdx != s->cmpProdIdx) {
640
 
                PVSCSIRingCmpDesc *e = ring + (s->cmpConsIdx &
641
 
                                               MASK(cmp_entries));
 
639
                struct PVSCSIRingCmpDesc *e = ring + (s->cmpConsIdx &
 
640
                                                      MASK(cmp_entries));
642
641
                /*
643
642
                 * This barrier() ensures that *e is not dereferenced while
644
643
                 * the device emulation still writes data into the slot.
664
663
static int pvscsi_queue_ring(struct pvscsi_adapter *adapter,
665
664
                             struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd)
666
665
{
667
 
        PVSCSIRingsState *s;
668
 
        PVSCSIRingReqDesc *e;
 
666
        struct PVSCSIRingsState *s;
 
667
        struct PVSCSIRingReqDesc *e;
669
668
        struct scsi_device *sdev;
670
669
        u32 req_entries;
671
670
 
958
957
};
959
958
 
960
959
static void pvscsi_process_msg(const struct pvscsi_adapter *adapter,
961
 
                               const PVSCSIRingMsgDesc *e)
 
960
                               const struct PVSCSIRingMsgDesc *e)
962
961
{
963
 
        PVSCSIRingsState *s = adapter->rings_state;
 
962
        struct PVSCSIRingsState *s = adapter->rings_state;
964
963
        struct Scsi_Host *host = adapter->host;
965
964
        struct scsi_device *sdev;
966
965
 
970
969
        ASSERT_ON_COMPILE(PVSCSI_MSG_LAST == 2);
971
970
 
972
971
        if (e->type == PVSCSI_MSG_DEV_ADDED) {
973
 
                PVSCSIMsgDescDevStatusChanged *desc;
974
 
                desc = (PVSCSIMsgDescDevStatusChanged *)e;
 
972
                struct PVSCSIMsgDescDevStatusChanged *desc;
 
973
                desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
975
974
 
976
975
                printk(KERN_INFO "pvscsi: msg: device added at scsi%u:%u:%u\n",
977
976
                       desc->bus, desc->target, desc->lun[1]);
990
989
 
991
990
                scsi_host_put(host);
992
991
        } else if (e->type == PVSCSI_MSG_DEV_REMOVED) {
993
 
                PVSCSIMsgDescDevStatusChanged *desc;
994
 
                desc = (PVSCSIMsgDescDevStatusChanged *)e;
 
992
                struct PVSCSIMsgDescDevStatusChanged *desc;
 
993
                desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
995
994
 
996
995
                printk(KERN_INFO "pvscsi: msg: device removed at scsi%u:%u:%u\n",
997
996
                       desc->bus, desc->target, desc->lun[1]);
1014
1013
 
1015
1014
static int pvscsi_msg_pending(const struct pvscsi_adapter *adapter)
1016
1015
{
1017
 
        PVSCSIRingsState *s = adapter->rings_state;
 
1016
        struct PVSCSIRingsState *s = adapter->rings_state;
1018
1017
 
1019
1018
        return s->msgProdIdx != s->msgConsIdx;
1020
1019
}
1021
1020
 
1022
1021
static void pvscsi_process_msg_ring(const struct pvscsi_adapter *adapter)
1023
1022
{
1024
 
        PVSCSIRingsState *s = adapter->rings_state;
1025
 
        PVSCSIRingMsgDesc *ring = adapter->msg_ring;
 
1023
        struct PVSCSIRingsState *s = adapter->rings_state;
 
1024
        struct PVSCSIRingMsgDesc *ring = adapter->msg_ring;
1026
1025
        u32 msg_entries = s->msgNumEntriesLog2;
1027
1026
 
1028
1027
        while (pvscsi_msg_pending(adapter)) {
1029
 
                PVSCSIRingMsgDesc *e = ring + (s->msgConsIdx &
1030
 
                                               MASK(msg_entries));
 
1028
                struct PVSCSIRingMsgDesc *e = ring + (s->msgConsIdx &
 
1029
                                                      MASK(msg_entries));
1031
1030
 
1032
1031
                barrier();
1033
1032
                pvscsi_process_msg(adapter, e);
1104
1103
        unsigned i;
1105
1104
 
1106
1105
        for (i = 0; i < adapter->req_depth; ++i, ++ctx)
1107
 
                pci_free_consistent(adapter->dev, PAGE_SIZE, ctx->sgl,
1108
 
                                    ctx->sglPA);
 
1106
                free_page((unsigned long)ctx->sgl);
1109
1107
}
1110
1108
 
1111
 
static int pvscsi_setup_msix(const struct pvscsi_adapter *adapter, int *irq)
 
1109
static int pvscsi_setup_msix(const struct pvscsi_adapter *adapter,
 
1110
                             unsigned int *irq)
1112
1111
{
1113
1112
#ifdef CONFIG_PCI_MSI
1114
1113
        struct msix_entry entry = { 0, PVSCSI_VECTOR_COMPLETION };
1153
1152
                destroy_workqueue(adapter->workqueue);
1154
1153
 
1155
1154
        if (adapter->mmioBase)
1156
 
                iounmap(adapter->mmioBase);
 
1155
                pci_iounmap(adapter->dev, adapter->mmioBase);
1157
1156
 
1158
1157
        pci_release_regions(adapter->dev);
1159
1158
 
1204
1203
        ASSERT_ON_COMPILE(sizeof(struct pvscsi_sg_list) <= PAGE_SIZE);
1205
1204
 
1206
1205
        for (i = 0; i < adapter->req_depth; ++i, ++ctx) {
1207
 
                ctx->sgl = pci_alloc_consistent(adapter->dev, PAGE_SIZE,
1208
 
                                                &ctx->sglPA);
 
1206
                ctx->sgl = (void *)__get_free_page(GFP_KERNEL);
 
1207
                ctx->sglPA = 0;
1209
1208
                BUG_ON(ctx->sglPA & ~PAGE_MASK);
1210
1209
                if (!ctx->sgl) {
1211
1210
                        for (; i >= 0; --i, --ctx) {
1212
 
                                pci_free_consistent(adapter->dev, PAGE_SIZE,
1213
 
                                                    ctx->sgl, ctx->sglPA);
 
1211
                                free_page((unsigned long)ctx->sgl);
1214
1212
                                ctx->sgl = NULL;
1215
 
                                ctx->sglPA = 0;
1216
1213
                        }
1217
1214
                        return -ENOMEM;
1218
1215
                }
1226
1223
{
1227
1224
        struct pvscsi_adapter *adapter;
1228
1225
        struct Scsi_Host *host;
1229
 
        unsigned long base, i;
 
1226
        unsigned int i;
1230
1227
        int error;
1231
1228
 
1232
1229
        error = -ENODEV;
1278
1275
                goto out_free_host;
1279
1276
        }
1280
1277
 
1281
 
        base = 0;
1282
1278
        for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1283
1279
                if ((pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO))
1284
1280
                        continue;
1285
1281
 
1286
 
                if (pci_resource_len(pdev, i) <
1287
 
                                        PVSCSI_MEM_SPACE_NUM_PAGES * PAGE_SIZE)
 
1282
                if (pci_resource_len(pdev, i) < PVSCSI_MEM_SPACE_SIZE)
1288
1283
                        continue;
1289
1284
 
1290
 
                base = pci_resource_start(pdev, i);
1291
1285
                break;
1292
1286
        }
1293
1287
 
1294
 
        if (base == 0) {
 
1288
        if (i == DEVICE_COUNT_RESOURCE) {
1295
1289
                printk(KERN_ERR "pvscsi: adapter has no suitable MMIO region\n");
1296
1290
                goto out_release_resources;
1297
1291
        }
1298
1292
 
1299
 
        adapter->mmioBase = ioremap(base, PVSCSI_MEM_SPACE_SIZE);
 
1293
        adapter->mmioBase = pci_iomap(pdev, i, PVSCSI_MEM_SPACE_SIZE);
1300
1294
        if (!adapter->mmioBase) {
1301
 
                printk(KERN_ERR "pvscsi: can't ioremap 0x%lx\n", base);
 
1295
                printk(KERN_ERR "pvscsi: can't iomap for BAR %d memsize %lu\n",
 
1296
                       i, PVSCSI_MEM_SPACE_SIZE);
1302
1297
                goto out_release_resources;
1303
1298
        }
1304
1299