scsi_dh_alua: Allow workqueue to run synchronously
[deliverable/linux.git] / drivers / scsi / device_handler / scsi_dh_alua.c
1 /*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
4 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
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 */
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_dbg.h>
28 #include <scsi/scsi_eh.h>
29 #include <scsi/scsi_dh.h>
30
31 #define ALUA_DH_NAME "alua"
32 #define ALUA_DH_VER "1.3"
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
38 #define TPGS_STATE_LBA_DEPENDENT 0x4
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
47 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
48 #define TPGS_SUPPORT_OFFLINE 0x40
49 #define TPGS_SUPPORT_TRANSITION 0x80
50
51 #define RTPG_FMT_MASK 0x70
52 #define RTPG_FMT_EXT_HDR 0x10
53
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
59 #define ALUA_RTPG_SIZE 128
60 #define ALUA_FAILOVER_TIMEOUT 60
61 #define ALUA_FAILOVER_RETRIES 5
62 #define ALUA_RTPG_DELAY_MSECS 5
63
64 /* device handler flags */
65 #define ALUA_OPTIMIZE_STPG 0x01
66 #define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
67 #define ALUA_SYNC_STPG 0x04
68 /* State machine flags */
69 #define ALUA_PG_RUN_RTPG 0x10
70 #define ALUA_PG_RUN_STPG 0x20
71 #define ALUA_PG_RUNNING 0x40
72
73 static uint optimize_stpg;
74 module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
75 MODULE_PARM_DESC(optimize_stpg, "Allow use of a non-optimized path, rather than sending a STPG, when implicit TPGS is supported (0=No,1=Yes). Default is 0.");
76
77 static LIST_HEAD(port_group_list);
78 static DEFINE_SPINLOCK(port_group_lock);
79 static struct workqueue_struct *kaluad_wq;
80 static struct workqueue_struct *kaluad_sync_wq;
81
82 struct alua_port_group {
83 struct kref kref;
84 struct rcu_head rcu;
85 struct list_head node;
86 unsigned char device_id_str[256];
87 int device_id_len;
88 int group_id;
89 int tpgs;
90 int state;
91 int pref;
92 unsigned flags; /* used for optimizing STPG */
93 unsigned char transition_tmo;
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;
100 };
101
102 struct alua_dh_data {
103 struct alua_port_group *pg;
104 int group_id;
105 spinlock_t pg_lock;
106 struct scsi_device *sdev;
107 int init_error;
108 struct mutex init_mutex;
109 };
110
111 struct alua_queue_data {
112 struct list_head entry;
113 activate_complete callback_fn;
114 void *callback_data;
115 };
116
117 #define ALUA_POLICY_SWITCH_CURRENT 0
118 #define ALUA_POLICY_SWITCH_ALL 1
119
120 static void alua_rtpg_work(struct work_struct *work);
121 static void alua_rtpg_queue(struct alua_port_group *pg,
122 struct scsi_device *sdev,
123 struct alua_queue_data *qdata);
124
125 static 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);
130 if (pg->rtpg_sdev)
131 flush_delayed_work(&pg->rtpg_work);
132 spin_lock(&port_group_lock);
133 list_del(&pg->node);
134 spin_unlock(&port_group_lock);
135 kfree_rcu(pg, rcu);
136 }
137
138 /*
139 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
140 * @sdev: sdev the command should be sent to
141 */
142 static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
143 int bufflen, struct scsi_sense_hdr *sshdr, int flags)
144 {
145 u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
146 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
147 REQ_FAILFAST_DRIVER;
148
149 /* Prepare the command. */
150 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN));
151 cdb[0] = MAINTENANCE_IN;
152 if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
153 cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
154 else
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);
162 }
163
164 /*
165 * submit_stpg - Issue a SET TARGET PORT GROUP command
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 */
171 static int submit_stpg(struct scsi_device *sdev, int group_id,
172 struct scsi_sense_hdr *sshdr)
173 {
174 u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)];
175 unsigned char stpg_data[8];
176 int stpg_len = 8;
177 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
178 REQ_FAILFAST_DRIVER;
179
180 /* Prepare the data buffer */
181 memset(stpg_data, 0, stpg_len);
182 stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f;
183 put_unaligned_be16(group_id, &stpg_data[6]);
184
185 /* Prepare the command. */
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);
195 }
196
197 struct 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
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 */
226 struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
227 int group_id, int tpgs)
228 {
229 struct alua_port_group *pg, *tmp_pg;
230
231 pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
232 if (!pg)
233 return ERR_PTR(-ENOMEM);
234
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 }
248 pg->group_id = group_id;
249 pg->tpgs = tpgs;
250 pg->state = TPGS_STATE_OPTIMIZED;
251 if (optimize_stpg)
252 pg->flags |= ALUA_OPTIMIZE_STPG;
253 kref_init(&pg->kref);
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);
258
259 spin_lock(&port_group_lock);
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
268 list_add(&pg->node, &port_group_list);
269 spin_unlock(&port_group_lock);
270
271 return pg;
272 }
273
274 /*
275 * alua_check_tpgs - Evaluate TPGS setting
276 * @sdev: device to be checked
277 *
278 * Examine the TPGS setting of the sdev to find out if ALUA
279 * is supported.
280 */
281 static int alua_check_tpgs(struct scsi_device *sdev)
282 {
283 int tpgs = TPGS_MODE_NONE;
284
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) {
290 sdev_printk(KERN_INFO, sdev,
291 "%s: disable for non-disk devices\n",
292 ALUA_DH_NAME);
293 return tpgs;
294 }
295
296 tpgs = scsi_device_tpgs(sdev);
297 switch (tpgs) {
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;
311 case TPGS_MODE_NONE:
312 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
313 ALUA_DH_NAME);
314 break;
315 default:
316 sdev_printk(KERN_INFO, sdev,
317 "%s: unsupported TPGS setting %d\n",
318 ALUA_DH_NAME, tpgs);
319 tpgs = TPGS_MODE_NONE;
320 break;
321 }
322
323 return tpgs;
324 }
325
326 /*
327 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
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 */
333 static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
334 int tpgs)
335 {
336 int rel_port = -1, group_id;
337 struct alua_port_group *pg, *old_pg = NULL;
338
339 group_id = scsi_vpd_tpg_id(sdev, &rel_port);
340 if (group_id < 0) {
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);
349 return SCSI_DH_DEV_UNSUPP;
350 }
351
352 pg = alua_alloc_pg(sdev, group_id, tpgs);
353 if (IS_ERR(pg)) {
354 if (PTR_ERR(pg) == -ENOMEM)
355 return SCSI_DH_NOMEM;
356 return SCSI_DH_DEV_UNSUPP;
357 }
358 sdev_printk(KERN_INFO, sdev,
359 "%s: device %s port group %x rel port %x\n",
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);
374
375 return SCSI_DH_OK;
376 }
377
378 static 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';
389 case TPGS_STATE_LBA_DEPENDENT:
390 return 'L';
391 case TPGS_STATE_OFFLINE:
392 return 'O';
393 case TPGS_STATE_TRANSITIONING:
394 return 'T';
395 default:
396 return 'X';
397 }
398 }
399
400 static 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 */
409 return ADD_TO_MLQUEUE;
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 */
416 return ADD_TO_MLQUEUE;
417 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
418 /*
419 * Device internal reset
420 */
421 return ADD_TO_MLQUEUE;
422 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
423 /*
424 * Mode Parameters Changed
425 */
426 return ADD_TO_MLQUEUE;
427 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
428 /*
429 * ALUA state changed
430 */
431 return ADD_TO_MLQUEUE;
432 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
433 /*
434 * Implicit ALUA state transition failed
435 */
436 return ADD_TO_MLQUEUE;
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)
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;
449 break;
450 }
451
452 return SCSI_RETURN_NOT_HANDLED;
453 }
454
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
461 * found to be unusable.
462 */
463 static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
464 {
465 struct scsi_sense_hdr sense_hdr;
466 int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE;
467 unsigned char *ucp, *buff;
468 unsigned err, retval;
469 unsigned int tpg_desc_tbl_off;
470 unsigned char orig_transition_tmo;
471
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 }
480
481 buff = kzalloc(bufflen, GFP_KERNEL);
482 if (!buff)
483 return SCSI_DH_DEV_TEMP_BUSY;
484
485 retry:
486 retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
487
488 if (retval) {
489 if (!scsi_sense_valid(&sense_hdr)) {
490 sdev_printk(KERN_INFO, sdev,
491 "%s: rtpg failed, result %d\n",
492 ALUA_DH_NAME, retval);
493 kfree(buff);
494 if (driver_byte(retval) == DRIVER_ERROR)
495 return SCSI_DH_DEV_TEMP_BUSY;
496 return SCSI_DH_IO;
497 }
498
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 */
507 if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
508 sense_hdr.sense_key == ILLEGAL_REQUEST &&
509 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
510 pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
511 goto retry;
512 }
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;
522 if (err == SCSI_DH_RETRY &&
523 pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
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);
527 return err;
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);
532 kfree(buff);
533 pg->expiry = 0;
534 return SCSI_DH_IO;
535 }
536
537 len = get_unaligned_be32(&buff[0]) + 4;
538
539 if (len > bufflen) {
540 /* Resubmit with the correct length */
541 kfree(buff);
542 bufflen = len;
543 buff = kmalloc(bufflen, GFP_KERNEL);
544 if (!buff) {
545 sdev_printk(KERN_WARNING, sdev,
546 "%s: kmalloc buffer failed\n",__func__);
547 /* Temporary failure, bypass */
548 pg->expiry = 0;
549 return SCSI_DH_DEV_TEMP_BUSY;
550 }
551 goto retry;
552 }
553
554 orig_transition_tmo = pg->transition_tmo;
555 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
556 pg->transition_tmo = buff[5];
557 else
558 pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
559
560 if (orig_transition_tmo != pg->transition_tmo) {
561 sdev_printk(KERN_INFO, sdev,
562 "%s: transition timeout set to %d seconds\n",
563 ALUA_DH_NAME, pg->transition_tmo);
564 pg->expiry = jiffies + pg->transition_tmo * HZ;
565 }
566
567 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
568 tpg_desc_tbl_off = 8;
569 else
570 tpg_desc_tbl_off = 4;
571
572 for (k = tpg_desc_tbl_off, ucp = buff + tpg_desc_tbl_off;
573 k < len;
574 k += off, ucp += off) {
575
576 if (pg->group_id == get_unaligned_be16(&ucp[2])) {
577 pg->state = ucp[0] & 0x0f;
578 pg->pref = ucp[0] >> 7;
579 valid_states = ucp[1];
580 }
581 off = 8 + (ucp[7] * 4);
582 }
583
584 sdev_printk(KERN_INFO, sdev,
585 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
586 ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
587 pg->pref ? "preferred" : "non-preferred",
588 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
589 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
590 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
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
596 switch (pg->state) {
597 case TPGS_STATE_TRANSITIONING:
598 if (time_before(jiffies, pg->expiry)) {
599 /* State transition, retry */
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;
607 }
608 break;
609 case TPGS_STATE_OFFLINE:
610 /* Path unusable */
611 err = SCSI_DH_DEV_OFFLINED;
612 pg->expiry = 0;
613 break;
614 default:
615 /* Useable path if active */
616 err = SCSI_DH_OK;
617 pg->expiry = 0;
618 break;
619 }
620 kfree(buff);
621 return err;
622 }
623
624 /*
625 * alua_stpg - Issue a SET TARGET PORT GROUP command
626 *
627 * Issue a SET TARGET PORT GROUP command and evaluate the
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.
631 */
632 static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
633 {
634 int retval;
635 struct scsi_sense_hdr sense_hdr;
636
637 if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
638 /* Only implicit ALUA supported, retry */
639 return SCSI_DH_RETRY;
640 }
641 switch (pg->state) {
642 case TPGS_STATE_OPTIMIZED:
643 return SCSI_DH_OK;
644 case TPGS_STATE_NONOPTIMIZED:
645 if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
646 !pg->pref &&
647 (pg->tpgs & TPGS_MODE_IMPLICIT))
648 return SCSI_DH_OK;
649 break;
650 case TPGS_STATE_STANDBY:
651 case TPGS_STATE_UNAVAILABLE:
652 break;
653 case TPGS_STATE_OFFLINE:
654 return SCSI_DH_IO;
655 case TPGS_STATE_TRANSITIONING:
656 break;
657 default:
658 sdev_printk(KERN_INFO, sdev,
659 "%s: stpg failed, unhandled TPGS state %d",
660 ALUA_DH_NAME, pg->state);
661 return SCSI_DH_NOSYS;
662 }
663 retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
664
665 if (retval) {
666 if (!scsi_sense_valid(&sense_hdr)) {
667 sdev_printk(KERN_INFO, sdev,
668 "%s: stpg failed, result %d",
669 ALUA_DH_NAME, retval);
670 if (driver_byte(retval) == DRIVER_ERROR)
671 return SCSI_DH_DEV_TEMP_BUSY;
672 } else {
673 sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
674 ALUA_DH_NAME);
675 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
676 }
677 }
678 /* Retry RTPG */
679 return SCSI_DH_RETRY;
680 }
681
682 static 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;
691 struct workqueue_struct *alua_wq = kaluad_wq;
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 }
701 if (pg->flags & ALUA_SYNC_STPG)
702 alua_wq = kaluad_sync_wq;
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);
713 queue_delayed_work(alua_wq, &pg->rtpg_work,
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);
730 queue_delayed_work(alua_wq, &pg->rtpg_work,
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
753 static 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;
759 struct workqueue_struct *alua_wq = kaluad_wq;
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 }
777 if (pg->flags & ALUA_SYNC_STPG)
778 alua_wq = kaluad_sync_wq;
779 spin_unlock_irqrestore(&pg->lock, flags);
780
781 if (start_queue &&
782 !queue_delayed_work(alua_wq, &pg->rtpg_work,
783 msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS))) {
784 scsi_device_put(sdev);
785 kref_put(&pg->kref, release_port_group);
786 }
787 }
788
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 */
796 static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
797 {
798 int err = SCSI_DH_DEV_UNSUPP, tpgs;
799
800 mutex_lock(&h->init_mutex);
801 tpgs = alua_check_tpgs(sdev);
802 if (tpgs != TPGS_MODE_NONE)
803 err = alua_check_vpd(sdev, h, tpgs);
804 h->init_error = err;
805 mutex_unlock(&h->init_mutex);
806 return err;
807 }
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 */
817 static int alua_set_params(struct scsi_device *sdev, const char *params)
818 {
819 struct alua_dh_data *h = sdev->handler_data;
820 struct alua_port_group __rcu *pg = NULL;
821 unsigned int optimize = 0, argc;
822 const char *p = params;
823 int result = SCSI_DH_OK;
824 unsigned long flags;
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
834 rcu_read_lock();
835 pg = rcu_dereference(h->pg);
836 if (!pg) {
837 rcu_read_unlock();
838 return -ENXIO;
839 }
840 spin_lock_irqsave(&pg->lock, flags);
841 if (optimize)
842 pg->flags |= ALUA_OPTIMIZE_STPG;
843 else
844 pg->flags &= ~ALUA_OPTIMIZE_STPG;
845 spin_unlock_irqrestore(&pg->lock, flags);
846 rcu_read_unlock();
847
848 return result;
849 }
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 */
861 static int alua_activate(struct scsi_device *sdev,
862 activate_complete fn, void *data)
863 {
864 struct alua_dh_data *h = sdev->handler_data;
865 int err = SCSI_DH_OK;
866 struct alua_queue_data *qdata;
867 struct alua_port_group __rcu *pg;
868
869 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
870 if (!qdata) {
871 err = SCSI_DH_RES_TEMP_UNAVAIL;
872 goto out;
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);
885 goto out;
886 }
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);
893 out:
894 if (fn)
895 fn(data, err);
896 return 0;
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 */
905 static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
906 {
907 struct alua_dh_data *h = sdev->handler_data;
908 struct alua_port_group __rcu *pg;
909 int state = TPGS_STATE_OPTIMIZED;
910 int ret = BLKPREP_OK;
911
912 rcu_read_lock();
913 pg = rcu_dereference(h->pg);
914 if (pg)
915 state = pg->state;
916 rcu_read_unlock();
917 if (state == TPGS_STATE_TRANSITIONING)
918 ret = BLKPREP_DEFER;
919 else if (state != TPGS_STATE_OPTIMIZED &&
920 state != TPGS_STATE_NONOPTIMIZED &&
921 state != TPGS_STATE_LBA_DEPENDENT) {
922 ret = BLKPREP_KILL;
923 req->cmd_flags |= REQ_QUIET;
924 }
925 return ret;
926
927 }
928
929 /*
930 * alua_bus_attach - Attach device handler
931 * @sdev: device to be attached to
932 */
933 static int alua_bus_attach(struct scsi_device *sdev)
934 {
935 struct alua_dh_data *h;
936 int err, ret = -EINVAL;
937
938 h = kzalloc(sizeof(*h) , GFP_KERNEL);
939 if (!h)
940 return -ENOMEM;
941 spin_lock_init(&h->pg_lock);
942 rcu_assign_pointer(h->pg, NULL);
943 h->init_error = SCSI_DH_OK;
944 h->sdev = sdev;
945
946 mutex_init(&h->init_mutex);
947 err = alua_initialize(sdev, h);
948 if (err == SCSI_DH_NOMEM)
949 ret = -ENOMEM;
950 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
951 goto failed;
952
953 sdev->handler_data = h;
954 return 0;
955 failed:
956 kfree(h);
957 return ret;
958 }
959
960 /*
961 * alua_bus_detach - Detach device handler
962 * @sdev: device to be detached from
963 */
964 static void alua_bus_detach(struct scsi_device *sdev)
965 {
966 struct alua_dh_data *h = sdev->handler_data;
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);
976
977 sdev->handler_data = NULL;
978 kfree(h);
979 }
980
981 static 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,
990 };
991
992 static int __init alua_init(void)
993 {
994 int r;
995
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 }
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 }
1006 r = scsi_register_device_handler(&alua_dh);
1007 if (r != 0) {
1008 printk(KERN_ERR "%s: Failed to register scsi device handler",
1009 ALUA_DH_NAME);
1010 destroy_workqueue(kaluad_sync_wq);
1011 destroy_workqueue(kaluad_wq);
1012 }
1013 return r;
1014 }
1015
1016 static void __exit alua_exit(void)
1017 {
1018 scsi_unregister_device_handler(&alua_dh);
1019 destroy_workqueue(kaluad_sync_wq);
1020 destroy_workqueue(kaluad_wq);
1021 }
1022
1023 module_init(alua_init);
1024 module_exit(alua_exit);
1025
1026 MODULE_DESCRIPTION("DM Multipath ALUA support");
1027 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1028 MODULE_LICENSE("GPL");
1029 MODULE_VERSION(ALUA_DH_VER);
This page took 0.13467 seconds and 5 git commands to generate.