scsi_dh_alua.c 20 KB

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