blob: 857fdd6032b25a8df5efee8e70740ea135ae07be [file] [log] [blame]
Mike Christief6dd3372008-05-01 14:49:59 -07001/*
2 * Basic HP/COMPAQ MSA 1000 support. This is only needed if your HW cannot be
3 * upgraded.
4 *
5 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2006 Mike Christie
Hannes Reinecke2aef6d52008-07-17 16:53:09 -07007 * Copyright (C) 2008 Hannes Reinecke <hare@suse.de>
Mike Christief6dd3372008-05-01 14:49:59 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <scsi/scsi.h>
25#include <scsi/scsi_dbg.h>
26#include <scsi/scsi_eh.h>
27#include <scsi/scsi_dh.h>
28
Hannes Reinecke2aef6d52008-07-17 16:53:09 -070029#define HP_SW_NAME "hp_sw"
Mike Christief6dd3372008-05-01 14:49:59 -070030
Hannes Reinecke2aef6d52008-07-17 16:53:09 -070031#define HP_SW_TIMEOUT (60 * HZ)
32#define HP_SW_RETRIES 3
33
34#define HP_SW_PATH_UNINITIALIZED -1
35#define HP_SW_PATH_ACTIVE 0
36#define HP_SW_PATH_PASSIVE 1
Mike Christief6dd3372008-05-01 14:49:59 -070037
38struct hp_sw_dh_data {
39 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
Hannes Reinecke2aef6d52008-07-17 16:53:09 -070040 int path_state;
Mike Christief6dd3372008-05-01 14:49:59 -070041 int retries;
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -070042 int retry_cnt;
43 struct scsi_device *sdev;
44 activate_complete callback_fn;
45 void *callback_data;
Mike Christief6dd3372008-05-01 14:49:59 -070046};
47
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -070048static int hp_sw_start_stop(struct hp_sw_dh_data *);
49
Mike Christief6dd3372008-05-01 14:49:59 -070050static inline struct hp_sw_dh_data *get_hp_sw_data(struct scsi_device *sdev)
51{
52 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
53 BUG_ON(scsi_dh_data == NULL);
54 return ((struct hp_sw_dh_data *) scsi_dh_data->buf);
55}
56
Hannes Reinecke2aef6d52008-07-17 16:53:09 -070057/*
58 * tur_done - Handle TEST UNIT READY return status
59 * @sdev: sdev the command has been sent to
60 * @errors: blk error code
61 *
62 * Returns SCSI_DH_DEV_OFFLINED if the sdev is on the passive path
63 */
64static int tur_done(struct scsi_device *sdev, unsigned char *sense)
Mike Christief6dd3372008-05-01 14:49:59 -070065{
Hannes Reinecke2aef6d52008-07-17 16:53:09 -070066 struct scsi_sense_hdr sshdr;
67 int ret;
68
69 ret = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
70 if (!ret) {
71 sdev_printk(KERN_WARNING, sdev,
72 "%s: sending tur failed, no sense available\n",
73 HP_SW_NAME);
74 ret = SCSI_DH_IO;
75 goto done;
76 }
77 switch (sshdr.sense_key) {
78 case UNIT_ATTENTION:
79 ret = SCSI_DH_IMM_RETRY;
80 break;
81 case NOT_READY:
82 if ((sshdr.asc == 0x04) && (sshdr.ascq == 2)) {
83 /*
84 * LUN not ready - Initialization command required
85 *
86 * This is the passive path
87 */
88 ret = SCSI_DH_DEV_OFFLINED;
89 break;
90 }
91 /* Fallthrough */
92 default:
93 sdev_printk(KERN_WARNING, sdev,
94 "%s: sending tur failed, sense %x/%x/%x\n",
95 HP_SW_NAME, sshdr.sense_key, sshdr.asc,
96 sshdr.ascq);
97 break;
98 }
99
100done:
101 return ret;
102}
103
104/*
105 * hp_sw_tur - Send TEST UNIT READY
106 * @sdev: sdev command should be sent to
107 *
108 * Use the TEST UNIT READY command to determine
109 * the path state.
110 */
111static int hp_sw_tur(struct scsi_device *sdev, struct hp_sw_dh_data *h)
112{
113 struct request *req;
114 int ret;
115
Alan D. Brunellefebd7a52008-12-09 15:52:15 +0100116retry:
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700117 req = blk_get_request(sdev->request_queue, WRITE, GFP_NOIO);
118 if (!req)
119 return SCSI_DH_RES_TEMP_UNAVAIL;
120
121 req->cmd_type = REQ_TYPE_BLOCK_PC;
Mike Christie6000a362008-08-19 18:45:30 -0500122 req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
123 REQ_FAILFAST_DRIVER;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700124 req->cmd_len = COMMAND_SIZE(TEST_UNIT_READY);
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700125 req->cmd[0] = TEST_UNIT_READY;
126 req->timeout = HP_SW_TIMEOUT;
127 req->sense = h->sense;
128 memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
129 req->sense_len = 0;
130
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700131 ret = blk_execute_rq(req->q, NULL, req, 1);
132 if (ret == -EIO) {
133 if (req->sense_len > 0) {
134 ret = tur_done(sdev, h->sense);
135 } else {
136 sdev_printk(KERN_WARNING, sdev,
137 "%s: sending tur failed with %x\n",
138 HP_SW_NAME, req->errors);
139 ret = SCSI_DH_IO;
140 }
141 } else {
142 h->path_state = HP_SW_PATH_ACTIVE;
143 ret = SCSI_DH_OK;
144 }
Alan D. Brunellefebd7a52008-12-09 15:52:15 +0100145 if (ret == SCSI_DH_IMM_RETRY) {
146 blk_put_request(req);
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700147 goto retry;
Alan D. Brunellefebd7a52008-12-09 15:52:15 +0100148 }
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700149 if (ret == SCSI_DH_DEV_OFFLINED) {
150 h->path_state = HP_SW_PATH_PASSIVE;
151 ret = SCSI_DH_OK;
152 }
153
154 blk_put_request(req);
155
156 return ret;
157}
158
159/*
160 * start_done - Handle START STOP UNIT return status
161 * @sdev: sdev the command has been sent to
162 * @errors: blk error code
163 */
164static int start_done(struct scsi_device *sdev, unsigned char *sense)
165{
Mike Christief6dd3372008-05-01 14:49:59 -0700166 struct scsi_sense_hdr sshdr;
167 int rc;
168
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700169 rc = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
170 if (!rc) {
171 sdev_printk(KERN_WARNING, sdev,
172 "%s: sending start_stop_unit failed, "
173 "no sense available\n",
174 HP_SW_NAME);
175 return SCSI_DH_IO;
176 }
Mike Christief6dd3372008-05-01 14:49:59 -0700177 switch (sshdr.sense_key) {
178 case NOT_READY:
179 if ((sshdr.asc == 0x04) && (sshdr.ascq == 3)) {
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700180 /*
181 * LUN not ready - manual intervention required
182 *
183 * Switch-over in progress, retry.
184 */
Mike Christief6dd3372008-05-01 14:49:59 -0700185 rc = SCSI_DH_RETRY;
Mike Christief6dd3372008-05-01 14:49:59 -0700186 break;
187 }
188 /* fall through */
189 default:
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700190 sdev_printk(KERN_WARNING, sdev,
191 "%s: sending start_stop_unit failed, sense %x/%x/%x\n",
192 HP_SW_NAME, sshdr.sense_key, sshdr.asc,
193 sshdr.ascq);
Mike Christief6dd3372008-05-01 14:49:59 -0700194 rc = SCSI_DH_IO;
195 }
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700196
Mike Christief6dd3372008-05-01 14:49:59 -0700197 return rc;
198}
199
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700200static void start_stop_endio(struct request *req, int error)
201{
202 struct hp_sw_dh_data *h = req->end_io_data;
203 unsigned err = SCSI_DH_OK;
204
205 if (error || host_byte(req->errors) != DID_OK ||
206 msg_byte(req->errors) != COMMAND_COMPLETE) {
207 sdev_printk(KERN_WARNING, h->sdev,
208 "%s: sending start_stop_unit failed with %x\n",
209 HP_SW_NAME, req->errors);
210 err = SCSI_DH_IO;
211 goto done;
212 }
213
214 if (req->sense_len > 0) {
215 err = start_done(h->sdev, h->sense);
216 if (err == SCSI_DH_RETRY) {
217 err = SCSI_DH_IO;
218 if (--h->retry_cnt) {
219 blk_put_request(req);
220 err = hp_sw_start_stop(h);
221 if (err == SCSI_DH_OK)
222 return;
223 }
224 }
225 }
226done:
227 blk_put_request(req);
228 if (h->callback_fn) {
229 h->callback_fn(h->callback_data, err);
230 h->callback_fn = h->callback_data = NULL;
231 }
232 return;
233
234}
235
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700236/*
237 * hp_sw_start_stop - Send START STOP UNIT command
238 * @sdev: sdev command should be sent to
239 *
240 * Sending START STOP UNIT activates the SP.
241 */
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700242static int hp_sw_start_stop(struct hp_sw_dh_data *h)
Mike Christief6dd3372008-05-01 14:49:59 -0700243{
Mike Christief6dd3372008-05-01 14:49:59 -0700244 struct request *req;
Mike Christief6dd3372008-05-01 14:49:59 -0700245
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700246 req = blk_get_request(h->sdev->request_queue, WRITE, GFP_ATOMIC);
Mike Christief6dd3372008-05-01 14:49:59 -0700247 if (!req)
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700248 return SCSI_DH_RES_TEMP_UNAVAIL;
Mike Christief6dd3372008-05-01 14:49:59 -0700249
250 req->cmd_type = REQ_TYPE_BLOCK_PC;
Mike Christie6000a362008-08-19 18:45:30 -0500251 req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
252 REQ_FAILFAST_DRIVER;
Mike Christief6dd3372008-05-01 14:49:59 -0700253 req->cmd_len = COMMAND_SIZE(START_STOP);
Mike Christief6dd3372008-05-01 14:49:59 -0700254 req->cmd[0] = START_STOP;
255 req->cmd[4] = 1; /* Start spin cycle */
256 req->timeout = HP_SW_TIMEOUT;
257 req->sense = h->sense;
258 memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
259 req->sense_len = 0;
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700260 req->end_io_data = h;
Mike Christief6dd3372008-05-01 14:49:59 -0700261
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700262 blk_execute_rq_nowait(req->q, NULL, req, 1, start_stop_endio);
263 return SCSI_DH_OK;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700264}
265
266static int hp_sw_prep_fn(struct scsi_device *sdev, struct request *req)
267{
268 struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
269 int ret = BLKPREP_OK;
270
271 if (h->path_state != HP_SW_PATH_ACTIVE) {
272 ret = BLKPREP_KILL;
273 req->cmd_flags |= REQ_QUIET;
274 }
275 return ret;
276
277}
278
279/*
280 * hp_sw_activate - Activate a path
281 * @sdev: sdev on the path to be activated
282 *
283 * The HP Active/Passive firmware is pretty simple;
284 * the passive path reports NOT READY with sense codes
285 * 0x04/0x02; a START STOP UNIT command will then
286 * activate the passive path (and deactivate the
287 * previously active one).
288 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700289static int hp_sw_activate(struct scsi_device *sdev,
290 activate_complete fn, void *data)
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700291{
292 int ret = SCSI_DH_OK;
293 struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
294
295 ret = hp_sw_tur(sdev, h);
296
297 if (ret == SCSI_DH_OK && h->path_state == HP_SW_PATH_PASSIVE) {
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700298 h->retry_cnt = h->retries;
299 h->callback_fn = fn;
300 h->callback_data = data;
301 ret = hp_sw_start_stop(h);
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700302 if (ret == SCSI_DH_OK)
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700303 return 0;
304 h->callback_fn = h->callback_data = NULL;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700305 }
306
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700307 if (fn)
308 fn(data, ret);
309 return 0;
Mike Christief6dd3372008-05-01 14:49:59 -0700310}
311
Adrian Bunkf08c0762008-08-11 11:59:21 -0700312static const struct scsi_dh_devlist hp_sw_dh_data_list[] = {
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700313 {"COMPAQ", "MSA1000 VOLUME"},
314 {"COMPAQ", "HSV110"},
315 {"HP", "HSV100"},
Mike Christief6dd3372008-05-01 14:49:59 -0700316 {"DEC", "HSG80"},
317 {NULL, NULL},
318};
319
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700320static int hp_sw_bus_attach(struct scsi_device *sdev);
321static void hp_sw_bus_detach(struct scsi_device *sdev);
Mike Christief6dd3372008-05-01 14:49:59 -0700322
323static struct scsi_device_handler hp_sw_dh = {
324 .name = HP_SW_NAME,
325 .module = THIS_MODULE,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700326 .devlist = hp_sw_dh_data_list,
327 .attach = hp_sw_bus_attach,
328 .detach = hp_sw_bus_detach,
Mike Christief6dd3372008-05-01 14:49:59 -0700329 .activate = hp_sw_activate,
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700330 .prep_fn = hp_sw_prep_fn,
Mike Christief6dd3372008-05-01 14:49:59 -0700331};
332
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700333static int hp_sw_bus_attach(struct scsi_device *sdev)
Mike Christief6dd3372008-05-01 14:49:59 -0700334{
Mike Christief6dd3372008-05-01 14:49:59 -0700335 struct scsi_dh_data *scsi_dh_data;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700336 struct hp_sw_dh_data *h;
Mike Christief6dd3372008-05-01 14:49:59 -0700337 unsigned long flags;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700338 int ret;
Mike Christief6dd3372008-05-01 14:49:59 -0700339
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700340 scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
341 + sizeof(struct hp_sw_dh_data) , GFP_KERNEL);
342 if (!scsi_dh_data) {
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700343 sdev_printk(KERN_ERR, sdev, "%s: Attach Failed\n",
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700344 HP_SW_NAME);
Chandra Seetharaman33af79d2008-07-16 17:35:08 -0700345 return 0;
Mike Christief6dd3372008-05-01 14:49:59 -0700346 }
347
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700348 scsi_dh_data->scsi_dh = &hp_sw_dh;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700349 h = (struct hp_sw_dh_data *) scsi_dh_data->buf;
350 h->path_state = HP_SW_PATH_UNINITIALIZED;
351 h->retries = HP_SW_RETRIES;
Chandra Seetharaman4e2ef862009-10-21 09:22:58 -0700352 h->sdev = sdev;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700353
354 ret = hp_sw_tur(sdev, h);
355 if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
356 goto failed;
357
358 if (!try_module_get(THIS_MODULE))
359 goto failed;
360
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700361 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
362 sdev->scsi_dh_data = scsi_dh_data;
363 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700364
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700365 sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
366 HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
367 "active":"passive");
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700368
Mike Christief6dd3372008-05-01 14:49:59 -0700369 return 0;
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700370
371failed:
372 kfree(scsi_dh_data);
373 sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
374 HP_SW_NAME);
375 return -EINVAL;
Mike Christief6dd3372008-05-01 14:49:59 -0700376}
377
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700378static void hp_sw_bus_detach( struct scsi_device *sdev )
379{
380 struct scsi_dh_data *scsi_dh_data;
381 unsigned long flags;
382
383 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
384 scsi_dh_data = sdev->scsi_dh_data;
385 sdev->scsi_dh_data = NULL;
386 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
387 module_put(THIS_MODULE);
388
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700389 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", HP_SW_NAME);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700390
391 kfree(scsi_dh_data);
392}
393
Mike Christief6dd3372008-05-01 14:49:59 -0700394static int __init hp_sw_init(void)
395{
396 return scsi_register_device_handler(&hp_sw_dh);
397}
398
399static void __exit hp_sw_exit(void)
400{
401 scsi_unregister_device_handler(&hp_sw_dh);
402}
403
404module_init(hp_sw_init);
405module_exit(hp_sw_exit);
406
Hannes Reinecke2aef6d52008-07-17 16:53:09 -0700407MODULE_DESCRIPTION("HP Active/Passive driver");
Mike Christief6dd3372008-05-01 14:49:59 -0700408MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu");
409MODULE_LICENSE("GPL");