scsi_dh_alua: Allow workqueue to run synchronously
[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 }
369 alua_rtpg_queue(h->pg, sdev, NULL);
370 spin_unlock(&h->pg_lock);
371
372 if (old_pg)
373 kref_put(&old_pg->kref, release_port_group);
057ea7c9 374
03197b61 375 return SCSI_DH_OK;
057ea7c9
HR
376}
377
378static char print_alua_state(int state)
379{
380 switch (state) {
381 case TPGS_STATE_OPTIMIZED:
382 return 'A';
383 case TPGS_STATE_NONOPTIMIZED:
384 return 'N';
385 case TPGS_STATE_STANDBY:
386 return 'S';
387 case TPGS_STATE_UNAVAILABLE:
388 return 'U';
69723d17
HR
389 case TPGS_STATE_LBA_DEPENDENT:
390 return 'L';
057ea7c9
HR
391 case TPGS_STATE_OFFLINE:
392 return 'O';
393 case TPGS_STATE_TRANSITIONING:
394 return 'T';
395 default:
396 return 'X';
397 }
398}
399
400static int alua_check_sense(struct scsi_device *sdev,
401 struct scsi_sense_hdr *sense_hdr)
402{
403 switch (sense_hdr->sense_key) {
404 case NOT_READY:
405 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
406 /*
407 * LUN Not Accessible - ALUA state transition
408 */
c7dbb627 409 return ADD_TO_MLQUEUE;
057ea7c9
HR
410 break;
411 case UNIT_ATTENTION:
412 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
413 /*
414 * Power On, Reset, or Bus Device Reset, just retry.
415 */
c7dbb627 416 return ADD_TO_MLQUEUE;
c20ee7b5
SS
417 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
418 /*
419 * Device internal reset
420 */
421 return ADD_TO_MLQUEUE;
410f02d8
MB
422 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
423 /*
424 * Mode Parameters Changed
425 */
426 return ADD_TO_MLQUEUE;
bf81973a 427 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
057ea7c9
HR
428 /*
429 * ALUA state changed
430 */
c7dbb627 431 return ADD_TO_MLQUEUE;
bf81973a 432 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
057ea7c9
HR
433 /*
434 * Implicit ALUA state transition failed
435 */
c7dbb627 436 return ADD_TO_MLQUEUE;
bf81973a
MB
437 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
438 /*
439 * Inquiry data has changed
440 */
441 return ADD_TO_MLQUEUE;
442 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
4d086f6b
IH
443 /*
444 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
445 * when switching controllers on targets like
446 * Intel Multi-Flex. We can just retry.
447 */
448 return ADD_TO_MLQUEUE;
057ea7c9
HR
449 break;
450 }
451
452 return SCSI_RETURN_NOT_HANDLED;
453}
454
057ea7c9
HR
455/*
456 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
457 * @sdev: the device to be evaluated.
458 *
459 * Evaluate the Target Port Group State.
460 * Returns SCSI_DH_DEV_OFFLINED if the path is
25985edc 461 * found to be unusable.
057ea7c9 462 */
28261402 463static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
057ea7c9
HR
464{
465 struct scsi_sense_hdr sense_hdr;
c49c8345
HR
466 int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE;
467 unsigned char *ucp, *buff;
5597cafc 468 unsigned err, retval;
3588c5a2
RE
469 unsigned int tpg_desc_tbl_off;
470 unsigned char orig_transition_tmo;
471
03197b61
HR
472 if (!pg->expiry) {
473 unsigned long transition_tmo = ALUA_FAILOVER_TIMEOUT * HZ;
474
475 if (pg->transition_tmo)
476 transition_tmo = pg->transition_tmo * HZ;
477
478 pg->expiry = round_jiffies_up(jiffies + transition_tmo);
479 }
057ea7c9 480
c49c8345
HR
481 buff = kzalloc(bufflen, GFP_KERNEL);
482 if (!buff)
483 return SCSI_DH_DEV_TEMP_BUSY;
484
057ea7c9 485 retry:
43394c67 486 retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
40bb61a7 487
5597cafc 488 if (retval) {
40bb61a7 489 if (!scsi_sense_valid(&sense_hdr)) {
5597cafc
HR
490 sdev_printk(KERN_INFO, sdev,
491 "%s: rtpg failed, result %d\n",
492 ALUA_DH_NAME, retval);
c49c8345 493 kfree(buff);
40bb61a7 494 if (driver_byte(retval) == DRIVER_ERROR)
5597cafc 495 return SCSI_DH_DEV_TEMP_BUSY;
057ea7c9 496 return SCSI_DH_IO;
5597cafc 497 }
057ea7c9 498
8e67ce60
RE
499 /*
500 * submit_rtpg() has failed on existing arrays
501 * when requesting extended header info, and
502 * the array doesn't support extended headers,
503 * even though it shouldn't according to T10.
504 * The retry without rtpg_ext_hdr_req set
505 * handles this.
506 */
43394c67 507 if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
8e67ce60
RE
508 sense_hdr.sense_key == ILLEGAL_REQUEST &&
509 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
43394c67 510 pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
8e67ce60
RE
511 goto retry;
512 }
e2d817db
HR
513 /*
514 * Retry on ALUA state transition or if any
515 * UNIT ATTENTION occurred.
516 */
517 if (sense_hdr.sense_key == NOT_READY &&
518 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
519 err = SCSI_DH_RETRY;
520 else if (sense_hdr.sense_key == UNIT_ATTENTION)
521 err = SCSI_DH_RETRY;
03197b61
HR
522 if (err == SCSI_DH_RETRY &&
523 pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
80bd68d6
HR
524 sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
525 ALUA_DH_NAME);
526 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
03197b61 527 return err;
80bd68d6
HR
528 }
529 sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
530 ALUA_DH_NAME);
531 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
c49c8345 532 kfree(buff);
03197b61 533 pg->expiry = 0;
80bd68d6 534 return SCSI_DH_IO;
057ea7c9 535 }
057ea7c9 536
c49c8345 537 len = get_unaligned_be32(&buff[0]) + 4;
057ea7c9 538
c49c8345 539 if (len > bufflen) {
057ea7c9 540 /* Resubmit with the correct length */
c49c8345
HR
541 kfree(buff);
542 bufflen = len;
543 buff = kmalloc(bufflen, GFP_KERNEL);
544 if (!buff) {
057ea7c9 545 sdev_printk(KERN_WARNING, sdev,
cadbd4a5 546 "%s: kmalloc buffer failed\n",__func__);
057ea7c9 547 /* Temporary failure, bypass */
03197b61 548 pg->expiry = 0;
057ea7c9
HR
549 return SCSI_DH_DEV_TEMP_BUSY;
550 }
551 goto retry;
552 }
553
43394c67 554 orig_transition_tmo = pg->transition_tmo;
c49c8345 555 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
43394c67 556 pg->transition_tmo = buff[5];
3588c5a2 557 else
43394c67 558 pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
3588c5a2 559
28261402 560 if (orig_transition_tmo != pg->transition_tmo) {
3588c5a2
RE
561 sdev_printk(KERN_INFO, sdev,
562 "%s: transition timeout set to %d seconds\n",
43394c67 563 ALUA_DH_NAME, pg->transition_tmo);
03197b61 564 pg->expiry = jiffies + pg->transition_tmo * HZ;
3588c5a2
RE
565 }
566
c49c8345 567 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
3588c5a2
RE
568 tpg_desc_tbl_off = 8;
569 else
570 tpg_desc_tbl_off = 4;
571
c49c8345 572 for (k = tpg_desc_tbl_off, ucp = buff + tpg_desc_tbl_off;
3588c5a2
RE
573 k < len;
574 k += off, ucp += off) {
575
43394c67
HR
576 if (pg->group_id == get_unaligned_be16(&ucp[2])) {
577 pg->state = ucp[0] & 0x0f;
578 pg->pref = ucp[0] >> 7;
057ea7c9
HR
579 valid_states = ucp[1];
580 }
581 off = 8 + (ucp[7] * 4);
582 }
583
584 sdev_printk(KERN_INFO, sdev,
dcd3a754 585 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
43394c67
HR
586 ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
587 pg->pref ? "preferred" : "non-preferred",
057ea7c9
HR
588 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
589 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
69723d17 590 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
057ea7c9
HR
591 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
592 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
593 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
594 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
595
43394c67 596 switch (pg->state) {
69723d17 597 case TPGS_STATE_TRANSITIONING:
03197b61 598 if (time_before(jiffies, pg->expiry)) {
28261402 599 /* State transition, retry */
03197b61
HR
600 pg->interval = 2;
601 err = SCSI_DH_RETRY;
602 } else {
603 /* Transitioning time exceeded, set port to standby */
604 err = SCSI_DH_IO;
605 pg->state = TPGS_STATE_STANDBY;
606 pg->expiry = 0;
057ea7c9 607 }
69723d17
HR
608 break;
609 case TPGS_STATE_OFFLINE:
e47f8976 610 /* Path unusable */
69723d17 611 err = SCSI_DH_DEV_OFFLINED;
03197b61 612 pg->expiry = 0;
69723d17
HR
613 break;
614 default:
615 /* Useable path if active */
616 err = SCSI_DH_OK;
03197b61 617 pg->expiry = 0;
69723d17 618 break;
057ea7c9 619 }
c49c8345 620 kfree(buff);
057ea7c9
HR
621 return err;
622}
623
f2ecf13a
HR
624/*
625 * alua_stpg - Issue a SET TARGET PORT GROUP command
626 *
627 * Issue a SET TARGET PORT GROUP command and evaluate the
b2460756
HR
628 * response. Returns SCSI_DH_RETRY per default to trigger
629 * a re-evaluation of the target group state or SCSI_DH_OK
630 * if no further action needs to be taken.
f2ecf13a 631 */
43394c67 632static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
f2ecf13a 633{
b2460756
HR
634 int retval;
635 struct scsi_sense_hdr sense_hdr;
f2ecf13a 636
43394c67 637 if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
b2460756
HR
638 /* Only implicit ALUA supported, retry */
639 return SCSI_DH_RETRY;
640 }
43394c67 641 switch (pg->state) {
b2460756
HR
642 case TPGS_STATE_OPTIMIZED:
643 return SCSI_DH_OK;
f2ecf13a 644 case TPGS_STATE_NONOPTIMIZED:
43394c67
HR
645 if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
646 !pg->pref &&
647 (pg->tpgs & TPGS_MODE_IMPLICIT))
b2460756 648 return SCSI_DH_OK;
f2ecf13a
HR
649 break;
650 case TPGS_STATE_STANDBY:
651 case TPGS_STATE_UNAVAILABLE:
f2ecf13a
HR
652 break;
653 case TPGS_STATE_OFFLINE:
b2460756 654 return SCSI_DH_IO;
f2ecf13a 655 case TPGS_STATE_TRANSITIONING:
f2ecf13a
HR
656 break;
657 default:
b2460756
HR
658 sdev_printk(KERN_INFO, sdev,
659 "%s: stpg failed, unhandled TPGS state %d",
43394c67 660 ALUA_DH_NAME, pg->state);
b2460756 661 return SCSI_DH_NOSYS;
f2ecf13a 662 }
43394c67 663 retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
f2ecf13a 664
b2460756 665 if (retval) {
40bb61a7 666 if (!scsi_sense_valid(&sense_hdr)) {
b2460756
HR
667 sdev_printk(KERN_INFO, sdev,
668 "%s: stpg failed, result %d",
669 ALUA_DH_NAME, retval);
40bb61a7 670 if (driver_byte(retval) == DRIVER_ERROR)
b2460756
HR
671 return SCSI_DH_DEV_TEMP_BUSY;
672 } else {
43394c67 673 sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
b2460756
HR
674 ALUA_DH_NAME);
675 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
676 }
f2ecf13a 677 }
b2460756
HR
678 /* Retry RTPG */
679 return SCSI_DH_RETRY;
f2ecf13a
HR
680}
681
03197b61
HR
682static void alua_rtpg_work(struct work_struct *work)
683{
684 struct alua_port_group *pg =
685 container_of(work, struct alua_port_group, rtpg_work.work);
686 struct scsi_device *sdev;
687 LIST_HEAD(qdata_list);
688 int err = SCSI_DH_OK;
689 struct alua_queue_data *qdata, *tmp;
690 unsigned long flags;
00642a1b 691 struct workqueue_struct *alua_wq = kaluad_wq;
03197b61
HR
692
693 spin_lock_irqsave(&pg->lock, flags);
694 sdev = pg->rtpg_sdev;
695 if (!sdev) {
696 WARN_ON(pg->flags & ALUA_PG_RUN_RTPG);
697 WARN_ON(pg->flags & ALUA_PG_RUN_STPG);
698 spin_unlock_irqrestore(&pg->lock, flags);
699 return;
700 }
00642a1b
HR
701 if (pg->flags & ALUA_SYNC_STPG)
702 alua_wq = kaluad_sync_wq;
03197b61
HR
703 pg->flags |= ALUA_PG_RUNNING;
704 if (pg->flags & ALUA_PG_RUN_RTPG) {
705 pg->flags &= ~ALUA_PG_RUN_RTPG;
706 spin_unlock_irqrestore(&pg->lock, flags);
707 err = alua_rtpg(sdev, pg);
708 spin_lock_irqsave(&pg->lock, flags);
709 if (err == SCSI_DH_RETRY) {
710 pg->flags &= ~ALUA_PG_RUNNING;
711 pg->flags |= ALUA_PG_RUN_RTPG;
712 spin_unlock_irqrestore(&pg->lock, flags);
00642a1b 713 queue_delayed_work(alua_wq, &pg->rtpg_work,
03197b61
HR
714 pg->interval * HZ);
715 return;
716 }
717 if (err != SCSI_DH_OK)
718 pg->flags &= ~ALUA_PG_RUN_STPG;
719 }
720 if (pg->flags & ALUA_PG_RUN_STPG) {
721 pg->flags &= ~ALUA_PG_RUN_STPG;
722 spin_unlock_irqrestore(&pg->lock, flags);
723 err = alua_stpg(sdev, pg);
724 spin_lock_irqsave(&pg->lock, flags);
725 if (err == SCSI_DH_RETRY) {
726 pg->flags |= ALUA_PG_RUN_RTPG;
727 pg->interval = 0;
728 pg->flags &= ~ALUA_PG_RUNNING;
729 spin_unlock_irqrestore(&pg->lock, flags);
00642a1b 730 queue_delayed_work(alua_wq, &pg->rtpg_work,
03197b61
HR
731 pg->interval * HZ);
732 return;
733 }
734 }
735
736 list_splice_init(&pg->rtpg_list, &qdata_list);
737 pg->rtpg_sdev = NULL;
738 spin_unlock_irqrestore(&pg->lock, flags);
739
740 list_for_each_entry_safe(qdata, tmp, &qdata_list, entry) {
741 list_del(&qdata->entry);
742 if (qdata->callback_fn)
743 qdata->callback_fn(qdata->callback_data, err);
744 kfree(qdata);
745 }
746 spin_lock_irqsave(&pg->lock, flags);
747 pg->flags &= ~ALUA_PG_RUNNING;
748 spin_unlock_irqrestore(&pg->lock, flags);
749 scsi_device_put(sdev);
750 kref_put(&pg->kref, release_port_group);
751}
752
753static void alua_rtpg_queue(struct alua_port_group *pg,
754 struct scsi_device *sdev,
755 struct alua_queue_data *qdata)
756{
757 int start_queue = 0;
758 unsigned long flags;
00642a1b 759 struct workqueue_struct *alua_wq = kaluad_wq;
03197b61
HR
760
761 if (!pg)
762 return;
763
764 spin_lock_irqsave(&pg->lock, flags);
765 if (qdata) {
766 list_add_tail(&qdata->entry, &pg->rtpg_list);
767 pg->flags |= ALUA_PG_RUN_STPG;
768 }
769 if (pg->rtpg_sdev == NULL) {
770 pg->interval = 0;
771 pg->flags |= ALUA_PG_RUN_RTPG;
772 kref_get(&pg->kref);
773 pg->rtpg_sdev = sdev;
774 scsi_device_get(sdev);
775 start_queue = 1;
776 }
00642a1b
HR
777 if (pg->flags & ALUA_SYNC_STPG)
778 alua_wq = kaluad_sync_wq;
03197b61
HR
779 spin_unlock_irqrestore(&pg->lock, flags);
780
781 if (start_queue &&
00642a1b 782 !queue_delayed_work(alua_wq, &pg->rtpg_work,
03197b61
HR
783 msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS))) {
784 scsi_device_put(sdev);
785 kref_put(&pg->kref, release_port_group);
786 }
787}
788
057ea7c9
HR
789/*
790 * alua_initialize - Initialize ALUA state
791 * @sdev: the device to be initialized
792 *
793 * For the prep_fn to work correctly we have
794 * to initialize the ALUA state for the device.
795 */
796static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
797{
43394c67 798 int err = SCSI_DH_DEV_UNSUPP, tpgs;
057ea7c9 799
03197b61 800 mutex_lock(&h->init_mutex);
43394c67 801 tpgs = alua_check_tpgs(sdev);
a4253fde
HR
802 if (tpgs != TPGS_MODE_NONE)
803 err = alua_check_vpd(sdev, h, tpgs);
03197b61
HR
804 h->init_error = err;
805 mutex_unlock(&h->init_mutex);
057ea7c9
HR
806 return err;
807}
4335d092
MB
808/*
809 * alua_set_params - set/unset the optimize flag
810 * @sdev: device on the path to be activated
811 * params - parameters in the following format
812 * "no_of_params\0param1\0param2\0param3\0...\0"
813 * For example, to set the flag pass the following parameters
814 * from multipath.conf
815 * hardware_handler "2 alua 1"
816 */
817static int alua_set_params(struct scsi_device *sdev, const char *params)
818{
ee14c674 819 struct alua_dh_data *h = sdev->handler_data;
03197b61 820 struct alua_port_group __rcu *pg = NULL;
4335d092
MB
821 unsigned int optimize = 0, argc;
822 const char *p = params;
823 int result = SCSI_DH_OK;
03197b61 824 unsigned long flags;
4335d092
MB
825
826 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
827 return -EINVAL;
828
829 while (*p++)
830 ;
831 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
832 return -EINVAL;
833
03197b61
HR
834 rcu_read_lock();
835 pg = rcu_dereference(h->pg);
836 if (!pg) {
837 rcu_read_unlock();
43394c67 838 return -ENXIO;
03197b61
HR
839 }
840 spin_lock_irqsave(&pg->lock, flags);
4335d092 841 if (optimize)
43394c67 842 pg->flags |= ALUA_OPTIMIZE_STPG;
4335d092 843 else
43394c67 844 pg->flags &= ~ALUA_OPTIMIZE_STPG;
03197b61
HR
845 spin_unlock_irqrestore(&pg->lock, flags);
846 rcu_read_unlock();
4335d092
MB
847
848 return result;
849}
057ea7c9
HR
850
851/*
852 * alua_activate - activate a path
853 * @sdev: device on the path to be activated
854 *
855 * We're currently switching the port group to be activated only and
856 * let the array figure out the rest.
857 * There may be other arrays which require us to switch all port groups
858 * based on a certain policy. But until we actually encounter them it
859 * should be okay.
860 */
3ae31f6a
CS
861static int alua_activate(struct scsi_device *sdev,
862 activate_complete fn, void *data)
057ea7c9 863{
ee14c674 864 struct alua_dh_data *h = sdev->handler_data;
057ea7c9 865 int err = SCSI_DH_OK;
03197b61
HR
866 struct alua_queue_data *qdata;
867 struct alua_port_group __rcu *pg;
057ea7c9 868
03197b61
HR
869 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
870 if (!qdata) {
871 err = SCSI_DH_RES_TEMP_UNAVAIL;
46ccf6b5 872 goto out;
03197b61
HR
873 }
874 qdata->callback_fn = fn;
875 qdata->callback_data = data;
876
877 mutex_lock(&h->init_mutex);
878 rcu_read_lock();
879 pg = rcu_dereference(h->pg);
880 if (!pg || !kref_get_unless_zero(&pg->kref)) {
881 rcu_read_unlock();
882 kfree(qdata);
883 err = h->init_error;
884 mutex_unlock(&h->init_mutex);
43394c67
HR
885 goto out;
886 }
03197b61
HR
887 fn = NULL;
888 rcu_read_unlock();
889 mutex_unlock(&h->init_mutex);
890
891 alua_rtpg_queue(pg, sdev, qdata);
892 kref_put(&pg->kref, release_port_group);
057ea7c9 893out:
b2460756 894 if (fn)
3ae31f6a
CS
895 fn(data, err);
896 return 0;
057ea7c9
HR
897}
898
899/*
900 * alua_prep_fn - request callback
901 *
902 * Fail I/O to all paths not in state
903 * active/optimized or active/non-optimized.
904 */
905static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
906{
ee14c674 907 struct alua_dh_data *h = sdev->handler_data;
03197b61
HR
908 struct alua_port_group __rcu *pg;
909 int state = TPGS_STATE_OPTIMIZED;
057ea7c9
HR
910 int ret = BLKPREP_OK;
911
03197b61
HR
912 rcu_read_lock();
913 pg = rcu_dereference(h->pg);
914 if (pg)
915 state = pg->state;
916 rcu_read_unlock();
43394c67 917 if (state == TPGS_STATE_TRANSITIONING)
69723d17 918 ret = BLKPREP_DEFER;
43394c67
HR
919 else if (state != TPGS_STATE_OPTIMIZED &&
920 state != TPGS_STATE_NONOPTIMIZED &&
921 state != TPGS_STATE_LBA_DEPENDENT) {
057ea7c9
HR
922 ret = BLKPREP_KILL;
923 req->cmd_flags |= REQ_QUIET;
924 }
925 return ret;
926
927}
928
057ea7c9
HR
929/*
930 * alua_bus_attach - Attach device handler
931 * @sdev: device to be attached to
932 */
ee14c674 933static int alua_bus_attach(struct scsi_device *sdev)
057ea7c9 934{
057ea7c9 935 struct alua_dh_data *h;
43394c67 936 int err, ret = -EINVAL;
057ea7c9 937
cd37743f 938 h = kzalloc(sizeof(*h) , GFP_KERNEL);
1d520328 939 if (!h)
ee14c674 940 return -ENOMEM;
03197b61
HR
941 spin_lock_init(&h->pg_lock);
942 rcu_assign_pointer(h->pg, NULL);
943 h->init_error = SCSI_DH_OK;
96e65865 944 h->sdev = sdev;
057ea7c9 945
03197b61 946 mutex_init(&h->init_mutex);
057ea7c9 947 err = alua_initialize(sdev, h);
43394c67
HR
948 if (err == SCSI_DH_NOMEM)
949 ret = -ENOMEM;
1d520328 950 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
057ea7c9
HR
951 goto failed;
952
ee14c674
CH
953 sdev->handler_data = h;
954 return 0;
057ea7c9 955failed:
cd37743f 956 kfree(h);
43394c67 957 return ret;
057ea7c9
HR
958}
959
960/*
961 * alua_bus_detach - Detach device handler
962 * @sdev: device to be detached from
963 */
964static void alua_bus_detach(struct scsi_device *sdev)
965{
ee14c674 966 struct alua_dh_data *h = sdev->handler_data;
03197b61
HR
967 struct alua_port_group *pg;
968
969 spin_lock(&h->pg_lock);
970 pg = h->pg;
971 rcu_assign_pointer(h->pg, NULL);
972 h->sdev = NULL;
973 spin_unlock(&h->pg_lock);
974 if (pg)
975 kref_put(&pg->kref, release_port_group);
057ea7c9 976
ee14c674 977 sdev->handler_data = NULL;
cd37743f 978 kfree(h);
057ea7c9
HR
979}
980
1d520328
CH
981static struct scsi_device_handler alua_dh = {
982 .name = ALUA_DH_NAME,
983 .module = THIS_MODULE,
984 .attach = alua_bus_attach,
985 .detach = alua_bus_detach,
986 .prep_fn = alua_prep_fn,
987 .check_sense = alua_check_sense,
988 .activate = alua_activate,
989 .set_params = alua_set_params,
1d520328
CH
990};
991
057ea7c9
HR
992static int __init alua_init(void)
993{
994 int r;
995
03197b61
HR
996 kaluad_wq = alloc_workqueue("kaluad", WQ_MEM_RECLAIM, 0);
997 if (!kaluad_wq) {
998 /* Temporary failure, bypass */
999 return SCSI_DH_DEV_TEMP_BUSY;
1000 }
00642a1b
HR
1001 kaluad_sync_wq = create_workqueue("kaluad_sync");
1002 if (!kaluad_sync_wq) {
1003 destroy_workqueue(kaluad_wq);
1004 return SCSI_DH_DEV_TEMP_BUSY;
1005 }
057ea7c9 1006 r = scsi_register_device_handler(&alua_dh);
03197b61 1007 if (r != 0) {
057ea7c9
HR
1008 printk(KERN_ERR "%s: Failed to register scsi device handler",
1009 ALUA_DH_NAME);
00642a1b 1010 destroy_workqueue(kaluad_sync_wq);
03197b61
HR
1011 destroy_workqueue(kaluad_wq);
1012 }
057ea7c9
HR
1013 return r;
1014}
1015
1016static void __exit alua_exit(void)
1017{
1018 scsi_unregister_device_handler(&alua_dh);
00642a1b 1019 destroy_workqueue(kaluad_sync_wq);
03197b61 1020 destroy_workqueue(kaluad_wq);
057ea7c9
HR
1021}
1022
1023module_init(alua_init);
1024module_exit(alua_exit);
1025
1026MODULE_DESCRIPTION("DM Multipath ALUA support");
1027MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1028MODULE_LICENSE("GPL");
1029MODULE_VERSION(ALUA_DH_VER);