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