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 | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 172 | * submit_stpg - Issue a SET TARGET PORT GROUP command |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 173 | * |
| 174 | * Currently we're only setting the current target port group state |
| 175 | * to 'active/optimized' and let the array firmware figure out |
| 176 | * the states of the remaining groups. |
| 177 | */ |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 178 | static unsigned submit_stpg(struct scsi_device *sdev, int group_id, |
| 179 | unsigned char *sense) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 180 | { |
| 181 | struct request *rq; |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 182 | unsigned char stpg_data[8]; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 183 | int stpg_len = 8; |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 184 | int err = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 185 | |
| 186 | /* Prepare the data buffer */ |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 187 | memset(stpg_data, 0, stpg_len); |
| 188 | stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f; |
| 189 | put_unaligned_be16(group_id, &stpg_data[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 190 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 191 | rq = get_alua_req(sdev, stpg_data, stpg_len, WRITE); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 192 | if (!rq) |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 193 | return DRIVER_BUSY << 24; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 194 | |
| 195 | /* Prepare the command. */ |
| 196 | rq->cmd[0] = MAINTENANCE_OUT; |
| 197 | rq->cmd[1] = MO_SET_TARGET_PGS; |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 198 | put_unaligned_be32(stpg_len, &rq->cmd[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 199 | rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT); |
| 200 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 201 | rq->sense = sense; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 202 | memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE); |
Hannes Reinecke | d3692a3 | 2015-12-01 10:16:46 +0100 | [diff] [blame] | 203 | rq->sense_len = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 204 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 205 | blk_execute_rq(rq->q, NULL, rq, 1); |
| 206 | if (rq->errors) |
| 207 | err = rq->errors; |
| 208 | |
| 209 | blk_put_request(rq); |
| 210 | |
| 211 | return err; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Hannes Reinecke | d7c48fe | 2011-08-24 10:51:13 +0200 | [diff] [blame] | 215 | * alua_check_tpgs - Evaluate TPGS setting |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 216 | * @sdev: device to be checked |
| 217 | * |
Hannes Reinecke | d7c48fe | 2011-08-24 10:51:13 +0200 | [diff] [blame] | 218 | * Examine the TPGS setting of the sdev to find out if ALUA |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 219 | * is supported. |
| 220 | */ |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 221 | static int alua_check_tpgs(struct scsi_device *sdev) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 222 | { |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 223 | int tpgs = TPGS_MODE_NONE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 224 | |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 225 | /* |
| 226 | * ALUA support for non-disk devices is fraught with |
| 227 | * difficulties, so disable it for now. |
| 228 | */ |
| 229 | if (sdev->type != TYPE_DISK) { |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 230 | sdev_printk(KERN_INFO, sdev, |
| 231 | "%s: disable for non-disk devices\n", |
| 232 | ALUA_DH_NAME); |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 233 | return tpgs; |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 236 | tpgs = scsi_device_tpgs(sdev); |
| 237 | switch (tpgs) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 238 | case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT: |
| 239 | sdev_printk(KERN_INFO, sdev, |
| 240 | "%s: supports implicit and explicit TPGS\n", |
| 241 | ALUA_DH_NAME); |
| 242 | break; |
| 243 | case TPGS_MODE_EXPLICIT: |
| 244 | sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n", |
| 245 | ALUA_DH_NAME); |
| 246 | break; |
| 247 | case TPGS_MODE_IMPLICIT: |
| 248 | sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n", |
| 249 | ALUA_DH_NAME); |
| 250 | break; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 251 | case TPGS_MODE_NONE: |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 252 | sdev_printk(KERN_INFO, sdev, "%s: not supported\n", |
| 253 | ALUA_DH_NAME); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 254 | break; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 255 | default: |
| 256 | sdev_printk(KERN_INFO, sdev, |
| 257 | "%s: unsupported TPGS setting %d\n", |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 258 | ALUA_DH_NAME, tpgs); |
| 259 | tpgs = TPGS_MODE_NONE; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 260 | break; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 263 | return tpgs; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 267 | * alua_check_vpd - Evaluate INQUIRY vpd page 0x83 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 268 | * @sdev: device to be checked |
| 269 | * |
| 270 | * Extract the relative target port and the target port group |
| 271 | * descriptor from the list of identificators. |
| 272 | */ |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 273 | 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] | 274 | { |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 275 | int rel_port = -1, group_id; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 276 | |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 277 | group_id = scsi_vpd_tpg_id(sdev, &rel_port); |
| 278 | if (group_id < 0) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 279 | /* |
| 280 | * Internal error; TPGS supported but required |
| 281 | * VPD identification descriptors not present. |
| 282 | * Disable ALUA support |
| 283 | */ |
| 284 | sdev_printk(KERN_INFO, sdev, |
| 285 | "%s: No target port descriptors found\n", |
| 286 | ALUA_DH_NAME); |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 287 | return SCSI_DH_DEV_UNSUPP; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 288 | } |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 289 | h->state = TPGS_STATE_OPTIMIZED; |
| 290 | h->group_id = group_id; |
| 291 | |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 292 | sdev_printk(KERN_INFO, sdev, |
| 293 | "%s: port group %02x rel port %02x\n", |
| 294 | ALUA_DH_NAME, h->group_id, h->rel_port); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 295 | |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 296 | return 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static char print_alua_state(int state) |
| 300 | { |
| 301 | switch (state) { |
| 302 | case TPGS_STATE_OPTIMIZED: |
| 303 | return 'A'; |
| 304 | case TPGS_STATE_NONOPTIMIZED: |
| 305 | return 'N'; |
| 306 | case TPGS_STATE_STANDBY: |
| 307 | return 'S'; |
| 308 | case TPGS_STATE_UNAVAILABLE: |
| 309 | return 'U'; |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 310 | case TPGS_STATE_LBA_DEPENDENT: |
| 311 | return 'L'; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 312 | case TPGS_STATE_OFFLINE: |
| 313 | return 'O'; |
| 314 | case TPGS_STATE_TRANSITIONING: |
| 315 | return 'T'; |
| 316 | default: |
| 317 | return 'X'; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | static int alua_check_sense(struct scsi_device *sdev, |
| 322 | struct scsi_sense_hdr *sense_hdr) |
| 323 | { |
| 324 | switch (sense_hdr->sense_key) { |
| 325 | case NOT_READY: |
| 326 | if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) |
| 327 | /* |
| 328 | * LUN Not Accessible - ALUA state transition |
| 329 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 330 | return ADD_TO_MLQUEUE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 331 | break; |
| 332 | case UNIT_ATTENTION: |
| 333 | if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) |
| 334 | /* |
| 335 | * Power On, Reset, or Bus Device Reset, just retry. |
| 336 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 337 | return ADD_TO_MLQUEUE; |
Stewart, Sean | c20ee7b | 2013-10-15 15:52:39 +0000 | [diff] [blame] | 338 | if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04) |
| 339 | /* |
| 340 | * Device internal reset |
| 341 | */ |
| 342 | return ADD_TO_MLQUEUE; |
Moger, Babu | 410f02d | 2011-12-21 18:01:37 -0500 | [diff] [blame] | 343 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01) |
| 344 | /* |
| 345 | * Mode Parameters Changed |
| 346 | */ |
| 347 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 348 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 349 | /* |
| 350 | * ALUA state changed |
| 351 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 352 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 353 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 354 | /* |
| 355 | * Implicit ALUA state transition failed |
| 356 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 357 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 358 | if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03) |
| 359 | /* |
| 360 | * Inquiry data has changed |
| 361 | */ |
| 362 | return ADD_TO_MLQUEUE; |
| 363 | if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e) |
Ilgu Hong | 4d086f6 | 2009-01-30 17:00:11 -0600 | [diff] [blame] | 364 | /* |
| 365 | * REPORTED_LUNS_DATA_HAS_CHANGED is reported |
| 366 | * when switching controllers on targets like |
| 367 | * Intel Multi-Flex. We can just retry. |
| 368 | */ |
| 369 | return ADD_TO_MLQUEUE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | |
| 373 | return SCSI_RETURN_NOT_HANDLED; |
| 374 | } |
| 375 | |
| 376 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 377 | * alua_rtpg - Evaluate REPORT TARGET GROUP STATES |
| 378 | * @sdev: the device to be evaluated. |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 379 | * @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] | 380 | * |
| 381 | * Evaluate the Target Port Group State. |
| 382 | * Returns SCSI_DH_DEV_OFFLINED if the path is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 383 | * found to be unusable. |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 384 | */ |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 385 | 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] | 386 | { |
| 387 | struct scsi_sense_hdr sense_hdr; |
| 388 | int len, k, off, valid_states = 0; |
Hannes Reinecke | cfde3fa | 2011-08-24 10:51:18 +0200 | [diff] [blame] | 389 | unsigned char *ucp; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 390 | unsigned err, retval; |
Rob Evers | bc97f4b | 2012-05-18 14:08:56 -0400 | [diff] [blame] | 391 | unsigned long expiry, interval = 0; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 392 | unsigned int tpg_desc_tbl_off; |
| 393 | unsigned char orig_transition_tmo; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 394 | |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 395 | if (!h->transition_tmo) |
| 396 | expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ); |
| 397 | else |
| 398 | expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ); |
| 399 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 400 | retry: |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame] | 401 | retval = submit_rtpg(sdev, h->buff, h->bufflen, h->sense, h->flags); |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 402 | if (retval) { |
Hannes Reinecke | d3692a3 | 2015-12-01 10:16:46 +0100 | [diff] [blame] | 403 | if (!scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE, |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 404 | &sense_hdr)) { |
| 405 | sdev_printk(KERN_INFO, sdev, |
| 406 | "%s: rtpg failed, result %d\n", |
| 407 | ALUA_DH_NAME, retval); |
| 408 | if (driver_byte(retval) == DRIVER_BUSY) |
| 409 | return SCSI_DH_DEV_TEMP_BUSY; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 410 | return SCSI_DH_IO; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 411 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 412 | |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 413 | /* |
| 414 | * submit_rtpg() has failed on existing arrays |
| 415 | * when requesting extended header info, and |
| 416 | * the array doesn't support extended headers, |
| 417 | * even though it shouldn't according to T10. |
| 418 | * The retry without rtpg_ext_hdr_req set |
| 419 | * handles this. |
| 420 | */ |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 421 | if (!(h->flags & ALUA_RTPG_EXT_HDR_UNSUPP) && |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 422 | sense_hdr.sense_key == ILLEGAL_REQUEST && |
| 423 | sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) { |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 424 | h->flags |= ALUA_RTPG_EXT_HDR_UNSUPP; |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 425 | goto retry; |
| 426 | } |
Hannes Reinecke | e2d817d | 2015-12-01 10:16:54 +0100 | [diff] [blame] | 427 | /* |
| 428 | * Retry on ALUA state transition or if any |
| 429 | * UNIT ATTENTION occurred. |
| 430 | */ |
| 431 | if (sense_hdr.sense_key == NOT_READY && |
| 432 | sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) |
| 433 | err = SCSI_DH_RETRY; |
| 434 | else if (sense_hdr.sense_key == UNIT_ATTENTION) |
| 435 | err = SCSI_DH_RETRY; |
| 436 | if (err == SCSI_DH_RETRY && time_before(jiffies, expiry)) { |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 437 | sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n", |
| 438 | ALUA_DH_NAME); |
| 439 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 440 | goto retry; |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 441 | } |
| 442 | sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n", |
| 443 | ALUA_DH_NAME); |
| 444 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
| 445 | return SCSI_DH_IO; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 446 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 447 | |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 448 | len = get_unaligned_be32(&h->buff[0]) + 4; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 449 | |
| 450 | if (len > h->bufflen) { |
| 451 | /* Resubmit with the correct length */ |
| 452 | if (realloc_buffer(h, len)) { |
| 453 | sdev_printk(KERN_WARNING, sdev, |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 454 | "%s: kmalloc buffer failed\n",__func__); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 455 | /* Temporary failure, bypass */ |
| 456 | return SCSI_DH_DEV_TEMP_BUSY; |
| 457 | } |
| 458 | goto retry; |
| 459 | } |
| 460 | |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 461 | orig_transition_tmo = h->transition_tmo; |
| 462 | if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0) |
| 463 | h->transition_tmo = h->buff[5]; |
| 464 | else |
| 465 | h->transition_tmo = ALUA_FAILOVER_TIMEOUT; |
| 466 | |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 467 | if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) { |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 468 | sdev_printk(KERN_INFO, sdev, |
| 469 | "%s: transition timeout set to %d seconds\n", |
| 470 | ALUA_DH_NAME, h->transition_tmo); |
| 471 | expiry = jiffies + h->transition_tmo * HZ; |
| 472 | } |
| 473 | |
| 474 | if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR) |
| 475 | tpg_desc_tbl_off = 8; |
| 476 | else |
| 477 | tpg_desc_tbl_off = 4; |
| 478 | |
| 479 | for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off; |
| 480 | k < len; |
| 481 | k += off, ucp += off) { |
| 482 | |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 483 | if (h->group_id == get_unaligned_be16(&ucp[2])) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 484 | h->state = ucp[0] & 0x0f; |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 485 | h->pref = ucp[0] >> 7; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 486 | valid_states = ucp[1]; |
| 487 | } |
| 488 | off = 8 + (ucp[7] * 4); |
| 489 | } |
| 490 | |
| 491 | sdev_printk(KERN_INFO, sdev, |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 492 | "%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] | 493 | ALUA_DH_NAME, h->group_id, print_alua_state(h->state), |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 494 | h->pref ? "preferred" : "non-preferred", |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 495 | valid_states&TPGS_SUPPORT_TRANSITION?'T':'t', |
| 496 | valid_states&TPGS_SUPPORT_OFFLINE?'O':'o', |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 497 | valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l', |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 498 | valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u', |
| 499 | valid_states&TPGS_SUPPORT_STANDBY?'S':'s', |
| 500 | valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n', |
| 501 | valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a'); |
| 502 | |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 503 | switch (h->state) { |
| 504 | case TPGS_STATE_TRANSITIONING: |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 505 | if (wait_for_transition) { |
| 506 | if (time_before(jiffies, expiry)) { |
| 507 | /* State transition, retry */ |
| 508 | interval += 2000; |
| 509 | msleep(interval); |
| 510 | goto retry; |
| 511 | } |
| 512 | err = SCSI_DH_RETRY; |
| 513 | } else { |
| 514 | err = SCSI_DH_OK; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 515 | } |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 516 | |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 517 | /* Transitioning time exceeded, set port to standby */ |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 518 | h->state = TPGS_STATE_STANDBY; |
| 519 | break; |
| 520 | case TPGS_STATE_OFFLINE: |
Bart Van Assche | e47f897 | 2012-08-24 09:08:41 +0000 | [diff] [blame] | 521 | /* Path unusable */ |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 522 | err = SCSI_DH_DEV_OFFLINED; |
| 523 | break; |
| 524 | default: |
| 525 | /* Useable path if active */ |
| 526 | err = SCSI_DH_OK; |
| 527 | break; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 528 | } |
| 529 | return err; |
| 530 | } |
| 531 | |
| 532 | /* |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 533 | * alua_stpg - Issue a SET TARGET PORT GROUP command |
| 534 | * |
| 535 | * Issue a SET TARGET PORT GROUP command and evaluate the |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 536 | * response. Returns SCSI_DH_RETRY per default to trigger |
| 537 | * a re-evaluation of the target group state or SCSI_DH_OK |
| 538 | * if no further action needs to be taken. |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 539 | */ |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 540 | static unsigned alua_stpg(struct scsi_device *sdev, struct alua_dh_data *h) |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 541 | { |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 542 | int retval; |
| 543 | struct scsi_sense_hdr sense_hdr; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 544 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 545 | if (!(h->tpgs & TPGS_MODE_EXPLICIT)) { |
| 546 | /* Only implicit ALUA supported, retry */ |
| 547 | return SCSI_DH_RETRY; |
| 548 | } |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 549 | switch (h->state) { |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 550 | case TPGS_STATE_OPTIMIZED: |
| 551 | return SCSI_DH_OK; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 552 | case TPGS_STATE_NONOPTIMIZED: |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 553 | if ((h->flags & ALUA_OPTIMIZE_STPG) && |
| 554 | !h->pref && |
| 555 | (h->tpgs & TPGS_MODE_IMPLICIT)) |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 556 | return SCSI_DH_OK; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 557 | break; |
| 558 | case TPGS_STATE_STANDBY: |
| 559 | case TPGS_STATE_UNAVAILABLE: |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 560 | break; |
| 561 | case TPGS_STATE_OFFLINE: |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 562 | return SCSI_DH_IO; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 563 | case TPGS_STATE_TRANSITIONING: |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 564 | break; |
| 565 | default: |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 566 | sdev_printk(KERN_INFO, sdev, |
| 567 | "%s: stpg failed, unhandled TPGS state %d", |
| 568 | ALUA_DH_NAME, h->state); |
| 569 | return SCSI_DH_NOSYS; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 570 | } |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 571 | retval = submit_stpg(sdev, h->group_id, h->sense); |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 572 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 573 | if (retval) { |
| 574 | if (!scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE, |
| 575 | &sense_hdr)) { |
| 576 | sdev_printk(KERN_INFO, sdev, |
| 577 | "%s: stpg failed, result %d", |
| 578 | ALUA_DH_NAME, retval); |
| 579 | if (driver_byte(retval) == DRIVER_BUSY) |
| 580 | return SCSI_DH_DEV_TEMP_BUSY; |
| 581 | } else { |
| 582 | sdev_printk(KERN_INFO, h->sdev, "%s: stpg failed\n", |
| 583 | ALUA_DH_NAME); |
| 584 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
| 585 | } |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 586 | } |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 587 | /* Retry RTPG */ |
| 588 | return SCSI_DH_RETRY; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 592 | * alua_initialize - Initialize ALUA state |
| 593 | * @sdev: the device to be initialized |
| 594 | * |
| 595 | * For the prep_fn to work correctly we have |
| 596 | * to initialize the ALUA state for the device. |
| 597 | */ |
| 598 | static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h) |
| 599 | { |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 600 | int err = SCSI_DH_DEV_UNSUPP; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 601 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 602 | h->tpgs = alua_check_tpgs(sdev); |
| 603 | if (h->tpgs == TPGS_MODE_NONE) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 604 | goto out; |
| 605 | |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 606 | err = alua_check_vpd(sdev, h); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 607 | if (err != SCSI_DH_OK) |
| 608 | goto out; |
| 609 | |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 610 | err = alua_rtpg(sdev, h, 0); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 611 | if (err != SCSI_DH_OK) |
| 612 | goto out; |
| 613 | |
| 614 | out: |
| 615 | return err; |
| 616 | } |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 617 | /* |
| 618 | * alua_set_params - set/unset the optimize flag |
| 619 | * @sdev: device on the path to be activated |
| 620 | * params - parameters in the following format |
| 621 | * "no_of_params\0param1\0param2\0param3\0...\0" |
| 622 | * For example, to set the flag pass the following parameters |
| 623 | * from multipath.conf |
| 624 | * hardware_handler "2 alua 1" |
| 625 | */ |
| 626 | static int alua_set_params(struct scsi_device *sdev, const char *params) |
| 627 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 628 | struct alua_dh_data *h = sdev->handler_data; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 629 | unsigned int optimize = 0, argc; |
| 630 | const char *p = params; |
| 631 | int result = SCSI_DH_OK; |
| 632 | |
| 633 | if ((sscanf(params, "%u", &argc) != 1) || (argc != 1)) |
| 634 | return -EINVAL; |
| 635 | |
| 636 | while (*p++) |
| 637 | ; |
| 638 | if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1)) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | if (optimize) |
| 642 | h->flags |= ALUA_OPTIMIZE_STPG; |
| 643 | else |
| 644 | h->flags &= ~ALUA_OPTIMIZE_STPG; |
| 645 | |
| 646 | return result; |
| 647 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 648 | |
Stewart, Sean | 7a3ad39 | 2013-04-04 14:54:47 +0000 | [diff] [blame] | 649 | static uint optimize_stpg; |
| 650 | module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR); |
| 651 | 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."); |
| 652 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 653 | /* |
| 654 | * alua_activate - activate a path |
| 655 | * @sdev: device on the path to be activated |
| 656 | * |
| 657 | * We're currently switching the port group to be activated only and |
| 658 | * let the array figure out the rest. |
| 659 | * There may be other arrays which require us to switch all port groups |
| 660 | * based on a certain policy. But until we actually encounter them it |
| 661 | * should be okay. |
| 662 | */ |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 663 | static int alua_activate(struct scsi_device *sdev, |
| 664 | activate_complete fn, void *data) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 665 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 666 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 667 | int err = SCSI_DH_OK; |
| 668 | |
Stewart, Sean | a8e5a2d | 2013-10-15 15:52:54 +0000 | [diff] [blame] | 669 | err = alua_rtpg(sdev, h, 1); |
Hannes Reinecke | 46ccf6b | 2011-08-24 10:51:16 +0200 | [diff] [blame] | 670 | if (err != SCSI_DH_OK) |
| 671 | goto out; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 672 | |
Stewart, Sean | 7a3ad39 | 2013-04-04 14:54:47 +0000 | [diff] [blame] | 673 | if (optimize_stpg) |
| 674 | h->flags |= ALUA_OPTIMIZE_STPG; |
| 675 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 676 | err = alua_stpg(sdev, h); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 677 | out: |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame^] | 678 | if (fn) |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 679 | fn(data, err); |
| 680 | return 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* |
| 684 | * alua_prep_fn - request callback |
| 685 | * |
| 686 | * Fail I/O to all paths not in state |
| 687 | * active/optimized or active/non-optimized. |
| 688 | */ |
| 689 | static int alua_prep_fn(struct scsi_device *sdev, struct request *req) |
| 690 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 691 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 692 | int ret = BLKPREP_OK; |
| 693 | |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 694 | if (h->state == TPGS_STATE_TRANSITIONING) |
| 695 | ret = BLKPREP_DEFER; |
| 696 | else if (h->state != TPGS_STATE_OPTIMIZED && |
| 697 | h->state != TPGS_STATE_NONOPTIMIZED && |
| 698 | h->state != TPGS_STATE_LBA_DEPENDENT) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 699 | ret = BLKPREP_KILL; |
| 700 | req->cmd_flags |= REQ_QUIET; |
| 701 | } |
| 702 | return ret; |
| 703 | |
| 704 | } |
| 705 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 706 | /* |
| 707 | * alua_bus_attach - Attach device handler |
| 708 | * @sdev: device to be attached to |
| 709 | */ |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 710 | static int alua_bus_attach(struct scsi_device *sdev) |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 711 | { |
| 712 | struct alua_dh_data *h; |
| 713 | int err; |
| 714 | |
| 715 | h = kzalloc(sizeof(*h) , GFP_KERNEL); |
| 716 | if (!h) |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 717 | return -ENOMEM; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 718 | h->tpgs = TPGS_MODE_UNINITIALIZED; |
| 719 | h->state = TPGS_STATE_OPTIMIZED; |
| 720 | h->group_id = -1; |
| 721 | h->rel_port = -1; |
| 722 | h->buff = h->inq; |
| 723 | h->bufflen = ALUA_INQUIRY_SIZE; |
| 724 | h->sdev = sdev; |
| 725 | |
| 726 | err = alua_initialize(sdev, h); |
| 727 | if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED) |
| 728 | goto failed; |
| 729 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 730 | sdev->handler_data = h; |
| 731 | return 0; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 732 | failed: |
| 733 | kfree(h); |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 734 | return -EINVAL; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* |
| 738 | * alua_bus_detach - Detach device handler |
| 739 | * @sdev: device to be detached from |
| 740 | */ |
| 741 | static void alua_bus_detach(struct scsi_device *sdev) |
| 742 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 743 | struct alua_dh_data *h = sdev->handler_data; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 744 | |
| 745 | if (h->buff && h->inq != h->buff) |
| 746 | kfree(h->buff); |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 747 | sdev->handler_data = NULL; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 748 | kfree(h); |
| 749 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 750 | |
| 751 | static struct scsi_device_handler alua_dh = { |
| 752 | .name = ALUA_DH_NAME, |
| 753 | .module = THIS_MODULE, |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 754 | .attach = alua_bus_attach, |
| 755 | .detach = alua_bus_detach, |
| 756 | .prep_fn = alua_prep_fn, |
| 757 | .check_sense = alua_check_sense, |
| 758 | .activate = alua_activate, |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 759 | .set_params = alua_set_params, |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 760 | }; |
| 761 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 762 | static int __init alua_init(void) |
| 763 | { |
| 764 | int r; |
| 765 | |
| 766 | r = scsi_register_device_handler(&alua_dh); |
| 767 | if (r != 0) |
| 768 | printk(KERN_ERR "%s: Failed to register scsi device handler", |
| 769 | ALUA_DH_NAME); |
| 770 | return r; |
| 771 | } |
| 772 | |
| 773 | static void __exit alua_exit(void) |
| 774 | { |
| 775 | scsi_unregister_device_handler(&alua_dh); |
| 776 | } |
| 777 | |
| 778 | module_init(alua_init); |
| 779 | module_exit(alua_exit); |
| 780 | |
| 781 | MODULE_DESCRIPTION("DM Multipath ALUA support"); |
| 782 | MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>"); |
| 783 | MODULE_LICENSE("GPL"); |
| 784 | MODULE_VERSION(ALUA_DH_VER); |