aboutsummaryrefslogtreecommitdiff
path: root/archive/sg_poll.c
blob: 8d3fe1358031f7b59eb7233ee11d2650a1df8f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "sg_include.h"
#include "sg_err.h"

/* Test code for D. Gilbert's extensions to the Linux OS SCSI generic ("sg")
   device driver.
*  Copyright (C) 1999-2001 D. Gilbert
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2, or (at your option)
*  any later version.

   This program tests out asynchronous parts of the 'sg' device driver.
   It only uses the SCSI read command on the 'sg' device.
   This program performs unbalanced, non-polling "write-write-read"
   sequences. Asynchronous notification is turned on and signals are
   counted. Due to the imbalance, when the close() is executed there
   are several packets still to be read() [some of which may not yet
   be awaiting a read()]. This tests how the device driver cleans up
   after an unexpected close().
   If the "-deb" flag is given then outputs state to console/log
   (for all active sg devices).

   Version 0.76 20010112
*/


/*
6 byte commands [READ: 0x08, WRITE: 0x0a]:
[cmd ][had|lu][midAdd][lowAdd][count ][flags ]
10 byte commands [EREAD: 0x28, EWRITE: 0x2a, READ_CAPACITY 0x25]:
[cmd ][   |lu][hiAddr][hmAddr][lmAddr][lowAdd][      ][hiCnt ][lowCnt][flags ]
12 byte commands [LREAD: 0xd8, LWRITE: 0xda]:
[cmd ][   |lu][hiAddr][hmAddr][lmAddr][lowAdd][hiCnt ][hmCnt ][lmCnt ][lowCnt]
 ... [      ][flags ]
*/
        
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
    /* union semun is defined by including <sys/sem.h> */
#else
    /* according to X/OPEN we have to define it ourselves */
union semun {
    int val;                    /* value for SETVAL */
    struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
    unsigned short int *array;  /* array for GETALL, SETALL */
    struct seminfo *__buf;      /* buffer for IPC_INFO */
};
#endif

#ifdef O_ASYNC
#define MY_ASYNC O_ASYNC
#else
#define MY_ASYNC FASYNC
#endif


// #define SG_DEBUG

#define OFF sizeof(struct sg_header)
// #define NUM_SECTORS 7777
// #define NUM_SECTORS 577
// #define NUM_SECTORS 97
#define NUM_SECTORS 150
#define BLOCK_SIZE 2048

volatile int hand_count = 0;
volatile int signo = 0;
volatile int poll_res = 0;
volatile short revents = 0;
volatile int sg_fd = 0;
int semset_id = 0;

int do_poll()
{
    struct pollfd a_pollfd = {0, POLLIN | POLLOUT, 0};

    a_pollfd.fd = sg_fd;
    if ((poll_res = poll(&a_pollfd, 1, 0)) < 0) {
        perror("poll error");
        return 0;
    }
    revents = a_pollfd.revents;
    return (a_pollfd.revents & POLLIN) ? 1 : 0;
}

void sg_sa_handler(int sig, siginfo_t *si, void * data)
{
    signo = sig;
    if (SIGRTMIN != sig)
    	fprintf(stderr, "Unexpected signal, signum=%d\n", sig);
    if (sg_fd != si->si_fd)
    	fprintf(stderr, "Unexpected fd, fd=%d\n", si->si_fd);
    ++hand_count;
    if (do_poll()) {
        struct sembuf a_sembuf;

        a_sembuf.sem_num = 0;
        a_sembuf.sem_op = 1;
        a_sembuf.sem_flg = 0;
        if (semop(semset_id, &a_sembuf, 1) < 0)
            perror("semop(sh) error");
    }
}

