Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Engenio/LSI RDAC SCSI Device Handler
  3 *
  4 * Copyright (C) 2005 Mike Christie. All rights reserved.
  5 * Copyright (C) Chandra Seetharaman, IBM Corp. 2007
  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 <scsi/scsi.h>
 23#include <scsi/scsi_eh.h>
 24#include <scsi/scsi_dh.h>
 25#include <linux/workqueue.h>
 26#include <linux/slab.h>
 
 27
 28#define RDAC_NAME "rdac"
 29#define RDAC_RETRY_COUNT 5
 30
 31/*
 32 * LSI mode page stuff
 33 *
 34 * These struct definitions and the forming of the
 35 * mode page were taken from the LSI RDAC 2.4 GPL'd
 36 * driver, and then converted to Linux conventions.
 37 */
 38#define RDAC_QUIESCENCE_TIME 20
 39/*
 40 * Page Codes
 41 */
 42#define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
 43
 44/*
 45 * Controller modes definitions
 46 */
 47#define RDAC_MODE_TRANSFER_SPECIFIED_LUNS	0x02
 48
 49/*
 50 * RDAC Options field
 51 */
 52#define RDAC_FORCED_QUIESENCE 0x02
 53
 54#define RDAC_TIMEOUT	(60 * HZ)
 55#define RDAC_RETRIES	3
 56
 57struct rdac_mode_6_hdr {
 58	u8	data_len;
 59	u8	medium_type;
 60	u8	device_params;
 61	u8	block_desc_len;
 62};
 63
 64struct rdac_mode_10_hdr {
 65	u16	data_len;
 66	u8	medium_type;
 67	u8	device_params;
 68	u16	reserved;
 69	u16	block_desc_len;
 70};
 71
 72struct rdac_mode_common {
 73	u8	controller_serial[16];
 74	u8	alt_controller_serial[16];
 75	u8	rdac_mode[2];
 76	u8	alt_rdac_mode[2];
 77	u8	quiescence_timeout;
 78	u8	rdac_options;
 79};
 80
 81struct rdac_pg_legacy {
 82	struct rdac_mode_6_hdr hdr;
 83	u8	page_code;
 84	u8	page_len;
 85	struct rdac_mode_common common;
 86#define MODE6_MAX_LUN	32
 87	u8	lun_table[MODE6_MAX_LUN];
 88	u8	reserved2[32];
 89	u8	reserved3;
 90	u8	reserved4;
 91};
 92
 93struct rdac_pg_expanded {
 94	struct rdac_mode_10_hdr hdr;
 95	u8	page_code;
 96	u8	subpage_code;
 97	u8	page_len[2];
 98	struct rdac_mode_common common;
 99	u8	lun_table[256];
100	u8	reserved3;
101	u8	reserved4;
102};
103
104struct c9_inquiry {
105	u8	peripheral_info;
106	u8	page_code;	/* 0xC9 */
107	u8	reserved1;
108	u8	page_len;
109	u8	page_id[4];	/* "vace" */
110	u8	avte_cvp;
111	u8	path_prio;
112	u8	reserved2[38];
113};
114
115#define SUBSYS_ID_LEN	16
116#define SLOT_ID_LEN	2
117#define ARRAY_LABEL_LEN	31
118
119struct c4_inquiry {
120	u8	peripheral_info;
121	u8	page_code;	/* 0xC4 */
122	u8	reserved1;
123	u8	page_len;
124	u8	page_id[4];	/* "subs" */
125	u8	subsys_id[SUBSYS_ID_LEN];
126	u8	revision[4];
127	u8	slot_id[SLOT_ID_LEN];
128	u8	reserved[2];
129};
130
131#define UNIQUE_ID_LEN 16
132struct c8_inquiry {
133	u8	peripheral_info;
134	u8	page_code; /* 0xC8 */
135	u8	reserved1;
136	u8	page_len;
137	u8	page_id[4]; /* "edid" */
138	u8	reserved2[3];
139	u8	vol_uniq_id_len;
140	u8	vol_uniq_id[16];
141	u8	vol_user_label_len;
142	u8	vol_user_label[60];
143	u8	array_uniq_id_len;
144	u8	array_unique_id[UNIQUE_ID_LEN];
145	u8	array_user_label_len;
146	u8	array_user_label[60];
147	u8	lun[8];
148};
149
150struct rdac_controller {
151	u8			array_id[UNIQUE_ID_LEN];
152	int			use_ms10;
153	struct kref		kref;
154	struct list_head	node; /* list of all controllers */
155	union			{
156		struct rdac_pg_legacy legacy;
157		struct rdac_pg_expanded expanded;
158	} mode_select;
159	u8	index;
160	u8	array_name[ARRAY_LABEL_LEN];
161	struct Scsi_Host	*host;
162	spinlock_t		ms_lock;
163	int			ms_queued;
164	struct work_struct	ms_work;
165	struct scsi_device	*ms_sdev;
166	struct list_head	ms_head;
 
167};
168
169struct c2_inquiry {
170	u8	peripheral_info;
171	u8	page_code;	/* 0xC2 */
172	u8	reserved1;
173	u8	page_len;
174	u8	page_id[4];	/* "swr4" */
175	u8	sw_version[3];
176	u8	sw_date[3];
177	u8	features_enabled;
178	u8	max_lun_supported;
179	u8	partitions[239]; /* Total allocation length should be 0xFF */
180};
181
182struct rdac_dh_data {
 
183	struct rdac_controller	*ctlr;
 
184#define UNINITIALIZED_LUN	(1 << 8)
185	unsigned		lun;
186
187#define RDAC_MODE		0
188#define RDAC_MODE_AVT		1
189#define RDAC_MODE_IOSHIP	2
190	unsigned char		mode;
191
192#define RDAC_STATE_ACTIVE	0
193#define RDAC_STATE_PASSIVE	1
194	unsigned char		state;
195
196#define RDAC_LUN_UNOWNED	0
197#define RDAC_LUN_OWNED		1
198	char			lun_state;
199
200#define RDAC_PREFERRED		0
201#define RDAC_NON_PREFERRED	1
202	char			preferred;
203
204	unsigned char		sense[SCSI_SENSE_BUFFERSIZE];
205	union			{
206		struct c2_inquiry c2;
207		struct c4_inquiry c4;
208		struct c8_inquiry c8;
209		struct c9_inquiry c9;
210	} inq;
211};
212
213static const char *mode[] = {
214	"RDAC",
215	"AVT",
216	"IOSHIP",
217};
218static const char *lun_state[] =
219{
220	"unowned",
221	"owned",
222};
223
224struct rdac_queue_data {
225	struct list_head	entry;
226	struct rdac_dh_data	*h;
227	activate_complete	callback_fn;
228	void			*callback_data;
229};
230
231static LIST_HEAD(ctlr_list);
232static DEFINE_SPINLOCK(list_lock);
233static struct workqueue_struct *kmpath_rdacd;
234static void send_mode_select(struct work_struct *work);
235
236/*
237 * module parameter to enable rdac debug logging.
238 * 2 bits for each type of logging, only two types defined for now
239 * Can be enhanced if required at later point
240 */
241static int rdac_logging = 1;
242module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
243MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
244		"Default is 1 - failover logging enabled, "
245		"set it to 0xF to enable all the logs");
246
247#define RDAC_LOG_FAILOVER	0
248#define RDAC_LOG_SENSE		2
249
250#define RDAC_LOG_BITS		2
251
252#define RDAC_LOG_LEVEL(SHIFT)  \
253	((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
254
255#define RDAC_LOG(SHIFT, sdev, f, arg...) \
256do { \
257	if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
258		sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
259} while (0);
260
261static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
 
 
262{
263	struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
264	BUG_ON(scsi_dh_data == NULL);
265	return ((struct rdac_dh_data *) scsi_dh_data->buf);
266}
267
268static struct request *get_rdac_req(struct scsi_device *sdev,
269			void *buffer, unsigned buflen, int rw)
270{
271	struct request *rq;
272	struct request_queue *q = sdev->request_queue;
273
274	rq = blk_get_request(q, rw, GFP_NOIO);
275
276	if (!rq) {
277		sdev_printk(KERN_INFO, sdev,
278				"get_rdac_req: blk_get_request failed.\n");
279		return NULL;
280	}
281
282	if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
283		blk_put_request(rq);
284		sdev_printk(KERN_INFO, sdev,
285				"get_rdac_req: blk_rq_map_kern failed.\n");
286		return NULL;
287	}
288
289	rq->cmd_type = REQ_TYPE_BLOCK_PC;
290	rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
291			 REQ_FAILFAST_DRIVER;
292	rq->retries = RDAC_RETRIES;
293	rq->timeout = RDAC_TIMEOUT;
294
295	return rq;
296}
297
298static struct request *rdac_failover_get(struct scsi_device *sdev,
299			struct rdac_dh_data *h, struct list_head *list)
300{
301	struct request *rq;
302	struct rdac_mode_common *common;
303	unsigned data_size;
304	struct rdac_queue_data *qdata;
305	u8 *lun_table;
306
307	if (h->ctlr->use_ms10) {
308		struct rdac_pg_expanded *rdac_pg;
309
310		data_size = sizeof(struct rdac_pg_expanded);
311		rdac_pg = &h->ctlr->mode_select.expanded;
312		memset(rdac_pg, 0, data_size);
313		common = &rdac_pg->common;
314		rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
315		rdac_pg->subpage_code = 0x1;
316		rdac_pg->page_len[0] = 0x01;
317		rdac_pg->page_len[1] = 0x28;
318		lun_table = rdac_pg->lun_table;
319	} else {
320		struct rdac_pg_legacy *rdac_pg;
321
322		data_size = sizeof(struct rdac_pg_legacy);
323		rdac_pg = &h->ctlr->mode_select.legacy;
324		memset(rdac_pg, 0, data_size);
325		common = &rdac_pg->common;
326		rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
327		rdac_pg->page_len = 0x68;
328		lun_table = rdac_pg->lun_table;
329	}
330	common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
331	common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
332	common->rdac_options = RDAC_FORCED_QUIESENCE;
333
334	list_for_each_entry(qdata, list, entry) {
335		lun_table[qdata->h->lun] = 0x81;
336	}
337
338	/* get request for block layer packet command */
339	rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
340	if (!rq)
341		return NULL;
342
343	/* Prepare the command. */
344	if (h->ctlr->use_ms10) {
345		rq->cmd[0] = MODE_SELECT_10;
346		rq->cmd[7] = data_size >> 8;
347		rq->cmd[8] = data_size & 0xff;
348	} else {
349		rq->cmd[0] = MODE_SELECT;
350		rq->cmd[4] = data_size;
351	}
352	rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
353
354	rq->sense = h->sense;
355	memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
356	rq->sense_len = 0;
357
358	return rq;
359}
360
361static void release_controller(struct kref *kref)
362{
363	struct rdac_controller *ctlr;
364	ctlr = container_of(kref, struct rdac_controller, kref);
365
366	flush_workqueue(kmpath_rdacd);
367	spin_lock(&list_lock);
368	list_del(&ctlr->node);
369	spin_unlock(&list_lock);
370	kfree(ctlr);
371}
372
373static struct rdac_controller *get_controller(int index, char *array_name,
374			u8 *array_id, struct scsi_device *sdev)
375{
376	struct rdac_controller *ctlr, *tmp;
377
378	spin_lock(&list_lock);
379
380	list_for_each_entry(tmp, &ctlr_list, node) {
381		if ((memcmp(tmp->array_id, array_id, UNIQUE_ID_LEN) == 0) &&
382			  (tmp->index == index) &&
383			  (tmp->host == sdev->host)) {
384			kref_get(&tmp->kref);
385			spin_unlock(&list_lock);
386			return tmp;
387		}
388	}
389	ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
390	if (!ctlr)
391		goto done;
392
393	/* initialize fields of controller */
394	memcpy(ctlr->array_id, array_id, UNIQUE_ID_LEN);
395	ctlr->index = index;
396	ctlr->host = sdev->host;
397	memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
398
399	kref_init(&ctlr->kref);
400	ctlr->use_ms10 = -1;
401	ctlr->ms_queued = 0;
402	ctlr->ms_sdev = NULL;
403	spin_lock_init(&ctlr->ms_lock);
404	INIT_WORK(&ctlr->ms_work, send_mode_select);
405	INIT_LIST_HEAD(&ctlr->ms_head);
406	list_add(&ctlr->node, &ctlr_list);
407done:
408	spin_unlock(&list_lock);
409	return ctlr;
410}
411
412static int submit_inquiry(struct scsi_device *sdev, int page_code,
413			  unsigned int len, struct rdac_dh_data *h)
414{
415	struct request *rq;
416	struct request_queue *q = sdev->request_queue;
417	int err = SCSI_DH_RES_TEMP_UNAVAIL;
418
419	rq = get_rdac_req(sdev, &h->inq, len, READ);
420	if (!rq)
421		goto done;
422
423	/* Prepare the command. */
424	rq->cmd[0] = INQUIRY;
425	rq->cmd[1] = 1;
426	rq->cmd[2] = page_code;
427	rq->cmd[4] = len;
428	rq->cmd_len = COMMAND_SIZE(INQUIRY);
429
430	rq->sense = h->sense;
431	memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
432	rq->sense_len = 0;
433
434	err = blk_execute_rq(q, NULL, rq, 1);
435	if (err == -EIO)
436		err = SCSI_DH_IO;
437
438	blk_put_request(rq);
439done:
440	return err;
441}
442
443static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
444			char *array_name, u8 *array_id)
445{
446	int err, i;
447	struct c8_inquiry *inqp;
448
449	err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
450	if (err == SCSI_DH_OK) {
451		inqp = &h->inq.c8;
452		if (inqp->page_code != 0xc8)
453			return SCSI_DH_NOSYS;
454		if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
455		    inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
456			return SCSI_DH_NOSYS;
457		h->lun = inqp->lun[7]; /* Uses only the last byte */
458
459		for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
460			*(array_name+i) = inqp->array_user_label[(2*i)+1];
461
462		*(array_name+ARRAY_LABEL_LEN-1) = '\0';
463		memset(array_id, 0, UNIQUE_ID_LEN);
464		memcpy(array_id, inqp->array_unique_id, inqp->array_uniq_id_len);
 
465	}
466	return err;
467}
468
469static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
470{
471	int err;
472	struct c9_inquiry *inqp;
 
473
474	h->state = RDAC_STATE_ACTIVE;
475	err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
476	if (err == SCSI_DH_OK) {
477		inqp = &h->inq.c9;
478		/* detect the operating mode */
479		if ((inqp->avte_cvp >> 5) & 0x1)
480			h->mode = RDAC_MODE_IOSHIP; /* LUN in IOSHIP mode */
481		else if (inqp->avte_cvp >> 7)
482			h->mode = RDAC_MODE_AVT; /* LUN in AVT mode */
483		else
484			h->mode = RDAC_MODE; /* LUN in RDAC mode */
485
486		/* Update ownership */
487		if (inqp->avte_cvp & 0x1)
488			h->lun_state = RDAC_LUN_OWNED;
489		else {
 
490			h->lun_state = RDAC_LUN_UNOWNED;
491			if (h->mode == RDAC_MODE)
492				h->state = RDAC_STATE_PASSIVE;
 
 
 
493		}
494
495		/* Update path prio*/
496		if (inqp->path_prio & 0x1)
497			h->preferred = RDAC_PREFERRED;
498		else
 
499			h->preferred = RDAC_NON_PREFERRED;
 
 
 
 
 
 
 
 
500	}
501
502	return err;
503}
504
505static int initialize_controller(struct scsi_device *sdev,
506		struct rdac_dh_data *h, char *array_name, u8 *array_id)
507{
508	int err, index;
509	struct c4_inquiry *inqp;
510
511	err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
512	if (err == SCSI_DH_OK) {
513		inqp = &h->inq.c4;
514		/* get the controller index */
515		if (inqp->slot_id[1] == 0x31)
516			index = 0;
517		else
518			index = 1;
 
 
519		h->ctlr = get_controller(index, array_name, array_id, sdev);
520		if (!h->ctlr)
521			err = SCSI_DH_RES_TEMP_UNAVAIL;
 
 
 
 
 
 
522	}
523	return err;
524}
525
526static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
527{
528	int err;
529	struct c2_inquiry *inqp;
530
531	err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
532	if (err == SCSI_DH_OK) {
533		inqp = &h->inq.c2;
534		/*
535		 * If more than MODE6_MAX_LUN luns are supported, use
536		 * mode select 10
537		 */
538		if (inqp->max_lun_supported >= MODE6_MAX_LUN)
539			h->ctlr->use_ms10 = 1;
540		else
541			h->ctlr->use_ms10 = 0;
 
542	}
543	return err;
544}
545
546static int mode_select_handle_sense(struct scsi_device *sdev,
547					unsigned char *sensebuf)
548{
549	struct scsi_sense_hdr sense_hdr;
550	int err = SCSI_DH_IO, ret;
551	struct rdac_dh_data *h = get_rdac_data(sdev);
552
553	ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
554	if (!ret)
555		goto done;
556
557	switch (sense_hdr.sense_key) {
558	case NO_SENSE:
559	case ABORTED_COMMAND:
560	case UNIT_ATTENTION:
561		err = SCSI_DH_RETRY;
562		break;
563	case NOT_READY:
564		if (sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x01)
565			/* LUN Not Ready and is in the Process of Becoming
566			 * Ready
567			 */
568			err = SCSI_DH_RETRY;
569		break;
570	case ILLEGAL_REQUEST:
571		if (sense_hdr.asc == 0x91 && sense_hdr.ascq == 0x36)
572			/*
573			 * Command Lock contention
574			 */
575			err = SCSI_DH_RETRY;
576		break;
577	default:
578		break;
579	}
580
581	RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
582		"MODE_SELECT returned with sense %02x/%02x/%02x",
583		(char *) h->ctlr->array_name, h->ctlr->index,
584		sense_hdr.sense_key, sense_hdr.asc, sense_hdr.ascq);
585
586done:
587	return err;
588}
589
590static void send_mode_select(struct work_struct *work)
591{
592	struct rdac_controller *ctlr =
593		container_of(work, struct rdac_controller, ms_work);
594	struct request *rq;
595	struct scsi_device *sdev = ctlr->ms_sdev;
596	struct rdac_dh_data *h = get_rdac_data(sdev);
597	struct request_queue *q = sdev->request_queue;
598	int err, retry_cnt = RDAC_RETRY_COUNT;
599	struct rdac_queue_data *tmp, *qdata;
600	LIST_HEAD(list);
 
 
 
 
 
601
602	spin_lock(&ctlr->ms_lock);
603	list_splice_init(&ctlr->ms_head, &list);
604	ctlr->ms_queued = 0;
605	ctlr->ms_sdev = NULL;
606	spin_unlock(&ctlr->ms_lock);
607
608retry:
609	err = SCSI_DH_RES_TEMP_UNAVAIL;
610	rq = rdac_failover_get(sdev, h, &list);
611	if (!rq)
612		goto done;
613
614	RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
615		"%s MODE_SELECT command",
616		(char *) h->ctlr->array_name, h->ctlr->index,
617		(retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
618
619	err = blk_execute_rq(q, NULL, rq, 1);
620	blk_put_request(rq);
621	if (err != SCSI_DH_OK) {
622		err = mode_select_handle_sense(sdev, h->sense);
623		if (err == SCSI_DH_RETRY && retry_cnt--)
624			goto retry;
 
 
625	}
626	if (err == SCSI_DH_OK) {
627		h->state = RDAC_STATE_ACTIVE;
628		RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
629				"MODE_SELECT completed",
630				(char *) h->ctlr->array_name, h->ctlr->index);
631	}
632
633done:
634	list_for_each_entry_safe(qdata, tmp, &list, entry) {
635		list_del(&qdata->entry);
636		if (err == SCSI_DH_OK)
637			qdata->h->state = RDAC_STATE_ACTIVE;
638		if (qdata->callback_fn)
639			qdata->callback_fn(qdata->callback_data, err);
640		kfree(qdata);
641	}
642	return;
643}
644
645static int queue_mode_select(struct scsi_device *sdev,
646				activate_complete fn, void *data)
647{
648	struct rdac_queue_data *qdata;
649	struct rdac_controller *ctlr;
650
651	qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
652	if (!qdata)
653		return SCSI_DH_RETRY;
654
655	qdata->h = get_rdac_data(sdev);
656	qdata->callback_fn = fn;
657	qdata->callback_data = data;
658
659	ctlr = qdata->h->ctlr;
660	spin_lock(&ctlr->ms_lock);
661	list_add_tail(&qdata->entry, &ctlr->ms_head);
662	if (!ctlr->ms_queued) {
663		ctlr->ms_queued = 1;
664		ctlr->ms_sdev = sdev;
665		queue_work(kmpath_rdacd, &ctlr->ms_work);
666	}
667	spin_unlock(&ctlr->ms_lock);
668	return SCSI_DH_OK;
669}
670
671static int rdac_activate(struct scsi_device *sdev,
672			activate_complete fn, void *data)
673{
674	struct rdac_dh_data *h = get_rdac_data(sdev);
675	int err = SCSI_DH_OK;
676	int act = 0;
677
678	err = check_ownership(sdev, h);
679	if (err != SCSI_DH_OK)
680		goto done;
681
682	switch (h->mode) {
683	case RDAC_MODE:
684		if (h->lun_state == RDAC_LUN_UNOWNED)
685			act = 1;
686		break;
687	case RDAC_MODE_IOSHIP:
688		if ((h->lun_state == RDAC_LUN_UNOWNED) &&
689		    (h->preferred == RDAC_PREFERRED))
690			act = 1;
691		break;
692	default:
693		break;
694	}
695
696	if (act) {
697		err = queue_mode_select(sdev, fn, data);
698		if (err == SCSI_DH_OK)
699			return 0;
700	}
701done:
702	if (fn)
703		fn(data, err);
704	return 0;
705}
706
707static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
708{
709	struct rdac_dh_data *h = get_rdac_data(sdev);
710	int ret = BLKPREP_OK;
711
712	if (h->state != RDAC_STATE_ACTIVE) {
713		ret = BLKPREP_KILL;
714		req->cmd_flags |= REQ_QUIET;
715	}
716	return ret;
717
 
718}
719
720static int rdac_check_sense(struct scsi_device *sdev,
721				struct scsi_sense_hdr *sense_hdr)
722{
723	struct rdac_dh_data *h = get_rdac_data(sdev);
724
725	RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
726			"I/O returned with sense %02x/%02x/%02x",
727			(char *) h->ctlr->array_name, h->ctlr->index,
728			sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
729
730	switch (sense_hdr->sense_key) {
731	case NOT_READY:
732		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
733			/* LUN Not Ready - Logical Unit Not Ready and is in
734			* the process of becoming ready
735			* Just retry.
736			*/
737			return ADD_TO_MLQUEUE;
738		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
739			/* LUN Not Ready - Storage firmware incompatible
740			 * Manual code synchonisation required.
741			 *
742			 * Nothing we can do here. Try to bypass the path.
743			 */
744			return SUCCESS;
745		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
746			/* LUN Not Ready - Quiescense in progress
747			 *
748			 * Just retry and wait.
749			 */
750			return ADD_TO_MLQUEUE;
751		if (sense_hdr->asc == 0xA1  && sense_hdr->ascq == 0x02)
752			/* LUN Not Ready - Quiescense in progress
753			 * or has been achieved
754			 * Just retry.
755			 */
756			return ADD_TO_MLQUEUE;
757		break;
758	case ILLEGAL_REQUEST:
759		if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
760			/* Invalid Request - Current Logical Unit Ownership.
761			 * Controller is not the current owner of the LUN,
762			 * Fail the path, so that the other path be used.
763			 */
764			h->state = RDAC_STATE_PASSIVE;
765			return SUCCESS;
766		}
767		break;
768	case UNIT_ATTENTION:
769		if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
770			/*
771			 * Power On, Reset, or Bus Device Reset, just retry.
772			 */
773			return ADD_TO_MLQUEUE;
774		if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
775			/*
776			 * Quiescence in progress , just retry.
777			 */
778			return ADD_TO_MLQUEUE;
779		break;
780	}
781	/* success just means we do not care what scsi-ml does */
782	return SCSI_RETURN_NOT_HANDLED;
783}
784
785static const struct scsi_dh_devlist rdac_dev_list[] = {
786	{"IBM", "1722"},
787	{"IBM", "1724"},
788	{"IBM", "1726"},
789	{"IBM", "1742"},
790	{"IBM", "1745"},
791	{"IBM", "1746"},
792	{"IBM", "1814"},
793	{"IBM", "1815"},
794	{"IBM", "1818"},
795	{"IBM", "3526"},
796	{"SGI", "TP9400"},
797	{"SGI", "TP9500"},
798	{"SGI", "IS"},
799	{"STK", "OPENstorage D280"},
800	{"SUN", "CSM200_R"},
801	{"SUN", "LCSM100_I"},
802	{"SUN", "LCSM100_S"},
803	{"SUN", "LCSM100_E"},
804	{"SUN", "LCSM100_F"},
805	{"DELL", "MD3000"},
806	{"DELL", "MD3000i"},
807	{"DELL", "MD32xx"},
808	{"DELL", "MD32xxi"},
809	{"DELL", "MD36xxi"},
810	{"DELL", "MD36xxf"},
811	{"LSI", "INF-01-00"},
812	{"ENGENIO", "INF-01-00"},
813	{"STK", "FLEXLINE 380"},
814	{"SUN", "CSM100_R_FC"},
815	{"SUN", "STK6580_6780"},
816	{"SUN", "SUN_6180"},
817	{NULL, NULL},
818};
819
820static int rdac_bus_attach(struct scsi_device *sdev);
821static void rdac_bus_detach(struct scsi_device *sdev);
822
823static struct scsi_device_handler rdac_dh = {
824	.name = RDAC_NAME,
825	.module = THIS_MODULE,
826	.devlist = rdac_dev_list,
827	.prep_fn = rdac_prep_fn,
828	.check_sense = rdac_check_sense,
829	.attach = rdac_bus_attach,
830	.detach = rdac_bus_detach,
831	.activate = rdac_activate,
832};
833
834static int rdac_bus_attach(struct scsi_device *sdev)
835{
836	struct scsi_dh_data *scsi_dh_data;
837	struct rdac_dh_data *h;
838	unsigned long flags;
839	int err;
840	char array_name[ARRAY_LABEL_LEN];
841	char array_id[UNIQUE_ID_LEN];
842
843	scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
844			       + sizeof(*h) , GFP_KERNEL);
845	if (!scsi_dh_data) {
846		sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
847			    RDAC_NAME);
848		return 0;
849	}
850
851	scsi_dh_data->scsi_dh = &rdac_dh;
852	h = (struct rdac_dh_data *) scsi_dh_data->buf;
853	h->lun = UNINITIALIZED_LUN;
854	h->state = RDAC_STATE_ACTIVE;
855
856	err = get_lun_info(sdev, h, array_name, array_id);
857	if (err != SCSI_DH_OK)
858		goto failed;
859
860	err = initialize_controller(sdev, h, array_name, array_id);
861	if (err != SCSI_DH_OK)
862		goto failed;
863
864	err = check_ownership(sdev, h);
865	if (err != SCSI_DH_OK)
866		goto clean_ctlr;
867
868	err = set_mode_select(sdev, h);
869	if (err != SCSI_DH_OK)
870		goto clean_ctlr;
871
872	if (!try_module_get(THIS_MODULE))
873		goto clean_ctlr;
874
875	spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
876	sdev->scsi_dh_data = scsi_dh_data;
877	spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
878
879	sdev_printk(KERN_NOTICE, sdev,
880		    "%s: LUN %d (%s) (%s)\n",
881		    RDAC_NAME, h->lun, mode[(int)h->mode],
882		    lun_state[(int)h->lun_state]);
883
884	return 0;
 
