scsi_dh_alua.c 20 KB

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