]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/scsi/device_handler/scsi_dh_hp_sw.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / device_handler / scsi_dh_hp_sw.c
CommitLineData
f6dd337e
MC
1/*
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
2aef6d5c 7 * Copyright (C) 2008 Hannes Reinecke <hare@suse.de>
f6dd337e
MC
8 *
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
5a0e3ad6 24#include <linux/slab.h>
f6dd337e
MC
25#include <scsi/scsi.h>
26#include <scsi/scsi_dbg.h>
27#include <scsi/scsi_eh.h>
28#include <scsi/scsi_dh.h>
29
2aef6d5c 30#define HP_SW_NAME "hp_sw"
f6dd337e 31
2aef6d5c
HR
32#define HP_SW_TIMEOUT (60 * HZ)
33#define HP_SW_RETRIES 3
34
35#define HP_SW_PATH_UNINITIALIZED -1
36#define HP_SW_PATH_ACTIVE 0
37#define HP_SW_PATH_PASSIVE 1
f6dd337e
MC
38
39struct hp_sw_dh_data {
40 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
2aef6d5c 41 int path_state;
f6dd337e 42 int retries;
4e2ef86c
CS
43 int retry_cnt;
44 struct scsi_device *sdev;
45 activate_complete callback_fn;
46 void *callback_data;
f6dd337e
MC
47};
48
4e2ef86c
CS
49static int hp_sw_start_stop(struct hp_sw_dh_data *);
50
f6dd337e
MC
51static inline struct hp_sw_dh_data *get_hp_sw_data(struct scsi_device *sdev)
52{
53 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
54 BUG_ON(scsi_dh_data == NULL);
55 return ((struct hp_sw_dh_data *) scsi_dh_data->buf);
56}
57
2aef6d5c
HR
58/*
59 * tur_done - Handle TEST UNIT READY return status
60 * @sdev: sdev the command has been sent to
61 * @errors: blk error code
62 *
63 * Returns SCSI_DH_DEV_OFFLINED if the sdev is on the passive path
64 */
65static int tur_done(struct scsi_device *sdev, unsigned char *sense)
f6dd337e 66{
f6dd337e 67 struct scsi_sense_hdr sshdr;
2aef6d5c 68 int ret;
f6dd337e 69
2aef6d5c
HR
70 ret = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
71 if (!ret) {
72 sdev_printk(KERN_WARNING, sdev,
73 "%s: sending tur failed, no sense available\n",
74 HP_SW_NAME);
75 ret = SCSI_DH_IO;
f6dd337e 76 goto done;
2aef6d5c 77 }
f6dd337e 78 switch (sshdr.sense_key) {
2aef6d5c
HR
79 case UNIT_ATTENTION:
80 ret = SCSI_DH_IMM_RETRY;
81 break;
f6dd337e 82 case NOT_READY:
2aef6d5c
HR
83 if ((sshdr.asc == 0x04) && (sshdr.ascq == 2)) {
84 /*
85 * LUN not ready - Initialization command required
86 *
87 * This is the passive path
88 */
89 ret = SCSI_DH_DEV_OFFLINED;
f6dd337e
MC
90 break;
91 }
2aef6d5c 92 /* Fallthrough */
f6dd337e 93 default:
2aef6d5c
HR
94 sdev_printk(KERN_WARNING, sdev,
95 "%s: sending tur failed, sense %x/%x/%x\n",
96 HP_SW_NAME, sshdr.sense_key, sshdr.asc,
97 sshdr.ascq);
98 break;
f6dd337e
MC
99 }
100
101done:
2aef6d5c
HR
102 return ret;
103}
104
105/*
106 * hp_sw_tur - Send TEST UNIT READY
107 * @sdev: sdev command should be sent to
108 *
109 * Use the TEST UNIT READY command to determine
110 * the path state.
111 */
112static int hp_sw_tur(struct scsi_device *sdev, struct hp_sw_dh_data *h)
113{
114 struct request *req;
115 int ret;
116
febd7a5c 117retry:
2aef6d5c
HR
118 req = blk_get_request(sdev->request_queue, WRITE, GFP_NOIO);
119 if (!req)
120 return SCSI_DH_RES_TEMP_UNAVAIL;
121
122 req->cmd_type = REQ_TYPE_BLOCK_PC;
6000a368
MC
123 req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
124 REQ_FAILFAST_DRIVER;
2aef6d5c 125 req->cmd_len = COMMAND_SIZE(TEST_UNIT_READY);
2aef6d5c
HR
126 req->cmd[0] = TEST_UNIT_READY;
127 req->timeout = HP_SW_TIMEOUT;
128 req->sense = h->sense;
129 memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
130 req->sense_len = 0;
131
2aef6d5c
HR
132 ret = blk_execute_rq(req->q, NULL, req, 1);
133 if (ret == -EIO) {
134 if (req->sense_len > 0) {
135 ret = tur_done(sdev, h->sense);
136 } else {
137 sdev_printk(KERN_WARNING, sdev,
138 "%s: sending tur failed with %x\n",
139 HP_SW_NAME, req->errors);
140 ret = SCSI_DH_IO;
141 }
142 } else {
143 h->path_state = HP_SW_PATH_ACTIVE;
144 ret = SCSI_DH_OK;
145 }
febd7a5c
AB
146 if (ret == SCSI_DH_IMM_RETRY) {
147 blk_put_request(req);
2aef6d5c 148 goto retry;
febd7a5c 149 }
2aef6d5c
HR
150 if (ret == SCSI_DH_DEV_OFFLINED) {
151 h->path_state = HP_SW_PATH_PASSIVE;
152 ret = SCSI_DH_OK;
153 }
154
155 blk_put_request(req);
156
157 return ret;
158}
159
160/*
161 * start_done - Handle START STOP UNIT return status
162 * @sdev: sdev the command has been sent to
163 * @errors: blk error code
164 */
165static int start_done(struct scsi_device *sdev, unsigned char *sense)
166{
167 struct scsi_sense_hdr sshdr;
168 int rc;
169
170 rc = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
171 if (!rc) {
172 sdev_printk(KERN_WARNING, sdev,
173 "%s: sending start_stop_unit failed, "
174 "no sense available\n",
175 HP_SW_NAME);
176 return SCSI_DH_IO;
177 }
178 switch (sshdr.sense_key) {
179 case NOT_READY:
180 if ((sshdr.asc == 0x04) && (sshdr.ascq == 3)) {
181 /*
182 * LUN not ready - manual intervention required
183 *
184 * Switch-over in progress, retry.
185 */
186 rc = SCSI_DH_RETRY;
187 break;
188 }
189 /* fall through */
190 default:
191 sdev_printk(KERN_WARNING, sdev,
192 "%s: sending start_stop_unit failed, sense %x/%x/%x\n",
193 HP_SW_NAME, sshdr.sense_key, sshdr.asc,
194 sshdr.ascq);
f6dd337e
MC
195 rc = SCSI_DH_IO;
196 }
2aef6d5c 197
f6dd337e
MC
198 return rc;
199}
200
4e2ef86c
CS
201static void start_stop_endio(struct request *req, int error)
202{
203 struct hp_sw_dh_data *h = req->end_io_data;
204 unsigned err = SCSI_DH_OK;
205
206 if (error || host_byte(req->errors) != DID_OK ||
207 msg_byte(req->errors) != COMMAND_COMPLETE) {
208 sdev_printk(KERN_WARNING, h->sdev,
209 "%s: sending start_stop_unit failed with %x\n",
210 HP_SW_NAME, req->errors);
211 err = SCSI_DH_IO;
212 goto done;
213 }
214
215 if (req->sense_len > 0) {
216 err = start_done(h->sdev, h->sense);
217 if (err == SCSI_DH_RETRY) {
218 err = SCSI_DH_IO;
219 if (--h->retry_cnt) {
220 blk_put_request(req);
221 err = hp_sw_start_stop(h);
222 if (err == SCSI_DH_OK)
223 return;
224 }
225 }
226 }
227done:
228 blk_put_request(req);
229 if (h->callback_fn) {
230 h->callback_fn(h->callback_data, err);
231 h->callback_fn = h->callback_data = NULL;
232 }
233 return;
234
235}
236
2aef6d5c
HR
237/*
238 * hp_sw_start_stop - Send START STOP UNIT command
239 * @sdev: sdev command should be sent to
240 *
241 * Sending START STOP UNIT activates the SP.
242 */
4e2ef86c 243static int hp_sw_start_stop(struct hp_sw_dh_data *h)
f6dd337e 244{
f6dd337e 245 struct request *req;
f6dd337e 246
4e2ef86c 247 req = blk_get_request(h->sdev->request_queue, WRITE, GFP_ATOMIC);
f6dd337e 248 if (!req)
2aef6d5c 249 return SCSI_DH_RES_TEMP_UNAVAIL;
f6dd337e
MC
250
251 req->cmd_type = REQ_TYPE_BLOCK_PC;
6000a368
MC
252 req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
253 REQ_FAILFAST_DRIVER;
f6dd337e 254 req->cmd_len = COMMAND_SIZE(START_STOP);
f6dd337e
MC
255 req->cmd[0] = START_STOP;
256 req->cmd[4] = 1; /* Start spin cycle */
257 req->timeout = HP_SW_TIMEOUT;
258 req->sense = h->sense;
259 memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
260 req->sense_len = 0;
4e2ef86c 261 req->end_io_data = h;
2aef6d5c 262
4e2ef86c
CS
263 blk_execute_rq_nowait(req->q, NULL, req, 1, start_stop_endio);
264 return SCSI_DH_OK;
2aef6d5c
HR
265}
266
267static int hp_sw_prep_fn(struct scsi_device *sdev, struct request *req)
268{
269 struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
270 int ret = BLKPREP_OK;
271
272 if (h->path_state != HP_SW_PATH_ACTIVE) {
273 ret = BLKPREP_KILL;
274 req->cmd_flags |= REQ_QUIET;
275 }
276 return ret;
277
278}
279
280/*
281 * hp_sw_activate - Activate a path
282 * @sdev: sdev on the path to be activated
283 *
284 * The HP Active/Passive firmware is pretty simple;
285 * the passive path reports NOT READY with sense codes
286 * 0x04/0x02; a START STOP UNIT command will then
287 * activate the passive path (and deactivate the
288 * previously active one).
289 */
3ae31f6a
CS
290static int hp_sw_activate(struct scsi_device *sdev,
291 activate_complete fn, void *data)
2aef6d5c
HR
292{
293 int ret = SCSI_DH_OK;
294 struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
295
296 ret = hp_sw_tur(sdev, h);
297
298 if (ret == SCSI_DH_OK && h->path_state == HP_SW_PATH_PASSIVE) {
4e2ef86c
CS
299 h->retry_cnt = h->retries;
300 h->callback_fn = fn;
301 h->callback_data = data;
302 ret = hp_sw_start_stop(h);
2aef6d5c 303 if (ret == SCSI_DH_OK)
4e2ef86c
CS
304 return 0;
305 h->callback_fn = h->callback_data = NULL;
2aef6d5c
HR
306 }
307
3ae31f6a
CS
308 if (fn)
309 fn(data, ret);
310 return 0;
f6dd337e
MC
311}
312
f08c0761 313static const struct scsi_dh_devlist hp_sw_dh_data_list[] = {
2aef6d5c
HR
314 {"COMPAQ", "MSA1000 VOLUME"},
315 {"COMPAQ", "HSV110"},
316 {"HP", "HSV100"},
f6dd337e
MC
317 {"DEC", "HSG80"},
318 {NULL, NULL},
319};
320
765cbc6d
HR
321static int hp_sw_bus_attach(struct scsi_device *sdev);
322static void hp_sw_bus_detach(struct scsi_device *sdev);
f6dd337e
MC
323
324static struct scsi_device_handler hp_sw_dh = {
325 .name = HP_SW_NAME,
326 .module = THIS_MODULE,
765cbc6d
HR
327 .devlist = hp_sw_dh_data_list,
328 .attach = hp_sw_bus_attach,
329 .detach = hp_sw_bus_detach,
f6dd337e 330 .activate = hp_sw_activate,
2aef6d5c 331 .prep_fn = hp_sw_prep_fn,
f6dd337e
MC
332};
333
765cbc6d 334static int hp_sw_bus_attach(struct scsi_device *sdev)
f6dd337e 335{
f6dd337e 336 struct scsi_dh_data *scsi_dh_data;
2aef6d5c 337 struct hp_sw_dh_data *h;
f6dd337e 338 unsigned long flags;
2aef6d5c 339 int ret;
f6dd337e 340
765cbc6d
HR
341 scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
342 + sizeof(struct hp_sw_dh_data) , GFP_KERNEL);
343 if (!scsi_dh_data) {
2aef6d5c 344 sdev_printk(KERN_ERR, sdev, "%s: Attach Failed\n",
765cbc6d 345 HP_SW_NAME);
33af79d1 346 return 0;
765cbc6d 347 }
33af79d1 348
765cbc6d 349 scsi_dh_data->scsi_dh = &hp_sw_dh;
2aef6d5c
HR
350 h = (struct hp_sw_dh_data *) scsi_dh_data->buf;
351 h->path_state = HP_SW_PATH_UNINITIALIZED;
352 h->retries = HP_SW_RETRIES;
4e2ef86c 353 h->sdev = sdev;
2aef6d5c
HR
354
355 ret = hp_sw_tur(sdev, h);
356 if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
357 goto failed;
358
359 if (!try_module_get(THIS_MODULE))
360 goto failed;
361
765cbc6d
HR
362 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
363 sdev->scsi_dh_data = scsi_dh_data;
364 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
f6dd337e 365
2aef6d5c
HR
366 sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
367 HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
368 "active":"passive");
f6dd337e 369
765cbc6d 370 return 0;
2aef6d5c
HR
371
372failed:
373 kfree(scsi_dh_data);
374 sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
375 HP_SW_NAME);
376 return -EINVAL;
765cbc6d 377}
f6dd337e 378
765cbc6d
HR
379static void hp_sw_bus_detach( struct scsi_device *sdev )
380{
381 struct scsi_dh_data *scsi_dh_data;
382 unsigned long flags;
f6dd337e 383
765cbc6d
HR
384 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
385 scsi_dh_data = sdev->scsi_dh_data;
386 sdev->scsi_dh_data = NULL;
387 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
388 module_put(THIS_MODULE);
f6dd337e 389
2aef6d5c 390 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", HP_SW_NAME);
f6dd337e 391
765cbc6d 392 kfree(scsi_dh_data);
f6dd337e
MC
393}
394
395static int __init hp_sw_init(void)
396{
397 return scsi_register_device_handler(&hp_sw_dh);
398}
399
400static void __exit hp_sw_exit(void)
401{
402 scsi_unregister_device_handler(&hp_sw_dh);
403}
404
405module_init(hp_sw_init);
406module_exit(hp_sw_exit);
407
2aef6d5c 408MODULE_DESCRIPTION("HP Active/Passive driver");
f6dd337e
MC
409MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu");
410MODULE_LICENSE("GPL");