1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Basic HP/COMPAQ MSA 1000 support. This is only needed if your HW cannot be
6 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2006 Mike Christie
8 * Copyright (C) 2008 Hannes Reinecke <hare@suse.de>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <scsi/scsi.h>
14 #include <scsi/scsi_dbg.h>
15 #include <scsi/scsi_eh.h>
16 #include <scsi/scsi_dh.h>
18 #define HP_SW_NAME "hp_sw"
20 #define HP_SW_TIMEOUT (60 * HZ)
21 #define HP_SW_RETRIES 3
23 #define HP_SW_PATH_UNINITIALIZED -1
24 #define HP_SW_PATH_ACTIVE 0
25 #define HP_SW_PATH_PASSIVE 1
27 struct hp_sw_dh_data {
31 struct scsi_device *sdev;
34 static int hp_sw_start_stop(struct hp_sw_dh_data *);
37 * tur_done - Handle TEST UNIT READY return status
38 * @sdev: sdev the command has been sent to
39 * @errors: blk error code
41 * Returns SCSI_DH_DEV_OFFLINED if the sdev is on the passive path
43 static int tur_done(struct scsi_device *sdev, struct hp_sw_dh_data *h,
44 struct scsi_sense_hdr *sshdr)
48 switch (sshdr->sense_key) {
50 ret = SCSI_DH_IMM_RETRY;
53 if (sshdr->asc == 0x04 && sshdr->ascq == 2) {
55 * LUN not ready - Initialization command required
57 * This is the passive path
59 h->path_state = HP_SW_PATH_PASSIVE;
65 sdev_printk(KERN_WARNING, sdev,
66 "%s: sending tur failed, sense %x/%x/%x\n",
67 HP_SW_NAME, sshdr->sense_key, sshdr->asc,
75 * hp_sw_tur - Send TEST UNIT READY
76 * @sdev: sdev command should be sent to
78 * Use the TEST UNIT READY command to determine
81 static int hp_sw_tur(struct scsi_device *sdev, struct hp_sw_dh_data *h)
83 unsigned char cmd[6] = { TEST_UNIT_READY };
84 struct scsi_sense_hdr sshdr;
86 blk_opf_t opf = REQ_OP_DRV_IN | REQ_FAILFAST_DEV |
87 REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER;
88 const struct scsi_exec_args exec_args = {
93 res = scsi_execute_cmd(sdev, cmd, opf, NULL, 0, HP_SW_TIMEOUT,
94 HP_SW_RETRIES, &exec_args);
95 if (res > 0 && scsi_sense_valid(&sshdr)) {
96 ret = tur_done(sdev, h, &sshdr);
97 } else if (res == 0) {
98 h->path_state = HP_SW_PATH_ACTIVE;
101 sdev_printk(KERN_WARNING, sdev,
102 "%s: sending tur failed with %x\n",
107 if (ret == SCSI_DH_IMM_RETRY)
114 * hp_sw_start_stop - Send START STOP UNIT command
115 * @sdev: sdev command should be sent to
117 * Sending START STOP UNIT activates the SP.
119 static int hp_sw_start_stop(struct hp_sw_dh_data *h)
121 unsigned char cmd[6] = { START_STOP, 0, 0, 0, 1, 0 };
122 struct scsi_sense_hdr sshdr;
123 struct scsi_device *sdev = h->sdev;
125 int retry_cnt = HP_SW_RETRIES;
126 blk_opf_t opf = REQ_OP_DRV_IN | REQ_FAILFAST_DEV |
127 REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER;
128 const struct scsi_exec_args exec_args = {
133 res = scsi_execute_cmd(sdev, cmd, opf, NULL, 0, HP_SW_TIMEOUT,
134 HP_SW_RETRIES, &exec_args);
137 } else if (res < 0 || !scsi_sense_valid(&sshdr)) {
138 sdev_printk(KERN_WARNING, sdev,
139 "%s: sending start_stop_unit failed, "
140 "no sense available\n", HP_SW_NAME);
144 switch (sshdr.sense_key) {
146 if (sshdr.asc == 0x04 && sshdr.ascq == 3) {
148 * LUN not ready - manual intervention required
150 * Switch-over in progress, retry.
159 sdev_printk(KERN_WARNING, sdev,
160 "%s: sending start_stop_unit failed, "
161 "sense %x/%x/%x\n", HP_SW_NAME,
162 sshdr.sense_key, sshdr.asc, sshdr.ascq);
169 static blk_status_t hp_sw_prep_fn(struct scsi_device *sdev, struct request *req)
171 struct hp_sw_dh_data *h = sdev->handler_data;
173 if (h->path_state != HP_SW_PATH_ACTIVE) {
174 req->rq_flags |= RQF_QUIET;
175 return BLK_STS_IOERR;
182 * hp_sw_activate - Activate a path
183 * @sdev: sdev on the path to be activated
185 * The HP Active/Passive firmware is pretty simple;
186 * the passive path reports NOT READY with sense codes
187 * 0x04/0x02; a START STOP UNIT command will then
188 * activate the passive path (and deactivate the
189 * previously active one).
191 static int hp_sw_activate(struct scsi_device *sdev,
192 activate_complete fn, void *data)
194 int ret = SCSI_DH_OK;
195 struct hp_sw_dh_data *h = sdev->handler_data;
197 ret = hp_sw_tur(sdev, h);
199 if (ret == SCSI_DH_OK && h->path_state == HP_SW_PATH_PASSIVE)
200 ret = hp_sw_start_stop(h);
207 static int hp_sw_bus_attach(struct scsi_device *sdev)
209 struct hp_sw_dh_data *h;
212 h = kzalloc(sizeof(*h), GFP_KERNEL);
214 return SCSI_DH_NOMEM;
215 h->path_state = HP_SW_PATH_UNINITIALIZED;
216 h->retries = HP_SW_RETRIES;
219 ret = hp_sw_tur(sdev, h);
220 if (ret != SCSI_DH_OK)
222 if (h->path_state == HP_SW_PATH_UNINITIALIZED) {
227 sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
228 HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
231 sdev->handler_data = h;
238 static void hp_sw_bus_detach( struct scsi_device *sdev )
240 kfree(sdev->handler_data);
241 sdev->handler_data = NULL;
244 static struct scsi_device_handler hp_sw_dh = {
246 .module = THIS_MODULE,
247 .attach = hp_sw_bus_attach,
248 .detach = hp_sw_bus_detach,
249 .activate = hp_sw_activate,
250 .prep_fn = hp_sw_prep_fn,
253 static int __init hp_sw_init(void)
255 return scsi_register_device_handler(&hp_sw_dh);
258 static void __exit hp_sw_exit(void)
260 scsi_unregister_device_handler(&hp_sw_dh);
263 module_init(hp_sw_init);
264 module_exit(hp_sw_exit);
266 MODULE_DESCRIPTION("HP Active/Passive driver");
267 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu");
268 MODULE_LICENSE("GPL");