scsi_dh_alua: Add new blacklist flag 'BLIST_SYNC_ALUA'
[linux-2.6-block.git] / drivers / scsi / device_handler / scsi_dh_alua.c
CommitLineData
057ea7c9
HR
1/*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
69723d17 4 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
057ea7c9
HR
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 */
5a0e3ad6 22#include <linux/slab.h>
69723d17 23#include <linux/delay.h>
acf3368f 24#include <linux/module.h>
a7089770 25#include <asm/unaligned.h>
057ea7c9 26#include <scsi/scsi.h>
80bd68d6 27#include <scsi/scsi_dbg.h>
057ea7c9
HR
28#include <scsi/scsi_eh.h>
29#include <scsi/scsi_dh.h>
30
31#define ALUA_DH_NAME "alua"
69723d17 32#define ALUA_DH_VER "1.3"
057ea7c9
HR
33
34#define TPGS_STATE_OPTIMIZED 0x0
35#define TPGS_STATE_NONOPTIMIZED 0x1
36#define TPGS_STATE_STANDBY 0x2
37#define TPGS_STATE_UNAVAILABLE 0x3
69723d17 38#define TPGS_STATE_LBA_DEPENDENT 0x4
057ea7c9
HR
39#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
69723d17 47#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
057ea7c9
HR
48#define TPGS_SUPPORT_OFFLINE 0x40
49#define TPGS_SUPPORT_TRANSITION 0x80
50
3588c5a2
RE
51#define RTPG_FMT_MASK 0x70
52#define RTPG_FMT_EXT_HDR 0x10
53
057ea7c9
HR
54#define TPGS_MODE_UNINITIALIZED -1
55#define TPGS_MODE_NONE 0x0
56#define TPGS_MODE_IMPLICIT 0x1
57#define TPGS_MODE_EXPLICIT 0x2
58
c49c8345 59#define ALUA_RTPG_SIZE 128
3588c5a2 60#define ALUA_FAILOVER_TIMEOUT 60
057ea7c9 61#define ALUA_FAILOVER_RETRIES 5
03197b61 62#define ALUA_RTPG_DELAY_MSECS 5
057ea7c9 63
6c4fc044 64/* device handler flags */
03197b61
HR
65#define ALUA_OPTIMIZE_STPG 0x01
66#define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
00642a1b 67#define ALUA_SYNC_STPG 0x04
03197b61
HR
68/* State machine flags */
69#define ALUA_PG_RUN_RTPG 0x10
70#define ALUA_PG_RUN_STPG 0x20
71#define ALUA_PG_RUNNING 0x40
4335d092 72
aa90f490
HR
73static uint optimize_stpg;
74module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
75MODULE_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.");
76
43394c67
HR
77static LIST_HEAD(port_group_list);
78static DEFINE_SPINLOCK(port_group_lock);
03197b61 79static struct workqueue_struct *kaluad_wq;
00642a1b 80static struct workqueue_struct *kaluad_sync_wq;
43394c67
HR
81
82struct alua_port_group {
83 struct kref kref;
03197b61 84 struct rcu_head rcu;
43394c67 85 struct list_head node;
0047220c
HR
86 unsigned char device_id_str[256];
87 int device_id_len;
057ea7c9 88 int group_id;
057ea7c9
HR
89 int tpgs;
90 int state;
dcd3a754 91 int pref;
4335d092 92 unsigned flags; /* used for optimizing STPG */
3588c5a2 93 unsigned char transition_tmo;
03197b61
HR
94 unsigned long expiry;
95 unsigned long interval;
96 struct delayed_work rtpg_work;
97 spinlock_t lock;
98 struct list_head rtpg_list;
99 struct scsi_device *rtpg_sdev;
43394c67
HR
100};
101
102struct alua_dh_data {
103 struct alua_port_group *pg;
104 int group_id;
03197b61 105 spinlock_t pg_lock;
96e65865 106 struct scsi_device *sdev;
03197b61
HR
107 int init_error;
108 struct mutex init_mutex;
109};
110
111struct alua_queue_data {
112 struct list_head entry;
96e65865
CS
113 activate_complete callback_fn;
114 void *callback_data;
057ea7c9
HR
115};
116
117#define ALUA_POLICY_SWITCH_CURRENT 0
118#define ALUA_POLICY_SWITCH_ALL 1
119
03197b61
HR
120static void alua_rtpg_work(struct work_struct *work);
121static void alua_rtpg_queue(struct alua_port_group *pg,
122 struct scsi_device *sdev,
123 struct alua_queue_data *qdata);
96e65865 124
43394c67
HR
125static void release_port_group(struct kref *kref)
126{
127 struct alua_port_group *pg;
128
129 pg = container_of(kref, struct alua_port_group, kref);
03197b61
HR
130 if (pg->rtpg_sdev)
131 flush_delayed_work(&pg->rtpg_work);
43394c67
HR
132 spin_lock(&port_group_lock);
133 list_del(&pg->node);
134 spin_unlock(&port_group_lock);
03197b61 135 kfree_rcu(pg, rcu);
43394c67
HR
136}
137
057ea7c9
HR
138/*
139 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
140 * @sdev: sdev the command should be sent to
141 */
40bb61a7
HR
142static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
143 int bufflen, struct scsi_sense_hdr *sshdr, int flags)
057ea7c9 144{
40bb61a7
HR
145 u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
146 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
147 REQ_FAILFAST_DRIVER;
057ea7c9
HR
148
149 /* Prepare the command. */
40bb61a7
HR
150 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN));
151 cdb[0] = MAINTENANCE_IN;
d42ae5f3 152 if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
40bb61a7 153 cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
8e67ce60 154 else
40bb61a7
HR
155 cdb[1] = MI_REPORT_TARGET_PGS;
156 put_unaligned_be32(bufflen, &cdb[6]);
157
158 return scsi_execute_req_flags(sdev, cdb, DMA_FROM_DEVICE,
159 buff, bufflen, sshdr,
160 ALUA_FAILOVER_TIMEOUT * HZ,
161 ALUA_FAILOVER_RETRIES, NULL, req_flags);
057ea7c9
HR
162}
163
96e65865 164/*
b2460756 165 * submit_stpg - Issue a SET TARGET PORT GROUP command
057ea7c9
HR
166 *
167 * Currently we're only setting the current target port group state
168 * to 'active/optimized' and let the array firmware figure out
169 * the states of the remaining groups.
170 */
40bb61a7
HR
171static int submit_stpg(struct scsi_device *sdev, int group_id,
172 struct scsi_sense_hdr *sshdr)
057ea7c9 173{
40bb61a7 174 u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)];
b2460756 175 unsigned char stpg_data[8];
057ea7c9 176 int stpg_len = 8;
40bb61a7
HR
177 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
178 REQ_FAILFAST_DRIVER;
057ea7c9
HR
179
180 /* Prepare the data buffer */
b2460756
HR
181 memset(stpg_data, 0, stpg_len);
182 stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f;
183 put_unaligned_be16(group_id, &stpg_data[6]);
057ea7c9 184
057ea7c9 185 /* Prepare the command. */
40bb61a7
HR
186 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_OUT));
187 cdb[0] = MAINTENANCE_OUT;
188 cdb[1] = MO_SET_TARGET_PGS;
189 put_unaligned_be32(stpg_len, &cdb[6]);
190
191 return scsi_execute_req_flags(sdev, cdb, DMA_TO_DEVICE,
192 stpg_data, stpg_len,
193 sshdr, ALUA_FAILOVER_TIMEOUT * HZ,
194 ALUA_FAILOVER_RETRIES, NULL, req_flags);
057ea7c9
HR
195}
196
0047220c
HR
197struct alua_port_group *alua_find_get_pg(char *id_str, size_t id_size,
198 int group_id)
199{
200 struct alua_port_group *pg;
201
202 list_for_each_entry(pg, &port_group_list, node) {
203 if (pg->group_id != group_id)
204 continue;
205 if (pg->device_id_len != id_size)
206 continue;
207 if (strncmp(pg->device_id_str, id_str, id_size))
208 continue;
209 if (!kref_get_unless_zero(&pg->kref))
210 continue;
211 return pg;
212 }
213
214 return NULL;
215}
216
43394c67
HR
217/*
218 * alua_alloc_pg - Allocate a new port_group structure
219 * @sdev: scsi device
220 * @h: alua device_handler data
221 * @group_id: port group id
222 *
223 * Allocate a new port_group structure for a given
224 * device.
225 */
226struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
227 int group_id, int tpgs)
228{
0047220c 229 struct alua_port_group *pg, *tmp_pg;
43394c67
HR
230
231 pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
232 if (!pg)
0047220c 233 return ERR_PTR(-ENOMEM);
43394c67 234
0047220c
HR
235 pg->device_id_len = scsi_vpd_lun_id(sdev, pg->device_id_str,
236 sizeof(pg->device_id_str));
237 if (pg->device_id_len <= 0) {
238 /*
239 * Internal error: TPGS supported but no device
240 * identifcation found. Disable ALUA support.
241 */
242 kfree(pg);
243 sdev_printk(KERN_INFO, sdev,
244 "%s: No device descriptors found\n",
245 ALUA_DH_NAME);
246 return ERR_PTR(-ENXIO);
247 }
43394c67
HR
248 pg->group_id = group_id;
249 pg->tpgs = tpgs;
250 pg->state = TPGS_STATE_OPTIMIZED;
aa90f490
HR
251 if (optimize_stpg)
252 pg->flags |= ALUA_OPTIMIZE_STPG;
43394c67 253 kref_init(&pg->kref);
03197b61
HR
254 INIT_DELAYED_WORK(&pg->rtpg_work, alua_rtpg_work);
255 INIT_LIST_HEAD(&pg->rtpg_list);
256 INIT_LIST_HEAD(&pg->node);
257 spin_lock_init(&pg->lock);
0047220c 258
43394c67 259 spin_lock(&port_group_lock);
0047220c
HR
260 tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
261 group_id);
262 if (tmp_pg) {
263 spin_unlock(&port_group_lock);
264 kfree(pg);
265 return tmp_pg;
266 }
267
43394c67
HR
268 list_add(&pg->node, &port_group_list);
269 spin_unlock(&port_group_lock);
270
271 return pg;
272}
273
057ea7c9 274/*
d7c48feb 275 * alua_check_tpgs - Evaluate TPGS setting
057ea7c9
HR
276 * @sdev: device to be checked
277 *
d7c48feb 278 * Examine the TPGS setting of the sdev to find out if ALUA
057ea7c9
HR
279 * is supported.
280 */
ad0ea64c 281static int alua_check_tpgs(struct scsi_device *sdev)
057ea7c9 282{
ad0ea64c 283 int tpgs = TPGS_MODE_NONE;
057ea7c9 284
db5a6a60
HR
285 /*
286 * ALUA support for non-disk devices is fraught with
287 * difficulties, so disable it for now.
288 */
289 if (sdev->type != TYPE_DISK) {
db5a6a60
HR
290 sdev_printk(KERN_INFO, sdev,
291 "%s: disable for non-disk devices\n",
292 ALUA_DH_NAME);
ad0ea64c 293 return tpgs;
db5a6a60
HR
294 }
295
ad0ea64c
HR
296 tpgs = scsi_device_tpgs(sdev);
297 switch (tpgs) {
057ea7c9
HR
298 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
299 sdev_printk(KERN_INFO, sdev,
300 "%s: supports implicit and explicit TPGS\n",
301 ALUA_DH_NAME);
302 break;
303 case TPGS_MODE_EXPLICIT:
304 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
305 ALUA_DH_NAME);
306 break;
307 case TPGS_MODE_IMPLICIT:
308 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
309 ALUA_DH_NAME);
310 break;
6cc05d45 311 case TPGS_MODE_NONE:
057ea7c9
HR
312 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
313 ALUA_DH_NAME);
057ea7c9 314 break;
6cc05d45
HR
315 default:
316 sdev_printk(KERN_INFO, sdev,
317 "%s: unsupported TPGS setting %d\n",
ad0ea64c
HR
318 ALUA_DH_NAME, tpgs);
319 tpgs = TPGS_MODE_NONE;
6cc05d45 320 break;
057ea7c9
HR
321 }
322
ad0ea64c 323 return tpgs;
057ea7c9
HR
324}
325
326/*
9b80dcec 327 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
057ea7c9
HR
328 * @sdev: device to be checked
329 *
330 * Extract the relative target port and the target port group
331 * descriptor from the list of identificators.
332 */
a4253fde
HR
333static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
334 int tpgs)
057ea7c9 335{
83ea0e5e 336 int rel_port = -1, group_id;
03197b61 337 struct alua_port_group *pg, *old_pg = NULL;
057ea7c9 338
83ea0e5e
HR
339 group_id = scsi_vpd_tpg_id(sdev, &rel_port);
340 if (group_id < 0) {
057ea7c9
HR
341 /*
342 * Internal error; TPGS supported but required
343 * VPD identification descriptors not present.
344 * Disable ALUA support
345 */
346 sdev_printk(KERN_INFO, sdev,
347 "%s: No target port descriptors found\n",
348 ALUA_DH_NAME);
9b80dcec 349 return SCSI_DH_DEV_UNSUPP;
057ea7c9 350 }
a4253fde 351
03197b61
HR
352 pg = alua_alloc_pg(sdev, group_id, tpgs);
353 if (IS_ERR(pg)) {
354 if (PTR_ERR(pg) == -ENOMEM)
a4253fde
HR
355 return SCSI_DH_NOMEM;
356 return SCSI_DH_DEV_UNSUPP;
357 }
9b80dcec 358 sdev_printk(KERN_INFO, sdev,
a4253fde 359 "%s: device %s port group %x rel port %x\n",
03197b61
HR
360 ALUA_DH_NAME, pg->device_id_str, group_id, rel_port);
361
362 /* Check for existing port group references */
363 spin_lock(&h->pg_lock);
364 old_pg = h->pg;
365 if (old_pg != pg) {
366 /* port group has changed. Update to new port group */
367 rcu_assign_pointer(h->pg, pg);
368 }
851cde99
HR
369 if (sdev->synchronous_alua)
370 pg->flags |= ALUA_SYNC_STPG;
03197b61
HR
371 alua_rtpg_queue(h->pg, sdev, NULL);
372 spin_unlock(&h->pg_lock);
373
374 if (old_pg)
375 kref_put(&old_pg->kref, release_port_group);
057ea7c9 376
03197b61 377 return SCSI_DH_OK;
057ea7c9
HR
378}
379
380static char print_alua_state(int state)
381{
382 switch (state) {
383 case TPGS_STATE_OPTIMIZED:
384 return 'A';
385 case TPGS_STATE_NONOPTIMIZED:
386 return 'N';
387 case TPGS_STATE_STANDBY:
388 return 'S';
389 case TPGS_STATE_UNAVAILABLE:
390 return 'U';
69723d17
HR
391 case TPGS_STATE_LBA_DEPENDENT:
392 return 'L';
057ea7c9
HR
393 case TPGS_STATE_OFFLINE:
394 return 'O';
395 case TPGS_STATE_TRANSITIONING:
396 return 'T';
397 default:
398 return 'X';
399 }
400}
401
402static int alua_check_sense(struct scsi_device *sdev,
403 struct scsi_sense_hdr *sense_hdr)
404{
405 switch (sense_hdr->sense_key) {
406 case NOT_READY:
407 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
408 /*
409 * LUN Not Accessible - ALUA state transition
410 */
c7dbb627 411 return ADD_TO_MLQUEUE;
057ea7c9
HR
412 break;
413 case UNIT_ATTENTION:
414 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
415 /*
416 * Power On, Reset, or Bus Device Reset, just retry.
417 */
c7dbb627 418 return ADD_TO_MLQUEUE;
c20ee7b5
SS
419 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
420 /*
421 * Device internal reset
422 */
423 return ADD_TO_MLQUEUE;
410f02d8
MB
424 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
425 /*
426 * Mode Parameters Changed
427 */
428 return ADD_TO_MLQUEUE;
bf81973a 429 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
057ea7c9
HR
430 /*
431 * ALUA state changed
432 */
c7dbb627 433 return ADD_TO_MLQUEUE;
bf81973a 434 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
057ea7c9
HR
435 /*
436 * Implicit ALUA state transition failed
437 */
c7dbb627 438 return ADD_TO_MLQUEUE;
bf81973a
MB
439 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
440 /*
441 * Inquiry data has changed
442 */
443 return ADD_TO_MLQUEUE;
444 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
4d086f6b
IH
445 /*
446 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
447 * when switching controllers on targets like
448 * Intel Multi-Flex. We can just retry.
449 */
450 return ADD_TO_MLQUEUE;
057ea7c9
HR
451 break;
452 }
453
454 return SCSI_RETURN_NOT_HANDLED;
455}
456
057ea7c9
HR
457/*
458 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
459 * @sdev: the device to be evaluated.
460 *
461 * Evaluate the Target Port Group State.
462 * Returns SCSI_DH_DEV_OFFLINED if the path is
25985edc 463 * found to be unusable.
057ea7c9 464 */
28261402 465static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
057ea7c9
HR
466{
467 struct scsi_sense_hdr sense_hdr;
c49c8345
HR
468 int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE;
469 unsigned char *ucp, *buff;
5597cafc 470 unsigned err, retval;
3588c5a2
RE
471 unsigned int tpg_desc_tbl_off;
472 unsigned char orig_transition_tmo;
473
03197b61
HR
474 if (!pg->expiry) {
475 unsigned long transition_tmo = ALUA_FAILOVER_TIMEOUT * HZ;
476
477 if (pg->transition_tmo)
478 transition_tmo = pg->transition_tmo * HZ;
479
480 pg->expiry = round_jiffies_up(jiffies + transition_tmo);
481 }
057ea7c9 482
c49c8345
HR
483 buff = kzalloc(bufflen, GFP_KERNEL);
484 if (!buff)
485 return SCSI_DH_DEV_TEMP_BUSY;
486
057ea7c9 487 retry:
43394c67 488 retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
40bb61a7 489
5597cafc 490 if (retval) {
40bb61a7 491 if (!scsi_sense_valid(&sense_hdr)) {
5597cafc
HR
492 sdev_printk(KERN_INFO, sdev,
493 "%s: rtpg failed, result %d\n",
494 ALUA_DH_NAME, retval);
c49c8345 495 kfree(buff);
40bb61a7 496 if (driver_byte(retval) == DRIVER_ERROR)
5597cafc 497 return SCSI_DH_DEV_TEMP_BUSY;
057ea7c9 498 return SCSI_DH_IO;
5597cafc 499 }
057ea7c9 500
8e67ce60
RE
501 /*
502 * submit_rtpg() has failed on existing arrays
503 * when requesting extended header info, and
504 * the array doesn't support extended headers,
505 * even though it shouldn't according to T10.
506 * The retry without rtpg_ext_hdr_req set
507 * handles this.
508 */
43394c67 509 if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
8e67ce60
RE
510 sense_hdr.sense_key == ILLEGAL_REQUEST &&
511 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
43394c67 512 pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
8e67ce60
RE
513 goto retry;
514 }
e2d817db
HR
515 /*
516 * Retry on ALUA state transition or if any
517 * UNIT ATTENTION occurred.
518 */
519 if (sense_hdr.sense_key == NOT_READY &&
520 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
521 err = SCSI_DH_RETRY;
522 else if (sense_hdr.sense_key == UNIT_ATTENTION)
523 err = SCSI_DH_RETRY;
03197b61
HR
524 if (err == SCSI_DH_RETRY &&
525 pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
80bd68d6
HR
526 sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
527 ALUA_DH_NAME);
528 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
03197b61 529 return err;
80bd68d6
HR
530 }
531 sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
532 ALUA_DH_NAME);
533 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
c49c8345 534 kfree(buff);
03197b61 535 pg->expiry = 0;
80bd68d6 536 return SCSI_DH_IO;
057ea7c9 537 }
057ea7c9 538
c49c8345 539 len = get_unaligned_be32(&buff[0]) + 4;
057ea7c9 540
c49c8345 541 if (len > bufflen) {
057ea7c9 542 /* Resubmit with the correct length */
c49c8345
HR
543 kfree(buff);
544 bufflen = len;
545 buff = kmalloc(bufflen, GFP_KERNEL);
546 if (!buff) {
057ea7c9 547 sdev_printk(KERN_WARNING, sdev,
cadbd4a5 548 "%s: kmalloc buffer failed\n",__func__);
057ea7c9 549 /* Temporary failure, bypass */
03197b61 550 pg->expiry = 0;
057ea7c9
HR
551 return SCSI_DH_DEV_TEMP_BUSY;
552 }
553 goto retry;
554 }
555
43394c67 556 orig_transition_tmo = pg->transition_tmo;
c49c8345 557 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
43394c67 558 pg->transition_tmo = buff[5];
3588c5a2 559 else
43394c67 560 pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
3588c5a2 561
28261402 562 if (orig_transition_tmo != pg->transition_tmo) {
3588c5a2
RE
563 sdev_printk(KERN_INFO, sdev,
564 "%s: transition timeout set to %d seconds\n",
43394c67 565 ALUA_DH_NAME, pg->transition_tmo);
03197b61 566 pg->expiry = jiffies + pg->transition_tmo * HZ;
3588c5a2
RE
567 }
568
c49c8345 569 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
3588c5a2
RE
570 tpg_desc_tbl_off = 8;
571 else
572 tpg_desc_tbl_off = 4;
573
c49c8345 574 for (k = tpg_desc_tbl_off, ucp = buff + tpg_desc_tbl_off;
3588c5a2
RE
575 k < len;
576 k += off, ucp += off) {
577
43394c67
HR
578 if (pg->group_id == get_unaligned_be16(&ucp[2])) {
579 pg->state = ucp[0] & 0x0f;
580 pg->pref = ucp[0] >> 7;
057ea7c9
HR
581 valid_states = ucp[1];
582 }
583 off = 8 + (ucp[7] * 4);
584 }
585
586 sdev_printk(KERN_INFO, sdev,
dcd3a754 587 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
43394c67
HR
588 ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
589 pg->pref ? "preferred" : "non-preferred",
057ea7c9
HR
590 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
591 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
69723d17 592 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
057ea7c9
HR
593 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
594 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
595 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
596 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
597
43394c67 598 switch (pg->state) {
69723d17 599 case TPGS_STATE_TRANSITIONING:
03197b61 600 if (time_before(jiffies, pg->expiry)) {
28261402 601 /* State transition, retry */
03197b61
HR
602 pg->interval = 2;
603 err = SCSI_DH_RETRY;
604 } else {
605 /* Transitioning time exceeded, set port to standby */
606 err = SCSI_DH_IO;
607 pg->state = TPGS_STATE_STANDBY;
608 pg->expiry = 0;
057ea7c9 609 }
69723d17
HR
610 break;
611 case TPGS_STATE_OFFLINE:
e47f8976 612 /* Path unusable */
69723d17 613 err = SCSI_DH_DEV_OFFLINED;
03197b61 614 pg->expiry = 0;
69723d17
HR
615 break;
616 default:
617 /* Useable path if active */
618 err = SCSI_DH_OK;
03197b61 619 pg->expiry = 0;
69723d17 620 break;
057ea7c9 621 }
c49c8345 622 kfree(buff);
057ea7c9
HR
623 return err;
624}
625
f2ecf13a
HR
626/*
627 * alua_stpg - Issue a SET TARGET PORT GROUP command
628 *
629 * Issue a SET TARGET PORT GROUP command and evaluate the
b2460756
HR
630 * response. Returns SCSI_DH_RETRY per default to trigger
631 * a re-evaluation of the target group state or SCSI_DH_OK
632 * if no further action needs to be taken.
f2ecf13a 633 */
43394c67 634static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
f2ecf13a 635{
b2460756
HR
636 int retval;
637 struct scsi_sense_hdr sense_hdr;
f2ecf13a 638
43394c67 639 if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
b2460756
HR
640 /* Only implicit ALUA supported, retry */
641 return SCSI_DH_RETRY;
642 }
43394c67 643 switch (pg->state) {
b2460756
HR
644 case TPGS_STATE_OPTIMIZED:
645 return SCSI_DH_OK;
f2ecf13a 646 case TPGS_STATE_NONOPTIMIZED:
43394c67
HR
647 if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
648 !pg->pref &&
649 (pg->tpgs & TPGS_MODE_IMPLICIT))
b2460756 650 return SCSI_DH_OK;
f2ecf13a
HR
651 break;
652 case TPGS_STATE_STANDBY:
653 case TPGS_STATE_UNAVAILABLE:
f2ecf13a
HR
654 break;
655 case TPGS_STATE_OFFLINE:
b2460756 656 return SCSI_DH_IO;
f2ecf13a 657 case TPGS_STATE_TRANSITIONING:
f2ecf13a
HR
658 break;
659 default:
b2460756
HR
660 sdev_printk(KERN_INFO, sdev,
661 "%s: stpg failed, unhandled TPGS state %d",
43394c67 662 ALUA_DH_NAME, pg->state);
b2460756 663 return SCSI_DH_NOSYS;
f2ecf13a 664 }
43394c67 665 retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
f2ecf13a 666
b2460756 667 if (retval) {
40bb61a7 668 if (!scsi_sense_valid(&sense_hdr)) {
b2460756
HR
669 sdev_printk(KERN_INFO, sdev,
670 "%s: stpg failed, result %d",
671 ALUA_DH_NAME, retval);
40bb61a7 672 if (driver_byte(retval) == DRIVER_ERROR)
b2460756
HR
673 return SCSI_DH_DEV_TEMP_BUSY;
674 } else {
43394c67 675 sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
b2460756
HR
676 ALUA_DH_NAME);
677 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
678 }
f2ecf13a 679 }
b2460756
HR
680 /* Retry RTPG */
681 return SCSI_DH_RETRY;
f2ecf13a
HR
682}
683
03197b61
HR
684static void alua_rtpg_work(struct work_struct *work)
685{
686 struct alua_port_group *pg =
687 container_of(work, struct alua_port_group, rtpg_work.work);
688 struct scsi_device *sdev;
689 LIST_HEAD(qdata_list);
690 int err = SCSI_DH_OK;
691 struct alua_queue_data *qdata, *tmp;
692 unsigned long flags;
00642a1b 693 struct workqueue_struct *alua_wq = kaluad_wq;
03197b61
HR
694
695 spin_lock_irqsave(&pg->lock, flags);
696 sdev = pg->rtpg_sdev;
697 if (!sdev) {
698 WARN_ON(pg->flags & ALUA_PG_RUN_RTPG);
699 WARN_ON(pg->flags & ALUA_PG_RUN_STPG);
700 spin_unlock_irqrestore(&pg->lock, flags);
701 return;
702 }
00642a1b
HR
703 if (pg->flags & ALUA_SYNC_STPG)
704 alua_wq = kaluad_sync_wq;
03197b61
HR
705 pg->flags |= ALUA_PG_RUNNING;
706 if (pg->flags & ALUA_PG_RUN_RTPG) {
707 pg->flags &= ~ALUA_PG_RUN_RTPG;
708 spin_unlock_irqrestore(&pg->lock, flags);
709 err = alua_rtpg(sdev, pg);
710 spin_lock_irqsave(&pg->lock, flags);
711 if (err == SCSI_DH_RETRY) {
712 pg->flags &= ~ALUA_PG_RUNNING;
713 pg->flags |= ALUA_PG_RUN_RTPG;
714 spin_unlock_irqrestore(&pg->lock, flags);
00642a1b 715 queue_delayed_work(alua_wq, &pg->rtpg_work,
03197b61
HR
716 pg->interval * HZ);
717 return;
718 }
719 if (err != SCSI_DH_OK)
720 pg->flags &= ~ALUA_PG_RUN_STPG;
721 }
722 if (pg->flags & ALUA_PG_RUN_STPG) {
723 pg->flags &= ~ALUA_PG_RUN_STPG;
724 spin_unlock_irqrestore(&pg->lock, flags);
725 err = alua_stpg(sdev, pg);
726 spin_lock_irqsave(&pg->lock, flags);
727 if (err == SCSI_DH_RETRY) {
728 pg->flags |= ALUA_PG_RUN_RTPG;
729 pg->interval = 0;
730 pg->flags &= ~ALUA_PG_RUNNING;
731 spin_unlock_irqrestore(&pg->lock, flags);
00642a1b 732 queue_delayed_work(alua_wq, &pg->rtpg_work,
03197b61
HR
733 pg->interval * HZ);
734 return;
735 }
736 }
737
738 list_splice_init(&pg->rtpg_list, &qdata_list);
739 pg->rtpg_sdev = NULL;
740 spin_unlock_irqrestore(&pg->lock, flags);
741
742 list_for_each_entry_safe(qdata, tmp, &qdata_list, entry) {
743 list_del(&qdata->entry);
744 if (qdata->callback_fn)
745 qdata->callback_fn(qdata->callback_data, err);
746 kfree(qdata);
747 }
748 spin_lock_irqsave(&pg->lock, flags);
749 pg->flags &= ~ALUA_PG_RUNNING;
750 spin_unlock_irqrestore(&pg->lock, flags);
751 scsi_device_put(sdev);
752 kref_put(&pg->kref, release_port_group);
753}
754
755static void alua_rtpg_queue(struct alua_port_group *pg,
756 struct scsi_device *sdev,
757 struct alua_queue_data *qdata)
758{
759 int start_queue = 0;
760 unsigned long flags;
00642a1b 761 struct workqueue_struct *alua_wq = kaluad_wq;
03197b61
HR
762
763 if (!pg)
764 return;
765
766 spin_lock_irqsave(&pg->lock, flags);
767 if (qdata) {
768 list_add_tail(&qdata->entry, &pg->rtpg_list);
769 pg->flags |= ALUA_PG_RUN_STPG;
770 }
771 if (pg->rtpg_sdev == NULL) {
772 pg->interval = 0;
773 pg->flags |= ALUA_PG_RUN_RTPG;
774 kref_get(&pg->kref);
775 pg->rtpg_sdev = sdev;
776 scsi_device_get(sdev);
777 start_queue = 1;
778 }
00642a1b
HR
779 if (pg->flags & ALUA_SYNC_STPG)
780 alua_wq = kaluad_sync_wq;
03197b61
HR
781 spin_unlock_irqrestore(&pg->lock, flags);
782
783 if (start_queue &&
00642a1b 784 !queue_delayed_work(alua_wq, &pg->rtpg_work,
03197b61
HR
785 msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS))) {
786 scsi_device_put(sdev);
787 kref_put(&pg->kref, release_port_group);
788 }
789}
790
057ea7c9
HR
791/*
792 * alua_initialize - Initialize ALUA state
793 * @sdev: the device to be initialized
794 *
795 * For the prep_fn to work correctly we have
796 * to initialize the ALUA state for the device.
797 */
798static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
799{
43394c67 800 int err = SCSI_DH_DEV_UNSUPP, tpgs;
057ea7c9 801
03197b61 802 mutex_lock(&h->init_mutex);
43394c67 803 tpgs = alua_check_tpgs(sdev);
a4253fde
HR
804 if (tpgs != TPGS_MODE_NONE)
805 err = alua_check_vpd(sdev, h, tpgs);
03197b61
HR
806 h->init_error = err;
807 mutex_unlock(&h->init_mutex);
057ea7c9
HR
808 return err;
809}
4335d092
MB
810/*
811 * alua_set_params - set/unset the optimize flag
812 * @sdev: device on the path to be activated
813 * params - parameters in the following format
814 * "no_of_params\0param1\0param2\0param3\0...\0"
815 * For example, to set the flag pass the following parameters
816 * from multipath.conf
817 * hardware_handler "2 alua 1"
818 */
819static int alua_set_params(struct scsi_device *sdev, const char *params)
820{
ee14c674 821 struct alua_dh_data *h = sdev->handler_data;
03197b61 822 struct alua_port_group __rcu *pg = NULL;
4335d092
MB
823 unsigned int optimize = 0, argc;
824 const char *p = params;
825 int result = SCSI_DH_OK;
03197b61 826 unsigned long flags;
4335d092
MB
827
828 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
829 return -EINVAL;
830
831 while (*p++)
832 ;
833 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
834 return -EINVAL;
835
03197b61
HR
836 rcu_read_lock();
837 pg = rcu_dereference(h->pg);
838 if (!pg) {
839 rcu_read_unlock();
43394c67 840 return -ENXIO;
03197b61
HR
841 }
842 spin_lock_irqsave(&pg->lock, flags);
4335d092 843 if (optimize)
43394c67 844 pg->flags |= ALUA_OPTIMIZE_STPG;
4335d092 845 else
43394c67 846 pg->flags &= ~ALUA_OPTIMIZE_STPG;
03197b61
HR
847 spin_unlock_irqrestore(&pg->lock, flags);
848 rcu_read_unlock();
4335d092
MB
849
850 return result;
851}
057ea7c9
HR
852
853/*
854 * alua_activate - activate a path
855 * @sdev: device on the path to be activated
856 *
857 * We're currently switching the port group to be activated only and
858 * let the array figure out the rest.
859 * There may be other arrays which require us to switch all port groups
860 * based on a certain policy. But until we actually encounter them it
861 * should be okay.
862 */
3ae31f6a
CS
863static int alua_activate(struct scsi_device *sdev,
864 activate_complete fn, void *data)
057ea7c9 865{
ee14c674 866 struct alua_dh_data *h = sdev->handler_data;
057ea7c9 867 int err = SCSI_DH_OK;
03197b61
HR
868 struct alua_queue_data *qdata;
869 struct alua_port_group __rcu *pg;
057ea7c9 870
03197b61
HR
871 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
872 if (!qdata) {
873 err = SCSI_DH_RES_TEMP_UNAVAIL;
46ccf6b5 874 goto out;
03197b61
HR
875 }
876 qdata->callback_fn = fn;
877 qdata->callback_data = data;
878
879 mutex_lock(&h->init_mutex);
880 rcu_read_lock();
881 pg = rcu_dereference(h->pg);
882 if (!pg || !kref_get_unless_zero(&pg->kref)) {
883 rcu_read_unlock();
884 kfree(qdata);
885 err = h->init_error;
886 mutex_unlock(&h->init_mutex);
43394c67
HR
887 goto out;
888 }
03197b61
HR
889 fn = NULL;
890 rcu_read_unlock();
891 mutex_unlock(&h->init_mutex);
892
893 alua_rtpg_queue(pg, sdev, qdata);
894 kref_put(&pg->kref, release_port_group);
057ea7c9 895out:
b2460756 896 if (fn)
3ae31f6a
CS
897 fn(data, err);
898 return 0;
057ea7c9
HR
899}
900
901/*
902 * alua_prep_fn - request callback
903 *
904 * Fail I/O to all paths not in state
905 * active/optimized or active/non-optimized.
906 */
907static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
908{
ee14c674 909 struct alua_dh_data *h = sdev->handler_data;
03197b61
HR
910 struct alua_port_group __rcu *pg;
911 int state = TPGS_STATE_OPTIMIZED;
057ea7c9
HR
912 int ret = BLKPREP_OK;
913
03197b61
HR
914 rcu_read_lock();
915 pg = rcu_dereference(h->pg);
916 if (pg)
917 state = pg->state;
918 rcu_read_unlock();
43394c67 919 if (state == TPGS_STATE_TRANSITIONING)
69723d17 920 ret = BLKPREP_DEFER;
43394c67
HR
921 else if (state != TPGS_STATE_OPTIMIZED &&
922 state != TPGS_STATE_NONOPTIMIZED &&
923 state != TPGS_STATE_LBA_DEPENDENT) {
057ea7c9
HR
924 ret = BLKPREP_KILL;
925 req->cmd_flags |= REQ_QUIET;
926 }
927 return ret;
928
929}
930
057ea7c9
HR
931/*
932 * alua_bus_attach - Attach device handler
933 * @sdev: device to be attached to
934 */
ee14c674 935static int alua_bus_attach(struct scsi_device *sdev)
057ea7c9 936{
057ea7c9 937 struct alua_dh_data *h;
43394c67 938 int err, ret = -EINVAL;
057ea7c9 939
cd37743f 940 h = kzalloc(sizeof(*h) , GFP_KERNEL);
1d520328 941 if (!h)
ee14c674 942 return -ENOMEM;
03197b61
HR
943 spin_lock_init(&h->pg_lock);
944 rcu_assign_pointer(h->pg, NULL);
945 h->init_error = SCSI_DH_OK;
96e65865 946 h->sdev = sdev;
057ea7c9 947
03197b61 948 mutex_init(&h->init_mutex);
057ea7c9 949 err = alua_initialize(sdev, h);
43394c67
HR
950 if (err == SCSI_DH_NOMEM)
951 ret = -ENOMEM;
1d520328 952 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
057ea7c9
HR
953 goto failed;
954
ee14c674
CH
955 sdev->handler_data = h;
956 return 0;
057ea7c9 957failed:
cd37743f 958 kfree(h);
43394c67 959 return ret;
057ea7c9
HR
960}
961
962/*
963 * alua_bus_detach - Detach device handler
964 * @sdev: device to be detached from
965 */
966static void alua_bus_detach(struct scsi_device *sdev)
967{
ee14c674 968 struct alua_dh_data *h = sdev->handler_data;
03197b61
HR
969 struct alua_port_group *pg;
970
971 spin_lock(&h->pg_lock);
972 pg = h->pg;
973 rcu_assign_pointer(h->pg, NULL);
974 h->sdev = NULL;
975 spin_unlock(&h->pg_lock);
976 if (pg)
977 kref_put(&pg->kref, release_port_group);
057ea7c9 978
ee14c674 979 sdev->handler_data = NULL;
cd37743f 980 kfree(h);
057ea7c9
HR
981}
982
1d520328
CH
983static struct scsi_device_handler alua_dh = {
984 .name = ALUA_DH_NAME,
985 .module = THIS_MODULE,
986 .attach = alua_bus_attach,
987 .detach = alua_bus_detach,
988 .prep_fn = alua_prep_fn,
989 .check_sense = alua_check_sense,
990 .activate = alua_activate,
991 .set_params = alua_set_params,
1d520328
CH
992};
993
057ea7c9
HR
994static int __init alua_init(void)
995{
996 int r;
997
03197b61
HR
998 kaluad_wq = alloc_workqueue("kaluad", WQ_MEM_RECLAIM, 0);
999 if (!kaluad_wq) {
1000 /* Temporary failure, bypass */
1001 return SCSI_DH_DEV_TEMP_BUSY;
1002 }
00642a1b
HR
1003 kaluad_sync_wq = create_workqueue("kaluad_sync");
1004 if (!kaluad_sync_wq) {
1005 destroy_workqueue(kaluad_wq);
1006 return SCSI_DH_DEV_TEMP_BUSY;
1007 }
057ea7c9 1008 r = scsi_register_device_handler(&alua_dh);
03197b61 1009 if (r != 0) {
057ea7c9
HR
1010 printk(KERN_ERR "%s: Failed to register scsi device handler",
1011 ALUA_DH_NAME);
00642a1b 1012 destroy_workqueue(kaluad_sync_wq);
03197b61
HR
1013 destroy_workqueue(kaluad_wq);
1014 }
057ea7c9
HR
1015 return r;
1016}
1017
1018static void __exit alua_exit(void)
1019{
1020 scsi_unregister_device_handler(&alua_dh);
00642a1b 1021 destroy_workqueue(kaluad_sync_wq);
03197b61 1022 destroy_workqueue(kaluad_wq);
057ea7c9
HR
1023}
1024
1025module_init(alua_init);
1026module_exit(alua_exit);
1027
1028MODULE_DESCRIPTION("DM Multipath ALUA support");
1029MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1030MODULE_LICENSE("GPL");
1031MODULE_VERSION(ALUA_DH_VER);