22 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <scsi/scsi.h>
29 #define ALUA_DH_NAME "alua"
30 #define ALUA_DH_VER "1.3"
32 #define TPGS_STATE_OPTIMIZED 0x0
33 #define TPGS_STATE_NONOPTIMIZED 0x1
34 #define TPGS_STATE_STANDBY 0x2
35 #define TPGS_STATE_UNAVAILABLE 0x3
36 #define TPGS_STATE_LBA_DEPENDENT 0x4
37 #define TPGS_STATE_OFFLINE 0xe
38 #define TPGS_STATE_TRANSITIONING 0xf
40 #define TPGS_SUPPORT_NONE 0x00
41 #define TPGS_SUPPORT_OPTIMIZED 0x01
42 #define TPGS_SUPPORT_NONOPTIMIZED 0x02
43 #define TPGS_SUPPORT_STANDBY 0x04
44 #define TPGS_SUPPORT_UNAVAILABLE 0x08
45 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
46 #define TPGS_SUPPORT_OFFLINE 0x40
47 #define TPGS_SUPPORT_TRANSITION 0x80
49 #define RTPG_FMT_MASK 0x70
50 #define RTPG_FMT_EXT_HDR 0x10
52 #define TPGS_MODE_UNINITIALIZED -1
53 #define TPGS_MODE_NONE 0x0
54 #define TPGS_MODE_IMPLICIT 0x1
55 #define TPGS_MODE_EXPLICIT 0x2
57 #define ALUA_INQUIRY_SIZE 36
58 #define ALUA_FAILOVER_TIMEOUT 60
59 #define ALUA_FAILOVER_RETRIES 5
62 #define ALUA_OPTIMIZE_STPG 1
82 #define ALUA_POLICY_SWITCH_CURRENT 0
83 #define ALUA_POLICY_SWITCH_ALL 1
85 static char print_alua_state(
int);
95 static int realloc_buffer(
struct alua_dh_data *
h,
unsigned len)
120 "%s: blk_get_request failed\n", __func__);
127 "%s: blk_rq_map_kern failed\n", __func__);
131 rq->cmd_type = REQ_TYPE_BLOCK_PC;
160 rq->sense = h->
sense;
167 "%s: evpd inquiry failed with %x\n",
182 bool rtpg_ext_hdr_req)
193 if (rtpg_ext_hdr_req)
197 rq->cmd[6] = (h->
bufflen >> 24) & 0xff;
198 rq->cmd[7] = (h->
bufflen >> 16) & 0xff;
199 rq->cmd[8] = (h->
bufflen >> 8) & 0xff;
200 rq->cmd[9] = h->
bufflen & 0xff;
203 rq->sense = h->
sense;
210 "%s: rtpg failed with %x\n",
248 err = alua_check_sense(h->
sdev, &sense_hdr);
254 "%s: stpg sense code: %02x/%02x/%02x\n",
256 sense_hdr.asc, sense_hdr.ascq);
262 "%s: port group %02x switched to state %c\n",
264 print_alua_state(h->
state));
267 req->end_io_data =
NULL;
295 rq = get_alua_req(sdev, h->
buff, stpg_len,
WRITE);
302 rq->cmd[6] = (stpg_len >> 24) & 0xff;
303 rq->cmd[7] = (stpg_len >> 16) & 0xff;
304 rq->cmd[8] = (stpg_len >> 8) & 0xff;
305 rq->cmd[9] = stpg_len & 0xff;
308 rq->sense = h->
sense;
328 h->
tpgs = scsi_device_tpgs(sdev);
332 "%s: supports implicit and explicit TPGS\n",
368 err = submit_vpd_inquiry(sdev, h);
374 len = (h->
buff[2] << 8) + h->
buff[3] + 4;
377 if (realloc_buffer(h, len)) {
379 "%s: kmalloc buffer failed\n",
391 while (d < h->buff + len) {
392 switch (d[1] & 0xf) {
414 "%s: No target port descriptors found\n",
421 "%s: port group %02x rel port %02x\n",
428 static char print_alua_state(
int state)
450 static int alua_check_sense(
struct scsi_device *sdev,
455 if (sense_hdr->
asc == 0x04 && sense_hdr->
ascq == 0x0a)
460 if (sense_hdr->
asc == 0x04 && sense_hdr->
ascq == 0x0b)
465 if (sense_hdr->
asc == 0x04 && sense_hdr->
ascq == 0x0c)
470 if (sense_hdr->
asc == 0x04 && sense_hdr->
ascq == 0x12)
477 if (sense_hdr->
asc == 0x29 && sense_hdr->
ascq == 0x00)
482 if (sense_hdr->
asc == 0x2a && sense_hdr->
ascq == 0x01)
487 if (sense_hdr->
asc == 0x2a && sense_hdr->
ascq == 0x06)
492 if (sense_hdr->
asc == 0x2a && sense_hdr->
ascq == 0x07)
497 if (sense_hdr->
asc == 0x3f && sense_hdr->
ascq == 0x03)
502 if (sense_hdr->
asc == 0x3f && sense_hdr->
ascq == 0x0e)
526 int len,
k, off, valid_states = 0;
529 bool rtpg_ext_hdr_req = 1;
531 unsigned int tpg_desc_tbl_off;
532 unsigned char orig_transition_tmo;
540 err = submit_rtpg(sdev, h, rtpg_ext_hdr_req);
556 if (rtpg_ext_hdr_req == 1 &&
558 sense_hdr.
asc == 0x24 && sense_hdr.
ascq == 0) {
559 rtpg_ext_hdr_req = 0;
563 err = alua_check_sense(sdev, &sense_hdr);
567 "%s: rtpg sense code %02x/%02x/%02x\n",
569 sense_hdr.
asc, sense_hdr.
ascq);
575 len = (h->
buff[0] << 24) + (h->
buff[1] << 16) +
580 if (realloc_buffer(h, len)) {
582 "%s: kmalloc buffer failed\n",__func__);
597 "%s: transition timeout set to %d seconds\n",
603 tpg_desc_tbl_off = 8;
605 tpg_desc_tbl_off = 4;
607 for (k = tpg_desc_tbl_off, ucp = h->
buff + tpg_desc_tbl_off;
609 k += off, ucp += off) {
611 if (h->
group_id == (ucp[2] << 8) + ucp[3]) {
612 h->
state = ucp[0] & 0x0f;
613 h->
pref = ucp[0] >> 7;
614 valid_states = ucp[1];
616 off = 8 + (ucp[7] * 4);
620 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
622 h->
pref ?
"preferred" :
"non-preferred",
666 err = alua_check_tpgs(sdev, h);
670 err = alua_vpd_inquiry(sdev, h);
674 err = alua_rtpg(sdev, h);
693 unsigned int optimize = 0,
argc;
702 if ((
sscanf(p,
"%u", &optimize) != 1) || (optimize > 1))
730 err = alua_rtpg(sdev, h);
761 err = submit_stpg(h);
782 int ret = BLKPREP_OK;
798 return (scsi_device_tpgs(sdev) != 0);
801 static int alua_bus_attach(
struct scsi_device *sdev);
802 static void alua_bus_detach(
struct scsi_device *sdev);
807 .attach = alua_bus_attach,
808 .detach = alua_bus_detach,
809 .prep_fn = alua_prep_fn,
810 .check_sense = alua_check_sense,
811 .activate = alua_activate,
812 .set_params = alua_set_params,
820 static int alua_bus_attach(
struct scsi_device *sdev)
827 scsi_dh_data = kzalloc(
sizeof(*scsi_dh_data)
835 scsi_dh_data->
scsi_dh = &alua_dh;
845 err = alua_initialize(sdev, h);
854 spin_unlock_irqrestore(sdev->
request_queue->queue_lock, flags);
869 static void alua_bus_detach(
struct scsi_device *sdev)
878 spin_unlock_irqrestore(sdev->
request_queue->queue_lock, flags);
888 static int __init alua_init(
void)
899 static void __exit alua_exit(
void)