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