blob: 47cfe1c49c3ec8ec3162fdd6fdb8cb06d6090ad4 [file] [log] [blame]
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -07001/*
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>
Chandra Seetharaman970f3f42009-10-21 09:22:51 -070025#include <linux/workqueue.h>
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -070026
27#define RDAC_NAME "rdac"
Chandra Seetharamanc85f8cb92008-11-05 20:06:37 -080028#define RDAC_RETRY_COUNT 5
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -070029
30/*
31 * LSI mode page stuff
32 *
33 * These struct definitions and the forming of the
34 * mode page were taken from the LSI RDAC 2.4 GPL'd
35 * driver, and then converted to Linux conventions.
36 */
37#define RDAC_QUIESCENCE_TIME 20;
38/*
39 * Page Codes
40 */
41#define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
42
43/*
44 * Controller modes definitions
45 */
46#define RDAC_MODE_TRANSFER_SPECIFIED_LUNS 0x02
47
48/*
49 * RDAC Options field
50 */
51#define RDAC_FORCED_QUIESENCE 0x02
52
53#define RDAC_TIMEOUT (60 * HZ)
54#define RDAC_RETRIES 3
55
56struct rdac_mode_6_hdr {
57 u8 data_len;
58 u8 medium_type;
59 u8 device_params;
60 u8 block_desc_len;
61};
62
63struct rdac_mode_10_hdr {
64 u16 data_len;
65 u8 medium_type;
66 u8 device_params;
67 u16 reserved;
68 u16 block_desc_len;
69};
70
71struct rdac_mode_common {
72 u8 controller_serial[16];
73 u8 alt_controller_serial[16];
74 u8 rdac_mode[2];
75 u8 alt_rdac_mode[2];
76 u8 quiescence_timeout;
77 u8 rdac_options;
78};
79
80struct rdac_pg_legacy {
81 struct rdac_mode_6_hdr hdr;
82 u8 page_code;
83 u8 page_len;
84 struct rdac_mode_common common;
85#define MODE6_MAX_LUN 32
86 u8 lun_table[MODE6_MAX_LUN];
87 u8 reserved2[32];
88 u8 reserved3;
89 u8 reserved4;
90};
91
92struct rdac_pg_expanded {
93 struct rdac_mode_10_hdr hdr;
94 u8 page_code;
95 u8 subpage_code;
96 u8 page_len[2];
97 struct rdac_mode_common common;
98 u8 lun_table[256];
99 u8 reserved3;
100 u8 reserved4;
101};
102
103struct c9_inquiry {
104 u8 peripheral_info;
105 u8 page_code; /* 0xC9 */
106 u8 reserved1;
107 u8 page_len;
108 u8 page_id[4]; /* "vace" */
109 u8 avte_cvp;
110 u8 path_prio;
111 u8 reserved2[38];
112};
113
114#define SUBSYS_ID_LEN 16
115#define SLOT_ID_LEN 2
Moger, Babu15276662009-09-03 21:42:21 -0600116#define ARRAY_LABEL_LEN 31
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700117
118struct c4_inquiry {
119 u8 peripheral_info;
120 u8 page_code; /* 0xC4 */
121 u8 reserved1;
122 u8 page_len;
123 u8 page_id[4]; /* "subs" */
124 u8 subsys_id[SUBSYS_ID_LEN];
125 u8 revision[4];
126 u8 slot_id[SLOT_ID_LEN];
127 u8 reserved[2];
128};
129
130struct rdac_controller {
131 u8 subsys_id[SUBSYS_ID_LEN];
132 u8 slot_id[SLOT_ID_LEN];
133 int use_ms10;
134 struct kref kref;
135 struct list_head node; /* list of all controllers */
136 union {
137 struct rdac_pg_legacy legacy;
138 struct rdac_pg_expanded expanded;
139 } mode_select;
Moger, Babu15276662009-09-03 21:42:21 -0600140 u8 index;
141 u8 array_name[ARRAY_LABEL_LEN];
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700142 spinlock_t ms_lock;
143 int ms_queued;
144 struct work_struct ms_work;
145 struct scsi_device *ms_sdev;
146 struct list_head ms_head;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700147};
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700148
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700149struct c8_inquiry {
150 u8 peripheral_info;
151 u8 page_code; /* 0xC8 */
152 u8 reserved1;
153 u8 page_len;
154 u8 page_id[4]; /* "edid" */
155 u8 reserved2[3];
156 u8 vol_uniq_id_len;
157 u8 vol_uniq_id[16];
158 u8 vol_user_label_len;
159 u8 vol_user_label[60];
160 u8 array_uniq_id_len;
161 u8 array_unique_id[16];
162 u8 array_user_label_len;
163 u8 array_user_label[60];
164 u8 lun[8];
165};
166
167struct c2_inquiry {
168 u8 peripheral_info;
169 u8 page_code; /* 0xC2 */
170 u8 reserved1;
171 u8 page_len;
172 u8 page_id[4]; /* "swr4" */
173 u8 sw_version[3];
174 u8 sw_date[3];
175 u8 features_enabled;
176 u8 max_lun_supported;
177 u8 partitions[239]; /* Total allocation length should be 0xFF */
178};
179
180struct rdac_dh_data {
181 struct rdac_controller *ctlr;
182#define UNINITIALIZED_LUN (1 << 8)
183 unsigned lun;
184#define RDAC_STATE_ACTIVE 0
185#define RDAC_STATE_PASSIVE 1
186 unsigned char state;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700187
188#define RDAC_LUN_UNOWNED 0
189#define RDAC_LUN_OWNED 1
190#define RDAC_LUN_AVT 2
191 char lun_state;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700192 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
193 union {
194 struct c2_inquiry c2;
195 struct c4_inquiry c4;
196 struct c8_inquiry c8;
197 struct c9_inquiry c9;
198 } inq;
199};
200
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700201static const char *lun_state[] =
202{
203 "unowned",
204 "owned",
205 "owned (AVT mode)",
206};
207
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700208struct rdac_queue_data {
209 struct list_head entry;
210 struct rdac_dh_data *h;
211 activate_complete callback_fn;
212 void *callback_data;
213};
214
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700215static LIST_HEAD(ctlr_list);
216static DEFINE_SPINLOCK(list_lock);
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700217static struct workqueue_struct *kmpath_rdacd;
218static void send_mode_select(struct work_struct *work);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700219
Moger, Babudd784ed2009-09-03 21:42:28 -0600220/*
221 * module parameter to enable rdac debug logging.
222 * 2 bits for each type of logging, only two types defined for now
223 * Can be enhanced if required at later point
224 */
225static int rdac_logging = 1;
226module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
227MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
228 "Default is 1 - failover logging enabled, "
229 "set it to 0xF to enable all the logs");
230
231#define RDAC_LOG_FAILOVER 0
232#define RDAC_LOG_SENSE 2
233
234#define RDAC_LOG_BITS 2
235
236#define RDAC_LOG_LEVEL(SHIFT) \
237 ((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
238
239#define RDAC_LOG(SHIFT, sdev, f, arg...) \
240do { \
241 if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
242 sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
243} while (0);
244
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700245static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
246{
247 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
248 BUG_ON(scsi_dh_data == NULL);
249 return ((struct rdac_dh_data *) scsi_dh_data->buf);
250}
251
252static struct request *get_rdac_req(struct scsi_device *sdev,
253 void *buffer, unsigned buflen, int rw)
254{
255 struct request *rq;
256 struct request_queue *q = sdev->request_queue;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700257
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700258 rq = blk_get_request(q, rw, GFP_NOIO);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700259
260 if (!rq) {
261 sdev_printk(KERN_INFO, sdev,
262 "get_rdac_req: blk_get_request failed.\n");
263 return NULL;
264 }
265
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700266 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700267 blk_put_request(rq);
268 sdev_printk(KERN_INFO, sdev,
269 "get_rdac_req: blk_rq_map_kern failed.\n");
270 return NULL;
271 }
272
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700273 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Mike Christie6000a362008-08-19 18:45:30 -0500274 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
275 REQ_FAILFAST_DRIVER;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700276 rq->retries = RDAC_RETRIES;
277 rq->timeout = RDAC_TIMEOUT;
278
279 return rq;
280}
281
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700282static struct request *rdac_failover_get(struct scsi_device *sdev,
283 struct rdac_dh_data *h)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700284{
285 struct request *rq;
286 struct rdac_mode_common *common;
287 unsigned data_size;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700288
289 if (h->ctlr->use_ms10) {
290 struct rdac_pg_expanded *rdac_pg;
291
292 data_size = sizeof(struct rdac_pg_expanded);
293 rdac_pg = &h->ctlr->mode_select.expanded;
294 memset(rdac_pg, 0, data_size);
295 common = &rdac_pg->common;
296 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
297 rdac_pg->subpage_code = 0x1;
298 rdac_pg->page_len[0] = 0x01;
299 rdac_pg->page_len[1] = 0x28;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700300 } else {
301 struct rdac_pg_legacy *rdac_pg;
302
303 data_size = sizeof(struct rdac_pg_legacy);
304 rdac_pg = &h->ctlr->mode_select.legacy;
305 memset(rdac_pg, 0, data_size);
306 common = &rdac_pg->common;
307 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
308 rdac_pg->page_len = 0x68;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700309 }
310 common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
311 common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
312 common->rdac_options = RDAC_FORCED_QUIESENCE;
313
314 /* get request for block layer packet command */
315 rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
316 if (!rq)
317 return NULL;
318
319 /* Prepare the command. */
320 if (h->ctlr->use_ms10) {
321 rq->cmd[0] = MODE_SELECT_10;
322 rq->cmd[7] = data_size >> 8;
323 rq->cmd[8] = data_size & 0xff;
324 } else {
325 rq->cmd[0] = MODE_SELECT;
326 rq->cmd[4] = data_size;
327 }
328 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
329
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700330 rq->sense = h->sense;
331 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
332 rq->sense_len = 0;
333
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700334 return rq;
335}
336
337static void release_controller(struct kref *kref)
338{
339 struct rdac_controller *ctlr;
340 ctlr = container_of(kref, struct rdac_controller, kref);
341
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700342 flush_workqueue(kmpath_rdacd);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700343 spin_lock(&list_lock);
344 list_del(&ctlr->node);
345 spin_unlock(&list_lock);
346 kfree(ctlr);
347}
348
Moger, Babu15276662009-09-03 21:42:21 -0600349static struct rdac_controller *get_controller(u8 *subsys_id, u8 *slot_id,
350 char *array_name)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700351{
352 struct rdac_controller *ctlr, *tmp;
353
354 spin_lock(&list_lock);
355
356 list_for_each_entry(tmp, &ctlr_list, node) {
357 if ((memcmp(tmp->subsys_id, subsys_id, SUBSYS_ID_LEN) == 0) &&
358 (memcmp(tmp->slot_id, slot_id, SLOT_ID_LEN) == 0)) {
359 kref_get(&tmp->kref);
360 spin_unlock(&list_lock);
361 return tmp;
362 }
363 }
364 ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
365 if (!ctlr)
366 goto done;
367
368 /* initialize fields of controller */
369 memcpy(ctlr->subsys_id, subsys_id, SUBSYS_ID_LEN);
370 memcpy(ctlr->slot_id, slot_id, SLOT_ID_LEN);
Moger, Babu15276662009-09-03 21:42:21 -0600371 memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
372
373 /* update the controller index */
374 if (slot_id[1] == 0x31)
375 ctlr->index = 0;
376 else
377 ctlr->index = 1;
378
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700379 kref_init(&ctlr->kref);
380 ctlr->use_ms10 = -1;
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700381 ctlr->ms_queued = 0;
382 ctlr->ms_sdev = NULL;
383 spin_lock_init(&ctlr->ms_lock);
384 INIT_WORK(&ctlr->ms_work, send_mode_select);
385 INIT_LIST_HEAD(&ctlr->ms_head);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700386 list_add(&ctlr->node, &ctlr_list);
387done:
388 spin_unlock(&list_lock);
389 return ctlr;
390}
391
392static int submit_inquiry(struct scsi_device *sdev, int page_code,
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700393 unsigned int len, struct rdac_dh_data *h)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700394{
395 struct request *rq;
396 struct request_queue *q = sdev->request_queue;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700397 int err = SCSI_DH_RES_TEMP_UNAVAIL;
398
399 rq = get_rdac_req(sdev, &h->inq, len, READ);
400 if (!rq)
401 goto done;
402
403 /* Prepare the command. */
404 rq->cmd[0] = INQUIRY;
405 rq->cmd[1] = 1;
406 rq->cmd[2] = page_code;
407 rq->cmd[4] = len;
408 rq->cmd_len = COMMAND_SIZE(INQUIRY);
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700409
410 rq->sense = h->sense;
411 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
412 rq->sense_len = 0;
413
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700414 err = blk_execute_rq(q, NULL, rq, 1);
415 if (err == -EIO)
416 err = SCSI_DH_IO;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700417
418 blk_put_request(rq);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700419done:
420 return err;
421}
422
Moger, Babu15276662009-09-03 21:42:21 -0600423static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
424 char *array_name)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700425{
Moger, Babu15276662009-09-03 21:42:21 -0600426 int err, i;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700427 struct c8_inquiry *inqp;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700428
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700429 err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700430 if (err == SCSI_DH_OK) {
431 inqp = &h->inq.c8;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700432 if (inqp->page_code != 0xc8)
433 return SCSI_DH_NOSYS;
434 if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
435 inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
436 return SCSI_DH_NOSYS;
Chandra Seetharaman8479fca2008-08-12 17:26:58 -0700437 h->lun = inqp->lun[7]; /* Uses only the last byte */
Moger, Babu15276662009-09-03 21:42:21 -0600438
439 for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
440 *(array_name+i) = inqp->array_user_label[(2*i)+1];
441
442 *(array_name+ARRAY_LABEL_LEN-1) = '\0';
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700443 }
444 return err;
445}
446
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700447static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700448{
449 int err;
450 struct c9_inquiry *inqp;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700451
Chandra Seetharamanfe426252008-08-12 17:26:59 -0700452 h->lun_state = RDAC_LUN_UNOWNED;
Chandra Seetharaman9eece962008-11-05 20:05:21 -0800453 h->state = RDAC_STATE_ACTIVE;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700454 err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700455 if (err == SCSI_DH_OK) {
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700456 inqp = &h->inq.c9;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700457 if ((inqp->avte_cvp >> 7) == 0x1) {
458 /* LUN in AVT mode */
459 sdev_printk(KERN_NOTICE, sdev,
460 "%s: AVT mode detected\n",
461 RDAC_NAME);
462 h->lun_state = RDAC_LUN_AVT;
463 } else if ((inqp->avte_cvp & 0x1) != 0) {
464 /* LUN was owned by the controller */
465 h->lun_state = RDAC_LUN_OWNED;
466 }
467 }
468
Chandra Seetharaman5a367562008-08-21 23:12:40 -0700469 if (h->lun_state == RDAC_LUN_UNOWNED)
470 h->state = RDAC_STATE_PASSIVE;
471
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700472 return err;
473}
474
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700475static int initialize_controller(struct scsi_device *sdev,
Moger, Babu15276662009-09-03 21:42:21 -0600476 struct rdac_dh_data *h, char *array_name)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700477{
478 int err;
479 struct c4_inquiry *inqp;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700480
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700481 err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700482 if (err == SCSI_DH_OK) {
483 inqp = &h->inq.c4;
Moger, Babu15276662009-09-03 21:42:21 -0600484 h->ctlr = get_controller(inqp->subsys_id, inqp->slot_id,
485 array_name);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700486 if (!h->ctlr)
487 err = SCSI_DH_RES_TEMP_UNAVAIL;
488 }
489 return err;
490}
491
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700492static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700493{
494 int err;
495 struct c2_inquiry *inqp;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700496
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700497 err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700498 if (err == SCSI_DH_OK) {
499 inqp = &h->inq.c2;
500 /*
501 * If more than MODE6_MAX_LUN luns are supported, use
502 * mode select 10
503 */
504 if (inqp->max_lun_supported >= MODE6_MAX_LUN)
505 h->ctlr->use_ms10 = 1;
506 else
507 h->ctlr->use_ms10 = 0;
508 }
509 return err;
510}
511
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700512static int mode_select_handle_sense(struct scsi_device *sdev,
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700513 unsigned char *sensebuf)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700514{
515 struct scsi_sense_hdr sense_hdr;
Chauhan, Vijay7687fb92009-03-04 12:17:54 +0530516 int err = SCSI_DH_IO, ret;
Moger, Babudd784ed2009-09-03 21:42:28 -0600517 struct rdac_dh_data *h = get_rdac_data(sdev);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700518
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700519 ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700520 if (!ret)
521 goto done;
522
Chauhan, Vijay7687fb92009-03-04 12:17:54 +0530523 switch (sense_hdr.sense_key) {
524 case NO_SENSE:
525 case ABORTED_COMMAND:
526 case UNIT_ATTENTION:
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700527 err = SCSI_DH_RETRY;
Chauhan, Vijay7687fb92009-03-04 12:17:54 +0530528 break;
529 case NOT_READY:
530 if (sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x01)
531 /* LUN Not Ready and is in the Process of Becoming
532 * Ready
533 */
534 err = SCSI_DH_RETRY;
535 break;
536 case ILLEGAL_REQUEST:
537 if (sense_hdr.asc == 0x91 && sense_hdr.ascq == 0x36)
538 /*
539 * Command Lock contention
540 */
541 err = SCSI_DH_RETRY;
542 break;
543 default:
Moger, Babudd784ed2009-09-03 21:42:28 -0600544 break;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700545 }
546
Moger, Babudd784ed2009-09-03 21:42:28 -0600547 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
548 "MODE_SELECT returned with sense %02x/%02x/%02x",
549 (char *) h->ctlr->array_name, h->ctlr->index,
550 sense_hdr.sense_key, sense_hdr.asc, sense_hdr.ascq);
551
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700552done:
553 return err;
554}
555
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700556static void send_mode_select(struct work_struct *work)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700557{
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700558 struct rdac_controller *ctlr =
559 container_of(work, struct rdac_controller, ms_work);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700560 struct request *rq;
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700561 struct scsi_device *sdev = ctlr->ms_sdev;
562 struct rdac_dh_data *h = get_rdac_data(sdev);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700563 struct request_queue *q = sdev->request_queue;
Chandra Seetharamanc85f8cb92008-11-05 20:06:37 -0800564 int err, retry_cnt = RDAC_RETRY_COUNT;
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700565 struct rdac_queue_data *tmp, *qdata;
566 LIST_HEAD(list);
567 u8 *lun_table;
568
569 spin_lock(&ctlr->ms_lock);
570 list_splice_init(&ctlr->ms_head, &list);
571 ctlr->ms_queued = 0;
572 ctlr->ms_sdev = NULL;
573 spin_unlock(&ctlr->ms_lock);
574
575 if (ctlr->use_ms10)
576 lun_table = ctlr->mode_select.expanded.lun_table;
577 else
578 lun_table = ctlr->mode_select.legacy.lun_table;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700579
Chandra Seetharamanc85f8cb92008-11-05 20:06:37 -0800580retry:
581 err = SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700582 rq = rdac_failover_get(sdev, h);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700583 if (!rq)
584 goto done;
585
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700586 list_for_each_entry(qdata, &list, entry) {
587 lun_table[qdata->h->lun] = 0x81;
588 }
589
Moger, Babudd784ed2009-09-03 21:42:28 -0600590 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
591 "%s MODE_SELECT command",
592 (char *) h->ctlr->array_name, h->ctlr->index,
Chandra Seetharamanc85f8cb92008-11-05 20:06:37 -0800593 (retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700594
595 err = blk_execute_rq(q, NULL, rq, 1);
Chandra Seetharamanc85f8cb92008-11-05 20:06:37 -0800596 blk_put_request(rq);
597 if (err != SCSI_DH_OK) {
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700598 err = mode_select_handle_sense(sdev, h->sense);
Chandra Seetharamanc85f8cb92008-11-05 20:06:37 -0800599 if (err == SCSI_DH_RETRY && retry_cnt--)
600 goto retry;
601 }
Moger, Babudd784ed2009-09-03 21:42:28 -0600602 if (err == SCSI_DH_OK) {
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700603 h->state = RDAC_STATE_ACTIVE;
Moger, Babudd784ed2009-09-03 21:42:28 -0600604 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
605 "MODE_SELECT completed",
606 (char *) h->ctlr->array_name, h->ctlr->index);
607 }
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700608
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700609done:
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700610 list_for_each_entry_safe(qdata, tmp, &list, entry) {
611 list_del(&qdata->entry);
612 if (err == SCSI_DH_OK)
613 qdata->h->state = RDAC_STATE_ACTIVE;
614 if (qdata->callback_fn)
615 qdata->callback_fn(qdata->callback_data, err);
616 kfree(qdata);
617 }
618 return;
619}
620
621static int queue_mode_select(struct scsi_device *sdev,
622 activate_complete fn, void *data)
623{
624 struct rdac_queue_data *qdata;
625 struct rdac_controller *ctlr;
626
627 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
628 if (!qdata)
629 return SCSI_DH_RETRY;
630
631 qdata->h = get_rdac_data(sdev);
632 qdata->callback_fn = fn;
633 qdata->callback_data = data;
634
635 ctlr = qdata->h->ctlr;
636 spin_lock(&ctlr->ms_lock);
637 list_add_tail(&qdata->entry, &ctlr->ms_head);
638 if (!ctlr->ms_queued) {
639 ctlr->ms_queued = 1;
640 ctlr->ms_sdev = sdev;
641 queue_work(kmpath_rdacd, &ctlr->ms_work);
642 }
643 spin_unlock(&ctlr->ms_lock);
644 return SCSI_DH_OK;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700645}
646
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700647static int rdac_activate(struct scsi_device *sdev,
648 activate_complete fn, void *data)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700649{
650 struct rdac_dh_data *h = get_rdac_data(sdev);
651 int err = SCSI_DH_OK;
652
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700653 err = check_ownership(sdev, h);
654 if (err != SCSI_DH_OK)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700655 goto done;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700656
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700657 if (h->lun_state == RDAC_LUN_UNOWNED) {
658 err = queue_mode_select(sdev, fn, data);
659 if (err == SCSI_DH_OK)
660 return 0;
661 }
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700662done:
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700663 if (fn)
664 fn(data, err);
665 return 0;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700666}
667
668static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
669{
670 struct rdac_dh_data *h = get_rdac_data(sdev);
671 int ret = BLKPREP_OK;
672
673 if (h->state != RDAC_STATE_ACTIVE) {
674 ret = BLKPREP_KILL;
675 req->cmd_flags |= REQ_QUIET;
676 }
677 return ret;
678
679}
680
681static int rdac_check_sense(struct scsi_device *sdev,
682 struct scsi_sense_hdr *sense_hdr)
683{
684 struct rdac_dh_data *h = get_rdac_data(sdev);
Moger, Babudd784ed2009-09-03 21:42:28 -0600685
686 RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
687 "I/O returned with sense %02x/%02x/%02x",
688 (char *) h->ctlr->array_name, h->ctlr->index,
689 sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
690
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700691 switch (sense_hdr->sense_key) {
692 case NOT_READY:
Chauhan, Vijay8f032262009-04-20 18:14:23 +0530693 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
694 /* LUN Not Ready - Logical Unit Not Ready and is in
695 * the process of becoming ready
696 * Just retry.
697 */
698 return ADD_TO_MLQUEUE;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700699 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
700 /* LUN Not Ready - Storage firmware incompatible
701 * Manual code synchonisation required.
702 *
703 * Nothing we can do here. Try to bypass the path.
704 */
705 return SUCCESS;
706 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
707 /* LUN Not Ready - Quiescense in progress
708 *
709 * Just retry and wait.
710 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700711 return ADD_TO_MLQUEUE;
Chauhan, Vijayaf50bb92009-03-17 18:51:40 +0530712 if (sense_hdr->asc == 0xA1 && sense_hdr->ascq == 0x02)
713 /* LUN Not Ready - Quiescense in progress
714 * or has been achieved
715 * Just retry.
716 */
717 return ADD_TO_MLQUEUE;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700718 break;
719 case ILLEGAL_REQUEST:
720 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
721 /* Invalid Request - Current Logical Unit Ownership.
722 * Controller is not the current owner of the LUN,
723 * Fail the path, so that the other path be used.
724 */
725 h->state = RDAC_STATE_PASSIVE;
726 return SUCCESS;
727 }
728 break;
729 case UNIT_ATTENTION:
730 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
731 /*
732 * Power On, Reset, or Bus Device Reset, just retry.
733 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700734 return ADD_TO_MLQUEUE;
Chauhan, Vijayea41e412009-01-26 21:29:37 +0530735 if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
736 /*
737 * Quiescence in progress , just retry.
738 */
739 return ADD_TO_MLQUEUE;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700740 break;
741 }
742 /* success just means we do not care what scsi-ml does */
743 return SCSI_RETURN_NOT_HANDLED;
744}
745
Adrian Bunkf08c0762008-08-11 11:59:21 -0700746static const struct scsi_dh_devlist rdac_dev_list[] = {
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700747 {"IBM", "1722"},
748 {"IBM", "1724"},
749 {"IBM", "1726"},
750 {"IBM", "1742"},
751 {"IBM", "1814"},
752 {"IBM", "1815"},
753 {"IBM", "1818"},
754 {"IBM", "3526"},
755 {"SGI", "TP9400"},
756 {"SGI", "TP9500"},
757 {"SGI", "IS"},
758 {"STK", "OPENstorage D280"},
759 {"SUN", "CSM200_R"},
Charlie Brady5bab0882009-08-26 14:16:57 -0400760 {"SUN", "LCSM100_I"},
761 {"SUN", "LCSM100_S"},
762 {"SUN", "LCSM100_E"},
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700763 {"SUN", "LCSM100_F"},
Yanqing_Liu@Dell.com650849d2008-10-02 12:18:33 -0500764 {"DELL", "MD3000"},
765 {"DELL", "MD3000i"},
Yanqing_Liu@Dell.comcdf69bb2009-08-17 14:31:42 -0500766 {"DELL", "MD32xx"},
767 {"DELL", "MD32xxi"},
Babu Moger273c4782008-11-10 16:17:48 -0700768 {"LSI", "INF-01-00"},
769 {"ENGENIO", "INF-01-00"},
Chandra Seetharaman4dbfb542009-08-12 12:43:20 -0700770 {"STK", "FLEXLINE 380"},
771 {"SUN", "CSM100_R_FC"},
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700772 {NULL, NULL},
773};
774
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700775static int rdac_bus_attach(struct scsi_device *sdev);
776static void rdac_bus_detach(struct scsi_device *sdev);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700777
778static struct scsi_device_handler rdac_dh = {
779 .name = RDAC_NAME,
780 .module = THIS_MODULE,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700781 .devlist = rdac_dev_list,
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700782 .prep_fn = rdac_prep_fn,
783 .check_sense = rdac_check_sense,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700784 .attach = rdac_bus_attach,
785 .detach = rdac_bus_detach,
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700786 .activate = rdac_activate,
787};
788
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700789static int rdac_bus_attach(struct scsi_device *sdev)
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700790{
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700791 struct scsi_dh_data *scsi_dh_data;
792 struct rdac_dh_data *h;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700793 unsigned long flags;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700794 int err;
Moger, Babu15276662009-09-03 21:42:21 -0600795 char array_name[ARRAY_LABEL_LEN];
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700796
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700797 scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
798 + sizeof(*h) , GFP_KERNEL);
799 if (!scsi_dh_data) {
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700800 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700801 RDAC_NAME);
Chandra Seetharaman33af79d2008-07-16 17:35:08 -0700802 return 0;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700803 }
804
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700805 scsi_dh_data->scsi_dh = &rdac_dh;
806 h = (struct rdac_dh_data *) scsi_dh_data->buf;
807 h->lun = UNINITIALIZED_LUN;
808 h->state = RDAC_STATE_ACTIVE;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700809
Moger, Babu15276662009-09-03 21:42:21 -0600810 err = get_lun_info(sdev, h, array_name);
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700811 if (err != SCSI_DH_OK)
812 goto failed;
813
Moger, Babu15276662009-09-03 21:42:21 -0600814 err = initialize_controller(sdev, h, array_name);
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700815 if (err != SCSI_DH_OK)
816 goto failed;
817
Moger, Babu87b79a52009-09-03 21:42:15 -0600818 err = check_ownership(sdev, h);
819 if (err != SCSI_DH_OK)
820 goto clean_ctlr;
821
822 err = set_mode_select(sdev, h);
823 if (err != SCSI_DH_OK)
824 goto clean_ctlr;
825
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700826 if (!try_module_get(THIS_MODULE))
Moger, Babu87b79a52009-09-03 21:42:15 -0600827 goto clean_ctlr;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700828
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700829 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
830 sdev->scsi_dh_data = scsi_dh_data;
831 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700832
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700833 sdev_printk(KERN_NOTICE, sdev,
834 "%s: LUN %d (%s)\n",
835 RDAC_NAME, h->lun, lun_state[(int)h->lun_state]);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700836
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700837 return 0;
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700838
Moger, Babu87b79a52009-09-03 21:42:15 -0600839clean_ctlr:
840 kref_put(&h->ctlr->kref, release_controller);
841
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700842failed:
843 kfree(scsi_dh_data);
844 sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
845 RDAC_NAME);
846 return -EINVAL;
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700847}
848
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700849static void rdac_bus_detach( struct scsi_device *sdev )
850{
851 struct scsi_dh_data *scsi_dh_data;
852 struct rdac_dh_data *h;
853 unsigned long flags;
854
855 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
856 scsi_dh_data = sdev->scsi_dh_data;
857 sdev->scsi_dh_data = NULL;
858 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
859
860 h = (struct rdac_dh_data *) scsi_dh_data->buf;
861 if (h->ctlr)
862 kref_put(&h->ctlr->kref, release_controller);
863 kfree(scsi_dh_data);
864 module_put(THIS_MODULE);
Hannes Reineckeca9f0082008-07-17 16:53:15 -0700865 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", RDAC_NAME);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700866}
867
868
869
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700870static int __init rdac_init(void)
871{
872 int r;
873
874 r = scsi_register_device_handler(&rdac_dh);
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700875 if (r != 0) {
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700876 printk(KERN_ERR "Failed to register scsi device handler.");
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700877 goto done;
878 }
879
880 /*
881 * Create workqueue to handle mode selects for rdac
882 */
883 kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
884 if (!kmpath_rdacd) {
885 scsi_unregister_device_handler(&rdac_dh);
886 printk(KERN_ERR "kmpath_rdacd creation failed.\n");
887 }
888done:
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700889 return r;
890}
891
892static void __exit rdac_exit(void)
893{
Chandra Seetharaman970f3f42009-10-21 09:22:51 -0700894 destroy_workqueue(kmpath_rdacd);
Chandra Seetharamanfbd7ab32008-05-01 14:49:52 -0700895 scsi_unregister_device_handler(&rdac_dh);
896}
897
898module_init(rdac_init);
899module_exit(rdac_exit);
900
901MODULE_DESCRIPTION("Multipath LSI/Engenio RDAC driver");
902MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
903MODULE_LICENSE("GPL");