11 #include <sys/ioctl.h>
14 #include "mpath_pr_ioctl.h"
15 #include "mpath_persist.h"
19 #define FILE_NAME_SIZE 256
24 int prin_do_scsi_ioctl(char * dev, int rq_servact, struct prin_resp *resp, int noisy);
25 void mpath_format_readkeys(struct prin_resp *pr_buff, int len , int noisy);
26 void mpath_format_readfullstatus(struct prin_resp *pr_buff, int len, int noisy);
27 int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr,
28 SenseData_t *Sensedata, int noisy);
29 void dumpHex(const char* str, int len, int no_ascii);
30 int prout_do_scsi_ioctl( char * dev, int rq_servact, int rq_scope,
31 unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy);
32 uint32_t format_transportids(struct prout_param_descriptor *paramp);
33 void mpath_reverse_uint32_byteorder(uint32_t *num);
34 void mpath_reverse_uint16_byteorder(uint16_t *num);
35 void decode_transport_id(struct prin_fulldescr *fdesc, unsigned char * p, int length);
36 int get_prin_length(int rq_servact);
37 int mpath_isLittleEndian(void);
39 extern unsigned int mpath_mx_alloc_len;
41 int prout_do_scsi_ioctl(char * dev, int rq_servact, int rq_scope,
42 unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy)
45 int status, paramlen = 24, ret = 0;
48 SenseData_t Sensedata;
49 struct sg_io_hdr io_hdr;
50 char devname[FILE_NAME_SIZE];
53 snprintf(devname, FILE_NAME_SIZE, "/dev/%s",dev);
54 fd = open(devname, O_WRONLY);
56 condlog (1, "%s: unable to open device.", dev);
57 return MPATH_PR_FILE_ERROR;
60 unsigned char cdb[MPATH_PROUT_CMDLEN] =
61 {MPATH_PROUT_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
64 if (paramp->sa_flags & MPATH_F_SPEC_I_PT_MASK)
66 translen = format_transportids(paramp);
67 paramlen = 24 + translen;
73 cdb[1] = (unsigned char)(rq_servact & 0x1f);
74 cdb[2] = (((rq_scope & 0xf) << 4) | (rq_type & 0xf));
75 cdb[7] = (unsigned char)((paramlen >> 8) & 0xff);
76 cdb[8] = (unsigned char)(paramlen & 0xff);
79 condlog(3, "%s: rq_servact = %d", dev, rq_servact);
80 condlog(3, "%s: rq_scope = %d ", dev, rq_scope);
81 condlog(3, "%s: rq_type = %d ", dev, rq_type);
82 condlog(3, "%s: paramlen = %d", dev, paramlen);
86 condlog(3, "%s: Persistent Reservation OUT parameter:", dev);
87 dumpHex((const char *)paramp, paramlen,1);
90 memset(&Sensedata, 0, sizeof(SenseData_t));
91 memset(&io_hdr,0 , sizeof( struct sg_io_hdr));
92 io_hdr.interface_id = 'S';
93 io_hdr.cmd_len = MPATH_PROUT_CMDLEN;
95 io_hdr.sbp = (void *)&Sensedata;
96 io_hdr.mx_sb_len = sizeof (SenseData_t);
97 io_hdr.timeout = TIMEOUT;
100 io_hdr.dxferp = (void *)paramp;
101 io_hdr.dxfer_len = paramlen;
102 io_hdr.dxfer_direction = SG_DXFER_TO_DEV ;
105 io_hdr.dxfer_direction = SG_DXFER_NONE;
107 ret = ioctl(fd, SG_IO, &io_hdr);
110 condlog(0, "%s: ioctl failed %d", dev, ret);
115 condlog(2, "%s: Duration=%u (ms)", dev, io_hdr.duration);
117 status = mpath_translate_response(dev, io_hdr, &Sensedata, noisy);
118 condlog(3, "%s: status = %d", dev, status);
120 if (status == MPATH_PR_SENSE_UNIT_ATTENTION && (retry > 0))
123 condlog(2, "%s: retrying for Unit Attention. Remaining retries = %d",
128 if (((status == MPATH_PR_SENSE_NOT_READY )&& (Sensedata.ASC == 0x04)&&
129 (Sensedata.ASCQ == 0x07))&& (retry > 0))
133 condlog(2, "%s: retrying for sense 02/04/07."
134 " Remaining retries = %d", dev, retry);
142 uint32_t format_transportids(struct prout_param_descriptor *paramp)
145 uint32_t buff_offset = 4;
146 memset(paramp->private_buffer, 0, MPATH_MAX_PARAM_LEN);
147 for (i=0; i < paramp->num_transportid; i++ )
149 paramp->private_buffer[buff_offset] = (uint8_t)((paramp->trnptid_list[i]->format_code & 0xff)|
150 (paramp->trnptid_list[i]->protocol_id & 0xff));
152 switch(paramp->trnptid_list[i]->protocol_id)
154 case MPATH_PROTOCOL_ID_FC:
156 memcpy(¶mp->private_buffer[buff_offset], ¶mp->trnptid_list[i]->n_port_name, 8);
160 case MPATH_PROTOCOL_ID_SAS:
162 memcpy(¶mp->private_buffer[buff_offset], ¶mp->trnptid_list[i]->sas_address, 8);
165 case MPATH_PROTOCOL_ID_ISCSI:
167 len = (paramp->trnptid_list[i]->iscsi_name[1] & 0xff)+2;
168 memcpy(¶mp->private_buffer[buff_offset], ¶mp->trnptid_list[i]->iscsi_name,len);
175 paramp->private_buffer[0] = (unsigned char)((buff_offset >> 24) & 0xff);
176 paramp->private_buffer[1] = (unsigned char)((buff_offset >> 16) & 0xff);
177 paramp->private_buffer[2] = (unsigned char)((buff_offset >> 8) & 0xff);
178 paramp->private_buffer[3] = (unsigned char)(buff_offset & 0xff);
183 void mpath_format_readkeys( struct prin_resp *pr_buff, int len, int noisy)
185 mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readkeys.prgeneration);
186 mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readkeys.additional_length);
189 void mpath_format_readresv(struct prin_resp *pr_buff, int len, int noisy)
192 mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readkeys.prgeneration);
193 mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readkeys.additional_length);
198 void mpath_format_reportcapabilities(struct prin_resp *pr_buff, int len, int noisy)
200 mpath_reverse_uint16_byteorder(&pr_buff->prin_descriptor.prin_readcap.length);
201 mpath_reverse_uint16_byteorder(&pr_buff->prin_descriptor.prin_readcap.pr_type_mask);
206 void mpath_format_readfullstatus(struct prin_resp *pr_buff, int len, int noisy)
208 int num, k, tid_len_len=0;
209 uint32_t fdesc_count=0;
212 uint32_t additional_length;
215 mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readfd.prgeneration);
216 mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readfd.number_of_descriptor);
218 if (0 == pr_buff->prin_descriptor.prin_readfd.number_of_descriptor)
224 if (pr_buff->prin_descriptor.prin_readfd.number_of_descriptor == 0)
226 condlog(2, "No registration or resrvation found.");
230 additional_length = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
232 char tempbuff[MPATH_MAX_PARAM_LEN];
233 struct prin_fulldescr fdesc;
234 memset(&fdesc, 0, sizeof(struct prin_fulldescr));
236 memcpy( tempbuff, pr_buff->prin_descriptor.prin_readfd.private_buffer,MPATH_MAX_PARAM_LEN );
237 memset(&pr_buff->prin_descriptor.prin_readfd.private_buffer, 0, MPATH_MAX_PARAM_LEN);
239 p =(unsigned char *)tempbuff;
240 ppbuff = (char *)pr_buff->prin_descriptor.prin_readfd.private_buffer;
242 for (k = 0; k < additional_length; k += num, p += num) {
243 memcpy(&fdesc.key, p, 8 );
245 fdesc.scope_type = p[13];
246 fdesc.rtpi = ((p[18] << 8) | p[19]);
248 tid_len_len = ((p[20] << 24) | (p[21] << 16) |
249 (p[22] << 8) | p[23]);
252 decode_transport_id( &fdesc, &p[24], tid_len_len);
254 num = 24 + tid_len_len;
255 memcpy(ppbuff, &fdesc, sizeof(struct prin_fulldescr));
256 pr_buff->prin_descriptor.prin_readfd.descriptors[fdesc_count]= (struct prin_fulldescr *)ppbuff;
257 ppbuff += sizeof(struct prin_fulldescr);
261 pr_buff->prin_descriptor.prin_readfd.number_of_descriptor = fdesc_count;
267 decode_transport_id(struct prin_fulldescr *fdesc, unsigned char * p, int length)
271 for (k = 0, jump = 24; k < length; k += jump, p += jump) {
272 fdesc->trnptid.format_code = ((p[0] >> 6) & 0x3);
273 fdesc->trnptid.protocol_id = (p[0] & 0xf);
274 switch (fdesc->trnptid.protocol_id) {
275 case MPATH_PROTOCOL_ID_FC:
276 memcpy(&fdesc->trnptid.n_port_name, &p[8], 8);
279 case MPATH_PROTOCOL_ID_ISCSI:
280 num = ((p[2] << 8) | p[3]);
281 memcpy(&fdesc->trnptid.iscsi_name, &p[4], num);
282 jump = (((num + 4) < 24) ? 24 : num + 4);
284 case MPATH_PROTOCOL_ID_SAS:
285 memcpy(&fdesc->trnptid.sas_address, &p[4], 8);
295 int prin_do_scsi_ioctl(char * dev, int rq_servact, struct prin_resp * resp, int noisy)
298 int ret, status, got, fd;
300 SenseData_t Sensedata;
301 int retry = MAXRETRY;
302 struct sg_io_hdr io_hdr;
303 char devname[FILE_NAME_SIZE];
304 unsigned char cdb[MPATH_PRIN_CMDLEN] =
305 {MPATH_PRIN_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
307 snprintf(devname, FILE_NAME_SIZE, "/dev/%s",dev);
308 fd = open(devname, O_WRONLY);
310 condlog(0, "%s: Unable to open device ", dev);
311 return MPATH_PR_FILE_ERROR;
314 if (mpath_mx_alloc_len)
315 mx_resp_len = mpath_mx_alloc_len;
317 mx_resp_len = get_prin_length(rq_servact);
319 if (mx_resp_len == 0) {
320 status = MPATH_PR_SYNTAX_ERROR;
324 cdb[1] = (unsigned char)(rq_servact & 0x1f);
325 cdb[7] = (unsigned char)((mx_resp_len >> 8) & 0xff);
326 cdb[8] = (unsigned char)(mx_resp_len & 0xff);
329 memset(&Sensedata, 0, sizeof(SenseData_t));
330 memset(&io_hdr,0 , sizeof( struct sg_io_hdr));
332 io_hdr.interface_id = 'S';
333 io_hdr.cmd_len = MPATH_PRIN_CMDLEN;
334 io_hdr.mx_sb_len = sizeof (SenseData_t);
335 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
337 io_hdr.sbp = (void *)&Sensedata;
338 io_hdr.timeout = TIMEOUT;
342 io_hdr.dxfer_len = mx_resp_len;
343 io_hdr.dxferp = (void *)resp;
345 ret =ioctl(fd, SG_IO, &io_hdr);
347 condlog(0, "%s: IOCTL failed %d", dev, ret);
348 status = MPATH_PR_OTHER;
352 got = mx_resp_len - io_hdr.resid;
354 condlog(2, "%s: duration = %u (ms)", dev, io_hdr.duration);
355 condlog(2, "%s: persistent reservation in: requested %d bytes but got %d bytes)", dev, mx_resp_len, got);
357 status = mpath_translate_response(dev, io_hdr, &Sensedata, noisy);
359 if (status == MPATH_PR_SENSE_UNIT_ATTENTION && (retry > 0))
362 condlog(2, "%s: retrying for Unit Attention. Remaining retries = %d", dev, retry);
366 if (((status == MPATH_PR_SENSE_NOT_READY )&& (Sensedata.ASC == 0x04)&&
367 (Sensedata.ASCQ == 0x07))&& (retry > 0))
371 condlog(2, "%s: retrying for 02/04/07. Remaining retries = %d", dev, retry);
375 if (status != MPATH_PR_SUCCESS)
379 dumpHex((const char *)resp, got , 1);
384 case MPATH_PRIN_RKEY_SA :
385 mpath_format_readkeys(resp, got, noisy);
387 case MPATH_PRIN_RRES_SA :
388 mpath_format_readresv(resp, got, noisy);
390 case MPATH_PRIN_RCAP_SA :
391 mpath_format_reportcapabilities(resp, got, noisy);
393 case MPATH_PRIN_RFSTAT_SA :
394 mpath_format_readfullstatus(resp, got, noisy);
402 int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr,
403 SenseData_t *Sensedata, int noisy)
405 condlog(3, "%s: status driver:%02x host:%02x scsi:%02x", dev,
406 io_hdr.driver_status, io_hdr.host_status ,io_hdr.status);
407 io_hdr.status &= 0x7e;
408 if ((0 == io_hdr.status) &&
409 (0 == io_hdr.host_status) &&
410 (0 == io_hdr.driver_status))
411 return MPATH_PR_SUCCESS;
413 switch(io_hdr.status) {
416 case SAM_STAT_CHECK_CONDITION:
417 condlog(2, "%s: Sense_Key=%02x, ASC=%02x ASCQ=%02x",
418 dev, Sensedata->Sense_Key,
419 Sensedata->ASC, Sensedata->ASCQ);
420 switch(Sensedata->Sense_Key) {
422 return MPATH_PR_NO_SENSE;
423 case RECOVERED_ERROR:
424 return MPATH_PR_SUCCESS;
426 return MPATH_PR_SENSE_NOT_READY;
428 return MPATH_PR_SENSE_MEDIUM_ERROR;
430 return MPATH_PR_OTHER;
432 return MPATH_PR_SENSE_HARDWARE_ERROR;
433 case ILLEGAL_REQUEST:
434 return MPATH_PR_ILLEGAL_REQ;
436 return MPATH_PR_SENSE_UNIT_ATTENTION;
439 return MPATH_PR_OTHER;
440 case ABORTED_COMMAND:
441 return MPATH_PR_SENSE_ABORTED_COMMAND;
444 return MPATH_PR_OTHER;
446 case SAM_STAT_RESERVATION_CONFLICT:
447 return MPATH_PR_RESERV_CONFLICT;
450 return MPATH_PR_OTHER;
453 switch(io_hdr.host_status) {
457 return MPATH_PR_OTHER;
459 switch(io_hdr.driver_status)
464 return MPATH_PR_OTHER;
466 return MPATH_PR_SUCCESS;
469 int mpath_isLittleEndian(void)
472 if(*(char *)&num == 1)
474 condlog(2, "Little-Endian");
478 condlog(2, "Big-Endian");
483 void mpath_reverse_uint16_byteorder(uint16_t *num)
485 uint16_t byte0, byte1;
487 byte0 = (*num & 0x000000FF) >> 0 ;
488 byte1 = (*num & 0x0000FF00) >> 8 ;
490 *num = ((byte0 << 8) | (byte1 << 0));
493 void mpath_reverse_uint32_byteorder(uint32_t *num)
495 uint32_t byte0, byte1, byte2, byte3;
497 byte0 = (*num & 0x000000FF) >> 0 ;
498 byte1 = (*num & 0x0000FF00) >> 8 ;
499 byte2 = (*num & 0x00FF0000) >> 16 ;
500 byte3 = (*num & 0xFF000000) >> 24 ;
502 *num = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | (byte3 << 0));
505 void mpath_reverse_8bytes_order(char * var)
510 for(i=0 ; i < 8 ; i++ )
514 for(i=0 ; i < 8 ; i++ )
516 var[7 - i] = byte[i];
521 dumpHex(const char* str, int len, int log)
523 const char * p = str;
526 const int bpstart = 5;
532 memset(buff, ' ', 80);
534 for (k = 0; k < len; k++) {
537 if (bpos == (bpstart + (9 * 3)))
539 sprintf(&buff[bpos], "%.2x", (int)(unsigned char)c);
540 buff[bpos + 2] = ' ';
541 if ((k > 0) && (0 == ((k + 1) % 16))) {
543 condlog(0, "%.76s" , buff);
545 printf("%.76s" , buff);
547 memset(buff, ' ', 80);
550 if (bpos > bpstart) {
551 buff[bpos + 2] = '\0';
553 condlog(0, "%s", buff);
555 printf("%s\n" , buff);
560 int get_prin_length(int rq_servact)
565 case MPATH_PRIN_RKEY_SA:
566 mx_resp_len = sizeof(struct prin_readdescr);
568 case MPATH_PRIN_RRES_SA :
569 mx_resp_len = sizeof(struct prin_resvdescr);
571 case MPATH_PRIN_RCAP_SA :
572 mx_resp_len = sizeof(struct prin_capdescr);
574 case MPATH_PRIN_RFSTAT_SA:
575 mx_resp_len = sizeof(struct print_fulldescr_list) + sizeof(struct prin_fulldescr *)*32;
578 condlog(0, "invalid service action, %d", rq_servact);