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