ses.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * SCSI Enclosure Services
  3. *
  4. * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
  5. *
  6. **-----------------------------------------------------------------------------
  7. **
  8. ** This program is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License
  10. ** version 2 as published by the Free Software Foundation.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/enclosure.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_dbg.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_driver.h>
  32. #include <scsi/scsi_host.h>
  33. struct ses_device {
  34. unsigned char *page1;
  35. unsigned char *page1_types;
  36. unsigned char *page2;
  37. unsigned char *page10;
  38. short page1_len;
  39. short page1_num_types;
  40. short page2_len;
  41. short page10_len;
  42. };
  43. struct ses_component {
  44. u64 addr;
  45. unsigned char *desc;
  46. };
  47. static int ses_probe(struct device *dev)
  48. {
  49. struct scsi_device *sdev = to_scsi_device(dev);
  50. int err = -ENODEV;
  51. if (sdev->type != TYPE_ENCLOSURE)
  52. goto out;
  53. err = 0;
  54. sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
  55. out:
  56. return err;
  57. }
  58. #define SES_TIMEOUT (30 * HZ)
  59. #define SES_RETRIES 3
  60. static int ses_recv_diag(struct scsi_device *sdev, int page_code,
  61. void *buf, int bufflen)
  62. {
  63. int ret;
  64. unsigned char cmd[] = {
  65. RECEIVE_DIAGNOSTIC,
  66. 1, /* Set PCV bit */
  67. page_code,
  68. bufflen >> 8,
  69. bufflen & 0xff,
  70. 0
  71. };
  72. unsigned char recv_page_code;
  73. ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
  74. NULL, SES_TIMEOUT, SES_RETRIES, NULL);
  75. if (unlikely(!ret))
  76. return ret;
  77. recv_page_code = ((unsigned char *)buf)[0];
  78. if (likely(recv_page_code == page_code))
  79. return ret;
  80. /* successful diagnostic but wrong page code. This happens to some
  81. * USB devices, just print a message and pretend there was an error */
  82. sdev_printk(KERN_ERR, sdev,
  83. "Wrong diagnostic page; asked for %d got %u\n",
  84. page_code, recv_page_code);
  85. return -EINVAL;
  86. }
  87. static int ses_send_diag(struct scsi_device *sdev, int page_code,
  88. void *buf, int bufflen)
  89. {
  90. u32 result;
  91. unsigned char cmd[] = {
  92. SEND_DIAGNOSTIC,
  93. 0x10, /* Set PF bit */
  94. 0,
  95. bufflen >> 8,
  96. bufflen & 0xff,
  97. 0
  98. };
  99. result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
  100. NULL, SES_TIMEOUT, SES_RETRIES, NULL);
  101. if (result)
  102. sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
  103. result);
  104. return result;
  105. }
  106. static int ses_set_page2_descriptor(struct enclosure_device *edev,
  107. struct enclosure_component *ecomp,
  108. unsigned char *desc)
  109. {
  110. int i, j, count = 0, descriptor = ecomp->number;
  111. struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
  112. struct ses_device *ses_dev = edev->scratch;
  113. unsigned char *type_ptr = ses_dev->page1_types;
  114. unsigned char *desc_ptr = ses_dev->page2 + 8;
  115. /* Clear everything */
  116. memset(desc_ptr, 0, ses_dev->page2_len - 8);
  117. for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
  118. for (j = 0; j < type_ptr[1]; j++) {
  119. desc_ptr += 4;
  120. if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
  121. type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  122. continue;
  123. if (count++ == descriptor) {
  124. memcpy(desc_ptr, desc, 4);
  125. /* set select */
  126. desc_ptr[0] |= 0x80;
  127. /* clear reserved, just in case */
  128. desc_ptr[0] &= 0xf0;
  129. }
  130. }
  131. }
  132. return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
  133. }
  134. static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
  135. struct enclosure_component *ecomp)
  136. {
  137. int i, j, count = 0, descriptor = ecomp->number;
  138. struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
  139. struct ses_device *ses_dev = edev->scratch;
  140. unsigned char *type_ptr = ses_dev->page1_types;
  141. unsigned char *desc_ptr = ses_dev->page2 + 8;
  142. ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
  143. for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
  144. for (j = 0; j < type_ptr[1]; j++) {
  145. desc_ptr += 4;
  146. if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
  147. type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  148. continue;
  149. if (count++ == descriptor)
  150. return desc_ptr;
  151. }
  152. }
  153. return NULL;
  154. }
  155. /* For device slot and array device slot elements, byte 3 bit 6
  156. * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
  157. * code stands these bits are shifted 4 positions right so in
  158. * sysfs they will appear as bits 2 and 1 respectively. Strange. */
  159. static void ses_get_fault(struct enclosure_device *edev,
  160. struct enclosure_component *ecomp)
  161. {
  162. unsigned char *desc;
  163. desc = ses_get_page2_descriptor(edev, ecomp);
  164. if (desc)
  165. ecomp->fault = (desc[3] & 0x60) >> 4;
  166. }
  167. static int ses_set_fault(struct enclosure_device *edev,
  168. struct enclosure_component *ecomp,
  169. enum enclosure_component_setting val)
  170. {
  171. unsigned char desc[4] = {0 };
  172. switch (val) {
  173. case ENCLOSURE_SETTING_DISABLED:
  174. /* zero is disabled */
  175. break;
  176. case ENCLOSURE_SETTING_ENABLED:
  177. desc[3] = 0x20;
  178. break;
  179. default:
  180. /* SES doesn't do the SGPIO blink settings */
  181. return -EINVAL;
  182. }
  183. return ses_set_page2_descriptor(edev, ecomp, desc);
  184. }
  185. static void ses_get_status(struct enclosure_device *edev,
  186. struct enclosure_component *ecomp)
  187. {
  188. unsigned char *desc;
  189. desc = ses_get_page2_descriptor(edev, ecomp);
  190. if (desc)
  191. ecomp->status = (desc[0] & 0x0f);
  192. }
  193. static void ses_get_locate(struct enclosure_device *edev,
  194. struct enclosure_component *ecomp)
  195. {
  196. unsigned char *desc;
  197. desc = ses_get_page2_descriptor(edev, ecomp);
  198. if (desc)
  199. ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
  200. }
  201. static int ses_set_locate(struct enclosure_device *edev,
  202. struct enclosure_component *ecomp,
  203. enum enclosure_component_setting val)
  204. {
  205. unsigned char desc[4] = {0 };
  206. switch (val) {
  207. case ENCLOSURE_SETTING_DISABLED:
  208. /* zero is disabled */
  209. break;
  210. case ENCLOSURE_SETTING_ENABLED:
  211. desc[2] = 0x02;
  212. break;
  213. default:
  214. /* SES doesn't do the SGPIO blink settings */
  215. return -EINVAL;
  216. }
  217. return ses_set_page2_descriptor(edev, ecomp, desc);
  218. }
  219. static int ses_set_active(struct enclosure_device *edev,
  220. struct enclosure_component *ecomp,
  221. enum enclosure_component_setting val)
  222. {
  223. unsigned char desc[4] = {0 };
  224. switch (val) {
  225. case ENCLOSURE_SETTING_DISABLED:
  226. /* zero is disabled */
  227. ecomp->active = 0;
  228. break;
  229. case ENCLOSURE_SETTING_ENABLED:
  230. desc[2] = 0x80;
  231. ecomp->active = 1;
  232. break;
  233. default:
  234. /* SES doesn't do the SGPIO blink settings */
  235. return -EINVAL;
  236. }
  237. return ses_set_page2_descriptor(edev, ecomp, desc);
  238. }
  239. static struct enclosure_component_callbacks ses_enclosure_callbacks = {
  240. .get_fault = ses_get_fault,
  241. .set_fault = ses_set_fault,
  242. .get_status = ses_get_status,
  243. .get_locate = ses_get_locate,
  244. .set_locate = ses_set_locate,
  245. .set_active = ses_set_active,
  246. };
  247. struct ses_host_edev {
  248. struct Scsi_Host *shost;
  249. struct enclosure_device *edev;
  250. };
  251. #if 0
  252. int ses_match_host(struct enclosure_device *edev, void *data)
  253. {
  254. struct ses_host_edev *sed = data;
  255. struct scsi_device *sdev;
  256. if (!scsi_is_sdev_device(edev->edev.parent))
  257. return 0;
  258. sdev = to_scsi_device(edev->edev.parent);
  259. if (sdev->host != sed->shost)
  260. return 0;
  261. sed->edev = edev;
  262. return 1;
  263. }
  264. #endif /* 0 */
  265. static void ses_process_descriptor(struct enclosure_component *ecomp,
  266. unsigned char *desc)
  267. {
  268. int eip = desc[0] & 0x10;
  269. int invalid = desc[0] & 0x80;
  270. enum scsi_protocol proto = desc[0] & 0x0f;
  271. u64 addr = 0;
  272. struct ses_component *scomp = ecomp->scratch;
  273. unsigned char *d;
  274. scomp->desc = desc;
  275. if (invalid)
  276. return;
  277. switch (proto) {
  278. case SCSI_PROTOCOL_SAS:
  279. if (eip)
  280. d = desc + 8;
  281. else
  282. d = desc + 4;
  283. /* only take the phy0 addr */
  284. addr = (u64)d[12] << 56 |
  285. (u64)d[13] << 48 |
  286. (u64)d[14] << 40 |
  287. (u64)d[15] << 32 |
  288. (u64)d[16] << 24 |
  289. (u64)d[17] << 16 |
  290. (u64)d[18] << 8 |
  291. (u64)d[19];
  292. break;
  293. default:
  294. /* FIXME: Need to add more protocols than just SAS */
  295. break;
  296. }
  297. scomp->addr = addr;
  298. }
  299. struct efd {
  300. u64 addr;
  301. struct device *dev;
  302. };
  303. static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
  304. void *data)
  305. {
  306. struct efd *efd = data;
  307. int i;
  308. struct ses_component *scomp;
  309. if (!edev->component[0].scratch)
  310. return 0;
  311. for (i = 0; i < edev->components; i++) {
  312. scomp = edev->component[i].scratch;
  313. if (scomp->addr != efd->addr)
  314. continue;
  315. enclosure_add_device(edev, i, efd->dev);
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. #define INIT_ALLOC_SIZE 32
  321. static void ses_enclosure_data_process(struct enclosure_device *edev,
  322. struct scsi_device *sdev,
  323. int create)
  324. {
  325. u32 result;
  326. unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
  327. int i, j, page7_len, len, components;
  328. struct ses_device *ses_dev = edev->scratch;
  329. int types = ses_dev->page1_num_types;
  330. unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  331. if (!hdr_buf)
  332. goto simple_populate;
  333. /* re-read page 10 */
  334. if (ses_dev->page10)
  335. ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
  336. /* Page 7 for the descriptors is optional */
  337. result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
  338. if (result)
  339. goto simple_populate;
  340. page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  341. /* add 1 for trailing '\0' we'll use */
  342. buf = kzalloc(len + 1, GFP_KERNEL);
  343. if (!buf)
  344. goto simple_populate;
  345. result = ses_recv_diag(sdev, 7, buf, len);
  346. if (result) {
  347. simple_populate:
  348. kfree(buf);
  349. buf = NULL;
  350. desc_ptr = NULL;
  351. len = 0;
  352. page7_len = 0;
  353. } else {
  354. desc_ptr = buf + 8;
  355. len = (desc_ptr[2] << 8) + desc_ptr[3];
  356. /* skip past overall descriptor */
  357. desc_ptr += len + 4;
  358. }
  359. if (ses_dev->page10)
  360. addl_desc_ptr = ses_dev->page10 + 8;
  361. type_ptr = ses_dev->page1_types;
  362. components = 0;
  363. for (i = 0; i < types; i++, type_ptr += 4) {
  364. for (j = 0; j < type_ptr[1]; j++) {
  365. char *name = NULL;
  366. struct enclosure_component *ecomp;
  367. if (desc_ptr) {
  368. if (desc_ptr >= buf + page7_len) {
  369. desc_ptr = NULL;
  370. } else {
  371. len = (desc_ptr[2] << 8) + desc_ptr[3];
  372. desc_ptr += 4;
  373. /* Add trailing zero - pushes into
  374. * reserved space */
  375. desc_ptr[len] = '\0';
  376. name = desc_ptr;
  377. }
  378. }
  379. if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  380. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
  381. if (create)
  382. ecomp = enclosure_component_register(edev,
  383. components++,
  384. type_ptr[0],
  385. name);
  386. else
  387. ecomp = &edev->component[components++];
  388. if (!IS_ERR(ecomp) && addl_desc_ptr)
  389. ses_process_descriptor(ecomp,
  390. addl_desc_ptr);
  391. }
  392. if (desc_ptr)
  393. desc_ptr += len;
  394. if (addl_desc_ptr &&
  395. /* only find additional descriptions for specific devices */
  396. (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  397. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
  398. type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
  399. /* these elements are optional */
  400. type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
  401. type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
  402. type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
  403. addl_desc_ptr += addl_desc_ptr[1] + 2;
  404. }
  405. }
  406. kfree(buf);
  407. kfree(hdr_buf);
  408. }
  409. static void ses_match_to_enclosure(struct enclosure_device *edev,
  410. struct scsi_device *sdev)
  411. {
  412. unsigned char *buf;
  413. unsigned char *desc;
  414. unsigned int vpd_len;
  415. struct efd efd = {
  416. .addr = 0,
  417. };
  418. buf = kmalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  419. if (!buf || scsi_get_vpd_page(sdev, 0x83, buf, INIT_ALLOC_SIZE))
  420. goto free;
  421. ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
  422. vpd_len = ((buf[2] << 8) | buf[3]) + 4;
  423. kfree(buf);
  424. buf = kmalloc(vpd_len, GFP_KERNEL);
  425. if (!buf ||scsi_get_vpd_page(sdev, 0x83, buf, vpd_len))
  426. goto free;
  427. desc = buf + 4;
  428. while (desc < buf + vpd_len) {
  429. enum scsi_protocol proto = desc[0] >> 4;
  430. u8 code_set = desc[0] & 0x0f;
  431. u8 piv = desc[1] & 0x80;
  432. u8 assoc = (desc[1] & 0x30) >> 4;
  433. u8 type = desc[1] & 0x0f;
  434. u8 len = desc[3];
  435. if (piv && code_set == 1 && assoc == 1
  436. && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
  437. efd.addr = (u64)desc[4] << 56 |
  438. (u64)desc[5] << 48 |
  439. (u64)desc[6] << 40 |
  440. (u64)desc[7] << 32 |
  441. (u64)desc[8] << 24 |
  442. (u64)desc[9] << 16 |
  443. (u64)desc[10] << 8 |
  444. (u64)desc[11];
  445. desc += len + 4;
  446. }
  447. if (!efd.addr)
  448. goto free;
  449. efd.dev = &sdev->sdev_gendev;
  450. enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
  451. free:
  452. kfree(buf);
  453. }
  454. static int ses_intf_add(struct device *cdev,
  455. struct class_interface *intf)
  456. {
  457. struct scsi_device *sdev = to_scsi_device(cdev->parent);
  458. struct scsi_device *tmp_sdev;
  459. unsigned char *buf = NULL, *hdr_buf, *type_ptr;
  460. struct ses_device *ses_dev;
  461. u32 result;
  462. int i, types, len, components = 0;
  463. int err = -ENOMEM;
  464. int num_enclosures;
  465. struct enclosure_device *edev;
  466. struct ses_component *scomp = NULL;
  467. if (!scsi_device_enclosure(sdev)) {
  468. /* not an enclosure, but might be in one */
  469. struct enclosure_device *prev = NULL;
  470. while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
  471. ses_match_to_enclosure(edev, sdev);
  472. prev = edev;
  473. }
  474. return -ENODEV;
  475. }
  476. /* TYPE_ENCLOSURE prints a message in probe */
  477. if (sdev->type != TYPE_ENCLOSURE)
  478. sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
  479. ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
  480. hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  481. if (!hdr_buf || !ses_dev)
  482. goto err_init_free;
  483. result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
  484. if (result)
  485. goto recv_failed;
  486. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  487. buf = kzalloc(len, GFP_KERNEL);
  488. if (!buf)
  489. goto err_free;
  490. result = ses_recv_diag(sdev, 1, buf, len);
  491. if (result)
  492. goto recv_failed;
  493. types = 0;
  494. /* we always have one main enclosure and the rest are referred
  495. * to as secondary subenclosures */
  496. num_enclosures = buf[1] + 1;
  497. /* begin at the enclosure descriptor */
  498. type_ptr = buf + 8;
  499. /* skip all the enclosure descriptors */
  500. for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
  501. types += type_ptr[2];
  502. type_ptr += type_ptr[3] + 4;
  503. }
  504. ses_dev->page1_types = type_ptr;
  505. ses_dev->page1_num_types = types;
  506. for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
  507. if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  508. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  509. components += type_ptr[1];
  510. }
  511. ses_dev->page1 = buf;
  512. ses_dev->page1_len = len;
  513. buf = NULL;
  514. result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
  515. if (result)
  516. goto recv_failed;
  517. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  518. buf = kzalloc(len, GFP_KERNEL);
  519. if (!buf)
  520. goto err_free;
  521. /* make sure getting page 2 actually works */
  522. result = ses_recv_diag(sdev, 2, buf, len);
  523. if (result)
  524. goto recv_failed;
  525. ses_dev->page2 = buf;
  526. ses_dev->page2_len = len;
  527. buf = NULL;
  528. /* The additional information page --- allows us
  529. * to match up the devices */
  530. result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
  531. if (!result) {
  532. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  533. buf = kzalloc(len, GFP_KERNEL);
  534. if (!buf)
  535. goto err_free;
  536. result = ses_recv_diag(sdev, 10, buf, len);
  537. if (result)
  538. goto recv_failed;
  539. ses_dev->page10 = buf;
  540. ses_dev->page10_len = len;
  541. buf = NULL;
  542. }
  543. scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
  544. if (!scomp)
  545. goto err_free;
  546. edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
  547. components, &ses_enclosure_callbacks);
  548. if (IS_ERR(edev)) {
  549. err = PTR_ERR(edev);
  550. goto err_free;
  551. }
  552. kfree(hdr_buf);
  553. edev->scratch = ses_dev;
  554. for (i = 0; i < components; i++)
  555. edev->component[i].scratch = scomp + i;
  556. ses_enclosure_data_process(edev, sdev, 1);
  557. /* see if there are any devices matching before
  558. * we found the enclosure */
  559. shost_for_each_device(tmp_sdev, sdev->host) {
  560. if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
  561. continue;
  562. ses_match_to_enclosure(edev, tmp_sdev);
  563. }
  564. return 0;
  565. recv_failed:
  566. sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
  567. result);
  568. err = -ENODEV;
  569. err_free:
  570. kfree(buf);
  571. kfree(scomp);
  572. kfree(ses_dev->page10);
  573. kfree(ses_dev->page2);
  574. kfree(ses_dev->page1);
  575. err_init_free:
  576. kfree(ses_dev);
  577. kfree(hdr_buf);
  578. sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
  579. return err;
  580. }
  581. static int ses_remove(struct device *dev)
  582. {
  583. return 0;
  584. }
  585. static void ses_intf_remove_component(struct scsi_device *sdev)
  586. {
  587. struct enclosure_device *edev, *prev = NULL;
  588. while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
  589. prev = edev;
  590. if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
  591. break;
  592. }
  593. if (edev)
  594. put_device(&edev->edev);
  595. }
  596. static void ses_intf_remove_enclosure(struct scsi_device *sdev)
  597. {
  598. struct enclosure_device *edev;
  599. struct ses_device *ses_dev;
  600. /* exact match to this enclosure */
  601. edev = enclosure_find(&sdev->sdev_gendev, NULL);
  602. if (!edev)
  603. return;
  604. ses_dev = edev->scratch;
  605. edev->scratch = NULL;
  606. kfree(ses_dev->page10);
  607. kfree(ses_dev->page1);
  608. kfree(ses_dev->page2);
  609. kfree(ses_dev);
  610. kfree(edev->component[0].scratch);
  611. put_device(&edev->edev);
  612. enclosure_unregister(edev);
  613. }
  614. static void ses_intf_remove(struct device *cdev,
  615. struct class_interface *intf)
  616. {
  617. struct scsi_device *sdev = to_scsi_device(cdev->parent);
  618. if (!scsi_device_enclosure(sdev))
  619. ses_intf_remove_component(sdev);
  620. else
  621. ses_intf_remove_enclosure(sdev);
  622. }
  623. static struct class_interface ses_interface = {
  624. .add_dev = ses_intf_add,
  625. .remove_dev = ses_intf_remove,
  626. };
  627. static struct scsi_driver ses_template = {
  628. .owner = THIS_MODULE,
  629. .gendrv = {
  630. .name = "ses",
  631. .probe = ses_probe,
  632. .remove = ses_remove,
  633. },
  634. };
  635. static int __init ses_init(void)
  636. {
  637. int err;
  638. err = scsi_register_interface(&ses_interface);
  639. if (err)
  640. return err;
  641. err = scsi_register_driver(&ses_template.gendrv);
  642. if (err)
  643. goto out_unreg;
  644. return 0;
  645. out_unreg:
  646. scsi_unregister_interface(&ses_interface);
  647. return err;
  648. }
  649. static void __exit ses_exit(void)
  650. {
  651. scsi_unregister_driver(&ses_template.gendrv);
  652. scsi_unregister_interface(&ses_interface);
  653. }
  654. module_init(ses_init);
  655. module_exit(ses_exit);
  656. MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
  657. MODULE_AUTHOR("James Bottomley");
  658. MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
  659. MODULE_LICENSE("GPL v2");