Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic SCSI-3 ALUA SCSI Device Handler |
| 3 | * |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 4 | * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH. |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | * |
| 21 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 23 | #include <linux/delay.h> |
Paul Gortmaker | acf3368f | 2011-05-27 09:47:43 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 26 | #include <scsi/scsi.h> |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 27 | #include <scsi/scsi_dbg.h> |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 28 | #include <scsi/scsi_eh.h> |
| 29 | #include <scsi/scsi_dh.h> |
| 30 | |
| 31 | #define ALUA_DH_NAME "alua" |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 32 | #define ALUA_DH_VER "1.3" |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 33 | |
| 34 | #define TPGS_STATE_OPTIMIZED 0x0 |
| 35 | #define TPGS_STATE_NONOPTIMIZED 0x1 |
| 36 | #define TPGS_STATE_STANDBY 0x2 |
| 37 | #define TPGS_STATE_UNAVAILABLE 0x3 |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 38 | #define TPGS_STATE_LBA_DEPENDENT 0x4 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 39 | #define TPGS_STATE_OFFLINE 0xe |
| 40 | #define TPGS_STATE_TRANSITIONING 0xf |
| 41 | |
| 42 | #define TPGS_SUPPORT_NONE 0x00 |
| 43 | #define TPGS_SUPPORT_OPTIMIZED 0x01 |
| 44 | #define TPGS_SUPPORT_NONOPTIMIZED 0x02 |
| 45 | #define TPGS_SUPPORT_STANDBY 0x04 |
| 46 | #define TPGS_SUPPORT_UNAVAILABLE 0x08 |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 47 | #define TPGS_SUPPORT_LBA_DEPENDENT 0x10 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 48 | #define TPGS_SUPPORT_OFFLINE 0x40 |
| 49 | #define TPGS_SUPPORT_TRANSITION 0x80 |
| 50 | |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 51 | #define RTPG_FMT_MASK 0x70 |
| 52 | #define RTPG_FMT_EXT_HDR 0x10 |
| 53 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 54 | #define TPGS_MODE_UNINITIALIZED -1 |
| 55 | #define TPGS_MODE_NONE 0x0 |
| 56 | #define TPGS_MODE_IMPLICIT 0x1 |
| 57 | #define TPGS_MODE_EXPLICIT 0x2 |
| 58 | |
| 59 | #define ALUA_INQUIRY_SIZE 36 |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 60 | #define ALUA_FAILOVER_TIMEOUT 60 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 61 | #define ALUA_FAILOVER_RETRIES 5 |
| 62 | |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 63 | /* device handler flags */ |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 64 | #define ALUA_OPTIMIZE_STPG 1 |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 65 | #define ALUA_RTPG_EXT_HDR_UNSUPP 2 |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 66 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 67 | struct alua_dh_data { |
| 68 | int group_id; |
| 69 | int rel_port; |
| 70 | int tpgs; |
| 71 | int state; |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 72 | int pref; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 73 | unsigned flags; /* used for optimizing STPG */ |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 74 | unsigned char inq[ALUA_INQUIRY_SIZE]; |
| 75 | unsigned char *buff; |
| 76 | int bufflen; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 77 | unsigned char transition_tmo; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 78 | unsigned char sense[SCSI_SENSE_BUFFERSIZE]; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 79 | struct scsi_device *sdev; |
| 80 | activate_complete callback_fn; |
| 81 | void *callback_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | #define ALUA_POLICY_SWITCH_CURRENT 0 |
| 85 | #define ALUA_POLICY_SWITCH_ALL 1 |
| 86 | |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 87 | static char print_alua_state(int); |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 88 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 89 | static int realloc_buffer(struct alua_dh_data *h, unsigned len) |
| 90 | { |
| 91 | if (h->buff && h->buff != h->inq) |
| 92 | kfree(h->buff); |
| 93 | |
| 94 | h->buff = kmalloc(len, GFP_NOIO); |
| 95 | if (!h->buff) { |
| 96 | h->buff = h->inq; |
| 97 | h->bufflen = ALUA_INQUIRY_SIZE; |
| 98 | return 1; |
| 99 | } |
| 100 | h->bufflen = len; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static struct request *get_alua_req(struct scsi_device *sdev, |
| 105 | void *buffer, unsigned buflen, int rw) |
| 106 | { |
| 107 | struct request *rq; |
| 108 | struct request_queue *q = sdev->request_queue; |
| 109 | |
| 110 | rq = blk_get_request(q, rw, GFP_NOIO); |
| 111 | |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 112 | if (IS_ERR(rq)) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 113 | sdev_printk(KERN_INFO, sdev, |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 114 | "%s: blk_get_request failed\n", __func__); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
Jens Axboe | f27b087 | 2014-06-06 07:57:37 -0600 | [diff] [blame] | 117 | blk_rq_set_block_pc(rq); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 118 | |
| 119 | if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) { |
| 120 | blk_put_request(rq); |
| 121 | sdev_printk(KERN_INFO, sdev, |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 122 | "%s: blk_rq_map_kern failed\n", __func__); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
| 125 | |
Mike Christie | 6000a36 | 2008-08-19 18:45:30 -0500 | [diff] [blame] | 126 | rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | |
Mike Christie | 64f84bc | 2008-09-06 08:39:16 -0500 | [diff] [blame] | 127 | REQ_FAILFAST_DRIVER; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 128 | rq->retries = ALUA_FAILOVER_RETRIES; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 129 | rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 130 | |
| 131 | return rq; |
| 132 | } |
| 133 | |
| 134 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 135 | * submit_rtpg - Issue a REPORT TARGET GROUP STATES command |
| 136 | * @sdev: sdev the command should be sent to |
| 137 | */ |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame^] | 138 | static unsigned submit_rtpg(struct scsi_device *sdev, unsigned char *buff, |
| 139 | int bufflen, unsigned char *sense, int flags) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 140 | { |
| 141 | struct request *rq; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 142 | int err = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 143 | |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame^] | 144 | rq = get_alua_req(sdev, buff, bufflen, READ); |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 145 | if (!rq) { |
| 146 | err = DRIVER_BUSY << 24; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 147 | goto done; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 148 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 149 | |
| 150 | /* Prepare the command. */ |
| 151 | rq->cmd[0] = MAINTENANCE_IN; |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame^] | 152 | if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP)) |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 153 | rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT; |
| 154 | else |
| 155 | rq->cmd[1] = MI_REPORT_TARGET_PGS; |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame^] | 156 | put_unaligned_be32(bufflen, &rq->cmd[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 157 | rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN); |
| 158 | |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame^] | 159 | rq->sense = sense; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 160 | memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE); |
Hannes Reinecke | d3692a3 | 2015-12-01 10:16:46 +0100 | [diff] [blame] | 161 | rq->sense_len = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 162 | |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 163 | blk_execute_rq(rq->q, NULL, rq, 1); |
| 164 | if (rq->errors) |
| 165 | err = rq->errors; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 166 | blk_put_request(rq); |
| 167 | done: |
| 168 | return err; |
| 169 | } |
| 170 | |
| 171 | /* |
Hannes Reinecke | dac173e | 2015-12-01 10:16:49 +0100 | [diff] [blame] | 172 | * stpg_endio - Evaluate SET TARGET GROUP STATES |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 173 | * @sdev: the device to be evaluated |
| 174 | * @state: the new target group state |
| 175 | * |
Hannes Reinecke | dac173e | 2015-12-01 10:16:49 +0100 | [diff] [blame] | 176 | * Evaluate a SET TARGET GROUP STATES command response. |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 177 | */ |
| 178 | static void stpg_endio(struct request *req, int error) |
| 179 | { |
| 180 | struct alua_dh_data *h = req->end_io_data; |
| 181 | struct scsi_sense_hdr sense_hdr; |
Joseph Gruher | 9349923 | 2011-01-05 16:00:22 -0500 | [diff] [blame] | 182 | unsigned err = SCSI_DH_OK; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 183 | |
Mike Christie | 27db682 | 2013-03-05 22:40:24 -0600 | [diff] [blame] | 184 | if (host_byte(req->errors) != DID_OK || |
| 185 | msg_byte(req->errors) != COMMAND_COMPLETE) { |
Joseph Gruher | 9349923 | 2011-01-05 16:00:22 -0500 | [diff] [blame] | 186 | err = SCSI_DH_IO; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 187 | goto done; |
Joseph Gruher | 9349923 | 2011-01-05 16:00:22 -0500 | [diff] [blame] | 188 | } |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 189 | |
Hannes Reinecke | d3692a3 | 2015-12-01 10:16:46 +0100 | [diff] [blame] | 190 | if (scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE, |
| 191 | &sense_hdr)) { |
Hannes Reinecke | e2d817d | 2015-12-01 10:16:54 +0100 | [diff] [blame] | 192 | if (sense_hdr.sense_key == NOT_READY && |
| 193 | sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) { |
| 194 | /* ALUA state transition already in progress */ |
| 195 | err = SCSI_DH_OK; |
| 196 | goto done; |
| 197 | } |
| 198 | if (sense_hdr.sense_key == UNIT_ATTENTION) { |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 199 | err = SCSI_DH_RETRY; |
| 200 | goto done; |
| 201 | } |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 202 | sdev_printk(KERN_INFO, h->sdev, "%s: stpg failed\n", |
| 203 | ALUA_DH_NAME); |
| 204 | scsi_print_sense_hdr(h->sdev, ALUA_DH_NAME, &sense_hdr); |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 205 | err = SCSI_DH_IO; |
Mike Christie | 27db682 | 2013-03-05 22:40:24 -0600 | [diff] [blame] | 206 | } else if (error) |
| 207 | err = SCSI_DH_IO; |
| 208 | |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 209 | if (err == SCSI_DH_OK) { |
| 210 | h->state = TPGS_STATE_OPTIMIZED; |
| 211 | sdev_printk(KERN_INFO, h->sdev, |
| 212 | "%s: port group %02x switched to state %c\n", |
| 213 | ALUA_DH_NAME, h->group_id, |
| 214 | print_alua_state(h->state)); |
| 215 | } |
| 216 | done: |
Joseph Gruher | ed0f36b | 2011-01-05 16:00:21 -0500 | [diff] [blame] | 217 | req->end_io_data = NULL; |
| 218 | __blk_put_request(req->q, req); |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 219 | if (h->callback_fn) { |
| 220 | h->callback_fn(h->callback_data, err); |
| 221 | h->callback_fn = h->callback_data = NULL; |
| 222 | } |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 227 | * submit_stpg - Issue a SET TARGET GROUP STATES command |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 228 | * |
| 229 | * Currently we're only setting the current target port group state |
| 230 | * to 'active/optimized' and let the array firmware figure out |
| 231 | * the states of the remaining groups. |
| 232 | */ |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 233 | static unsigned submit_stpg(struct alua_dh_data *h) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 234 | { |
| 235 | struct request *rq; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 236 | int stpg_len = 8; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 237 | struct scsi_device *sdev = h->sdev; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 238 | |
| 239 | /* Prepare the data buffer */ |
| 240 | memset(h->buff, 0, stpg_len); |
| 241 | h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f; |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 242 | put_unaligned_be16(h->group_id, &h->buff[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 243 | |
| 244 | rq = get_alua_req(sdev, h->buff, stpg_len, WRITE); |
| 245 | if (!rq) |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 246 | return SCSI_DH_RES_TEMP_UNAVAIL; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 247 | |
| 248 | /* Prepare the command. */ |
| 249 | rq->cmd[0] = MAINTENANCE_OUT; |
| 250 | rq->cmd[1] = MO_SET_TARGET_PGS; |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 251 | put_unaligned_be32(stpg_len, &rq->cmd[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 252 | rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT); |
| 253 | |
| 254 | rq->sense = h->sense; |
| 255 | memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE); |
Hannes Reinecke | d3692a3 | 2015-12-01 10:16:46 +0100 | [diff] [blame] | 256 | rq->sense_len = 0; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 257 | rq->end_io_data = h; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 258 | |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 259 | blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio); |
Joseph Gruher | 7c66e9a | 2011-01-05 16:00:20 -0500 | [diff] [blame] | 260 | return SCSI_DH_OK; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
Hannes Reinecke | d7c48fe | 2011-08-24 10:51:13 +0200 | [diff] [blame] | 264 | * alua_check_tpgs - Evaluate TPGS setting |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 265 | * @sdev: device to be checked |
| 266 | * |
Hannes Reinecke | d7c48fe | 2011-08-24 10:51:13 +0200 | [diff] [blame] | 267 | * Examine the TPGS setting of the sdev to find out if ALUA |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 268 | * is supported. |
| 269 | */ |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 270 | static int alua_check_tpgs(struct scsi_device *sdev) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 271 | { |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 272 | int tpgs = TPGS_MODE_NONE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 273 | |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 274 | /* |
| 275 | * ALUA support for non-disk devices is fraught with |
| 276 | * difficulties, so disable it for now. |
| 277 | */ |
| 278 | if (sdev->type != TYPE_DISK) { |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 279 | sdev_printk(KERN_INFO, sdev, |
| 280 | "%s: disable for non-disk devices\n", |
| 281 | ALUA_DH_NAME); |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 282 | return tpgs; |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 285 | tpgs = scsi_device_tpgs(sdev); |
| 286 | switch (tpgs) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 287 | case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT: |
| 288 | sdev_printk(KERN_INFO, sdev, |
| 289 | "%s: supports implicit and explicit TPGS\n", |
| 290 | ALUA_DH_NAME); |
| 291 | break; |
| 292 | case TPGS_MODE_EXPLICIT: |
| 293 | sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n", |
| 294 | ALUA_DH_NAME); |
| 295 | break; |
| 296 | case TPGS_MODE_IMPLICIT: |
| 297 | sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n", |
| 298 | ALUA_DH_NAME); |
| 299 | break; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 300 | case TPGS_MODE_NONE: |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 301 | sdev_printk(KERN_INFO, sdev, "%s: not supported\n", |
| 302 | ALUA_DH_NAME); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 303 | break; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 304 | default: |
| 305 | sdev_printk(KERN_INFO, sdev, |
| 306 | "%s: unsupported TPGS setting %d\n", |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 307 | ALUA_DH_NAME, tpgs); |
| 308 | tpgs = TPGS_MODE_NONE; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 309 | break; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 312 | return tpgs; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 316 | * alua_check_vpd - Evaluate INQUIRY vpd page 0x83 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 317 | * @sdev: device to be checked |
| 318 | * |
| 319 | * Extract the relative target port and the target port group |
| 320 | * descriptor from the list of identificators. |
| 321 | */ |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 322 | static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 323 | { |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 324 | int rel_port = -1, group_id; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 325 | |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 326 | group_id = scsi_vpd_tpg_id(sdev, &rel_port); |
| 327 | if (group_id < 0) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 328 | /* |
| 329 | * Internal error; TPGS supported but required |
| 330 | * VPD identification descriptors not present. |
| 331 | * Disable ALUA support |
| 332 | */ |
| 333 | sdev_printk(KERN_INFO, sdev, |
| 334 | "%s: No target port descriptors found\n", |
| 335 | ALUA_DH_NAME); |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 336 | return SCSI_DH_DEV_UNSUPP; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 337 | } |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 338 | h->state = TPGS_STATE_OPTIMIZED; |
| 339 | h->group_id = group_id; |
| 340 | |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 341 | sdev_printk(KERN_INFO, sdev, |
| 342 | "%s: port group %02x rel port %02x\n", |
| 343 | ALUA_DH_NAME, h->group_id, h->rel_port); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 344 | |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 345 | return 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static char print_alua_state(int state) |
| 349 | { |
| 350 | switch (state) { |
| 351 | case TPGS_STATE_OPTIMIZED: |
| 352 | return 'A'; |
| 353 | case TPGS_STATE_NONOPTIMIZED: |
| 354 | return 'N'; |
| 355 | case TPGS_STATE_STANDBY: |
| 356 | return 'S'; |
| 357 | case TPGS_STATE_UNAVAILABLE: |
| 358 | return 'U'; |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 359 | case TPGS_STATE_LBA_DEPENDENT: |
| 360 | return 'L'; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 361 | case TPGS_STATE_OFFLINE: |
| 362 | return 'O'; |
| 363 | case TPGS_STATE_TRANSITIONING: |
| 364 | return 'T'; |
| 365 | default: |
| 366 | return 'X'; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | static int alua_check_sense(struct scsi_device *sdev, |
| 371 | struct scsi_sense_hdr *sense_hdr) |
| 372 | { |
| 373 | switch (sense_hdr->sense_key) { |
| 374 | case NOT_READY: |
| 375 | if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) |
| 376 | /* |
| 377 | * LUN Not Accessible - ALUA state transition |
| 378 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 379 | return ADD_TO_MLQUEUE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 380 | break; |
| 381 | case UNIT_ATTENTION: |
| 382 | if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) |
| 383 | /* |
| 384 | * Power On, Reset, or Bus Device Reset, just retry. |
| 385 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 386 | return ADD_TO_MLQUEUE; |
Stewart, Sean | c20ee7b | 2013-10-15 15:52:39 +0000 | [diff] [blame] | 387 | if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04) |
| 388 | /* |
| 389 | * Device internal reset |
| 390 | */ |
| 391 | return ADD_TO_MLQUEUE; |
Moger, Babu | 410f02d | 2011-12-21 18:01:37 -0500 | [diff] [blame] | 392 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01) |
| 393 | /* |
| 394 | * Mode Parameters Changed |
| 395 | */ |
| 396 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 397 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 398 | /* |
| 399 | * ALUA state changed |
| 400 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 401 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 402 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 403 | /* |
| 404 | * Implicit ALUA state transition failed |
| 405 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 406 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 407 | if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03) |
| 408 | /* |
| 409 | * Inquiry data has changed |
| 410 | */ |
| 411 | return ADD_TO_MLQUEUE; |
| 412 | if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e) |
Ilgu Hong | 4d086f6 | 2009-01-30 17:00:11 -0600 | [diff] [blame] | 413 | /* |
| 414 | * REPORTED_LUNS_DATA_HAS_CHANGED is reported |
| 415 | * when switching controllers on targets like |
| 416 | * Intel Multi-Flex. We can just retry. |
| 417 | */ |
| 418 | return ADD_TO_MLQUEUE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | |
| 422 | return SCSI_RETURN_NOT_HANDLED; |
| 423 | } |
| 424 | |
| 425 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 426 | * alua_rtpg - Evaluate REPORT TARGET GROUP STATES |
| 427 | * @sdev: the device to be evaluated. |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 428 | * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 429 | * |
| 430 | * Evaluate the Target Port Group State. |
| 431 | * Returns SCSI_DH_DEV_OFFLINED if the path is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 432 | * found to be unusable. |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 433 | */ |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 434 | static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 435 | { |
| 436 | struct scsi_sense_hdr sense_hdr; |
| 437 | int len, k, off, valid_states = 0; |
Hannes Reinecke | cfde3fa | 2011-08-24 10:51:18 +0200 | [diff] [blame] | 438 | unsigned char *ucp; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 439 | unsigned err, retval; |
Rob Evers | bc97f4b | 2012-05-18 14:08:56 -0400 | [diff] [blame] | 440 | unsigned long expiry, interval = 0; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 441 | unsigned int tpg_desc_tbl_off; |
| 442 | unsigned char orig_transition_tmo; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 443 | |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 444 | if (!h->transition_tmo) |
| 445 | expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ); |
| 446 | else |
| 447 | expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ); |
| 448 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 449 | retry: |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame^] | 450 | retval = submit_rtpg(sdev, h->buff, h->bufflen, h->sense, h->flags); |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 451 | if (retval) { |
Hannes Reinecke | d3692a3 | 2015-12-01 10:16:46 +0100 | [diff] [blame] | 452 | if (!scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE, |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 453 | &sense_hdr)) { |
| 454 | sdev_printk(KERN_INFO, sdev, |
| 455 | "%s: rtpg failed, result %d\n", |
| 456 | ALUA_DH_NAME, retval); |
| 457 | if (driver_byte(retval) == DRIVER_BUSY) |
| 458 | return SCSI_DH_DEV_TEMP_BUSY; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 459 | return SCSI_DH_IO; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 460 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 461 | |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 462 | /* |
| 463 | * submit_rtpg() has failed on existing arrays |
| 464 | * when requesting extended header info, and |
| 465 | * the array doesn't support extended headers, |
| 466 | * even though it shouldn't according to T10. |
| 467 | * The retry without rtpg_ext_hdr_req set |
| 468 | * handles this. |
| 469 | */ |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 470 | if (!(h->flags & ALUA_RTPG_EXT_HDR_UNSUPP) && |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 471 | sense_hdr.sense_key == ILLEGAL_REQUEST && |
| 472 | sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) { |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 473 | h->flags |= ALUA_RTPG_EXT_HDR_UNSUPP; |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 474 | goto retry; |
| 475 | } |
Hannes Reinecke | e2d817d | 2015-12-01 10:16:54 +0100 | [diff] [blame] | 476 | /* |
| 477 | * Retry on ALUA state transition or if any |
| 478 | * UNIT ATTENTION occurred. |
| 479 | */ |
| 480 | if (sense_hdr.sense_key == NOT_READY && |
| 481 | sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) |
| 482 | err = SCSI_DH_RETRY; |
| 483 | else if (sense_hdr.sense_key == UNIT_ATTENTION) |
| 484 | err = SCSI_DH_RETRY; |
| 485 | if (err == SCSI_DH_RETRY && time_before(jiffies, expiry)) { |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 486 | sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n", |
| 487 | ALUA_DH_NAME); |
| 488 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 489 | goto retry; |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 490 | } |
| 491 | sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n", |
| 492 | ALUA_DH_NAME); |
| 493 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
| 494 | return SCSI_DH_IO; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 495 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 496 | |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 497 | len = get_unaligned_be32(&h->buff[0]) + 4; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 498 | |
| 499 | if (len > h->bufflen) { |
| 500 | /* Resubmit with the correct length */ |
| 501 | if (realloc_buffer(h, len)) { |
| 502 | sdev_printk(KERN_WARNING, sdev, |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 503 | "%s: kmalloc buffer failed\n",__func__); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 504 | /* Temporary failure, bypass */ |
| 505 | return SCSI_DH_DEV_TEMP_BUSY; |
| 506 | } |
| 507 | goto retry; |
| 508 | } |
| 509 | |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 510 | orig_transition_tmo = h->transition_tmo; |
| 511 | if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0) |
| 512 | h->transition_tmo = h->buff[5]; |
| 513 | else |
| 514 | h->transition_tmo = ALUA_FAILOVER_TIMEOUT; |
| 515 | |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 516 | if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) { |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 517 | sdev_printk(KERN_INFO, sdev, |
| 518 | "%s: transition timeout set to %d seconds\n", |
| 519 | ALUA_DH_NAME, h->transition_tmo); |
| 520 | expiry = jiffies + h->transition_tmo * HZ; |
| 521 | } |
| 522 | |
| 523 | if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR) |
| 524 | tpg_desc_tbl_off = 8; |
| 525 | else |
| 526 | tpg_desc_tbl_off = 4; |
| 527 | |
| 528 | for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off; |
| 529 | k < len; |
| 530 | k += off, ucp += off) { |
| 531 | |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 532 | if (h->group_id == get_unaligned_be16(&ucp[2])) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 533 | h->state = ucp[0] & 0x0f; |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 534 | h->pref = ucp[0] >> 7; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 535 | valid_states = ucp[1]; |
| 536 | } |
| 537 | off = 8 + (ucp[7] * 4); |
| 538 | } |
| 539 | |
| 540 | sdev_printk(KERN_INFO, sdev, |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 541 | "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n", |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 542 | ALUA_DH_NAME, h->group_id, print_alua_state(h->state), |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 543 | h->pref ? "preferred" : "non-preferred", |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 544 | valid_states&TPGS_SUPPORT_TRANSITION?'T':'t', |
| 545 | valid_states&TPGS_SUPPORT_OFFLINE?'O':'o', |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 546 | valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l', |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 547 | valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u', |
| 548 | valid_states&TPGS_SUPPORT_STANDBY?'S':'s', |
| 549 | valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n', |
| 550 | valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a'); |
| 551 | |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 552 | switch (h->state) { |
| 553 | case TPGS_STATE_TRANSITIONING: |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 554 | if (wait_for_transition) { |
| 555 | if (time_before(jiffies, expiry)) { |
| 556 | /* State transition, retry */ |
| 557 | interval += 2000; |
| 558 | msleep(interval); |
| 559 | goto retry; |
| 560 | } |
| 561 | err = SCSI_DH_RETRY; |
| 562 | } else { |
| 563 | err = SCSI_DH_OK; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 564 | } |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 565 | |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 566 | /* Transitioning time exceeded, set port to standby */ |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 567 | h->state = TPGS_STATE_STANDBY; |
| 568 | break; |
| 569 | case TPGS_STATE_OFFLINE: |
Bart Van Assche | e47f897 | 2012-08-24 09:08:41 +0000 | [diff] [blame] | 570 | /* Path unusable */ |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 571 | err = SCSI_DH_DEV_OFFLINED; |
| 572 | break; |
| 573 | default: |
| 574 | /* Useable path if active */ |
| 575 | err = SCSI_DH_OK; |
| 576 | break; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 577 | } |
| 578 | return err; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * alua_initialize - Initialize ALUA state |
| 583 | * @sdev: the device to be initialized |
| 584 | * |
| 585 | * For the prep_fn to work correctly we have |
| 586 | * to initialize the ALUA state for the device. |
| 587 | */ |
| 588 | static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h) |
| 589 | { |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 590 | int err = SCSI_DH_DEV_UNSUPP; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 591 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 592 | h->tpgs = alua_check_tpgs(sdev); |
| 593 | if (h->tpgs == TPGS_MODE_NONE) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 594 | goto out; |
| 595 | |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 596 | err = alua_check_vpd(sdev, h); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 597 | if (err != SCSI_DH_OK) |
| 598 | goto out; |
| 599 | |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 600 | err = alua_rtpg(sdev, h, 0); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 601 | if (err != SCSI_DH_OK) |
| 602 | goto out; |
| 603 | |
| 604 | out: |
| 605 | return err; |
| 606 | } |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 607 | /* |
| 608 | * alua_set_params - set/unset the optimize flag |
| 609 | * @sdev: device on the path to be activated |
| 610 | * params - parameters in the following format |
| 611 | * "no_of_params\0param1\0param2\0param3\0...\0" |
| 612 | * For example, to set the flag pass the following parameters |
| 613 | * from multipath.conf |
| 614 | * hardware_handler "2 alua 1" |
| 615 | */ |
| 616 | static int alua_set_params(struct scsi_device *sdev, const char *params) |
| 617 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 618 | struct alua_dh_data *h = sdev->handler_data; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 619 | unsigned int optimize = 0, argc; |
| 620 | const char *p = params; |
| 621 | int result = SCSI_DH_OK; |
| 622 | |
| 623 | if ((sscanf(params, "%u", &argc) != 1) || (argc != 1)) |
| 624 | return -EINVAL; |
| 625 | |
| 626 | while (*p++) |
| 627 | ; |
| 628 | if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1)) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | if (optimize) |
| 632 | h->flags |= ALUA_OPTIMIZE_STPG; |
| 633 | else |
| 634 | h->flags &= ~ALUA_OPTIMIZE_STPG; |
| 635 | |
| 636 | return result; |
| 637 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 638 | |
Stewart, Sean | 7a3ad39 | 2013-04-04 14:54:47 +0000 | [diff] [blame] | 639 | static uint optimize_stpg; |
| 640 | module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR); |
| 641 | MODULE_PARM_DESC(optimize_stpg, "Allow use of a non-optimized path, rather than sending a STPG, when implicit TPGS is supported (0=No,1=Yes). Default is 0."); |
| 642 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 643 | /* |
| 644 | * alua_activate - activate a path |
| 645 | * @sdev: device on the path to be activated |
| 646 | * |
| 647 | * We're currently switching the port group to be activated only and |
| 648 | * let the array figure out the rest. |
| 649 | * There may be other arrays which require us to switch all port groups |
| 650 | * based on a certain policy. But until we actually encounter them it |
| 651 | * should be okay. |
| 652 | */ |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 653 | static int alua_activate(struct scsi_device *sdev, |
| 654 | activate_complete fn, void *data) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 655 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 656 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 657 | int err = SCSI_DH_OK; |
Moger, Babu | 72d9e0f | 2012-03-27 20:56:20 +0000 | [diff] [blame] | 658 | int stpg = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 659 | |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 660 | err = alua_rtpg(sdev, h, 1); |
Hannes Reinecke | 46ccf6b | 2011-08-24 10:51:16 +0200 | [diff] [blame] | 661 | if (err != SCSI_DH_OK) |
| 662 | goto out; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 663 | |
Stewart, Sean | 7a3ad39 | 2013-04-04 14:54:47 +0000 | [diff] [blame] | 664 | if (optimize_stpg) |
| 665 | h->flags |= ALUA_OPTIMIZE_STPG; |
| 666 | |
Moger, Babu | 72d9e0f | 2012-03-27 20:56:20 +0000 | [diff] [blame] | 667 | if (h->tpgs & TPGS_MODE_EXPLICIT) { |
| 668 | switch (h->state) { |
| 669 | case TPGS_STATE_NONOPTIMIZED: |
| 670 | stpg = 1; |
| 671 | if ((h->flags & ALUA_OPTIMIZE_STPG) && |
| 672 | (!h->pref) && |
| 673 | (h->tpgs & TPGS_MODE_IMPLICIT)) |
| 674 | stpg = 0; |
| 675 | break; |
| 676 | case TPGS_STATE_STANDBY: |
Bart Van Assche | bb2c94a | 2012-06-22 08:49:38 +0000 | [diff] [blame] | 677 | case TPGS_STATE_UNAVAILABLE: |
Moger, Babu | 72d9e0f | 2012-03-27 20:56:20 +0000 | [diff] [blame] | 678 | stpg = 1; |
| 679 | break; |
Moger, Babu | 72d9e0f | 2012-03-27 20:56:20 +0000 | [diff] [blame] | 680 | case TPGS_STATE_OFFLINE: |
| 681 | err = SCSI_DH_IO; |
| 682 | break; |
| 683 | case TPGS_STATE_TRANSITIONING: |
| 684 | err = SCSI_DH_RETRY; |
| 685 | break; |
| 686 | default: |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | if (stpg) { |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 692 | h->callback_fn = fn; |
| 693 | h->callback_data = data; |
| 694 | err = submit_stpg(h); |
| 695 | if (err == SCSI_DH_OK) |
| 696 | return 0; |
| 697 | h->callback_fn = h->callback_data = NULL; |
| 698 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 699 | |
| 700 | out: |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 701 | if (fn) |
| 702 | fn(data, err); |
| 703 | return 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
| 707 | * alua_prep_fn - request callback |
| 708 | * |
| 709 | * Fail I/O to all paths not in state |
| 710 | * active/optimized or active/non-optimized. |
| 711 | */ |
| 712 | static int alua_prep_fn(struct scsi_device *sdev, struct request *req) |
| 713 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 714 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 715 | int ret = BLKPREP_OK; |
| 716 | |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 717 | if (h->state == TPGS_STATE_TRANSITIONING) |
| 718 | ret = BLKPREP_DEFER; |
| 719 | else if (h->state != TPGS_STATE_OPTIMIZED && |
| 720 | h->state != TPGS_STATE_NONOPTIMIZED && |
| 721 | h->state != TPGS_STATE_LBA_DEPENDENT) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 722 | ret = BLKPREP_KILL; |
| 723 | req->cmd_flags |= REQ_QUIET; |
| 724 | } |
| 725 | return ret; |
| 726 | |
| 727 | } |
| 728 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 729 | /* |
| 730 | * alua_bus_attach - Attach device handler |
| 731 | * @sdev: device to be attached to |
| 732 | */ |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 733 | static int alua_bus_attach(struct scsi_device *sdev) |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 734 | { |
| 735 | struct alua_dh_data *h; |
| 736 | int err; |
| 737 | |
| 738 | h = kzalloc(sizeof(*h) , GFP_KERNEL); |
| 739 | if (!h) |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 740 | return -ENOMEM; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 741 | h->tpgs = TPGS_MODE_UNINITIALIZED; |
| 742 | h->state = TPGS_STATE_OPTIMIZED; |
| 743 | h->group_id = -1; |
| 744 | h->rel_port = -1; |
| 745 | h->buff = h->inq; |
| 746 | h->bufflen = ALUA_INQUIRY_SIZE; |
| 747 | h->sdev = sdev; |
| 748 | |
| 749 | err = alua_initialize(sdev, h); |
| 750 | if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED) |
| 751 | goto failed; |
| 752 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 753 | sdev->handler_data = h; |
| 754 | return 0; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 755 | failed: |
| 756 | kfree(h); |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 757 | return -EINVAL; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* |
| 761 | * alua_bus_detach - Detach device handler |
| 762 | * @sdev: device to be detached from |
| 763 | */ |
| 764 | static void alua_bus_detach(struct scsi_device *sdev) |
| 765 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 766 | struct alua_dh_data *h = sdev->handler_data; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 767 | |
| 768 | if (h->buff && h->inq != h->buff) |
| 769 | kfree(h->buff); |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 770 | sdev->handler_data = NULL; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 771 | kfree(h); |
| 772 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 773 | |
| 774 | static struct scsi_device_handler alua_dh = { |
| 775 | .name = ALUA_DH_NAME, |
| 776 | .module = THIS_MODULE, |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 777 | .attach = alua_bus_attach, |
| 778 | .detach = alua_bus_detach, |
| 779 | .prep_fn = alua_prep_fn, |
| 780 | .check_sense = alua_check_sense, |
| 781 | .activate = alua_activate, |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 782 | .set_params = alua_set_params, |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 783 | }; |
| 784 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 785 | static int __init alua_init(void) |
| 786 | { |
| 787 | int r; |
| 788 | |
| 789 | r = scsi_register_device_handler(&alua_dh); |
| 790 | if (r != 0) |
| 791 | printk(KERN_ERR "%s: Failed to register scsi device handler", |
| 792 | ALUA_DH_NAME); |
| 793 | return r; |
| 794 | } |
| 795 | |
| 796 | static void __exit alua_exit(void) |
| 797 | { |
| 798 | scsi_unregister_device_handler(&alua_dh); |
| 799 | } |
| 800 | |
| 801 | module_init(alua_init); |
| 802 | module_exit(alua_exit); |
| 803 | |
| 804 | MODULE_DESCRIPTION("DM Multipath ALUA support"); |
| 805 | MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>"); |
| 806 | MODULE_LICENSE("GPL"); |
| 807 | MODULE_VERSION(ALUA_DH_VER); |