scsi_dh_alua.c 19 KB

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