blob: a6fe3ae50c2556589f136601492c2271f582e9bf [file] [log] [blame]
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07001/*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
Hannes Reinecke69723d12010-09-24 15:57:04 +02004 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07005 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Hannes Reinecke69723d12010-09-24 15:57:04 +020023#include <linux/delay.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040024#include <linux/module.h>
Hannes Reineckea7089772015-12-01 10:16:52 +010025#include <asm/unaligned.h>
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070026#include <scsi/scsi.h>
Hannes Reinecke80bd68d2015-12-01 10:16:47 +010027#include <scsi/scsi_dbg.h>
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070028#include <scsi/scsi_eh.h>
29#include <scsi/scsi_dh.h>
30
31#define ALUA_DH_NAME "alua"
Hannes Reinecke69723d12010-09-24 15:57:04 +020032#define ALUA_DH_VER "1.3"
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070033
34#define TPGS_STATE_OPTIMIZED 0x0
35#define TPGS_STATE_NONOPTIMIZED 0x1
36#define TPGS_STATE_STANDBY 0x2
37#define TPGS_STATE_UNAVAILABLE 0x3
Hannes Reinecke69723d12010-09-24 15:57:04 +020038#define TPGS_STATE_LBA_DEPENDENT 0x4
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070039#define TPGS_STATE_OFFLINE 0xe
40#define TPGS_STATE_TRANSITIONING 0xf
41
42#define TPGS_SUPPORT_NONE 0x00
43#define TPGS_SUPPORT_OPTIMIZED 0x01
44#define TPGS_SUPPORT_NONOPTIMIZED 0x02
45#define TPGS_SUPPORT_STANDBY 0x04
46#define TPGS_SUPPORT_UNAVAILABLE 0x08
Hannes Reinecke69723d12010-09-24 15:57:04 +020047#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070048#define TPGS_SUPPORT_OFFLINE 0x40
49#define TPGS_SUPPORT_TRANSITION 0x80
50
Rob Evers3588c5a2012-05-18 14:08:54 -040051#define RTPG_FMT_MASK 0x70
52#define RTPG_FMT_EXT_HDR 0x10
53
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070054#define TPGS_MODE_UNINITIALIZED -1
55#define TPGS_MODE_NONE 0x0
56#define TPGS_MODE_IMPLICIT 0x1
57#define TPGS_MODE_EXPLICIT 0x2
58
Hannes Reineckec49c8342016-02-19 09:17:03 +010059#define ALUA_RTPG_SIZE 128
Rob Evers3588c5a2012-05-18 14:08:54 -040060#define ALUA_FAILOVER_TIMEOUT 60
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070061#define ALUA_FAILOVER_RETRIES 5
62
Hannes Reinecke6c4fc042015-12-01 10:16:51 +010063/* device handler flags */
Moger, Babu4335d092012-03-27 20:55:49 +000064#define ALUA_OPTIMIZE_STPG 1
Hannes Reinecke6c4fc042015-12-01 10:16:51 +010065#define ALUA_RTPG_EXT_HDR_UNSUPP 2
Moger, Babu4335d092012-03-27 20:55:49 +000066
Hannes Reinecke43394c62016-02-19 09:17:04 +010067static LIST_HEAD(port_group_list);
68static DEFINE_SPINLOCK(port_group_lock);
69
70struct alua_port_group {
71 struct kref kref;
72 struct list_head node;
Hannes Reinecke00472202016-02-19 09:17:05 +010073 unsigned char device_id_str[256];
74 int device_id_len;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070075 int group_id;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070076 int tpgs;
77 int state;
Moger, Babudcd3a752012-03-27 20:56:08 +000078 int pref;
Moger, Babu4335d092012-03-27 20:55:49 +000079 unsigned flags; /* used for optimizing STPG */
Rob Evers3588c5a2012-05-18 14:08:54 -040080 unsigned char transition_tmo;
Hannes Reinecke43394c62016-02-19 09:17:04 +010081};
82
83struct alua_dh_data {
84 struct alua_port_group *pg;
85 int group_id;
86 int rel_port;
Chandra Seetharaman96e65862009-10-21 09:23:04 -070087 struct scsi_device *sdev;
88 activate_complete callback_fn;
89 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070090};
91
92#define ALUA_POLICY_SWITCH_CURRENT 0
93#define ALUA_POLICY_SWITCH_ALL 1
94
Hannes Reineckea4253fd2016-02-19 09:17:06 +010095static int alua_rtpg(struct scsi_device *, struct alua_port_group *, int);
Chandra Seetharaman96e65862009-10-21 09:23:04 -070096static char print_alua_state(int);
Chandra Seetharaman96e65862009-10-21 09:23:04 -070097
Hannes Reinecke43394c62016-02-19 09:17:04 +010098static void release_port_group(struct kref *kref)
99{
100 struct alua_port_group *pg;
101
102 pg = container_of(kref, struct alua_port_group, kref);
103 spin_lock(&port_group_lock);
104 list_del(&pg->node);
105 spin_unlock(&port_group_lock);
106 kfree(pg);
107}
108
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700109/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700110 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
111 * @sdev: sdev the command should be sent to
112 */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100113static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
114 int bufflen, struct scsi_sense_hdr *sshdr, int flags)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700115{
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100116 u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
117 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
118 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700119
120 /* Prepare the command. */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100121 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN));
122 cdb[0] = MAINTENANCE_IN;
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100123 if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100124 cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
Rob Evers8e67ce62012-05-18 14:08:55 -0400125 else
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100126 cdb[1] = MI_REPORT_TARGET_PGS;
127 put_unaligned_be32(bufflen, &cdb[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700128
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100129 return scsi_execute_req_flags(sdev, cdb, DMA_FROM_DEVICE,
130 buff, bufflen, sshdr,
131 ALUA_FAILOVER_TIMEOUT * HZ,
132 ALUA_FAILOVER_RETRIES, NULL, req_flags);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700133}
134
135/*
Hannes Reineckeb2460752016-02-19 09:17:00 +0100136 * submit_stpg - Issue a SET TARGET PORT GROUP command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700137 *
138 * Currently we're only setting the current target port group state
139 * to 'active/optimized' and let the array firmware figure out
140 * the states of the remaining groups.
141 */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100142static int submit_stpg(struct scsi_device *sdev, int group_id,
143 struct scsi_sense_hdr *sshdr)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700144{
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100145 u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)];
Hannes Reineckeb2460752016-02-19 09:17:00 +0100146 unsigned char stpg_data[8];
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700147 int stpg_len = 8;
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100148 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
149 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700150
151 /* Prepare the data buffer */
Hannes Reineckeb2460752016-02-19 09:17:00 +0100152 memset(stpg_data, 0, stpg_len);
153 stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f;
154 put_unaligned_be16(group_id, &stpg_data[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700155
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700156 /* Prepare the command. */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100157 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_OUT));
158 cdb[0] = MAINTENANCE_OUT;
159 cdb[1] = MO_SET_TARGET_PGS;
160 put_unaligned_be32(stpg_len, &cdb[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700161
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100162 return scsi_execute_req_flags(sdev, cdb, DMA_TO_DEVICE,
163 stpg_data, stpg_len,
164 sshdr, ALUA_FAILOVER_TIMEOUT * HZ,
165 ALUA_FAILOVER_RETRIES, NULL, req_flags);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700166}
167
Hannes Reinecke00472202016-02-19 09:17:05 +0100168struct alua_port_group *alua_find_get_pg(char *id_str, size_t id_size,
169 int group_id)
170{
171 struct alua_port_group *pg;
172
173 list_for_each_entry(pg, &port_group_list, node) {
174 if (pg->group_id != group_id)
175 continue;
176 if (pg->device_id_len != id_size)
177 continue;
178 if (strncmp(pg->device_id_str, id_str, id_size))
179 continue;
180 if (!kref_get_unless_zero(&pg->kref))
181 continue;
182 return pg;
183 }
184
185 return NULL;
186}
187
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700188/*
Hannes Reinecke43394c62016-02-19 09:17:04 +0100189 * alua_alloc_pg - Allocate a new port_group structure
190 * @sdev: scsi device
191 * @h: alua device_handler data
192 * @group_id: port group id
193 *
194 * Allocate a new port_group structure for a given
195 * device.
196 */
197struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
198 int group_id, int tpgs)
199{
Hannes Reinecke00472202016-02-19 09:17:05 +0100200 struct alua_port_group *pg, *tmp_pg;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100201
202 pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
203 if (!pg)
Hannes Reinecke00472202016-02-19 09:17:05 +0100204 return ERR_PTR(-ENOMEM);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100205
Hannes Reinecke00472202016-02-19 09:17:05 +0100206 pg->device_id_len = scsi_vpd_lun_id(sdev, pg->device_id_str,
207 sizeof(pg->device_id_str));
208 if (pg->device_id_len <= 0) {
209 /*
210 * Internal error: TPGS supported but no device
211 * identifcation found. Disable ALUA support.
212 */
213 kfree(pg);
214 sdev_printk(KERN_INFO, sdev,
215 "%s: No device descriptors found\n",
216 ALUA_DH_NAME);
217 return ERR_PTR(-ENXIO);
218 }
Hannes Reinecke43394c62016-02-19 09:17:04 +0100219 pg->group_id = group_id;
220 pg->tpgs = tpgs;
221 pg->state = TPGS_STATE_OPTIMIZED;
222 kref_init(&pg->kref);
Hannes Reinecke00472202016-02-19 09:17:05 +0100223
Hannes Reinecke43394c62016-02-19 09:17:04 +0100224 spin_lock(&port_group_lock);
Hannes Reinecke00472202016-02-19 09:17:05 +0100225 tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
226 group_id);
227 if (tmp_pg) {
228 spin_unlock(&port_group_lock);
229 kfree(pg);
230 return tmp_pg;
231 }
232
Hannes Reinecke43394c62016-02-19 09:17:04 +0100233 list_add(&pg->node, &port_group_list);
234 spin_unlock(&port_group_lock);
235
236 return pg;
237}
238
239/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200240 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700241 * @sdev: device to be checked
242 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200243 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700244 * is supported.
245 */
Hannes Reineckead0ea642015-12-01 10:16:53 +0100246static int alua_check_tpgs(struct scsi_device *sdev)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700247{
Hannes Reineckead0ea642015-12-01 10:16:53 +0100248 int tpgs = TPGS_MODE_NONE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700249
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100250 /*
251 * ALUA support for non-disk devices is fraught with
252 * difficulties, so disable it for now.
253 */
254 if (sdev->type != TYPE_DISK) {
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100255 sdev_printk(KERN_INFO, sdev,
256 "%s: disable for non-disk devices\n",
257 ALUA_DH_NAME);
Hannes Reineckead0ea642015-12-01 10:16:53 +0100258 return tpgs;
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100259 }
260
Hannes Reineckead0ea642015-12-01 10:16:53 +0100261 tpgs = scsi_device_tpgs(sdev);
262 switch (tpgs) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700263 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
264 sdev_printk(KERN_INFO, sdev,
265 "%s: supports implicit and explicit TPGS\n",
266 ALUA_DH_NAME);
267 break;
268 case TPGS_MODE_EXPLICIT:
269 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
270 ALUA_DH_NAME);
271 break;
272 case TPGS_MODE_IMPLICIT:
273 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
274 ALUA_DH_NAME);
275 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100276 case TPGS_MODE_NONE:
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700277 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
278 ALUA_DH_NAME);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700279 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100280 default:
281 sdev_printk(KERN_INFO, sdev,
282 "%s: unsupported TPGS setting %d\n",
Hannes Reineckead0ea642015-12-01 10:16:53 +0100283 ALUA_DH_NAME, tpgs);
284 tpgs = TPGS_MODE_NONE;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100285 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700286 }
287
Hannes Reineckead0ea642015-12-01 10:16:53 +0100288 return tpgs;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700289}
290
291/*
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100292 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700293 * @sdev: device to be checked
294 *
295 * Extract the relative target port and the target port group
296 * descriptor from the list of identificators.
297 */
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100298static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
299 int tpgs)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700300{
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100301 int rel_port = -1, group_id;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700302
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100303 group_id = scsi_vpd_tpg_id(sdev, &rel_port);
304 if (group_id < 0) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700305 /*
306 * Internal error; TPGS supported but required
307 * VPD identification descriptors not present.
308 * Disable ALUA support
309 */
310 sdev_printk(KERN_INFO, sdev,
311 "%s: No target port descriptors found\n",
312 ALUA_DH_NAME);
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100313 return SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700314 }
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100315
316 h->pg = alua_alloc_pg(sdev, group_id, tpgs);
317 if (IS_ERR(h->pg)) {
318 if (PTR_ERR(h->pg) == -ENOMEM)
319 return SCSI_DH_NOMEM;
320 return SCSI_DH_DEV_UNSUPP;
321 }
322 h->rel_port = rel_port;
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100323
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100324 sdev_printk(KERN_INFO, sdev,
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100325 "%s: device %s port group %x rel port %x\n",
326 ALUA_DH_NAME, h->pg->device_id_str,
327 h->group_id, h->rel_port);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700328
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100329 return alua_rtpg(sdev, h->pg, 0);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700330}
331
332static char print_alua_state(int state)
333{
334 switch (state) {
335 case TPGS_STATE_OPTIMIZED:
336 return 'A';
337 case TPGS_STATE_NONOPTIMIZED:
338 return 'N';
339 case TPGS_STATE_STANDBY:
340 return 'S';
341 case TPGS_STATE_UNAVAILABLE:
342 return 'U';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200343 case TPGS_STATE_LBA_DEPENDENT:
344 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700345 case TPGS_STATE_OFFLINE:
346 return 'O';
347 case TPGS_STATE_TRANSITIONING:
348 return 'T';
349 default:
350 return 'X';
351 }
352}
353
354static int alua_check_sense(struct scsi_device *sdev,
355 struct scsi_sense_hdr *sense_hdr)
356{
357 switch (sense_hdr->sense_key) {
358 case NOT_READY:
359 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
360 /*
361 * LUN Not Accessible - ALUA state transition
362 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700363 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700364 break;
365 case UNIT_ATTENTION:
366 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
367 /*
368 * Power On, Reset, or Bus Device Reset, just retry.
369 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700370 return ADD_TO_MLQUEUE;
Stewart, Seanc20ee7b2013-10-15 15:52:39 +0000371 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
372 /*
373 * Device internal reset
374 */
375 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500376 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
377 /*
378 * Mode Parameters Changed
379 */
380 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400381 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700382 /*
383 * ALUA state changed
384 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700385 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400386 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700387 /*
388 * Implicit ALUA state transition failed
389 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700390 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400391 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
392 /*
393 * Inquiry data has changed
394 */
395 return ADD_TO_MLQUEUE;
396 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600397 /*
398 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
399 * when switching controllers on targets like
400 * Intel Multi-Flex. We can just retry.
401 */
402 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700403 break;
404 }
405
406 return SCSI_RETURN_NOT_HANDLED;
407}
408
409/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700410 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
411 * @sdev: the device to be evaluated.
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000412 * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700413 *
414 * Evaluate the Target Port Group State.
415 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300416 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700417 */
Hannes Reinecke43394c62016-02-19 09:17:04 +0100418static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg, int wait_for_transition)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700419{
420 struct scsi_sense_hdr sense_hdr;
Hannes Reineckec49c8342016-02-19 09:17:03 +0100421 int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE;
422 unsigned char *ucp, *buff;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100423 unsigned err, retval;
Rob Eversbc97f4b2012-05-18 14:08:56 -0400424 unsigned long expiry, interval = 0;
Rob Evers3588c5a2012-05-18 14:08:54 -0400425 unsigned int tpg_desc_tbl_off;
426 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700427
Hannes Reinecke43394c62016-02-19 09:17:04 +0100428 if (!pg->transition_tmo)
Rob Evers3588c5a2012-05-18 14:08:54 -0400429 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
430 else
Hannes Reinecke43394c62016-02-19 09:17:04 +0100431 expiry = round_jiffies_up(jiffies + pg->transition_tmo * HZ);
Rob Evers3588c5a2012-05-18 14:08:54 -0400432
Hannes Reineckec49c8342016-02-19 09:17:03 +0100433 buff = kzalloc(bufflen, GFP_KERNEL);
434 if (!buff)
435 return SCSI_DH_DEV_TEMP_BUSY;
436
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700437 retry:
Hannes Reinecke43394c62016-02-19 09:17:04 +0100438 retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100439
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100440 if (retval) {
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100441 if (!scsi_sense_valid(&sense_hdr)) {
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100442 sdev_printk(KERN_INFO, sdev,
443 "%s: rtpg failed, result %d\n",
444 ALUA_DH_NAME, retval);
Hannes Reineckec49c8342016-02-19 09:17:03 +0100445 kfree(buff);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100446 if (driver_byte(retval) == DRIVER_ERROR)
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100447 return SCSI_DH_DEV_TEMP_BUSY;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700448 return SCSI_DH_IO;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100449 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700450
Rob Evers8e67ce62012-05-18 14:08:55 -0400451 /*
452 * submit_rtpg() has failed on existing arrays
453 * when requesting extended header info, and
454 * the array doesn't support extended headers,
455 * even though it shouldn't according to T10.
456 * The retry without rtpg_ext_hdr_req set
457 * handles this.
458 */
Hannes Reinecke43394c62016-02-19 09:17:04 +0100459 if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
Rob Evers8e67ce62012-05-18 14:08:55 -0400460 sense_hdr.sense_key == ILLEGAL_REQUEST &&
461 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
Hannes Reinecke43394c62016-02-19 09:17:04 +0100462 pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
Rob Evers8e67ce62012-05-18 14:08:55 -0400463 goto retry;
464 }
Hannes Reineckee2d817d2015-12-01 10:16:54 +0100465 /*
466 * Retry on ALUA state transition or if any
467 * UNIT ATTENTION occurred.
468 */
469 if (sense_hdr.sense_key == NOT_READY &&
470 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
471 err = SCSI_DH_RETRY;
472 else if (sense_hdr.sense_key == UNIT_ATTENTION)
473 err = SCSI_DH_RETRY;
474 if (err == SCSI_DH_RETRY && time_before(jiffies, expiry)) {
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100475 sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
476 ALUA_DH_NAME);
477 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700478 goto retry;
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100479 }
480 sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
481 ALUA_DH_NAME);
482 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
Hannes Reineckec49c8342016-02-19 09:17:03 +0100483 kfree(buff);
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100484 return SCSI_DH_IO;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700485 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700486
Hannes Reineckec49c8342016-02-19 09:17:03 +0100487 len = get_unaligned_be32(&buff[0]) + 4;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700488
Hannes Reineckec49c8342016-02-19 09:17:03 +0100489 if (len > bufflen) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700490 /* Resubmit with the correct length */
Hannes Reineckec49c8342016-02-19 09:17:03 +0100491 kfree(buff);
492 bufflen = len;
493 buff = kmalloc(bufflen, GFP_KERNEL);
494 if (!buff) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700495 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700496 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700497 /* Temporary failure, bypass */
498 return SCSI_DH_DEV_TEMP_BUSY;
499 }
500 goto retry;
501 }
502
Hannes Reinecke43394c62016-02-19 09:17:04 +0100503 orig_transition_tmo = pg->transition_tmo;
Hannes Reineckec49c8342016-02-19 09:17:03 +0100504 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
Hannes Reinecke43394c62016-02-19 09:17:04 +0100505 pg->transition_tmo = buff[5];
Rob Evers3588c5a2012-05-18 14:08:54 -0400506 else
Hannes Reinecke43394c62016-02-19 09:17:04 +0100507 pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
Rob Evers3588c5a2012-05-18 14:08:54 -0400508
Hannes Reineckec49c8342016-02-19 09:17:03 +0100509 if (wait_for_transition &&
Hannes Reinecke43394c62016-02-19 09:17:04 +0100510 (orig_transition_tmo != pg->transition_tmo)) {
Rob Evers3588c5a2012-05-18 14:08:54 -0400511 sdev_printk(KERN_INFO, sdev,
512 "%s: transition timeout set to %d seconds\n",
Hannes Reinecke43394c62016-02-19 09:17:04 +0100513 ALUA_DH_NAME, pg->transition_tmo);
514 expiry = jiffies + pg->transition_tmo * HZ;
Rob Evers3588c5a2012-05-18 14:08:54 -0400515 }
516
Hannes Reineckec49c8342016-02-19 09:17:03 +0100517 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
Rob Evers3588c5a2012-05-18 14:08:54 -0400518 tpg_desc_tbl_off = 8;
519 else
520 tpg_desc_tbl_off = 4;
521
Hannes Reineckec49c8342016-02-19 09:17:03 +0100522 for (k = tpg_desc_tbl_off, ucp = buff + tpg_desc_tbl_off;
Rob Evers3588c5a2012-05-18 14:08:54 -0400523 k < len;
524 k += off, ucp += off) {
525
Hannes Reinecke43394c62016-02-19 09:17:04 +0100526 if (pg->group_id == get_unaligned_be16(&ucp[2])) {
527 pg->state = ucp[0] & 0x0f;
528 pg->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700529 valid_states = ucp[1];
530 }
531 off = 8 + (ucp[7] * 4);
532 }
533
534 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000535 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke43394c62016-02-19 09:17:04 +0100536 ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
537 pg->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700538 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
539 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200540 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700541 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
542 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
543 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
544 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
545
Hannes Reinecke43394c62016-02-19 09:17:04 +0100546 switch (pg->state) {
Hannes Reinecke69723d12010-09-24 15:57:04 +0200547 case TPGS_STATE_TRANSITIONING:
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000548 if (wait_for_transition) {
549 if (time_before(jiffies, expiry)) {
550 /* State transition, retry */
551 interval += 2000;
552 msleep(interval);
553 goto retry;
554 }
555 err = SCSI_DH_RETRY;
556 } else {
557 err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700558 }
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000559
Hannes Reinecke69723d12010-09-24 15:57:04 +0200560 /* Transitioning time exceeded, set port to standby */
Hannes Reinecke43394c62016-02-19 09:17:04 +0100561 pg->state = TPGS_STATE_STANDBY;
Hannes Reinecke69723d12010-09-24 15:57:04 +0200562 break;
563 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000564 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200565 err = SCSI_DH_DEV_OFFLINED;
566 break;
567 default:
568 /* Useable path if active */
569 err = SCSI_DH_OK;
570 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700571 }
Hannes Reineckec49c8342016-02-19 09:17:03 +0100572 kfree(buff);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700573 return err;
574}
575
576/*
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100577 * alua_stpg - Issue a SET TARGET PORT GROUP command
578 *
579 * Issue a SET TARGET PORT GROUP command and evaluate the
Hannes Reineckeb2460752016-02-19 09:17:00 +0100580 * response. Returns SCSI_DH_RETRY per default to trigger
581 * a re-evaluation of the target group state or SCSI_DH_OK
582 * if no further action needs to be taken.
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100583 */
Hannes Reinecke43394c62016-02-19 09:17:04 +0100584static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100585{
Hannes Reineckeb2460752016-02-19 09:17:00 +0100586 int retval;
587 struct scsi_sense_hdr sense_hdr;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100588
Hannes Reinecke43394c62016-02-19 09:17:04 +0100589 if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100590 /* Only implicit ALUA supported, retry */
591 return SCSI_DH_RETRY;
592 }
Hannes Reinecke43394c62016-02-19 09:17:04 +0100593 switch (pg->state) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100594 case TPGS_STATE_OPTIMIZED:
595 return SCSI_DH_OK;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100596 case TPGS_STATE_NONOPTIMIZED:
Hannes Reinecke43394c62016-02-19 09:17:04 +0100597 if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
598 !pg->pref &&
599 (pg->tpgs & TPGS_MODE_IMPLICIT))
Hannes Reineckeb2460752016-02-19 09:17:00 +0100600 return SCSI_DH_OK;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100601 break;
602 case TPGS_STATE_STANDBY:
603 case TPGS_STATE_UNAVAILABLE:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100604 break;
605 case TPGS_STATE_OFFLINE:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100606 return SCSI_DH_IO;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100607 case TPGS_STATE_TRANSITIONING:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100608 break;
609 default:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100610 sdev_printk(KERN_INFO, sdev,
611 "%s: stpg failed, unhandled TPGS state %d",
Hannes Reinecke43394c62016-02-19 09:17:04 +0100612 ALUA_DH_NAME, pg->state);
Hannes Reineckeb2460752016-02-19 09:17:00 +0100613 return SCSI_DH_NOSYS;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100614 }
Hannes Reinecke43394c62016-02-19 09:17:04 +0100615 retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100616
Hannes Reineckeb2460752016-02-19 09:17:00 +0100617 if (retval) {
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100618 if (!scsi_sense_valid(&sense_hdr)) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100619 sdev_printk(KERN_INFO, sdev,
620 "%s: stpg failed, result %d",
621 ALUA_DH_NAME, retval);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100622 if (driver_byte(retval) == DRIVER_ERROR)
Hannes Reineckeb2460752016-02-19 09:17:00 +0100623 return SCSI_DH_DEV_TEMP_BUSY;
624 } else {
Hannes Reinecke43394c62016-02-19 09:17:04 +0100625 sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
Hannes Reineckeb2460752016-02-19 09:17:00 +0100626 ALUA_DH_NAME);
627 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
628 }
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100629 }
Hannes Reineckeb2460752016-02-19 09:17:00 +0100630 /* Retry RTPG */
631 return SCSI_DH_RETRY;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100632}
633
634/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700635 * alua_initialize - Initialize ALUA state
636 * @sdev: the device to be initialized
637 *
638 * For the prep_fn to work correctly we have
639 * to initialize the ALUA state for the device.
640 */
641static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
642{
Hannes Reinecke43394c62016-02-19 09:17:04 +0100643 int err = SCSI_DH_DEV_UNSUPP, tpgs;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700644
Hannes Reinecke43394c62016-02-19 09:17:04 +0100645 tpgs = alua_check_tpgs(sdev);
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100646 if (tpgs != TPGS_MODE_NONE)
647 err = alua_check_vpd(sdev, h, tpgs);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700648
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700649 return err;
650}
Moger, Babu4335d092012-03-27 20:55:49 +0000651/*
652 * alua_set_params - set/unset the optimize flag
653 * @sdev: device on the path to be activated
654 * params - parameters in the following format
655 * "no_of_params\0param1\0param2\0param3\0...\0"
656 * For example, to set the flag pass the following parameters
657 * from multipath.conf
658 * hardware_handler "2 alua 1"
659 */
660static int alua_set_params(struct scsi_device *sdev, const char *params)
661{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200662 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100663 struct alua_port_group *pg = NULL;
Moger, Babu4335d092012-03-27 20:55:49 +0000664 unsigned int optimize = 0, argc;
665 const char *p = params;
666 int result = SCSI_DH_OK;
667
668 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
669 return -EINVAL;
670
671 while (*p++)
672 ;
673 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
674 return -EINVAL;
675
Hannes Reinecke43394c62016-02-19 09:17:04 +0100676 pg = h->pg;
677 if (!pg)
678 return -ENXIO;
679
Moger, Babu4335d092012-03-27 20:55:49 +0000680 if (optimize)
Hannes Reinecke43394c62016-02-19 09:17:04 +0100681 pg->flags |= ALUA_OPTIMIZE_STPG;
Moger, Babu4335d092012-03-27 20:55:49 +0000682 else
Hannes Reinecke43394c62016-02-19 09:17:04 +0100683 pg->flags &= ~ALUA_OPTIMIZE_STPG;
Moger, Babu4335d092012-03-27 20:55:49 +0000684
685 return result;
686}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700687
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000688static uint optimize_stpg;
689module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
690MODULE_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.");
691
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700692/*
693 * alua_activate - activate a path
694 * @sdev: device on the path to be activated
695 *
696 * We're currently switching the port group to be activated only and
697 * let the array figure out the rest.
698 * There may be other arrays which require us to switch all port groups
699 * based on a certain policy. But until we actually encounter them it
700 * should be okay.
701 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700702static int alua_activate(struct scsi_device *sdev,
703 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700704{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200705 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700706 int err = SCSI_DH_OK;
707
Hannes Reinecke43394c62016-02-19 09:17:04 +0100708 if (!h->pg)
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200709 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700710
Hannes Reinecke43394c62016-02-19 09:17:04 +0100711 kref_get(&h->pg->kref);
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000712
Hannes Reinecke43394c62016-02-19 09:17:04 +0100713 if (optimize_stpg)
714 h->pg->flags |= ALUA_OPTIMIZE_STPG;
715
716 err = alua_rtpg(sdev, h->pg, 1);
717 if (err != SCSI_DH_OK) {
718 kref_put(&h->pg->kref, release_port_group);
719 goto out;
720 }
721 err = alua_stpg(sdev, h->pg);
Hannes Reineckedd5cc402016-02-19 09:17:01 +0100722 if (err == SCSI_DH_RETRY)
Hannes Reinecke43394c62016-02-19 09:17:04 +0100723 err = alua_rtpg(sdev, h->pg, 1);
724 kref_put(&h->pg->kref, release_port_group);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700725out:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100726 if (fn)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700727 fn(data, err);
728 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700729}
730
731/*
732 * alua_prep_fn - request callback
733 *
734 * Fail I/O to all paths not in state
735 * active/optimized or active/non-optimized.
736 */
737static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
738{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200739 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100740 int state;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700741 int ret = BLKPREP_OK;
742
Hannes Reinecke43394c62016-02-19 09:17:04 +0100743 if (!h->pg)
744 return ret;
745 kref_get(&h->pg->kref);
746 state = h->pg->state;
747 kref_put(&h->pg->kref, release_port_group);
748 if (state == TPGS_STATE_TRANSITIONING)
Hannes Reinecke69723d12010-09-24 15:57:04 +0200749 ret = BLKPREP_DEFER;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100750 else if (state != TPGS_STATE_OPTIMIZED &&
751 state != TPGS_STATE_NONOPTIMIZED &&
752 state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700753 ret = BLKPREP_KILL;
754 req->cmd_flags |= REQ_QUIET;
755 }
756 return ret;
757
758}
759
Christoph Hellwig1d520322014-09-14 11:08:21 -0700760/*
761 * alua_bus_attach - Attach device handler
762 * @sdev: device to be attached to
763 */
Christoph Hellwigee14c672015-08-27 14:16:59 +0200764static int alua_bus_attach(struct scsi_device *sdev)
Christoph Hellwig1d520322014-09-14 11:08:21 -0700765{
766 struct alua_dh_data *h;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100767 int err, ret = -EINVAL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700768
769 h = kzalloc(sizeof(*h) , GFP_KERNEL);
770 if (!h)
Christoph Hellwigee14c672015-08-27 14:16:59 +0200771 return -ENOMEM;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100772 h->pg = NULL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700773 h->rel_port = -1;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700774 h->sdev = sdev;
775
776 err = alua_initialize(sdev, h);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100777 if (err == SCSI_DH_NOMEM)
778 ret = -ENOMEM;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700779 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
780 goto failed;
781
Christoph Hellwigee14c672015-08-27 14:16:59 +0200782 sdev->handler_data = h;
783 return 0;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700784failed:
785 kfree(h);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100786 return ret;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700787}
788
789/*
790 * alua_bus_detach - Detach device handler
791 * @sdev: device to be detached from
792 */
793static void alua_bus_detach(struct scsi_device *sdev)
794{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200795 struct alua_dh_data *h = sdev->handler_data;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700796
Hannes Reinecke43394c62016-02-19 09:17:04 +0100797 if (h->pg) {
798 kref_put(&h->pg->kref, release_port_group);
799 h->pg = NULL;
800 }
Christoph Hellwigee14c672015-08-27 14:16:59 +0200801 sdev->handler_data = NULL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700802 kfree(h);
803}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700804
805static struct scsi_device_handler alua_dh = {
806 .name = ALUA_DH_NAME,
807 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700808 .attach = alua_bus_attach,
809 .detach = alua_bus_detach,
810 .prep_fn = alua_prep_fn,
811 .check_sense = alua_check_sense,
812 .activate = alua_activate,
Moger, Babu4335d092012-03-27 20:55:49 +0000813 .set_params = alua_set_params,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700814};
815
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700816static int __init alua_init(void)
817{
818 int r;
819
820 r = scsi_register_device_handler(&alua_dh);
821 if (r != 0)
822 printk(KERN_ERR "%s: Failed to register scsi device handler",
823 ALUA_DH_NAME);
824 return r;
825}
826
827static void __exit alua_exit(void)
828{
829 scsi_unregister_device_handler(&alua_dh);
830}
831
832module_init(alua_init);
833module_exit(alua_exit);
834
835MODULE_DESCRIPTION("DM Multipath ALUA support");
836MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
837MODULE_LICENSE("GPL");
838MODULE_VERSION(ALUA_DH_VER);