scsi_dh_alua.c 19 KB

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