1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Target driver for EMC CLARiiON AX/CX-series hardware.
4 * Based on code from Lars Marowsky-Bree <lmb@suse.de>
5 * and Ed Goggin <egoggin@emc.com>.
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
8 * Copyright (C) 2006 Mike Christie
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_eh.h>
14 #include <scsi/scsi_dh.h>
15 #include <scsi/scsi_device.h>
17 #define CLARIION_NAME "emc"
19 #define CLARIION_TRESPASS_PAGE 0x22
20 #define CLARIION_BUFFER_SIZE 0xFC
21 #define CLARIION_TIMEOUT (60 * HZ)
22 #define CLARIION_RETRIES 3
23 #define CLARIION_UNBOUND_LU -1
24 #define CLARIION_SP_A 0
25 #define CLARIION_SP_B 1
28 #define CLARIION_SHORT_TRESPASS 1
29 #define CLARIION_HONOR_RESERVATIONS 2
32 #define CLARIION_LUN_UNINITIALIZED -1
33 #define CLARIION_LUN_UNBOUND 0
34 #define CLARIION_LUN_BOUND 1
35 #define CLARIION_LUN_OWNED 2
37 static unsigned char long_trespass
[] = {
38 0, 0, 0, 0, 0, 0, 0, 0,
39 CLARIION_TRESPASS_PAGE
, /* Page code */
40 0x09, /* Page length - 2 */
41 0x01, /* Trespass code */
42 0xff, 0xff, /* Trespass target */
43 0, 0, 0, 0, 0, 0 /* Reserved bytes / unknown */
46 static unsigned char short_trespass
[] = {
48 CLARIION_TRESPASS_PAGE
, /* Page code */
49 0x02, /* Page length - 2 */
50 0x01, /* Trespass code */
51 0xff, /* Trespass target */
54 static const char * lun_state
[] =
61 struct clariion_dh_data
{
64 * CLARIION_SHORT_TRESPASS
65 * Use short trespass command (FC-series) or the long version
66 * (default for AX/CX CLARiiON arrays).
68 * CLARIION_HONOR_RESERVATIONS
69 * Whether or not (default) to honor SCSI reservations when
70 * initiating a switch-over.
74 * I/O buffer for both MODE_SELECT and INQUIRY commands.
76 unsigned char buffer
[CLARIION_BUFFER_SIZE
];
86 * which SP (A=0,B=1,UNBOUND=-1) is the default SP for this
91 * which SP (A=0,B=1,UNBOUND=-1) is the active SP for this
98 * Parse MODE_SELECT cmd reply.
100 static int trespass_endio(struct scsi_device
*sdev
,
101 struct scsi_sense_hdr
*sshdr
)
103 int err
= SCSI_DH_IO
;
105 sdev_printk(KERN_ERR
, sdev
, "%s: Found valid sense data 0x%2x, "
106 "0x%2x, 0x%2x while sending CLARiiON trespass "
107 "command.\n", CLARIION_NAME
, sshdr
->sense_key
,
108 sshdr
->asc
, sshdr
->ascq
);
110 if (sshdr
->sense_key
== 0x05 && sshdr
->asc
== 0x04 &&
111 sshdr
->ascq
== 0x00) {
113 * Array based copy in progress -- do not send
114 * mode_select or copy will be aborted mid-stream.
116 sdev_printk(KERN_INFO
, sdev
, "%s: Array Based Copy in "
117 "progress while sending CLARiiON trespass "
118 "command.\n", CLARIION_NAME
);
119 err
= SCSI_DH_DEV_TEMP_BUSY
;
120 } else if (sshdr
->sense_key
== 0x02 && sshdr
->asc
== 0x04 &&
121 sshdr
->ascq
== 0x03) {
123 * LUN Not Ready - Manual Intervention Required
124 * indicates in-progress ucode upgrade (NDU).
126 sdev_printk(KERN_INFO
, sdev
, "%s: Detected in-progress "
127 "ucode upgrade NDU operation while sending "
128 "CLARiiON trespass command.\n", CLARIION_NAME
);
129 err
= SCSI_DH_DEV_TEMP_BUSY
;
131 err
= SCSI_DH_DEV_FAILED
;
135 static int parse_sp_info_reply(struct scsi_device
*sdev
,
136 struct clariion_dh_data
*csdev
)
138 int err
= SCSI_DH_OK
;
140 /* check for in-progress ucode upgrade (NDU) */
141 if (csdev
->buffer
[48] != 0) {
142 sdev_printk(KERN_NOTICE
, sdev
, "%s: Detected in-progress "
143 "ucode upgrade NDU operation while finding "
144 "current active SP.", CLARIION_NAME
);
145 err
= SCSI_DH_DEV_TEMP_BUSY
;
148 if (csdev
->buffer
[4] > 2) {
149 /* Invalid buffer format */
150 sdev_printk(KERN_NOTICE
, sdev
,
151 "%s: invalid VPD page 0xC0 format\n",
156 switch (csdev
->buffer
[28] & 0x0f) {
158 sdev_printk(KERN_NOTICE
, sdev
,
159 "%s: ALUA failover mode detected\n",
166 sdev_printk(KERN_WARNING
, sdev
,
167 "%s: Invalid failover mode %d\n",
168 CLARIION_NAME
, csdev
->buffer
[28] & 0x0f);
173 csdev
->default_sp
= csdev
->buffer
[5];
174 csdev
->lun_state
= csdev
->buffer
[4];
175 csdev
->current_sp
= csdev
->buffer
[8];
176 csdev
->port
= csdev
->buffer
[7];
177 if (csdev
->lun_state
== CLARIION_LUN_OWNED
)
178 sdev
->access_state
= SCSI_ACCESS_STATE_OPTIMAL
;
180 sdev
->access_state
= SCSI_ACCESS_STATE_STANDBY
;
181 if (csdev
->default_sp
== csdev
->current_sp
)
182 sdev
->access_state
|= SCSI_ACCESS_STATE_PREFERRED
;
187 #define emc_default_str "FC (Legacy)"
189 static char * parse_sp_model(struct scsi_device
*sdev
, unsigned char *buffer
)
191 unsigned char len
= buffer
[4] + 5;
192 char *sp_model
= NULL
;
193 unsigned char sp_len
, serial_len
;
196 sdev_printk(KERN_WARNING
, sdev
,
197 "%s: Invalid information section length %d\n",
199 /* Check for old FC arrays */
200 if (!strncmp(buffer
+ 8, "DGC", 3)) {
201 /* Old FC array, not supporting extended information */
202 sp_model
= emc_default_str
;
208 * Parse extended information for SP model number
210 serial_len
= buffer
[160];
211 if (serial_len
== 0 || serial_len
+ 161 > len
) {
212 sdev_printk(KERN_WARNING
, sdev
,
213 "%s: Invalid array serial number length %d\n",
214 CLARIION_NAME
, serial_len
);
218 if (sp_len
== 0 || serial_len
+ sp_len
+ 161 > len
) {
219 sdev_printk(KERN_WARNING
, sdev
,
220 "%s: Invalid model number length %d\n",
221 CLARIION_NAME
, sp_len
);
224 sp_model
= &buffer
[serial_len
+ 161];
225 /* Strip whitespace at the end */
226 while (sp_len
> 1 && sp_model
[sp_len
- 1] == ' ')
229 sp_model
[sp_len
] = '\0';
235 static int send_trespass_cmd(struct scsi_device
*sdev
,
236 struct clariion_dh_data
*csdev
)
238 unsigned char *page22
;
239 unsigned char cdb
[MAX_COMMAND_SIZE
];
240 int err
, res
= SCSI_DH_OK
, len
;
241 struct scsi_sense_hdr sshdr
;
242 u64 req_flags
= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
245 if (csdev
->flags
& CLARIION_SHORT_TRESPASS
) {
246 page22
= short_trespass
;
247 if (!(csdev
->flags
& CLARIION_HONOR_RESERVATIONS
))
248 /* Set Honor Reservations bit */
250 len
= sizeof(short_trespass
);
251 cdb
[0] = MODE_SELECT
;
255 page22
= long_trespass
;
256 if (!(csdev
->flags
& CLARIION_HONOR_RESERVATIONS
))
257 /* Set Honor Reservations bit */
259 len
= sizeof(long_trespass
);
260 cdb
[0] = MODE_SELECT_10
;
263 BUG_ON((len
> CLARIION_BUFFER_SIZE
));
264 memcpy(csdev
->buffer
, page22
, len
);
266 err
= scsi_execute(sdev
, cdb
, DMA_TO_DEVICE
, csdev
->buffer
, len
, NULL
,
267 &sshdr
, CLARIION_TIMEOUT
* HZ
, CLARIION_RETRIES
,
270 if (scsi_sense_valid(&sshdr
))
271 res
= trespass_endio(sdev
, &sshdr
);
273 sdev_printk(KERN_INFO
, sdev
,
274 "%s: failed to send MODE SELECT: %x\n",
283 static int clariion_check_sense(struct scsi_device
*sdev
,
284 struct scsi_sense_hdr
*sense_hdr
)
286 switch (sense_hdr
->sense_key
) {
288 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x03)
290 * LUN Not Ready - Manual Intervention Required
291 * indicates this is a passive path.
293 * FIXME: However, if this is seen and EVPD C0
294 * indicates that this is due to a NDU in
295 * progress, we should set FAIL_PATH too.
296 * This indicates we might have to do a SCSI
297 * inquiry in the end_io path. Ugh.
299 * Can return FAILED only when we want the error
300 * recovery process to kick in.
304 case ILLEGAL_REQUEST
:
305 if (sense_hdr
->asc
== 0x25 && sense_hdr
->ascq
== 0x01)
307 * An array based copy is in progress. Do not
308 * fail the path, do not bypass to another PG,
309 * do not retry. Fail the IO immediately.
310 * (Actually this is the same conclusion as in
311 * the default handler, but lets make sure.)
313 * Can return FAILED only when we want the error
314 * recovery process to kick in.
319 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x00)
321 * Unit Attention Code. This is the first IO
322 * to the new path, so just retry.
324 return ADD_TO_MLQUEUE
;
328 return SCSI_RETURN_NOT_HANDLED
;
331 static blk_status_t
clariion_prep_fn(struct scsi_device
*sdev
,
334 struct clariion_dh_data
*h
= sdev
->handler_data
;
336 if (h
->lun_state
!= CLARIION_LUN_OWNED
) {
337 req
->rq_flags
|= RQF_QUIET
;
338 return BLK_STS_IOERR
;
344 static int clariion_std_inquiry(struct scsi_device
*sdev
,
345 struct clariion_dh_data
*csdev
)
347 int err
= SCSI_DH_OK
;
350 sp_model
= parse_sp_model(sdev
, sdev
->inquiry
);
352 err
= SCSI_DH_DEV_UNSUPP
;
357 * FC Series arrays do not support long trespass
359 if (!strlen(sp_model
) || !strncmp(sp_model
, "FC",2))
360 csdev
->flags
|= CLARIION_SHORT_TRESPASS
;
362 sdev_printk(KERN_INFO
, sdev
,
363 "%s: detected Clariion %s, flags %x\n",
364 CLARIION_NAME
, sp_model
, csdev
->flags
);
369 static int clariion_send_inquiry(struct scsi_device
*sdev
,
370 struct clariion_dh_data
*csdev
)
372 int err
= SCSI_DH_IO
;
374 if (!scsi_get_vpd_page(sdev
, 0xC0, csdev
->buffer
,
375 CLARIION_BUFFER_SIZE
))
376 err
= parse_sp_info_reply(sdev
, csdev
);
381 static int clariion_activate(struct scsi_device
*sdev
,
382 activate_complete fn
, void *data
)
384 struct clariion_dh_data
*csdev
= sdev
->handler_data
;
387 result
= clariion_send_inquiry(sdev
, csdev
);
388 if (result
!= SCSI_DH_OK
)
391 if (csdev
->lun_state
== CLARIION_LUN_OWNED
)
394 result
= send_trespass_cmd(sdev
, csdev
);
395 if (result
!= SCSI_DH_OK
)
397 sdev_printk(KERN_INFO
, sdev
,"%s: %s trespass command sent\n",
399 csdev
->flags
&CLARIION_SHORT_TRESPASS
?"short":"long" );
402 result
= clariion_send_inquiry(sdev
, csdev
);
403 if (result
!= SCSI_DH_OK
)
407 sdev_printk(KERN_INFO
, sdev
,
408 "%s: at SP %c Port %d (%s, default SP %c)\n",
409 CLARIION_NAME
, csdev
->current_sp
+ 'A',
410 csdev
->port
, lun_state
[csdev
->lun_state
],
411 csdev
->default_sp
+ 'A');
418 * params - parameters in the following format
419 * "no_of_params\0param1\0param2\0param3\0...\0"
420 * for example, string for 2 parameters with value 10 and 21
421 * is specified as "2\010\021\0".
423 static int clariion_set_params(struct scsi_device
*sdev
, const char *params
)
425 struct clariion_dh_data
*csdev
= sdev
->handler_data
;
426 unsigned int hr
= 0, st
= 0, argc
;
427 const char *p
= params
;
428 int result
= SCSI_DH_OK
;
430 if ((sscanf(params
, "%u", &argc
) != 1) || (argc
!= 2))
435 if ((sscanf(p
, "%u", &st
) != 1) || (st
> 1))
440 if ((sscanf(p
, "%u", &hr
) != 1) || (hr
> 1))
444 csdev
->flags
|= CLARIION_SHORT_TRESPASS
;
446 csdev
->flags
&= ~CLARIION_SHORT_TRESPASS
;
449 csdev
->flags
|= CLARIION_HONOR_RESERVATIONS
;
451 csdev
->flags
&= ~CLARIION_HONOR_RESERVATIONS
;
454 * If this path is owned, we have to send a trespass command
455 * with the new parameters. If not, simply return. Next trespass
456 * command would use the parameters.
458 if (csdev
->lun_state
!= CLARIION_LUN_OWNED
)
461 csdev
->lun_state
= CLARIION_LUN_UNINITIALIZED
;
462 result
= send_trespass_cmd(sdev
, csdev
);
463 if (result
!= SCSI_DH_OK
)
467 result
= clariion_send_inquiry(sdev
, csdev
);
473 static int clariion_bus_attach(struct scsi_device
*sdev
)
475 struct clariion_dh_data
*h
;
478 h
= kzalloc(sizeof(*h
) , GFP_KERNEL
);
480 return SCSI_DH_NOMEM
;
481 h
->lun_state
= CLARIION_LUN_UNINITIALIZED
;
482 h
->default_sp
= CLARIION_UNBOUND_LU
;
483 h
->current_sp
= CLARIION_UNBOUND_LU
;
485 err
= clariion_std_inquiry(sdev
, h
);
486 if (err
!= SCSI_DH_OK
)
489 err
= clariion_send_inquiry(sdev
, h
);
490 if (err
!= SCSI_DH_OK
)
493 sdev_printk(KERN_INFO
, sdev
,
494 "%s: connected to SP %c Port %d (%s, default SP %c)\n",
495 CLARIION_NAME
, h
->current_sp
+ 'A',
496 h
->port
, lun_state
[h
->lun_state
],
497 h
->default_sp
+ 'A');
499 sdev
->handler_data
= h
;
507 static void clariion_bus_detach(struct scsi_device
*sdev
)
509 kfree(sdev
->handler_data
);
510 sdev
->handler_data
= NULL
;
513 static struct scsi_device_handler clariion_dh
= {
514 .name
= CLARIION_NAME
,
515 .module
= THIS_MODULE
,
516 .attach
= clariion_bus_attach
,
517 .detach
= clariion_bus_detach
,
518 .check_sense
= clariion_check_sense
,
519 .activate
= clariion_activate
,
520 .prep_fn
= clariion_prep_fn
,
521 .set_params
= clariion_set_params
,
524 static int __init
clariion_init(void)
528 r
= scsi_register_device_handler(&clariion_dh
);
530 printk(KERN_ERR
"%s: Failed to register scsi device handler.",
535 static void __exit
clariion_exit(void)
537 scsi_unregister_device_handler(&clariion_dh
);
540 module_init(clariion_init
);
541 module_exit(clariion_exit
);
543 MODULE_DESCRIPTION("EMC CX/AX/FC-family driver");
544 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, Chandra Seetharaman <sekharan@us.ibm.com>");
545 MODULE_LICENSE("GPL");