885
886clean_ctlr:
 
887	kref_put(&h->ctlr->kref, release_controller);
 
888
889failed:
890	kfree(scsi_dh_data);
891	sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
892		    RDAC_NAME);
893	return -EINVAL;
894}
895
896static void rdac_bus_detach( struct scsi_device *sdev )
897{
898	struct scsi_dh_data *scsi_dh_data;
899	struct rdac_dh_data *h;
900	unsigned long flags;
901
902	spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
903	scsi_dh_data = sdev->scsi_dh_data;
904	sdev->scsi_dh_data = NULL;
905	spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
906
907	h = (struct rdac_dh_data *) scsi_dh_data->buf;
908	if (h->ctlr)
 
 
909		kref_put(&h->ctlr->kref, release_controller);
910	kfree(scsi_dh_data);
911	module_put(THIS_MODULE);
912	sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", RDAC_NAME);
 
913}
914
915
 
 
 
 
 
 
 
 
916
917static int __init rdac_init(void)
918{
919	int r;
920
921	r = scsi_register_device_handler(&rdac_dh);
922	if (r != 0) {
923		printk(KERN_ERR "Failed to register scsi device handler.");
924		goto done;
925	}
926
927	/*
928	 * Create workqueue to handle mode selects for rdac
929	 */
930	kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
931	if (!kmpath_rdacd) {
932		scsi_unregister_device_handler(&rdac_dh);
933		printk(KERN_ERR "kmpath_rdacd creation failed.\n");
 
 
934	}
935done:
936	return r;
937}
938
939static void __exit rdac_exit(void)
940{
941	destroy_workqueue(kmpath_rdacd);
942	scsi_unregister_device_handler(&rdac_dh);
943}
944
945module_init(rdac_init);
946module_exit(rdac_exit);
947
948MODULE_DESCRIPTION("Multipath LSI/Engenio RDAC driver");
949MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
950MODULE_VERSION("01.00.0000.0000");
951MODULE_LICENSE("GPL");
v5.9
  1/*
  2 * LSI/Engenio/NetApp E-Series RDAC SCSI Device Handler
  3 *
  4 * Copyright (C) 2005 Mike Christie. All rights reserved.
  5 * Copyright (C) Chandra Seetharaman, IBM Corp. 2007
  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 <scsi/scsi.h>
 23#include <scsi/scsi_eh.h>
 24#include <scsi/scsi_dh.h>
 25#include <linux/workqueue.h>
 26#include <linux/slab.h>
 27#include <linux/module.h>
 28
 29#define RDAC_NAME "rdac"
 30#define RDAC_RETRY_COUNT 5
 31
 32/*
 33 * LSI mode page stuff
 34 *
 35 * These struct definitions and the forming of the
 36 * mode page were taken from the LSI RDAC 2.4 GPL'd
 37 * driver, and then converted to Linux conventions.
 38 */
 39#define RDAC_QUIESCENCE_TIME 20
 40/*
 41 * Page Codes
 42 */
 43#define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
 44
 45/*
 46 * Controller modes definitions
 47 */
 48#define RDAC_MODE_TRANSFER_SPECIFIED_LUNS	0x02
 49
 50/*
 51 * RDAC Options field
 52 */
 53#define RDAC_FORCED_QUIESENCE 0x02
 54
 55#define RDAC_TIMEOUT	(60 * HZ)
 56#define RDAC_RETRIES	3
 57
 58struct rdac_mode_6_hdr {
 59	u8	data_len;
 60	u8	medium_type;
 61	u8	device_params;
 62	u8	block_desc_len;
 63};
 64
 65struct rdac_mode_10_hdr {
 66	u16	data_len;
 67	u8	medium_type;
 68	u8	device_params;
 69	u16	reserved;
 70	u16	block_desc_len;
 71};
 72
 73struct rdac_mode_common {
 74	u8	controller_serial[16];
 75	u8	alt_controller_serial[16];
 76	u8	rdac_mode[2];
 77	u8	alt_rdac_mode[2];
 78	u8	quiescence_timeout;
 79	u8	rdac_options;
 80};
 81
 82struct rdac_pg_legacy {
 83	struct rdac_mode_6_hdr hdr;
 84	u8	page_code;
 85	u8	page_len;
 86	struct rdac_mode_common common;
 87#define MODE6_MAX_LUN	32
 88	u8	lun_table[MODE6_MAX_LUN];
 89	u8	reserved2[32];
 90	u8	reserved3;
 91	u8	reserved4;
 92};
 93
 94struct rdac_pg_expanded {
 95	struct rdac_mode_10_hdr hdr;
 96	u8	page_code;
 97	u8	subpage_code;
 98	u8	page_len[2];
 99	struct rdac_mode_common common;
100	u8	lun_table[256];
101	u8	reserved3;
102	u8	reserved4;
103};
104
105struct c9_inquiry {
106	u8	peripheral_info;
107	u8	page_code;	/* 0xC9 */
108	u8	reserved1;
109	u8	page_len;
110	u8	page_id[4];	/* "vace" */
111	u8	avte_cvp;
112	u8	path_prio;
113	u8	reserved2[38];
114};
115
116#define SUBSYS_ID_LEN	16
117#define SLOT_ID_LEN	2
118#define ARRAY_LABEL_LEN	31
119
120struct c4_inquiry {
121	u8	peripheral_info;
122	u8	page_code;	/* 0xC4 */
123	u8	reserved1;
124	u8	page_len;
125	u8	page_id[4];	/* "subs" */
126	u8	subsys_id[SUBSYS_ID_LEN];
127	u8	revision[4];
128	u8	slot_id[SLOT_ID_LEN];
129	u8	reserved[2];
130};
131
132#define UNIQUE_ID_LEN 16
133struct c8_inquiry {
134	u8	peripheral_info;
135	u8	page_code; /* 0xC8 */
136	u8	reserved1;
137	u8	page_len;
138	u8	page_id[4]; /* "edid" */
139	u8	reserved2[3];
140	u8	vol_uniq_id_len;
141	u8	vol_uniq_id[16];
142	u8	vol_user_label_len;
143	u8	vol_user_label[60];
144	u8	array_uniq_id_len;
145	u8	array_unique_id[UNIQUE_ID_LEN];
146	u8	array_user_label_len;
147	u8	array_user_label[60];
148	u8	lun[8];
149};
150
151struct rdac_controller {
152	u8			array_id[UNIQUE_ID_LEN];
153	int			use_ms10;
154	struct kref		kref;
155	struct list_head	node; /* list of all controllers */
156	union			{
157		struct rdac_pg_legacy legacy;
158		struct rdac_pg_expanded expanded;
159	} mode_select;
160	u8	index;
161	u8	array_name[ARRAY_LABEL_LEN];
162	struct Scsi_Host	*host;
163	spinlock_t		ms_lock;
164	int			ms_queued;
165	struct work_struct	ms_work;
166	struct scsi_device	*ms_sdev;
167	struct list_head	ms_head;
168	struct list_head	dh_list;
169};
170
171struct c2_inquiry {
172	u8	peripheral_info;
173	u8	page_code;	/* 0xC2 */
174	u8	reserved1;
175	u8	page_len;
176	u8	page_id[4];	/* "swr4" */
177	u8	sw_version[3];
178	u8	sw_date[3];
179	u8	features_enabled;
180	u8	max_lun_supported;
181	u8	partitions[239]; /* Total allocation length should be 0xFF */
182};
183
184struct rdac_dh_data {
185	struct list_head	node;
186	struct rdac_controller	*ctlr;
187	struct scsi_device	*sdev;
188#define UNINITIALIZED_LUN	(1 << 8)
189	unsigned		lun;
190
191#define RDAC_MODE		0
192#define RDAC_MODE_AVT		1
193#define RDAC_MODE_IOSHIP	2
194	unsigned char		mode;
195
196#define RDAC_STATE_ACTIVE	0
197#define RDAC_STATE_PASSIVE	1
198	unsigned char		state;
199
200#define RDAC_LUN_UNOWNED	0
201#define RDAC_LUN_OWNED		1
202	char			lun_state;
203
204#define RDAC_PREFERRED		0
205#define RDAC_NON_PREFERRED	1
206	char			preferred;
207
 
208	union			{
209		struct c2_inquiry c2;
210		struct c4_inquiry c4;
211		struct c8_inquiry c8;
212		struct c9_inquiry c9;
213	} inq;
214};
215
216static const char *mode[] = {
217	"RDAC",
218	"AVT",
219	"IOSHIP",
220};
221static const char *lun_state[] =
222{
223	"unowned",
224	"owned",
225};
226
227struct rdac_queue_data {
228	struct list_head	entry;
229	struct rdac_dh_data	*h;
230	activate_complete	callback_fn;
231	void			*callback_data;
232};
233
234static LIST_HEAD(ctlr_list);
235static DEFINE_SPINLOCK(list_lock);
236static struct workqueue_struct *kmpath_rdacd;
237static void send_mode_select(struct work_struct *work);
238
239/*
240 * module parameter to enable rdac debug logging.
241 * 2 bits for each type of logging, only two types defined for now
242 * Can be enhanced if required at later point
243 */
244static int rdac_logging = 1;
245module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
246MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
247		"Default is 1 - failover logging enabled, "
248		"set it to 0xF to enable all the logs");
249
250#define RDAC_LOG_FAILOVER	0
251#define RDAC_LOG_SENSE		2
252
253#define RDAC_LOG_BITS		2
254
255#define RDAC_LOG_LEVEL(SHIFT)  \
256	((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
257
258#define RDAC_LOG(SHIFT, sdev, f, arg...) \
259do { \
260	if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
261		sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
262} while (0);
263
264static unsigned int rdac_failover_get(struct rdac_controller *ctlr,
265				      struct list_head *list,
266				      unsigned char *cdb)
267{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268	struct rdac_mode_common *common;
269	unsigned data_size;
270	struct rdac_queue_data *qdata;
271	u8 *lun_table;
272
273	if (ctlr->use_ms10) {
274		struct rdac_pg_expanded *rdac_pg;
275
276		data_size = sizeof(struct rdac_pg_expanded);
277		rdac_pg = &ctlr->mode_select.expanded;
278		memset(rdac_pg, 0, data_size);
279		common = &rdac_pg->common;
280		rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
281		rdac_pg->subpage_code = 0x1;
282		rdac_pg->page_len[0] = 0x01;
283		rdac_pg->page_len[1] = 0x28;
284		lun_table = rdac_pg->lun_table;
285	} else {
286		struct rdac_pg_legacy *rdac_pg;
287
288		data_size = sizeof(struct rdac_pg_legacy);
289		rdac_pg = &ctlr->mode_select.legacy;
290		memset(rdac_pg, 0, data_size);
291		common = &rdac_pg->common;
292		rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
293		rdac_pg->page_len = 0x68;
294		lun_table = rdac_pg->lun_table;
295	}
296	common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
297	common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
298	common->rdac_options = RDAC_FORCED_QUIESENCE;
299
300	list_for_each_entry(qdata, list, entry) {
301		lun_table[qdata->h->lun] = 0x81;
302	}
303
 
 
 
 
 
304	/* Prepare the command. */
305	if (ctlr->use_ms10) {
306		cdb[0] = MODE_SELECT_10;
307		cdb[7] = data_size >> 8;
308		cdb[8] = data_size & 0xff;
309	} else {
310		cdb[0] = MODE_SELECT;
311		cdb[4] = data_size;
312	}
 
 
 
 
 
313
314	return data_size;
315}
316
317static void release_controller(struct kref *kref)
318{
319	struct rdac_controller *ctlr;
320	ctlr = container_of(kref, struct rdac_controller, kref);
321
 
 
322	list_del(&ctlr->node);
 
323	kfree(ctlr);
324}
325
326static struct rdac_controller *get_controller(int index, char *array_name,
327			u8 *array_id, struct scsi_device *sdev)
328{
329	struct rdac_controller *ctlr, *tmp;
330
 
 
331	list_for_each_entry(tmp, &ctlr_list, node) {
332		if ((memcmp(tmp->array_id, array_id, UNIQUE_ID_LEN) == 0) &&
333			  (tmp->index == index) &&
334			  (tmp->host == sdev->host)) {
335			kref_get(&tmp->kref);
 
336			return tmp;
337		}
338	}
339	ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
340	if (!ctlr)
341		return NULL;
342
343	/* initialize fields of controller */
344	memcpy(ctlr->array_id, array_id, UNIQUE_ID_LEN);
345	ctlr->index = index;
346	ctlr->host = sdev->host;
347	memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
348
349	kref_init(&ctlr->kref);
350	ctlr->use_ms10 = -1;
351	ctlr->ms_queued = 0;
352	ctlr->ms_sdev = NULL;
353	spin_lock_init(&ctlr->ms_lock);
354	INIT_WORK(&ctlr->ms_work, send_mode_select);
355	INIT_LIST_HEAD(&ctlr->ms_head);
356	list_add(&ctlr->node, &ctlr_list);
357	INIT_LIST_HEAD(&ctlr->dh_list);
 
 
 
 
 
 
 
 
 
 
358
359	return ctlr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360}
361
362static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
363			char *array_name, u8 *array_id)
364{
365	int err = SCSI_DH_IO, i;
366	struct c8_inquiry *inqp = &h->inq.c8;
367
368	if (!scsi_get_vpd_page(sdev, 0xC8, (unsigned char *)inqp,
369			       sizeof(struct c8_inquiry))) {
 
370		if (inqp->page_code != 0xc8)
371			return SCSI_DH_NOSYS;
372		if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
373		    inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
374			return SCSI_DH_NOSYS;
375		h->lun = inqp->lun[7]; /* Uses only the last byte */
376
377		for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
378			*(array_name+i) = inqp->array_user_label[(2*i)+1];
379
380		*(array_name+ARRAY_LABEL_LEN-1) = '\0';
381		memset(array_id, 0, UNIQUE_ID_LEN);
382		memcpy(array_id, inqp->array_unique_id, inqp->array_uniq_id_len);
383		err = SCSI_DH_OK;
384	}
385	return err;
386}
387
388static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
389{
390	int err = SCSI_DH_IO, access_state;
391	struct rdac_dh_data *tmp;
392	struct c9_inquiry *inqp = &h->inq.c9;
393
394	h->state = RDAC_STATE_ACTIVE;
395	if (!scsi_get_vpd_page(sdev, 0xC9, (unsigned char *)inqp,
396			       sizeof(struct c9_inquiry))) {
 
397		/* detect the operating mode */
398		if ((inqp->avte_cvp >> 5) & 0x1)
399			h->mode = RDAC_MODE_IOSHIP; /* LUN in IOSHIP mode */
400		else if (inqp->avte_cvp >> 7)
401			h->mode = RDAC_MODE_AVT; /* LUN in AVT mode */
402		else
403			h->mode = RDAC_MODE; /* LUN in RDAC mode */
404
405		/* Update ownership */
406		if (inqp->avte_cvp & 0x1) {
407			h->lun_state = RDAC_LUN_OWNED;
408			access_state = SCSI_ACCESS_STATE_OPTIMAL;
409		} else {
410			h->lun_state = RDAC_LUN_UNOWNED;
411			if (h->mode == RDAC_MODE) {
412				h->state = RDAC_STATE_PASSIVE;
413				access_state = SCSI_ACCESS_STATE_STANDBY;
414			} else
415				access_state = SCSI_ACCESS_STATE_ACTIVE;
416		}
417
418		/* Update path prio*/
419		if (inqp->path_prio & 0x1) {
420			h->preferred = RDAC_PREFERRED;
421			access_state |= SCSI_ACCESS_STATE_PREFERRED;
422		} else
423			h->preferred = RDAC_NON_PREFERRED;
424		rcu_read_lock();
425		list_for_each_entry_rcu(tmp, &h->ctlr->dh_list, node) {
426			/* h->sdev should always be valid */
427			BUG_ON(!tmp->sdev);
428			tmp->sdev->access_state = access_state;
429		}
430		rcu_read_unlock();
431		err = SCSI_DH_OK;
432	}
433
434	return err;
435}
436
437static int initialize_controller(struct scsi_device *sdev,
438		struct rdac_dh_data *h, char *array_name, u8 *array_id)
439{
440	int err = SCSI_DH_IO, index;
441	struct c4_inquiry *inqp = &h->inq.c4;
442
443	if (!scsi_get_vpd_page(sdev, 0xC4, (unsigned char *)inqp,
444			       sizeof(struct c4_inquiry))) {
 
445		/* get the controller index */
446		if (inqp->slot_id[1] == 0x31)
447			index = 0;
448		else
449			index = 1;
450
451		spin_lock(&list_lock);
452		h->ctlr = get_controller(index, array_name, array_id, sdev);
453		if (!h->ctlr)
454			err = SCSI_DH_RES_TEMP_UNAVAIL;
455		else {
456			list_add_rcu(&h->node, &h->ctlr->dh_list);
457			h->sdev = sdev;
458		}
459		spin_unlock(&list_lock);
460		err = SCSI_DH_OK;
461	}
462	return err;
463}
464
465static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
466{
467	int err = SCSI_DH_IO;
468	struct c2_inquiry *inqp = &h->inq.c2;
469
470	if (!scsi_get_vpd_page(sdev, 0xC2, (unsigned char *)inqp,
471			       sizeof(struct c2_inquiry))) {
 
472		/*
473		 * If more than MODE6_MAX_LUN luns are supported, use
474		 * mode select 10
475		 */
476		if (inqp->max_lun_supported >= MODE6_MAX_LUN)
477			h->ctlr->use_ms10 = 1;
478		else
479			h->ctlr->use_ms10 = 0;
480		err = SCSI_DH_OK;
481	}
482	return err;
483}
484
485static int mode_select_handle_sense(struct scsi_device *sdev,
486				    struct scsi_sense_hdr *sense_hdr)
487{
488	int err = SCSI_DH_IO;
489	struct rdac_dh_data *h = sdev->handler_data;
 
490
491	if (!scsi_sense_valid(sense_hdr))
 
492		goto done;
493
494	switch (sense_hdr->sense_key) {
495	case NO_SENSE:
496	case ABORTED_COMMAND:
497	case UNIT_ATTENTION:
498		err = SCSI_DH_RETRY;
499		break;
500	case NOT_READY:
501		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
502			/* LUN Not Ready and is in the Process of Becoming
503			 * Ready
504			 */
505			err = SCSI_DH_RETRY;
506		break;
507	case ILLEGAL_REQUEST:
508		if (sense_hdr->asc == 0x91 && sense_hdr->ascq == 0x36)
509			/*
510			 * Command Lock contention
511			 */
512			err = SCSI_DH_IMM_RETRY;
513		break;
514	default:
515		break;
516	}
517
518	RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
519		"MODE_SELECT returned with sense %02x/%02x/%02x",
520		(char *) h->ctlr->array_name, h->ctlr->index,
521		sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
522
523done:
524	return err;
525}
526
527static void send_mode_select(struct work_struct *work)
528{
529	struct rdac_controller *ctlr =
530		container_of(work, struct rdac_controller, ms_work);
 
531	struct scsi_device *sdev = ctlr->ms_sdev;
532	struct rdac_dh_data *h = sdev->handler_data;
533	int err = SCSI_DH_OK, retry_cnt = RDAC_RETRY_COUNT;
 
534	struct rdac_queue_data *tmp, *qdata;
535	LIST_HEAD(list);
536	unsigned char cdb[MAX_COMMAND_SIZE];
537	struct scsi_sense_hdr sshdr;
538	unsigned int data_size;
539	u64 req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
540		REQ_FAILFAST_DRIVER;
541
542	spin_lock(&ctlr->ms_lock);
543	list_splice_init(&ctlr->ms_head, &list);
544	ctlr->ms_queued = 0;
545	ctlr->ms_sdev = NULL;
546	spin_unlock(&ctlr->ms_lock);
547
548 retry:
549	memset(cdb, 0, sizeof(cdb));
550
551	data_size = rdac_failover_get(ctlr, &list, cdb);
 
552
553	RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
554		"%s MODE_SELECT command",
555		(char *) h->ctlr->array_name, h->ctlr->index,
556		(retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
557
558	if (scsi_execute(sdev, cdb, DMA_TO_DEVICE, &h->ctlr->mode_select,
559			data_size, NULL, &sshdr, RDAC_TIMEOUT * HZ,
560			RDAC_RETRIES, req_flags, 0, NULL)) {
561		err = mode_select_handle_sense(sdev, &sshdr);
562		if (err == SCSI_DH_RETRY && retry_cnt--)
563			goto retry;
564		if (err == SCSI_DH_IMM_RETRY)
565			goto retry;
566	}
567	if (err == SCSI_DH_OK) {
568		h->state = RDAC_STATE_ACTIVE;
569		RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
570				"MODE_SELECT completed",
571				(char *) h->ctlr->array_name, h->ctlr->index);
572	}
573
 
574	list_for_each_entry_safe(qdata, tmp, &list, entry) {
575		list_del(&qdata->entry);
576		if (err == SCSI_DH_OK)
577			qdata->h->state = RDAC_STATE_ACTIVE;
578		if (qdata->callback_fn)
579			qdata->callback_fn(qdata->callback_data, err);
580		kfree(qdata);
581	}
582	return;
583}
584
585static int queue_mode_select(struct scsi_device *sdev,
586				activate_complete fn, void *data)
587{
588	struct rdac_queue_data *qdata;
589	struct rdac_controller *ctlr;
590
591	qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
592	if (!qdata)
593		return SCSI_DH_RETRY;
594
595	qdata->h = sdev->handler_data;
596	qdata->callback_fn = fn;
597	qdata->callback_data = data;
598
599	ctlr = qdata->h->ctlr;
600	spin_lock(&ctlr->ms_lock);
601	list_add_tail(&qdata->entry, &ctlr->ms_head);
602	if (!ctlr->ms_queued) {
603		ctlr->ms_queued = 1;
604		ctlr->ms_sdev = sdev;
605		queue_work(kmpath_rdacd, &ctlr->ms_work);
606	}
607	spin_unlock(&ctlr->ms_lock);
608	return SCSI_DH_OK;
609}
610
611static int rdac_activate(struct scsi_device *sdev,
612			activate_complete fn, void *data)
613{
614	struct rdac_dh_data *h = sdev->handler_data;
615	int err = SCSI_DH_OK;
616	int act = 0;
617
618	err = check_ownership(sdev, h);
619	if (err != SCSI_DH_OK)
620		goto done;
621
622	switch (h->mode) {
623	case RDAC_MODE:
624		if (h->lun_state == RDAC_LUN_UNOWNED)
625			act = 1;
626		break;
627	case RDAC_MODE_IOSHIP:
628		if ((h->lun_state == RDAC_LUN_UNOWNED) &&
629		    (h->preferred == RDAC_PREFERRED))
630			act = 1;
631		break;
632	default:
633		break;
634	}
635
636	if (act) {
637		err = queue_mode_select(sdev, fn, data);
638		if (err == SCSI_DH_OK)
639			return 0;
640	}
641done:
642	if (fn)
643		fn(data, err);
644	return 0;
645}
646
647static blk_status_t rdac_prep_fn(struct scsi_device *sdev, struct request *req)
648{
649	struct rdac_dh_data *h = sdev->handler_data;
 
650
651	if (h->state != RDAC_STATE_ACTIVE) {
652		req->rq_flags |= RQF_QUIET;
653		return BLK_STS_IOERR;
654	}
 
655
656	return BLK_STS_OK;
657}
658
659static int rdac_check_sense(struct scsi_device *sdev,
660				struct scsi_sense_hdr *sense_hdr)
661{
662	struct rdac_dh_data *h = sdev->handler_data;
663
664	RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
665			"I/O returned with sense %02x/%02x/%02x",
666			(char *) h->ctlr->array_name, h->ctlr->index,
667			sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
668
669	switch (sense_hdr->sense_key) {
670	case NOT_READY:
671		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
672			/* LUN Not Ready - Logical Unit Not Ready and is in
673			* the process of becoming ready
674			* Just retry.
675			*/
676			return ADD_TO_MLQUEUE;
677		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
678			/* LUN Not Ready - Storage firmware incompatible
679			 * Manual code synchonisation required.
680			 *
681			 * Nothing we can do here. Try to bypass the path.
682			 */
683			return SUCCESS;
684		if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
685			/* LUN Not Ready - Quiescense in progress
686			 *
687			 * Just retry and wait.
688			 */
689			return ADD_TO_MLQUEUE;
690		if (sense_hdr->asc == 0xA1  && sense_hdr->ascq == 0x02)
691			/* LUN Not Ready - Quiescense in progress
692			 * or has been achieved
693			 * Just retry.
694			 */
695			return ADD_TO_MLQUEUE;
696		break;
697	case ILLEGAL_REQUEST:
698		if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
699			/* Invalid Request - Current Logical Unit Ownership.
700			 * Controller is not the current owner of the LUN,
701			 * Fail the path, so that the other path be used.
702			 */
703			h->state = RDAC_STATE_PASSIVE;
704			return SUCCESS;
705		}
706		break;
707	case UNIT_ATTENTION:
708		if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
709			/*
710			 * Power On, Reset, or Bus Device Reset, just retry.
711			 */
712			return ADD_TO_MLQUEUE;
713		if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
714			/*
715			 * Quiescence in progress , just retry.
716			 */
717			return ADD_TO_MLQUEUE;
718		break;
719	}
720	/* success just means we do not care what scsi-ml does */
721	return SCSI_RETURN_NOT_HANDLED;
722}
723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
724static int rdac_bus_attach(struct scsi_device *sdev)
725{
 
726	struct rdac_dh_data *h;
 
727	int err;
728	char array_name[ARRAY_LABEL_LEN];
729	char array_id[UNIQUE_ID_LEN];
730
731	h = kzalloc(sizeof(*h) , GFP_KERNEL);
732	if (!h)
733		return SCSI_DH_NOMEM;
 
 
 
 
 
 
 
734	h->lun = UNINITIALIZED_LUN;
735	h->state = RDAC_STATE_ACTIVE;
736
737	err = get_lun_info(sdev, h, array_name, array_id);
738	if (err != SCSI_DH_OK)
739		goto failed;
740
741	err = initialize_controller(sdev, h, array_name, array_id);
742	if (err != SCSI_DH_OK)
743		goto failed;
744
745	err = check_ownership(sdev, h);
746	if (err != SCSI_DH_OK)
747		goto clean_ctlr;
748
749	err = set_mode_select(sdev, h);
750	if (err != SCSI_DH_OK)
751		goto clean_ctlr;
752
 
 
 
 
 
 
 
753	sdev_printk(KERN_NOTICE, sdev,
754		    "%s: LUN %d (%s) (%s)\n",
755		    RDAC_NAME, h->lun, mode[(int)h->mode],
756		    lun_state[(int)h->lun_state]);
757
758	sdev->handler_data = h;
759	return SCSI_DH_OK;
760
761clean_ctlr:
762	spin_lock(&list_lock);
763	kref_put(&h->ctlr->kref, release_controller);
764	spin_unlock(&list_lock);
765
766failed:
767	kfree(h);
768	return err;
 
 
769}
770
771static void rdac_bus_detach( struct scsi_device *sdev )
772{
773	struct rdac_dh_data *h = sdev->handler_data;
 
 
774
775	if (h->ctlr && h->ctlr->ms_queued)
776		flush_workqueue(kmpath_rdacd);
 
 
777
778	spin_lock(&list_lock);
779	if (h->ctlr) {
780		list_del_rcu(&h->node);
781		h->sdev = NULL;
782		kref_put(&h->ctlr->kref, release_controller);
783	}
784	spin_unlock(&list_lock);
785	sdev->handler_data = NULL;
786	kfree(h);
787}
788
789static struct scsi_device_handler rdac_dh = {
790	.name = RDAC_NAME,
791	.module = THIS_MODULE,
792	.prep_fn = rdac_prep_fn,
793	.check_sense = rdac_check_sense,
794	.attach = rdac_bus_attach,
795	.detach = rdac_bus_detach,
796	.activate = rdac_activate,
797};
798
799static int __init rdac_init(void)
800{
801	int r;
802
803	r = scsi_register_device_handler(&rdac_dh);
804	if (r != 0) {
805		printk(KERN_ERR "Failed to register scsi device handler.");
806		goto done;
807	}
808
809	/*
810	 * Create workqueue to handle mode selects for rdac
811	 */
812	kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
813	if (!kmpath_rdacd) {
814		scsi_unregister_device_handler(&rdac_dh);
815		printk(KERN_ERR "kmpath_rdacd creation failed.\n");
816
817		r = -EINVAL;
818	}
819done:
820	return r;
821}
822
823static void __exit rdac_exit(void)
824{
825	destroy_workqueue(kmpath_rdacd);
826	scsi_unregister_device_handler(&rdac_dh);
827}
828
829module_init(rdac_init);
830module_exit(rdac_exit);
831
832MODULE_DESCRIPTION("Multipath LSI/Engenio/NetApp E-Series RDAC driver");
833MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
834MODULE_VERSION("01.00.0000.0000");
835MODULE_LICENSE("GPL");