ses.c 17 KB

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