scsi_dh_alua.c 19 KB

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