scsi_dh_rdac.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * LSI/Engenio/NetApp E-Series RDAC SCSI Device Handler
  3. *
  4. * Copyright (C) 2005 Mike Christie. All rights reserved.
  5. * Copyright (C) Chandra Seetharaman, IBM Corp. 2007
  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. #include <linux/workqueue.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #define RDAC_NAME "rdac"
  29. #define RDAC_RETRY_COUNT 5
  30. /*
  31. * LSI mode page stuff
  32. *
  33. * These struct definitions and the forming of the
  34. * mode page were taken from the LSI RDAC 2.4 GPL'd
  35. * driver, and then converted to Linux conventions.
  36. */
  37. #define RDAC_QUIESCENCE_TIME 20
  38. /*
  39. * Page Codes
  40. */
  41. #define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
  42. /*
  43. * Controller modes definitions
  44. */
  45. #define RDAC_MODE_TRANSFER_SPECIFIED_LUNS 0x02
  46. /*
  47. * RDAC Options field
  48. */
  49. #define RDAC_FORCED_QUIESENCE 0x02
  50. #define RDAC_TIMEOUT (60 * HZ)
  51. #define RDAC_RETRIES 3
  52. struct rdac_mode_6_hdr {
  53. u8 data_len;
  54. u8 medium_type;
  55. u8 device_params;
  56. u8 block_desc_len;
  57. };
  58. struct rdac_mode_10_hdr {
  59. u16 data_len;
  60. u8 medium_type;
  61. u8 device_params;
  62. u16 reserved;
  63. u16 block_desc_len;
  64. };
  65. struct rdac_mode_common {
  66. u8 controller_serial[16];
  67. u8 alt_controller_serial[16];
  68. u8 rdac_mode[2];
  69. u8 alt_rdac_mode[2];
  70. u8 quiescence_timeout;
  71. u8 rdac_options;
  72. };
  73. struct rdac_pg_legacy {
  74. struct rdac_mode_6_hdr hdr;
  75. u8 page_code;
  76. u8 page_len;
  77. struct rdac_mode_common common;
  78. #define MODE6_MAX_LUN 32
  79. u8 lun_table[MODE6_MAX_LUN];
  80. u8 reserved2[32];
  81. u8 reserved3;
  82. u8 reserved4;
  83. };
  84. struct rdac_pg_expanded {
  85. struct rdac_mode_10_hdr hdr;
  86. u8 page_code;
  87. u8 subpage_code;
  88. u8 page_len[2];
  89. struct rdac_mode_common common;
  90. u8 lun_table[256];
  91. u8 reserved3;
  92. u8 reserved4;
  93. };
  94. struct c9_inquiry {
  95. u8 peripheral_info;
  96. u8 page_code; /* 0xC9 */
  97. u8 reserved1;
  98. u8 page_len;
  99. u8 page_id[4]; /* "vace" */
  100. u8 avte_cvp;
  101. u8 path_prio;
  102. u8 reserved2[38];
  103. };
  104. #define SUBSYS_ID_LEN 16
  105. #define SLOT_ID_LEN 2
  106. #define ARRAY_LABEL_LEN 31
  107. struct c4_inquiry {
  108. u8 peripheral_info;
  109. u8 page_code; /* 0xC4 */
  110. u8 reserved1;
  111. u8 page_len;
  112. u8 page_id[4]; /* "subs" */
  113. u8 subsys_id[SUBSYS_ID_LEN];
  114. u8 revision[4];
  115. u8 slot_id[SLOT_ID_LEN];
  116. u8 reserved[2];
  117. };
  118. #define UNIQUE_ID_LEN 16
  119. struct c8_inquiry {
  120. u8 peripheral_info;
  121. u8 page_code; /* 0xC8 */
  122. u8 reserved1;
  123. u8 page_len;
  124. u8 page_id[4]; /* "edid" */
  125. u8 reserved2[3];
  126. u8 vol_uniq_id_len;
  127. u8 vol_uniq_id[16];
  128. u8 vol_user_label_len;
  129. u8 vol_user_label[60];
  130. u8 array_uniq_id_len;
  131. u8 array_unique_id[UNIQUE_ID_LEN];
  132. u8 array_user_label_len;
  133. u8 array_user_label[60];
  134. u8 lun[8];
  135. };
  136. struct rdac_controller {
  137. u8 array_id[UNIQUE_ID_LEN];
  138. int use_ms10;
  139. struct kref kref;
  140. struct list_head node; /* list of all controllers */
  141. union {
  142. struct rdac_pg_legacy legacy;
  143. struct rdac_pg_expanded expanded;
  144. } mode_select;
  145. u8 index;
  146. u8 array_name[ARRAY_LABEL_LEN];
  147. struct Scsi_Host *host;
  148. spinlock_t ms_lock;
  149. int ms_queued;
  150. struct work_struct ms_work;
  151. struct scsi_device *ms_sdev;
  152. struct list_head ms_head;
  153. };
  154. struct c2_inquiry {
  155. u8 peripheral_info;
  156. u8 page_code; /* 0xC2 */
  157. u8 reserved1;
  158. u8 page_len;
  159. u8 page_id[4]; /* "swr4" */
  160. u8 sw_version[3];
  161. u8 sw_date[3];
  162. u8 features_enabled;
  163. u8 max_lun_supported;
  164. u8 partitions[239]; /* Total allocation length should be 0xFF */
  165. };
  166. struct rdac_dh_data {
  167. struct scsi_dh_data dh_data;
  168. struct rdac_controller *ctlr;
  169. #define UNINITIALIZED_LUN (1 << 8)
  170. unsigned lun;
  171. #define RDAC_MODE 0
  172. #define RDAC_MODE_AVT 1
  173. #define RDAC_MODE_IOSHIP 2
  174. unsigned char mode;
  175. #define RDAC_STATE_ACTIVE 0
  176. #define RDAC_STATE_PASSIVE 1
  177. unsigned char state;
  178. #define RDAC_LUN_UNOWNED 0
  179. #define RDAC_LUN_OWNED 1
  180. char lun_state;
  181. #define RDAC_PREFERRED 0
  182. #define RDAC_NON_PREFERRED 1
  183. char preferred;
  184. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  185. union {
  186. struct c2_inquiry c2;
  187. struct c4_inquiry c4;
  188. struct c8_inquiry c8;
  189. struct c9_inquiry c9;
  190. } inq;
  191. };
  192. static const char *mode[] = {
  193. "RDAC",
  194. "AVT",
  195. "IOSHIP",
  196. };
  197. static const char *lun_state[] =
  198. {
  199. "unowned",
  200. "owned",
  201. };
  202. struct rdac_queue_data {
  203. struct list_head entry;
  204. struct rdac_dh_data *h;
  205. activate_complete callback_fn;
  206. void *callback_data;
  207. };
  208. static LIST_HEAD(ctlr_list);
  209. static DEFINE_SPINLOCK(list_lock);
  210. static struct workqueue_struct *kmpath_rdacd;
  211. static void send_mode_select(struct work_struct *work);
  212. /*
  213. * module parameter to enable rdac debug logging.
  214. * 2 bits for each type of logging, only two types defined for now
  215. * Can be enhanced if required at later point
  216. */
  217. static int rdac_logging = 1;
  218. module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
  219. MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
  220. "Default is 1 - failover logging enabled, "
  221. "set it to 0xF to enable all the logs");
  222. #define RDAC_LOG_FAILOVER 0
  223. #define RDAC_LOG_SENSE 2
  224. #define RDAC_LOG_BITS 2
  225. #define RDAC_LOG_LEVEL(SHIFT) \
  226. ((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
  227. #define RDAC_LOG(SHIFT, sdev, f, arg...) \
  228. do { \
  229. if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
  230. sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
  231. } while (0);
  232. static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
  233. {
  234. return container_of(sdev->scsi_dh_data, struct rdac_dh_data, dh_data);
  235. }
  236. static struct request *get_rdac_req(struct scsi_device *sdev,
  237. void *buffer, unsigned buflen, int rw)
  238. {
  239. struct request *rq;
  240. struct request_queue *q = sdev->request_queue;
  241. rq = blk_get_request(q, rw, GFP_NOIO);
  242. if (IS_ERR(rq)) {
  243. sdev_printk(KERN_INFO, sdev,
  244. "get_rdac_req: blk_get_request failed.\n");
  245. return NULL;
  246. }
  247. blk_rq_set_block_pc(rq);
  248. if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
  249. blk_put_request(rq);
  250. sdev_printk(KERN_INFO, sdev,
  251. "get_rdac_req: blk_rq_map_kern failed.\n");
  252. return NULL;
  253. }
  254. rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  255. REQ_FAILFAST_DRIVER;
  256. rq->retries = RDAC_RETRIES;
  257. rq->timeout = RDAC_TIMEOUT;
  258. return rq;
  259. }
  260. static struct request *rdac_failover_get(struct scsi_device *sdev,
  261. struct rdac_dh_data *h, struct list_head *list)
  262. {
  263. struct request *rq;
  264. struct rdac_mode_common *common;
  265. unsigned data_size;
  266. struct rdac_queue_data *qdata;
  267. u8 *lun_table;
  268. if (h->ctlr->use_ms10) {
  269. struct rdac_pg_expanded *rdac_pg;
  270. data_size = sizeof(struct rdac_pg_expanded);
  271. rdac_pg = &h->ctlr->mode_select.expanded;
  272. memset(rdac_pg, 0, data_size);
  273. common = &rdac_pg->common;
  274. rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
  275. rdac_pg->subpage_code = 0x1;
  276. rdac_pg->page_len[0] = 0x01;
  277. rdac_pg->page_len[1] = 0x28;
  278. lun_table = rdac_pg->lun_table;
  279. } else {
  280. struct rdac_pg_legacy *rdac_pg;
  281. data_size = sizeof(struct rdac_pg_legacy);
  282. rdac_pg = &h->ctlr->mode_select.legacy;
  283. memset(rdac_pg, 0, data_size);
  284. common = &rdac_pg->common;
  285. rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
  286. rdac_pg->page_len = 0x68;
  287. lun_table = rdac_pg->lun_table;
  288. }
  289. common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
  290. common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
  291. common->rdac_options = RDAC_FORCED_QUIESENCE;
  292. list_for_each_entry(qdata, list, entry) {
  293. lun_table[qdata->h->lun] = 0x81;
  294. }
  295. /* get request for block layer packet command */
  296. rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
  297. if (!rq)
  298. return NULL;
  299. /* Prepare the command. */
  300. if (h->ctlr->use_ms10) {
  301. rq->cmd[0] = MODE_SELECT_10;
  302. rq->cmd[7] = data_size >> 8;
  303. rq->cmd[8] = data_size & 0xff;
  304. } else {
  305. rq->cmd[0] = MODE_SELECT;
  306. rq->cmd[4] = data_size;
  307. }
  308. rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
  309. rq->sense = h->sense;
  310. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  311. rq->sense_len = 0;
  312. return rq;
  313. }
  314. static void release_controller(struct kref *kref)
  315. {
  316. struct rdac_controller *ctlr;
  317. ctlr = container_of(kref, struct rdac_controller, kref);
  318. list_del(&ctlr->node);
  319. kfree(ctlr);
  320. }
  321. static struct rdac_controller *get_controller(int index, char *array_name,
  322. u8 *array_id, struct scsi_device *sdev)
  323. {
  324. struct rdac_controller *ctlr, *tmp;
  325. list_for_each_entry(tmp, &ctlr_list, node) {
  326. if ((memcmp(tmp->array_id, array_id, UNIQUE_ID_LEN) == 0) &&
  327. (tmp->index == index) &&
  328. (tmp->host == sdev->host)) {
  329. kref_get(&tmp->kref);
  330. return tmp;
  331. }
  332. }
  333. ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
  334. if (!ctlr)
  335. return NULL;
  336. /* initialize fields of controller */
  337. memcpy(ctlr->array_id, array_id, UNIQUE_ID_LEN);
  338. ctlr->index = index;
  339. ctlr->host = sdev->host;
  340. memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
  341. kref_init(&ctlr->kref);
  342. ctlr->use_ms10 = -1;
  343. ctlr->ms_queued = 0;
  344. ctlr->ms_sdev = NULL;
  345. spin_lock_init(&ctlr->ms_lock);
  346. INIT_WORK(&ctlr->ms_work, send_mode_select);
  347. INIT_LIST_HEAD(&ctlr->ms_head);
  348. list_add(&ctlr->node, &ctlr_list);
  349. return ctlr;
  350. }
  351. static int submit_inquiry(struct scsi_device *sdev, int page_code,
  352. unsigned int len, struct rdac_dh_data *h)
  353. {
  354. struct request *rq;
  355. struct request_queue *q = sdev->request_queue;
  356. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  357. rq = get_rdac_req(sdev, &h->inq, len, READ);
  358. if (!rq)
  359. goto done;
  360. /* Prepare the command. */
  361. rq->cmd[0] = INQUIRY;
  362. rq->cmd[1] = 1;
  363. rq->cmd[2] = page_code;
  364. rq->cmd[4] = len;
  365. rq->cmd_len = COMMAND_SIZE(INQUIRY);
  366. rq->sense = h->sense;
  367. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  368. rq->sense_len = 0;
  369. err = blk_execute_rq(q, NULL, rq, 1);
  370. if (err == -EIO)
  371. err = SCSI_DH_IO;
  372. blk_put_request(rq);
  373. done:
  374. return err;
  375. }
  376. static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
  377. char *array_name, u8 *array_id)
  378. {
  379. int err, i;
  380. struct c8_inquiry *inqp;
  381. err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
  382. if (err == SCSI_DH_OK) {
  383. inqp = &h->inq.c8;
  384. if (inqp->page_code != 0xc8)
  385. return SCSI_DH_NOSYS;
  386. if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
  387. inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
  388. return SCSI_DH_NOSYS;
  389. h->lun = inqp->lun[7]; /* Uses only the last byte */
  390. for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
  391. *(array_name+i) = inqp->array_user_label[(2*i)+1];
  392. *(array_name+ARRAY_LABEL_LEN-1) = '\0';
  393. memset(array_id, 0, UNIQUE_ID_LEN);
  394. memcpy(array_id, inqp->array_unique_id, inqp->array_uniq_id_len);
  395. }
  396. return err;
  397. }
  398. static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
  399. {
  400. int err;
  401. struct c9_inquiry *inqp;
  402. h->state = RDAC_STATE_ACTIVE;
  403. err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
  404. if (err == SCSI_DH_OK) {
  405. inqp = &h->inq.c9;
  406. /* detect the operating mode */
  407. if ((inqp->avte_cvp >> 5) & 0x1)
  408. h->mode = RDAC_MODE_IOSHIP; /* LUN in IOSHIP mode */
  409. else if (inqp->avte_cvp >> 7)
  410. h->mode = RDAC_MODE_AVT; /* LUN in AVT mode */
  411. else
  412. h->mode = RDAC_MODE; /* LUN in RDAC mode */
  413. /* Update ownership */
  414. if (inqp->avte_cvp & 0x1)
  415. h->lun_state = RDAC_LUN_OWNED;
  416. else {
  417. h->lun_state = RDAC_LUN_UNOWNED;
  418. if (h->mode == RDAC_MODE)
  419. h->state = RDAC_STATE_PASSIVE;
  420. }
  421. /* Update path prio*/
  422. if (inqp->path_prio & 0x1)
  423. h->preferred = RDAC_PREFERRED;
  424. else
  425. h->preferred = RDAC_NON_PREFERRED;
  426. }
  427. return err;
  428. }
  429. static int initialize_controller(struct scsi_device *sdev,
  430. struct rdac_dh_data *h, char *array_name, u8 *array_id)
  431. {
  432. int err, index;
  433. struct c4_inquiry *inqp;
  434. err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
  435. if (err == SCSI_DH_OK) {
  436. inqp = &h->inq.c4;
  437. /* get the controller index */
  438. if (inqp->slot_id[1] == 0x31)
  439. index = 0;
  440. else
  441. index = 1;
  442. spin_lock(&list_lock);
  443. h->ctlr = get_controller(index, array_name, array_id, sdev);
  444. if (!h->ctlr)
  445. err = SCSI_DH_RES_TEMP_UNAVAIL;
  446. spin_unlock(&list_lock);
  447. }
  448. return err;
  449. }
  450. static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
  451. {
  452. int err;
  453. struct c2_inquiry *inqp;
  454. err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
  455. if (err == SCSI_DH_OK) {
  456. inqp = &h->inq.c2;
  457. /*
  458. * If more than MODE6_MAX_LUN luns are supported, use
  459. * mode select 10
  460. */
  461. if (inqp->max_lun_supported >= MODE6_MAX_LUN)
  462. h->ctlr->use_ms10 = 1;
  463. else
  464. h->ctlr->use_ms10 = 0;
  465. }
  466. return err;
  467. }
  468. static int mode_select_handle_sense(struct scsi_device *sdev,
  469. unsigned char *sensebuf)
  470. {
  471. struct scsi_sense_hdr sense_hdr;
  472. int err = SCSI_DH_IO, ret;
  473. struct rdac_dh_data *h = get_rdac_data(sdev);
  474. ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
  475. if (!ret)
  476. goto done;
  477. switch (sense_hdr.sense_key) {
  478. case NO_SENSE:
  479. case ABORTED_COMMAND:
  480. case UNIT_ATTENTION:
  481. err = SCSI_DH_RETRY;
  482. break;
  483. case NOT_READY:
  484. if (sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x01)
  485. /* LUN Not Ready and is in the Process of Becoming
  486. * Ready
  487. */
  488. err = SCSI_DH_RETRY;
  489. break;
  490. case ILLEGAL_REQUEST:
  491. if (sense_hdr.asc == 0x91 && sense_hdr.ascq == 0x36)
  492. /*
  493. * Command Lock contention
  494. */
  495. err = SCSI_DH_RETRY;
  496. break;
  497. default:
  498. break;
  499. }
  500. RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
  501. "MODE_SELECT returned with sense %02x/%02x/%02x",
  502. (char *) h->ctlr->array_name, h->ctlr->index,
  503. sense_hdr.sense_key, sense_hdr.asc, sense_hdr.ascq);
  504. done:
  505. return err;
  506. }
  507. static void send_mode_select(struct work_struct *work)
  508. {
  509. struct rdac_controller *ctlr =
  510. container_of(work, struct rdac_controller, ms_work);
  511. struct request *rq;
  512. struct scsi_device *sdev = ctlr->ms_sdev;
  513. struct rdac_dh_data *h = get_rdac_data(sdev);
  514. struct request_queue *q = sdev->request_queue;
  515. int err, retry_cnt = RDAC_RETRY_COUNT;
  516. struct rdac_queue_data *tmp, *qdata;
  517. LIST_HEAD(list);
  518. spin_lock(&ctlr->ms_lock);
  519. list_splice_init(&ctlr->ms_head, &list);
  520. ctlr->ms_queued = 0;
  521. ctlr->ms_sdev = NULL;
  522. spin_unlock(&ctlr->ms_lock);
  523. retry:
  524. err = SCSI_DH_RES_TEMP_UNAVAIL;
  525. rq = rdac_failover_get(sdev, h, &list);
  526. if (!rq)
  527. goto done;
  528. RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
  529. "%s MODE_SELECT command",
  530. (char *) h->ctlr->array_name, h->ctlr->index,
  531. (retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
  532. err = blk_execute_rq(q, NULL, rq, 1);
  533. blk_put_request(rq);
  534. if (err != SCSI_DH_OK) {
  535. err = mode_select_handle_sense(sdev, h->sense);
  536. if (err == SCSI_DH_RETRY && retry_cnt--)
  537. goto retry;
  538. }
  539. if (err == SCSI_DH_OK) {
  540. h->state = RDAC_STATE_ACTIVE;
  541. RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
  542. "MODE_SELECT completed",
  543. (char *) h->ctlr->array_name, h->ctlr->index);
  544. }
  545. done:
  546. list_for_each_entry_safe(qdata, tmp, &list, entry) {
  547. list_del(&qdata->entry);
  548. if (err == SCSI_DH_OK)
  549. qdata->h->state = RDAC_STATE_ACTIVE;
  550. if (qdata->callback_fn)
  551. qdata->callback_fn(qdata->callback_data, err);
  552. kfree(qdata);
  553. }
  554. return;
  555. }
  556. static int queue_mode_select(struct scsi_device *sdev,
  557. activate_complete fn, void *data)
  558. {
  559. struct rdac_queue_data *qdata;
  560. struct rdac_controller *ctlr;
  561. qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
  562. if (!qdata)
  563. return SCSI_DH_RETRY;
  564. qdata->h = get_rdac_data(sdev);
  565. qdata->callback_fn = fn;
  566. qdata->callback_data = data;
  567. ctlr = qdata->h->ctlr;
  568. spin_lock(&ctlr->ms_lock);
  569. list_add_tail(&qdata->entry, &ctlr->ms_head);
  570. if (!ctlr->ms_queued) {
  571. ctlr->ms_queued = 1;
  572. ctlr->ms_sdev = sdev;
  573. queue_work(kmpath_rdacd, &ctlr->ms_work);
  574. }
  575. spin_unlock(&ctlr->ms_lock);
  576. return SCSI_DH_OK;
  577. }
  578. static int rdac_activate(struct scsi_device *sdev,
  579. activate_complete fn, void *data)
  580. {
  581. struct rdac_dh_data *h = get_rdac_data(sdev);
  582. int err = SCSI_DH_OK;
  583. int act = 0;
  584. err = check_ownership(sdev, h);
  585. if (err != SCSI_DH_OK)
  586. goto done;
  587. switch (h->mode) {
  588. case RDAC_MODE:
  589. if (h->lun_state == RDAC_LUN_UNOWNED)
  590. act = 1;
  591. break;
  592. case RDAC_MODE_IOSHIP:
  593. if ((h->lun_state == RDAC_LUN_UNOWNED) &&
  594. (h->preferred == RDAC_PREFERRED))
  595. act = 1;
  596. break;
  597. default:
  598. break;
  599. }
  600. if (act) {
  601. err = queue_mode_select(sdev, fn, data);
  602. if (err == SCSI_DH_OK)
  603. return 0;
  604. }
  605. done:
  606. if (fn)
  607. fn(data, err);
  608. return 0;
  609. }
  610. static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
  611. {
  612. struct rdac_dh_data *h = get_rdac_data(sdev);
  613. int ret = BLKPREP_OK;
  614. if (h->state != RDAC_STATE_ACTIVE) {
  615. ret = BLKPREP_KILL;
  616. req->cmd_flags |= REQ_QUIET;
  617. }
  618. return ret;
  619. }
  620. static int rdac_check_sense(struct scsi_device *sdev,
  621. struct scsi_sense_hdr *sense_hdr)
  622. {
  623. struct rdac_dh_data *h = get_rdac_data(sdev);
  624. RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
  625. "I/O returned with sense %02x/%02x/%02x",
  626. (char *) h->ctlr->array_name, h->ctlr->index,
  627. sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
  628. switch (sense_hdr->sense_key) {
  629. case NOT_READY:
  630. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
  631. /* LUN Not Ready - Logical Unit Not Ready and is in
  632. * the process of becoming ready
  633. * Just retry.
  634. */
  635. return ADD_TO_MLQUEUE;
  636. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
  637. /* LUN Not Ready - Storage firmware incompatible
  638. * Manual code synchonisation required.
  639. *
  640. * Nothing we can do here. Try to bypass the path.
  641. */
  642. return SUCCESS;
  643. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
  644. /* LUN Not Ready - Quiescense in progress
  645. *
  646. * Just retry and wait.
  647. */
  648. return ADD_TO_MLQUEUE;
  649. if (sense_hdr->asc == 0xA1 && sense_hdr->ascq == 0x02)
  650. /* LUN Not Ready - Quiescense in progress
  651. * or has been achieved
  652. * Just retry.
  653. */
  654. return ADD_TO_MLQUEUE;
  655. break;
  656. case ILLEGAL_REQUEST:
  657. if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
  658. /* Invalid Request - Current Logical Unit Ownership.
  659. * Controller is not the current owner of the LUN,
  660. * Fail the path, so that the other path be used.
  661. */
  662. h->state = RDAC_STATE_PASSIVE;
  663. return SUCCESS;
  664. }
  665. break;
  666. case UNIT_ATTENTION:
  667. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
  668. /*
  669. * Power On, Reset, or Bus Device Reset, just retry.
  670. */
  671. return ADD_TO_MLQUEUE;
  672. if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
  673. /*
  674. * Quiescence in progress , just retry.
  675. */
  676. return ADD_TO_MLQUEUE;
  677. break;
  678. }
  679. /* success just means we do not care what scsi-ml does */
  680. return SCSI_RETURN_NOT_HANDLED;
  681. }
  682. static const struct {
  683. char *vendor;
  684. char *model;
  685. } rdac_dev_list[] = {
  686. {"IBM", "1722"},
  687. {"IBM", "1724"},
  688. {"IBM", "1726"},
  689. {"IBM", "1742"},
  690. {"IBM", "1745"},
  691. {"IBM", "1746"},
  692. {"IBM", "1813"},
  693. {"IBM", "1814"},
  694. {"IBM", "1815"},
  695. {"IBM", "1818"},
  696. {"IBM", "3526"},
  697. {"SGI", "TP9"},
  698. {"SGI", "IS"},
  699. {"STK", "OPENstorage D280"},
  700. {"STK", "FLEXLINE 380"},
  701. {"SUN", "CSM"},
  702. {"SUN", "LCSM100"},
  703. {"SUN", "STK6580_6780"},
  704. {"SUN", "SUN_6180"},
  705. {"SUN", "ArrayStorage"},
  706. {"DELL", "MD3"},
  707. {"NETAPP", "INF-01-00"},
  708. {"LSI", "INF-01-00"},
  709. {"ENGENIO", "INF-01-00"},
  710. {NULL, NULL},
  711. };
  712. static bool rdac_match(struct scsi_device *sdev)
  713. {
  714. int i;
  715. if (scsi_device_tpgs(sdev))
  716. return false;
  717. for (i = 0; rdac_dev_list[i].vendor; i++) {
  718. if (!strncmp(sdev->vendor, rdac_dev_list[i].vendor,
  719. strlen(rdac_dev_list[i].vendor)) &&
  720. !strncmp(sdev->model, rdac_dev_list[i].model,
  721. strlen(rdac_dev_list[i].model))) {
  722. return true;
  723. }
  724. }
  725. return false;
  726. }
  727. static struct scsi_dh_data *rdac_bus_attach(struct scsi_device *sdev)
  728. {
  729. struct rdac_dh_data *h;
  730. int err;
  731. char array_name[ARRAY_LABEL_LEN];
  732. char array_id[UNIQUE_ID_LEN];
  733. h = kzalloc(sizeof(*h) , GFP_KERNEL);
  734. if (!h)
  735. return ERR_PTR(-ENOMEM);
  736. h->lun = UNINITIALIZED_LUN;
  737. h->state = RDAC_STATE_ACTIVE;
  738. err = get_lun_info(sdev, h, array_name, array_id);
  739. if (err != SCSI_DH_OK)
  740. goto failed;
  741. err = initialize_controller(sdev, h, array_name, array_id);
  742. if (err != SCSI_DH_OK)
  743. goto failed;
  744. err = check_ownership(sdev, h);
  745. if (err != SCSI_DH_OK)
  746. goto clean_ctlr;
  747. err = set_mode_select(sdev, h);
  748. if (err != SCSI_DH_OK)
  749. goto clean_ctlr;
  750. sdev_printk(KERN_NOTICE, sdev,
  751. "%s: LUN %d (%s) (%s)\n",
  752. RDAC_NAME, h->lun, mode[(int)h->mode],
  753. lun_state[(int)h->lun_state]);
  754. return &h->dh_data;
  755. clean_ctlr:
  756. spin_lock(&list_lock);
  757. kref_put(&h->ctlr->kref, release_controller);
  758. spin_unlock(&list_lock);
  759. failed:
  760. kfree(h);
  761. return ERR_PTR(-EINVAL);
  762. }
  763. static void rdac_bus_detach( struct scsi_device *sdev )
  764. {
  765. struct rdac_dh_data *h = get_rdac_data(sdev);
  766. if (h->ctlr && h->ctlr->ms_queued)
  767. flush_workqueue(kmpath_rdacd);
  768. spin_lock(&list_lock);
  769. if (h->ctlr)
  770. kref_put(&h->ctlr->kref, release_controller);
  771. spin_unlock(&list_lock);
  772. kfree(h);
  773. }
  774. static struct scsi_device_handler rdac_dh = {
  775. .name = RDAC_NAME,
  776. .module = THIS_MODULE,
  777. .prep_fn = rdac_prep_fn,
  778. .check_sense = rdac_check_sense,
  779. .attach = rdac_bus_attach,
  780. .detach = rdac_bus_detach,
  781. .activate = rdac_activate,
  782. .match = rdac_match,
  783. };
  784. static int __init rdac_init(void)
  785. {
  786. int r;
  787. r = scsi_register_device_handler(&rdac_dh);
  788. if (r != 0) {
  789. printk(KERN_ERR "Failed to register scsi device handler.");
  790. goto done;
  791. }
  792. /*
  793. * Create workqueue to handle mode selects for rdac
  794. */
  795. kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
  796. if (!kmpath_rdacd) {
  797. scsi_unregister_device_handler(&rdac_dh);
  798. printk(KERN_ERR "kmpath_rdacd creation failed.\n");
  799. r = -EINVAL;
  800. }
  801. done:
  802. return r;
  803. }
  804. static void __exit rdac_exit(void)
  805. {
  806. destroy_workqueue(kmpath_rdacd);
  807. scsi_unregister_device_handler(&rdac_dh);
  808. }
  809. module_init(rdac_init);
  810. module_exit(rdac_exit);
  811. MODULE_DESCRIPTION("Multipath LSI/Engenio/NetApp E-Series RDAC driver");
  812. MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
  813. MODULE_VERSION("01.00.0000.0000");
  814. MODULE_LICENSE("GPL");