scsi_dh_alua.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Generic SCSI-3 ALUA SCSI Device Handler
  3. *
  4. * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
  5. * All rights reserved.
  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 <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_eh.h>
  27. #include <scsi/scsi_dh.h>
  28. #define ALUA_DH_NAME "alua"
  29. #define ALUA_DH_VER "1.3"
  30. #define TPGS_STATE_OPTIMIZED 0x0
  31. #define TPGS_STATE_NONOPTIMIZED 0x1
  32. #define TPGS_STATE_STANDBY 0x2
  33. #define TPGS_STATE_UNAVAILABLE 0x3
  34. #define TPGS_STATE_LBA_DEPENDENT 0x4
  35. #define TPGS_STATE_OFFLINE 0xe
  36. #define TPGS_STATE_TRANSITIONING 0xf
  37. #define TPGS_SUPPORT_NONE 0x00
  38. #define TPGS_SUPPORT_OPTIMIZED 0x01
  39. #define TPGS_SUPPORT_NONOPTIMIZED 0x02
  40. #define TPGS_SUPPORT_STANDBY 0x04
  41. #define TPGS_SUPPORT_UNAVAILABLE 0x08
  42. #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
  43. #define TPGS_SUPPORT_OFFLINE 0x40
  44. #define TPGS_SUPPORT_TRANSITION 0x80
  45. #define TPGS_MODE_UNINITIALIZED -1
  46. #define TPGS_MODE_NONE 0x0
  47. #define TPGS_MODE_IMPLICIT 0x1
  48. #define TPGS_MODE_EXPLICIT 0x2
  49. #define ALUA_INQUIRY_SIZE 36
  50. #define ALUA_FAILOVER_TIMEOUT (60 * HZ)
  51. #define ALUA_FAILOVER_RETRIES 5
  52. struct alua_dh_data {
  53. int group_id;
  54. int rel_port;
  55. int tpgs;
  56. int state;
  57. unsigned char inq[ALUA_INQUIRY_SIZE];
  58. unsigned char *buff;
  59. int bufflen;
  60. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  61. int senselen;
  62. struct scsi_device *sdev;
  63. activate_complete callback_fn;
  64. void *callback_data;
  65. };
  66. #define ALUA_POLICY_SWITCH_CURRENT 0
  67. #define ALUA_POLICY_SWITCH_ALL 1
  68. static char print_alua_state(int);
  69. static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
  70. static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
  71. {
  72. struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
  73. BUG_ON(scsi_dh_data == NULL);
  74. return ((struct alua_dh_data *) scsi_dh_data->buf);
  75. }
  76. static int realloc_buffer(struct alua_dh_data *h, unsigned len)
  77. {
  78. if (h->buff && h->buff != h->inq)
  79. kfree(h->buff);
  80. h->buff = kmalloc(len, GFP_NOIO);
  81. if (!h->buff) {
  82. h->buff = h->inq;
  83. h->bufflen = ALUA_INQUIRY_SIZE;
  84. return 1;
  85. }
  86. h->bufflen = len;
  87. return 0;
  88. }
  89. static struct request *get_alua_req(struct scsi_device *sdev,
  90. void *buffer, unsigned buflen, int rw)
  91. {
  92. struct request *rq;
  93. struct request_queue *q = sdev->request_queue;
  94. rq = blk_get_request(q, rw, GFP_NOIO);
  95. if (!rq) {
  96. sdev_printk(KERN_INFO, sdev,
  97. "%s: blk_get_request failed\n", __func__);
  98. return NULL;
  99. }
  100. if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
  101. blk_put_request(rq);
  102. sdev_printk(KERN_INFO, sdev,
  103. "%s: blk_rq_map_kern failed\n", __func__);
  104. return NULL;
  105. }
  106. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  107. rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  108. REQ_FAILFAST_DRIVER;
  109. rq->retries = ALUA_FAILOVER_RETRIES;
  110. rq->timeout = ALUA_FAILOVER_TIMEOUT;
  111. return rq;
  112. }
  113. /*
  114. * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
  115. * @sdev: sdev the command should be sent to
  116. */
  117. static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  118. {
  119. struct request *rq;
  120. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  121. rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
  122. if (!rq)
  123. goto done;
  124. /* Prepare the command. */
  125. rq->cmd[0] = INQUIRY;
  126. rq->cmd[1] = 1;
  127. rq->cmd[2] = 0x83;
  128. rq->cmd[4] = h->bufflen;
  129. rq->cmd_len = COMMAND_SIZE(INQUIRY);
  130. rq->sense = h->sense;
  131. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  132. rq->sense_len = h->senselen = 0;
  133. err = blk_execute_rq(rq->q, NULL, rq, 1);
  134. if (err == -EIO) {
  135. sdev_printk(KERN_INFO, sdev,
  136. "%s: evpd inquiry failed with %x\n",
  137. ALUA_DH_NAME, rq->errors);
  138. h->senselen = rq->sense_len;
  139. err = SCSI_DH_IO;
  140. }
  141. blk_put_request(rq);
  142. done:
  143. return err;
  144. }
  145. /*
  146. * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
  147. * @sdev: sdev the command should be sent to
  148. */
  149. static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
  150. {
  151. struct request *rq;
  152. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  153. rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
  154. if (!rq)
  155. goto done;
  156. /* Prepare the command. */
  157. rq->cmd[0] = MAINTENANCE_IN;
  158. rq->cmd[1] = MI_REPORT_TARGET_PGS;
  159. rq->cmd[6] = (h->bufflen >> 24) & 0xff;
  160. rq->cmd[7] = (h->bufflen >> 16) & 0xff;
  161. rq->cmd[8] = (h->bufflen >> 8) & 0xff;
  162. rq->cmd[9] = h->bufflen & 0xff;
  163. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
  164. rq->sense = h->sense;
  165. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  166. rq->sense_len = h->senselen = 0;
  167. err = blk_execute_rq(rq->q, NULL, rq, 1);
  168. if (err == -EIO) {
  169. sdev_printk(KERN_INFO, sdev,
  170. "%s: rtpg failed with %x\n",
  171. ALUA_DH_NAME, rq->errors);
  172. h->senselen = rq->sense_len;
  173. err = SCSI_DH_IO;
  174. }
  175. blk_put_request(rq);
  176. done:
  177. return err;
  178. }
  179. /*
  180. * alua_stpg - Evaluate SET TARGET GROUP STATES
  181. * @sdev: the device to be evaluated
  182. * @state: the new target group state
  183. *
  184. * Send a SET TARGET GROUP STATES command to the device.
  185. * We only have to test here if we should resubmit the command;
  186. * any other error is assumed as a failure.
  187. */
  188. static void stpg_endio(struct request *req, int error)
  189. {
  190. struct alua_dh_data *h = req->end_io_data;
  191. struct scsi_sense_hdr sense_hdr;
  192. unsigned err = SCSI_DH_OK;
  193. if (error || host_byte(req->errors) != DID_OK ||
  194. msg_byte(req->errors) != COMMAND_COMPLETE) {
  195. err = SCSI_DH_IO;
  196. goto done;
  197. }
  198. if (h->senselen > 0) {
  199. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  200. &sense_hdr);
  201. if (!err) {
  202. err = SCSI_DH_IO;
  203. goto done;
  204. }
  205. err = alua_check_sense(h->sdev, &sense_hdr);
  206. if (err == ADD_TO_MLQUEUE) {
  207. err = SCSI_DH_RETRY;
  208. goto done;
  209. }
  210. sdev_printk(KERN_INFO, h->sdev,
  211. "%s: stpg sense code: %02x/%02x/%02x\n",
  212. ALUA_DH_NAME, sense_hdr.sense_key,
  213. sense_hdr.asc, sense_hdr.ascq);
  214. err = SCSI_DH_IO;
  215. }
  216. if (err == SCSI_DH_OK) {
  217. h->state = TPGS_STATE_OPTIMIZED;
  218. sdev_printk(KERN_INFO, h->sdev,
  219. "%s: port group %02x switched to state %c\n",
  220. ALUA_DH_NAME, h->group_id,
  221. print_alua_state(h->state));
  222. }
  223. done:
  224. req->end_io_data = NULL;
  225. __blk_put_request(req->q, req);
  226. if (h->callback_fn) {
  227. h->callback_fn(h->callback_data, err);
  228. h->callback_fn = h->callback_data = NULL;
  229. }
  230. return;
  231. }
  232. /*
  233. * submit_stpg - Issue a SET TARGET GROUP STATES command
  234. *
  235. * Currently we're only setting the current target port group state
  236. * to 'active/optimized' and let the array firmware figure out
  237. * the states of the remaining groups.
  238. */
  239. static unsigned submit_stpg(struct alua_dh_data *h)
  240. {
  241. struct request *rq;
  242. int stpg_len = 8;
  243. struct scsi_device *sdev = h->sdev;
  244. /* Prepare the data buffer */
  245. memset(h->buff, 0, stpg_len);
  246. h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
  247. h->buff[6] = (h->group_id >> 8) & 0xff;
  248. h->buff[7] = h->group_id & 0xff;
  249. rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
  250. if (!rq)
  251. return SCSI_DH_RES_TEMP_UNAVAIL;
  252. /* Prepare the command. */
  253. rq->cmd[0] = MAINTENANCE_OUT;
  254. rq->cmd[1] = MO_SET_TARGET_PGS;
  255. rq->cmd[6] = (stpg_len >> 24) & 0xff;
  256. rq->cmd[7] = (stpg_len >> 16) & 0xff;
  257. rq->cmd[8] = (stpg_len >> 8) & 0xff;
  258. rq->cmd[9] = stpg_len & 0xff;
  259. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
  260. rq->sense = h->sense;
  261. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  262. rq->sense_len = h->senselen = 0;
  263. rq->end_io_data = h;
  264. blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
  265. return SCSI_DH_OK;
  266. }
  267. /*
  268. * alua_check_tpgs - Evaluate TPGS setting
  269. * @sdev: device to be checked
  270. *
  271. * Examine the TPGS setting of the sdev to find out if ALUA
  272. * is supported.
  273. */
  274. static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
  275. {
  276. int err = SCSI_DH_OK;
  277. h->tpgs = scsi_device_tpgs(sdev);
  278. switch (h->tpgs) {
  279. case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
  280. sdev_printk(KERN_INFO, sdev,
  281. "%s: supports implicit and explicit TPGS\n",
  282. ALUA_DH_NAME);
  283. break;
  284. case TPGS_MODE_EXPLICIT:
  285. sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
  286. ALUA_DH_NAME);
  287. break;
  288. case TPGS_MODE_IMPLICIT:
  289. sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
  290. ALUA_DH_NAME);
  291. break;
  292. default:
  293. h->tpgs = TPGS_MODE_NONE;
  294. sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
  295. ALUA_DH_NAME);
  296. err = SCSI_DH_DEV_UNSUPP;
  297. break;
  298. }
  299. return err;
  300. }
  301. /*
  302. * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
  303. * @sdev: device to be checked
  304. *
  305. * Extract the relative target port and the target port group
  306. * descriptor from the list of identificators.
  307. */
  308. static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  309. {
  310. int len;
  311. unsigned err;
  312. unsigned char *d;
  313. retry:
  314. err = submit_vpd_inquiry(sdev, h);
  315. if (err != SCSI_DH_OK)
  316. return err;
  317. /* Check if vpd page exceeds initial buffer */
  318. len = (h->buff[2] << 8) + h->buff[3] + 4;
  319. if (len > h->bufflen) {
  320. /* Resubmit with the correct length */
  321. if (realloc_buffer(h, len)) {
  322. sdev_printk(KERN_WARNING, sdev,
  323. "%s: kmalloc buffer failed\n",
  324. ALUA_DH_NAME);
  325. /* Temporary failure, bypass */
  326. return SCSI_DH_DEV_TEMP_BUSY;
  327. }
  328. goto retry;
  329. }
  330. /*
  331. * Now look for the correct descriptor.
  332. */
  333. d = h->buff + 4;
  334. while (d < h->buff + len) {
  335. switch (d[1] & 0xf) {
  336. case 0x4:
  337. /* Relative target port */
  338. h->rel_port = (d[6] << 8) + d[7];
  339. break;
  340. case 0x5:
  341. /* Target port group */
  342. h->group_id = (d[6] << 8) + d[7];
  343. break;
  344. default:
  345. break;
  346. }
  347. d += d[3] + 4;
  348. }
  349. if (h->group_id == -1) {
  350. /*
  351. * Internal error; TPGS supported but required
  352. * VPD identification descriptors not present.
  353. * Disable ALUA support
  354. */
  355. sdev_printk(KERN_INFO, sdev,
  356. "%s: No target port descriptors found\n",
  357. ALUA_DH_NAME);
  358. h->state = TPGS_STATE_OPTIMIZED;
  359. h->tpgs = TPGS_MODE_NONE;
  360. err = SCSI_DH_DEV_UNSUPP;
  361. } else {
  362. sdev_printk(KERN_INFO, sdev,
  363. "%s: port group %02x rel port %02x\n",
  364. ALUA_DH_NAME, h->group_id, h->rel_port);
  365. }
  366. return err;
  367. }
  368. static char print_alua_state(int state)
  369. {
  370. switch (state) {
  371. case TPGS_STATE_OPTIMIZED:
  372. return 'A';
  373. case TPGS_STATE_NONOPTIMIZED:
  374. return 'N';
  375. case TPGS_STATE_STANDBY:
  376. return 'S';
  377. case TPGS_STATE_UNAVAILABLE:
  378. return 'U';
  379. case TPGS_STATE_LBA_DEPENDENT:
  380. return 'L';
  381. case TPGS_STATE_OFFLINE:
  382. return 'O';
  383. case TPGS_STATE_TRANSITIONING:
  384. return 'T';
  385. default:
  386. return 'X';
  387. }
  388. }
  389. static int alua_check_sense(struct scsi_device *sdev,
  390. struct scsi_sense_hdr *sense_hdr)
  391. {
  392. switch (sense_hdr->sense_key) {
  393. case NOT_READY:
  394. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
  395. /*
  396. * LUN Not Accessible - ALUA state transition
  397. */
  398. return ADD_TO_MLQUEUE;
  399. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
  400. /*
  401. * LUN Not Accessible -- Target port in standby state
  402. */
  403. return SUCCESS;
  404. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
  405. /*
  406. * LUN Not Accessible -- Target port in unavailable state
  407. */
  408. return SUCCESS;
  409. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
  410. /*
  411. * LUN Not Ready -- Offline
  412. */
  413. return SUCCESS;
  414. break;
  415. case UNIT_ATTENTION:
  416. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
  417. /*
  418. * Power On, Reset, or Bus Device Reset, just retry.
  419. */
  420. return ADD_TO_MLQUEUE;
  421. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
  422. /*
  423. * ALUA state changed
  424. */
  425. return ADD_TO_MLQUEUE;
  426. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
  427. /*
  428. * Implicit ALUA state transition failed
  429. */
  430. return ADD_TO_MLQUEUE;
  431. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
  432. /*
  433. * Inquiry data has changed
  434. */
  435. return ADD_TO_MLQUEUE;
  436. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
  437. /*
  438. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  439. * when switching controllers on targets like
  440. * Intel Multi-Flex. We can just retry.
  441. */
  442. return ADD_TO_MLQUEUE;
  443. break;
  444. }
  445. return SCSI_RETURN_NOT_HANDLED;
  446. }
  447. /*
  448. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  449. * @sdev: the device to be evaluated.
  450. *
  451. * Evaluate the Target Port Group State.
  452. * Returns SCSI_DH_DEV_OFFLINED if the path is
  453. * found to be unusable.
  454. */
  455. static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
  456. {
  457. struct scsi_sense_hdr sense_hdr;
  458. int len, k, off, valid_states = 0;
  459. unsigned char *ucp;
  460. unsigned err;
  461. unsigned long expiry, interval = 1000;
  462. expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
  463. retry:
  464. err = submit_rtpg(sdev, h);
  465. if (err == SCSI_DH_IO && h->senselen > 0) {
  466. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  467. &sense_hdr);
  468. if (!err)
  469. return SCSI_DH_IO;
  470. err = alua_check_sense(sdev, &sense_hdr);
  471. if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
  472. goto retry;
  473. sdev_printk(KERN_INFO, sdev,
  474. "%s: rtpg sense code %02x/%02x/%02x\n",
  475. ALUA_DH_NAME, sense_hdr.sense_key,
  476. sense_hdr.asc, sense_hdr.ascq);
  477. err = SCSI_DH_IO;
  478. }
  479. if (err != SCSI_DH_OK)
  480. return err;
  481. len = (h->buff[0] << 24) + (h->buff[1] << 16) +
  482. (h->buff[2] << 8) + h->buff[3] + 4;
  483. if (len > h->bufflen) {
  484. /* Resubmit with the correct length */
  485. if (realloc_buffer(h, len)) {
  486. sdev_printk(KERN_WARNING, sdev,
  487. "%s: kmalloc buffer failed\n",__func__);
  488. /* Temporary failure, bypass */
  489. return SCSI_DH_DEV_TEMP_BUSY;
  490. }
  491. goto retry;
  492. }
  493. for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
  494. if (h->group_id == (ucp[2] << 8) + ucp[3]) {
  495. h->state = ucp[0] & 0x0f;
  496. valid_states = ucp[1];
  497. }
  498. off = 8 + (ucp[7] * 4);
  499. }
  500. sdev_printk(KERN_INFO, sdev,
  501. "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
  502. ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
  503. valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  504. valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  505. valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  506. valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  507. valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  508. valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  509. valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  510. switch (h->state) {
  511. case TPGS_STATE_TRANSITIONING:
  512. if (time_before(jiffies, expiry)) {
  513. /* State transition, retry */
  514. interval *= 2;
  515. msleep(interval);
  516. goto retry;
  517. }
  518. /* Transitioning time exceeded, set port to standby */
  519. err = SCSI_DH_RETRY;
  520. h->state = TPGS_STATE_STANDBY;
  521. break;
  522. case TPGS_STATE_OFFLINE:
  523. case TPGS_STATE_UNAVAILABLE:
  524. /* Path unusable for unavailable/offline */
  525. err = SCSI_DH_DEV_OFFLINED;
  526. break;
  527. default:
  528. /* Useable path if active */
  529. err = SCSI_DH_OK;
  530. break;
  531. }
  532. return err;
  533. }
  534. /*
  535. * alua_initialize - Initialize ALUA state
  536. * @sdev: the device to be initialized
  537. *
  538. * For the prep_fn to work correctly we have
  539. * to initialize the ALUA state for the device.
  540. */
  541. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  542. {
  543. int err;
  544. err = alua_check_tpgs(sdev, h);
  545. if (err != SCSI_DH_OK)
  546. goto out;
  547. err = alua_vpd_inquiry(sdev, h);
  548. if (err != SCSI_DH_OK)
  549. goto out;
  550. err = alua_rtpg(sdev, h);
  551. if (err != SCSI_DH_OK)
  552. goto out;
  553. out:
  554. return err;
  555. }
  556. /*
  557. * alua_activate - activate a path
  558. * @sdev: device on the path to be activated
  559. *
  560. * We're currently switching the port group to be activated only and
  561. * let the array figure out the rest.
  562. * There may be other arrays which require us to switch all port groups
  563. * based on a certain policy. But until we actually encounter them it
  564. * should be okay.
  565. */
  566. static int alua_activate(struct scsi_device *sdev,
  567. activate_complete fn, void *data)
  568. {
  569. struct alua_dh_data *h = get_alua_data(sdev);
  570. int err = SCSI_DH_OK;
  571. err = alua_rtpg(sdev, h);
  572. if (err != SCSI_DH_OK)
  573. goto out;
  574. if (h->tpgs & TPGS_MODE_EXPLICIT &&
  575. h->state != TPGS_STATE_OPTIMIZED &&
  576. h->state != TPGS_STATE_LBA_DEPENDENT) {
  577. h->callback_fn = fn;
  578. h->callback_data = data;
  579. err = submit_stpg(h);
  580. if (err == SCSI_DH_OK)
  581. return 0;
  582. h->callback_fn = h->callback_data = NULL;
  583. }
  584. out:
  585. if (fn)
  586. fn(data, err);
  587. return 0;
  588. }
  589. /*
  590. * alua_prep_fn - request callback
  591. *
  592. * Fail I/O to all paths not in state
  593. * active/optimized or active/non-optimized.
  594. */
  595. static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
  596. {
  597. struct alua_dh_data *h = get_alua_data(sdev);
  598. int ret = BLKPREP_OK;
  599. if (h->state == TPGS_STATE_TRANSITIONING)
  600. ret = BLKPREP_DEFER;
  601. else if (h->state != TPGS_STATE_OPTIMIZED &&
  602. h->state != TPGS_STATE_NONOPTIMIZED &&
  603. h->state != TPGS_STATE_LBA_DEPENDENT) {
  604. ret = BLKPREP_KILL;
  605. req->cmd_flags |= REQ_QUIET;
  606. }
  607. return ret;
  608. }
  609. static bool alua_match(struct scsi_device *sdev)
  610. {
  611. return (scsi_device_tpgs(sdev) != 0);
  612. }
  613. static int alua_bus_attach(struct scsi_device *sdev);
  614. static void alua_bus_detach(struct scsi_device *sdev);
  615. static struct scsi_device_handler alua_dh = {
  616. .name = ALUA_DH_NAME,
  617. .module = THIS_MODULE,
  618. .attach = alua_bus_attach,
  619. .detach = alua_bus_detach,
  620. .prep_fn = alua_prep_fn,
  621. .check_sense = alua_check_sense,
  622. .activate = alua_activate,
  623. .match = alua_match,
  624. };
  625. /*
  626. * alua_bus_attach - Attach device handler
  627. * @sdev: device to be attached to
  628. */
  629. static int alua_bus_attach(struct scsi_device *sdev)
  630. {
  631. struct scsi_dh_data *scsi_dh_data;
  632. struct alua_dh_data *h;
  633. unsigned long flags;
  634. int err = SCSI_DH_OK;
  635. scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
  636. + sizeof(*h) , GFP_KERNEL);
  637. if (!scsi_dh_data) {
  638. sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
  639. ALUA_DH_NAME);
  640. return -ENOMEM;
  641. }
  642. scsi_dh_data->scsi_dh = &alua_dh;
  643. h = (struct alua_dh_data *) scsi_dh_data->buf;
  644. h->tpgs = TPGS_MODE_UNINITIALIZED;
  645. h->state = TPGS_STATE_OPTIMIZED;
  646. h->group_id = -1;
  647. h->rel_port = -1;
  648. h->buff = h->inq;
  649. h->bufflen = ALUA_INQUIRY_SIZE;
  650. h->sdev = sdev;
  651. err = alua_initialize(sdev, h);
  652. if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
  653. goto failed;
  654. if (!try_module_get(THIS_MODULE))
  655. goto failed;
  656. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  657. sdev->scsi_dh_data = scsi_dh_data;
  658. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  659. sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
  660. return 0;
  661. failed:
  662. kfree(scsi_dh_data);
  663. sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
  664. return -EINVAL;
  665. }
  666. /*
  667. * alua_bus_detach - Detach device handler
  668. * @sdev: device to be detached from
  669. */
  670. static void alua_bus_detach(struct scsi_device *sdev)
  671. {
  672. struct scsi_dh_data *scsi_dh_data;
  673. struct alua_dh_data *h;
  674. unsigned long flags;
  675. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  676. scsi_dh_data = sdev->scsi_dh_data;
  677. sdev->scsi_dh_data = NULL;
  678. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  679. h = (struct alua_dh_data *) scsi_dh_data->buf;
  680. if (h->buff && h->inq != h->buff)
  681. kfree(h->buff);
  682. kfree(scsi_dh_data);
  683. module_put(THIS_MODULE);
  684. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
  685. }
  686. static int __init alua_init(void)
  687. {
  688. int r;
  689. r = scsi_register_device_handler(&alua_dh);
  690. if (r != 0)
  691. printk(KERN_ERR "%s: Failed to register scsi device handler",
  692. ALUA_DH_NAME);
  693. return r;
  694. }
  695. static void __exit alua_exit(void)
  696. {
  697. scsi_unregister_device_handler(&alua_dh);
  698. }
  699. module_init(alua_init);
  700. module_exit(alua_exit);
  701. MODULE_DESCRIPTION("DM Multipath ALUA support");
  702. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  703. MODULE_LICENSE("GPL");
  704. MODULE_VERSION(ALUA_DH_VER);