scsi_dh_alua.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. blk_rq_set_block_pc(rq);
  101. if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
  102. blk_put_request(rq);
  103. sdev_printk(KERN_INFO, sdev,
  104. "%s: blk_rq_map_kern failed\n", __func__);
  105. return NULL;
  106. }
  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 == 0x01)
  422. /*
  423. * Mode Parameters Changed
  424. */
  425. return ADD_TO_MLQUEUE;
  426. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
  427. /*
  428. * ALUA state changed
  429. */
  430. return ADD_TO_MLQUEUE;
  431. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
  432. /*
  433. * Implicit ALUA state transition failed
  434. */
  435. return ADD_TO_MLQUEUE;
  436. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
  437. /*
  438. * Inquiry data has changed
  439. */
  440. return ADD_TO_MLQUEUE;
  441. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
  442. /*
  443. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  444. * when switching controllers on targets like
  445. * Intel Multi-Flex. We can just retry.
  446. */
  447. return ADD_TO_MLQUEUE;
  448. break;
  449. }
  450. return SCSI_RETURN_NOT_HANDLED;
  451. }
  452. /*
  453. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  454. * @sdev: the device to be evaluated.
  455. *
  456. * Evaluate the Target Port Group State.
  457. * Returns SCSI_DH_DEV_OFFLINED if the path is
  458. * found to be unusable.
  459. */
  460. static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
  461. {
  462. struct scsi_sense_hdr sense_hdr;
  463. int len, k, off, valid_states = 0;
  464. unsigned char *ucp;
  465. unsigned err;
  466. unsigned long expiry, interval = 1000;
  467. expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
  468. retry:
  469. err = submit_rtpg(sdev, h);
  470. if (err == SCSI_DH_IO && h->senselen > 0) {
  471. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  472. &sense_hdr);
  473. if (!err)
  474. return SCSI_DH_IO;
  475. err = alua_check_sense(sdev, &sense_hdr);
  476. if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
  477. goto retry;
  478. sdev_printk(KERN_INFO, sdev,
  479. "%s: rtpg sense code %02x/%02x/%02x\n",
  480. ALUA_DH_NAME, sense_hdr.sense_key,
  481. sense_hdr.asc, sense_hdr.ascq);
  482. err = SCSI_DH_IO;
  483. }
  484. if (err != SCSI_DH_OK)
  485. return err;
  486. len = (h->buff[0] << 24) + (h->buff[1] << 16) +
  487. (h->buff[2] << 8) + h->buff[3] + 4;
  488. if (len > h->bufflen) {
  489. /* Resubmit with the correct length */
  490. if (realloc_buffer(h, len)) {
  491. sdev_printk(KERN_WARNING, sdev,
  492. "%s: kmalloc buffer failed\n",__func__);
  493. /* Temporary failure, bypass */
  494. return SCSI_DH_DEV_TEMP_BUSY;
  495. }
  496. goto retry;
  497. }
  498. for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
  499. if (h->group_id == (ucp[2] << 8) + ucp[3]) {
  500. h->state = ucp[0] & 0x0f;
  501. valid_states = ucp[1];
  502. }
  503. off = 8 + (ucp[7] * 4);
  504. }
  505. sdev_printk(KERN_INFO, sdev,
  506. "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
  507. ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
  508. valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  509. valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  510. valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  511. valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  512. valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  513. valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  514. valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  515. switch (h->state) {
  516. case TPGS_STATE_TRANSITIONING:
  517. if (time_before(jiffies, expiry)) {
  518. /* State transition, retry */
  519. interval *= 2;
  520. msleep(interval);
  521. goto retry;
  522. }
  523. /* Transitioning time exceeded, set port to standby */
  524. err = SCSI_DH_RETRY;
  525. h->state = TPGS_STATE_STANDBY;
  526. break;
  527. case TPGS_STATE_OFFLINE:
  528. /* Path unusable */
  529. err = SCSI_DH_DEV_OFFLINED;
  530. break;
  531. default:
  532. /* Useable path if active */
  533. err = SCSI_DH_OK;
  534. break;
  535. }
  536. return err;
  537. }
  538. /*
  539. * alua_initialize - Initialize ALUA state
  540. * @sdev: the device to be initialized
  541. *
  542. * For the prep_fn to work correctly we have
  543. * to initialize the ALUA state for the device.
  544. */
  545. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  546. {
  547. int err;
  548. err = alua_check_tpgs(sdev, h);
  549. if (err != SCSI_DH_OK)
  550. goto out;
  551. err = alua_vpd_inquiry(sdev, h);
  552. if (err != SCSI_DH_OK)
  553. goto out;
  554. err = alua_rtpg(sdev, h);
  555. if (err != SCSI_DH_OK)
  556. goto out;
  557. out:
  558. return err;
  559. }
  560. /*
  561. * alua_activate - activate a path
  562. * @sdev: device on the path to be activated
  563. *
  564. * We're currently switching the port group to be activated only and
  565. * let the array figure out the rest.
  566. * There may be other arrays which require us to switch all port groups
  567. * based on a certain policy. But until we actually encounter them it
  568. * should be okay.
  569. */
  570. static int alua_activate(struct scsi_device *sdev,
  571. activate_complete fn, void *data)
  572. {
  573. struct alua_dh_data *h = get_alua_data(sdev);
  574. int err = SCSI_DH_OK;
  575. err = alua_rtpg(sdev, h);
  576. if (err != SCSI_DH_OK)
  577. goto out;
  578. if (h->tpgs & TPGS_MODE_EXPLICIT &&
  579. h->state != TPGS_STATE_OPTIMIZED &&
  580. h->state != TPGS_STATE_LBA_DEPENDENT) {
  581. h->callback_fn = fn;
  582. h->callback_data = data;
  583. err = submit_stpg(h);
  584. if (err == SCSI_DH_OK)
  585. return 0;
  586. h->callback_fn = h->callback_data = NULL;
  587. }
  588. out:
  589. if (fn)
  590. fn(data, err);
  591. return 0;
  592. }
  593. /*
  594. * alua_prep_fn - request callback
  595. *
  596. * Fail I/O to all paths not in state
  597. * active/optimized or active/non-optimized.
  598. */
  599. static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
  600. {
  601. struct alua_dh_data *h = get_alua_data(sdev);
  602. int ret = BLKPREP_OK;
  603. if (h->state == TPGS_STATE_TRANSITIONING)
  604. ret = BLKPREP_DEFER;
  605. else if (h->state != TPGS_STATE_OPTIMIZED &&
  606. h->state != TPGS_STATE_NONOPTIMIZED &&
  607. h->state != TPGS_STATE_LBA_DEPENDENT) {
  608. ret = BLKPREP_KILL;
  609. req->cmd_flags |= REQ_QUIET;
  610. }
  611. return ret;
  612. }
  613. static bool alua_match(struct scsi_device *sdev)
  614. {
  615. return (scsi_device_tpgs(sdev) != 0);
  616. }
  617. static int alua_bus_attach(struct scsi_device *sdev);
  618. static void alua_bus_detach(struct scsi_device *sdev);
  619. static struct scsi_device_handler alua_dh = {
  620. .name = ALUA_DH_NAME,
  621. .module = THIS_MODULE,
  622. .attach = alua_bus_attach,
  623. .detach = alua_bus_detach,
  624. .prep_fn = alua_prep_fn,
  625. .check_sense = alua_check_sense,
  626. .activate = alua_activate,
  627. .match = alua_match,
  628. };
  629. /*
  630. * alua_bus_attach - Attach device handler
  631. * @sdev: device to be attached to
  632. */
  633. static int alua_bus_attach(struct scsi_device *sdev)
  634. {
  635. struct scsi_dh_data *scsi_dh_data;
  636. struct alua_dh_data *h;
  637. unsigned long flags;
  638. int err = SCSI_DH_OK;
  639. scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
  640. + sizeof(*h) , GFP_KERNEL);
  641. if (!scsi_dh_data) {
  642. sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
  643. ALUA_DH_NAME);
  644. return -ENOMEM;
  645. }
  646. scsi_dh_data->scsi_dh = &alua_dh;
  647. h = (struct alua_dh_data *) scsi_dh_data->buf;
  648. h->tpgs = TPGS_MODE_UNINITIALIZED;
  649. h->state = TPGS_STATE_OPTIMIZED;
  650. h->group_id = -1;
  651. h->rel_port = -1;
  652. h->buff = h->inq;
  653. h->bufflen = ALUA_INQUIRY_SIZE;
  654. h->sdev = sdev;
  655. err = alua_initialize(sdev, h);
  656. if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
  657. goto failed;
  658. if (!try_module_get(THIS_MODULE))
  659. goto failed;
  660. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  661. sdev->scsi_dh_data = scsi_dh_data;
  662. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  663. sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
  664. return 0;
  665. failed:
  666. kfree(scsi_dh_data);
  667. sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
  668. return -EINVAL;
  669. }
  670. /*
  671. * alua_bus_detach - Detach device handler
  672. * @sdev: device to be detached from
  673. */
  674. static void alua_bus_detach(struct scsi_device *sdev)
  675. {
  676. struct scsi_dh_data *scsi_dh_data;
  677. struct alua_dh_data *h;
  678. unsigned long flags;
  679. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  680. scsi_dh_data = sdev->scsi_dh_data;
  681. sdev->scsi_dh_data = NULL;
  682. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  683. h = (struct alua_dh_data *) scsi_dh_data->buf;
  684. if (h->buff && h->inq != h->buff)
  685. kfree(h->buff);
  686. kfree(scsi_dh_data);
  687. module_put(THIS_MODULE);
  688. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
  689. }
  690. static int __init alua_init(void)
  691. {
  692. int r;
  693. r = scsi_register_device_handler(&alua_dh);
  694. if (r != 0)
  695. printk(KERN_ERR "%s: Failed to register scsi device handler",
  696. ALUA_DH_NAME);
  697. return r;
  698. }
  699. static void __exit alua_exit(void)
  700. {
  701. scsi_unregister_device_handler(&alua_dh);
  702. }
  703. module_init(alua_init);
  704. module_exit(alua_exit);
  705. MODULE_DESCRIPTION("DM Multipath ALUA support");
  706. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  707. MODULE_LICENSE("GPL");
  708. MODULE_VERSION(ALUA_DH_VER);