int main(int argc, char * argv[])
{
    int flags;
    int res;
    int k;
    unsigned char rdCmdBlk [10] = {0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    unsigned char * rdBuff = malloc(OFF + sizeof(rdCmdBlk) + 
                                    (BLOCK_SIZE * NUM_SECTORS));
    unsigned char * rdBuff2 = malloc(OFF + sizeof(rdCmdBlk) + 
                                    (BLOCK_SIZE * NUM_SECTORS));
    int rdInLen = OFF + sizeof(rdCmdBlk);
    int rdOutLen;
    unsigned char * rdCmd = rdBuff + OFF;
    unsigned char * rdCmd2 = rdBuff + OFF;
    struct sg_header * rsghp = (struct sg_header *)rdBuff;
    struct sg_header * rsghp2 = (struct sg_header *)rdBuff2;
    int sectorNo = 10000;
    int sectorNo2;
    int numSectors = NUM_SECTORS;
    const int times = 3;
    struct sigaction s_action;
    union semun a_semun;
    struct sembuf a_sembuf;
    struct sg_scsi_id sg_id;
    char ebuff[256];
    int deb = 0;
    char * file_name = 0;

    for (k = 1; k < argc; ++k) {
        if (0 == memcmp("-deb", argv[k], 4))
            deb = 10;
        else if (*argv[k] != '-')
            file_name = argv[k];
    }
    if (0 == file_name) {
printf("Usage: 'sg_poll [-deb] <generic_device>'  eg: sg_poll /dev/sg0\n");
        return 1;
    }

    semset_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
    if (-1 == semset_id) {
        perror("semget error");
        return 1;
    }
    a_semun.val = 0;
    res = semctl(semset_id, 0, SETVAL, a_semun);
    if (-1 == res) {
        perror("semctl(val) error");
        return 1;
    }


    sg_fd = open(file_name, O_RDWR | O_NONBLOCK);
    if (sg_fd < 0) {
        sprintf(ebuff, "sg_poll: open error on %s", file_name);
        perror(ebuff);
        return 1;
    }
    res = ioctl(sg_fd, SG_GET_SCSI_ID, &sg_id);
    if (res < 0) {
        /* perror("ioctl on generic device, error"); */
        printf("sg_poll: %s not a scsi generic device\n", file_name);
        return 1;
    }
    printf("scsi%d, channel=%d, device=%d, lun=%d,  scsi_type=%d\n", 
           sg_id.host_no, sg_id.channel, sg_id.scsi_id, sg_id.lun,
           sg_id.scsi_type);

#ifdef SG_DEBUG
    ioctl(sg_fd, SG_SET_DEBUG, &deb);
#endif
    res = ioctl(sg_fd, SG_GET_COMMAND_Q, &k);
    if (res < 0) {
        perror("SG_GET_COMMAND_Q ioctl error");
        return 1;
    }
    if (0 == k) {
        k = 1;
        res = ioctl(sg_fd, SG_SET_COMMAND_Q, &k);
        if (res < 0) {
            perror("SG_SET_COMMAND_Q ioctl error");
            return 1;
        }
    }

    s_action.sa_flags = SA_SIGINFO;
    s_action.sa_sigaction = sg_sa_handler;
    sigemptyset(&s_action.sa_mask);
    res = sigaction(SIGRTMIN, &s_action, NULL);
    if (res == -1) {
        perror("sg_poll: sigaction error");
        return 1;
    }
    res = fcntl(sg_fd, F_SETOWN, getpid());
    if (res == -1) {
        perror("sg_poll: fcntl(setown) error");
        return 1;
    }
    flags = fcntl(sg_fd, F_GETFL);
    res = fcntl(sg_fd, F_SETFL, flags | MY_ASYNC);
    if (res == -1) {
        perror("sg_poll: fcntl(setfl) error");
        return 1;
    }
    fcntl(sg_fd, F_SETSIG, SIGRTMIN);
    
    do_poll();
    printf("pre-loop check, poll_res=%d, revents=%d\n", poll_res, (int)revents); 
    

    for (k = 0; k < times; ++k, sectorNo += numSectors) {
    
    rdOutLen = OFF + (BLOCK_SIZE * numSectors);
    rsghp->pack_len = 999;                /* don't care */
    rsghp->pack_id = k;
    rsghp->reply_len = rdOutLen;
    rsghp->twelve_byte = 0;
    rsghp->result = 0;
    memcpy(rdBuff + OFF, rdCmdBlk, sizeof(rdCmdBlk));
    rdCmd[3] = (unsigned char)((sectorNo >> 16) & 0xFF);
    rdCmd[4] = (unsigned char)((sectorNo >> 8) & 0xFF);
    rdCmd[5] = (unsigned char)(sectorNo & 0xFF);
    rdCmd[7] = (unsigned char)((numSectors >> 8) & 0xff);
    rdCmd[8] = (unsigned char)(numSectors & 0xff);

    res = write(sg_fd, rdBuff, rdInLen);
    if (res < 0) {
        perror("sg_poll: write (rd) error");
        return 1;
    }
    if (res < rdInLen) {
        printf("sg_poll: wrote less (rd), ask=%d, got=%d", rdInLen, res);
        return 1;
    }
    
    rsghp2->pack_len = 888;                /* don't care */
    rsghp2->pack_id = k + 100;
    rsghp2->reply_len = rdOutLen;
    rsghp2->twelve_byte = 0;
    rsghp2->result = 0;
    memcpy(rdBuff2 + OFF, rdCmdBlk, sizeof(rdCmdBlk));
    sectorNo2 = sectorNo + 6666;
    rdCmd2[3] = (unsigned char)((sectorNo2 >> 16) & 0xFF);
    rdCmd2[4] = (unsigned char)((sectorNo2 >> 8) & 0xFF);
    rdCmd2[5] = (unsigned char)(sectorNo2 & 0xFF);
    rdCmd2[7] = (unsigned char)((numSectors >> 8) & 0xff);
    rdCmd2[8] = (unsigned char)(numSectors & 0xff);

#if 1
    res = write(sg_fd, rdBuff2, rdInLen);
    if (res < 0) {
        perror("sg_poll: write2 (rd) error");
        return 1;
    }
    if (res < rdInLen) {
        printf("sg_poll: wrote less (rd), ask=%d, got=%d", rdInLen, res);
        return 1;
    }
#endif

    do_poll();
    printf("pre-write pause, k=%d, " 
           "hand_count=%d, signo=%d, poll_res=%d, revents=%d\n",
           k, hand_count, signo, poll_res, (int)revents); 
#ifdef SG_DEBUG
    ioctl(sg_fd, SG_SET_DEBUG, &deb);
#endif
    system("cat /proc/scsi/sg/debug");

    a_sembuf.sem_num = 0;
    a_sembuf.sem_op = -1;
    a_sembuf.sem_flg = 0;
    while (semop(semset_id, &a_sembuf, 1) < 0) {
        if (EINTR != errno) {
            perror("semop(main) error");
            return 0;
        }
    }
    /* pause(); */

    printf("post-write pause, k=%d, " 
           "hand_count=%d, signo=%d, poll_res=%d, revents=%d\n",
           k, hand_count, signo, poll_res, (int)revents); 
#ifdef SG_DEBUG
    ioctl(sg_fd, SG_SET_DEBUG, &deb);
#endif

    res = read(sg_fd, rdBuff, rdOutLen);
    if (res < 0) {
        perror("sg_poll: read (rd) error");
        return 1;
    }
    if (res < rdOutLen) {
        printf("sg_poll: read less (rd), ask=%d, got=%d", rdOutLen, res);
        return 1;
    }
    sg_chk_n_print("after read(rd)", rsghp->target_status, 
                   rsghp->host_status, rsghp->driver_status, 
                   rsghp->sense_buffer, SG_MAX_SENSE);

    }
    printf("\treq_len=%d, dma_count=%d\n", rsghp->reply_len, rsghp->pack_len);

#ifdef SG_DEBUG
    ioctl(sg_fd, SG_SET_DEBUG, &deb);
#endif
    res = close(sg_fd);
    if (res < 0) {
        perror("sg_poll: close error");
        return 1;
    }
    
    if (deb > 0) {
        sg_fd = open(file_name, O_RDONLY);
        if (sg_fd < 0) {
            sprintf(ebuff, "sg_poll: open (2) error on %s", file_name);
            perror(ebuff);
            return 1;
        }
        res = ioctl(sg_fd, SG_SET_DEBUG, &deb);
        if (res < 0) {
            perror("ioctl (2) error");
            return 1;
        }
        res = close(sg_fd);
        if (res < 0) {
            perror("sg_poll: close (2) error");
            return 1;
        }
    }

    return 0;
}