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