sas_discover.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Serial Attached SCSI (SAS) Discover process
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <linux/pci.h>
  25. #include <linux/scatterlist.h>
  26. #include <scsi/scsi_host.h>
  27. #include <scsi/scsi_eh.h>
  28. #include "sas_internal.h"
  29. #include <scsi/scsi_transport.h>
  30. #include <scsi/scsi_transport_sas.h>
  31. #include "../scsi_sas_internal.h"
  32. /* ---------- Basic task processing for discovery purposes ---------- */
  33. void sas_init_dev(struct domain_device *dev)
  34. {
  35. INIT_LIST_HEAD(&dev->siblings);
  36. INIT_LIST_HEAD(&dev->dev_list_node);
  37. switch (dev->dev_type) {
  38. case SAS_END_DEV:
  39. break;
  40. case EDGE_DEV:
  41. case FANOUT_DEV:
  42. INIT_LIST_HEAD(&dev->ex_dev.children);
  43. break;
  44. case SATA_DEV:
  45. case SATA_PM:
  46. case SATA_PM_PORT:
  47. INIT_LIST_HEAD(&dev->sata_dev.children);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. static void sas_task_timedout(unsigned long _task)
  54. {
  55. struct sas_task *task = (void *) _task;
  56. unsigned long flags;
  57. spin_lock_irqsave(&task->task_state_lock, flags);
  58. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  59. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  60. spin_unlock_irqrestore(&task->task_state_lock, flags);
  61. complete(&task->completion);
  62. }
  63. static void sas_disc_task_done(struct sas_task *task)
  64. {
  65. if (!del_timer(&task->timer))
  66. return;
  67. complete(&task->completion);
  68. }
  69. #define SAS_DEV_TIMEOUT 10
  70. /**
  71. * sas_execute_task -- Basic task processing for discovery
  72. * @task: the task to be executed
  73. * @buffer: pointer to buffer to do I/O
  74. * @size: size of @buffer
  75. * @pci_dma_dir: PCI_DMA_...
  76. */
  77. static int sas_execute_task(struct sas_task *task, void *buffer, int size,
  78. int pci_dma_dir)
  79. {
  80. int res = 0;
  81. struct scatterlist *scatter = NULL;
  82. struct task_status_struct *ts = &task->task_status;
  83. int num_scatter = 0;
  84. int retries = 0;
  85. struct sas_internal *i =
  86. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  87. if (pci_dma_dir != PCI_DMA_NONE) {
  88. scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
  89. if (!scatter)
  90. goto out;
  91. sg_init_one(scatter, buffer, size);
  92. num_scatter = 1;
  93. }
  94. task->task_proto = task->dev->tproto;
  95. task->scatter = scatter;
  96. task->num_scatter = num_scatter;
  97. task->total_xfer_len = size;
  98. task->data_dir = pci_dma_dir;
  99. task->task_done = sas_disc_task_done;
  100. for (retries = 0; retries < 5; retries++) {
  101. task->task_state_flags = SAS_TASK_STATE_PENDING;
  102. init_completion(&task->completion);
  103. task->timer.data = (unsigned long) task;
  104. task->timer.function = sas_task_timedout;
  105. task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
  106. add_timer(&task->timer);
  107. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  108. if (res) {
  109. del_timer(&task->timer);
  110. SAS_DPRINTK("executing SAS discovery task failed:%d\n",
  111. res);
  112. goto ex_err;
  113. }
  114. wait_for_completion(&task->completion);
  115. res = -ETASK;
  116. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  117. int res2;
  118. SAS_DPRINTK("task aborted, flags:0x%x\n",
  119. task->task_state_flags);
  120. res2 = i->dft->lldd_abort_task(task);
  121. SAS_DPRINTK("came back from abort task\n");
  122. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  123. if (res2 == TMF_RESP_FUNC_COMPLETE)
  124. continue; /* Retry the task */
  125. else
  126. goto ex_err;
  127. }
  128. }
  129. if (task->task_status.stat == SAM_BUSY ||
  130. task->task_status.stat == SAM_TASK_SET_FULL ||
  131. task->task_status.stat == SAS_QUEUE_FULL) {
  132. SAS_DPRINTK("task: q busy, sleeping...\n");
  133. schedule_timeout_interruptible(HZ);
  134. } else if (task->task_status.stat == SAM_CHECK_COND) {
  135. struct scsi_sense_hdr shdr;
  136. if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
  137. &shdr)) {
  138. SAS_DPRINTK("couldn't normalize sense\n");
  139. continue;
  140. }
  141. if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
  142. (shdr.sense_key == 2 && shdr.asc == 4 &&
  143. shdr.ascq == 1)) {
  144. SAS_DPRINTK("device %016llx LUN: %016llx "
  145. "powering up or not ready yet, "
  146. "sleeping...\n",
  147. SAS_ADDR(task->dev->sas_addr),
  148. SAS_ADDR(task->ssp_task.LUN));
  149. schedule_timeout_interruptible(5*HZ);
  150. } else if (shdr.sense_key == 1) {
  151. res = 0;
  152. break;
  153. } else if (shdr.sense_key == 5) {
  154. break;
  155. } else {
  156. SAS_DPRINTK("dev %016llx LUN: %016llx "
  157. "sense key:0x%x ASC:0x%x ASCQ:0x%x"
  158. "\n",
  159. SAS_ADDR(task->dev->sas_addr),
  160. SAS_ADDR(task->ssp_task.LUN),
  161. shdr.sense_key,
  162. shdr.asc, shdr.ascq);
  163. }
  164. } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
  165. task->task_status.stat != SAM_GOOD) {
  166. SAS_DPRINTK("task finished with resp:0x%x, "
  167. "stat:0x%x\n",
  168. task->task_status.resp,
  169. task->task_status.stat);
  170. goto ex_err;
  171. } else {
  172. res = 0;
  173. break;
  174. }
  175. }
  176. ex_err:
  177. if (pci_dma_dir != PCI_DMA_NONE)
  178. kfree(scatter);
  179. out:
  180. return res;
  181. }
  182. /* ---------- Domain device discovery ---------- */
  183. /**
  184. * sas_get_port_device -- Discover devices which caused port creation
  185. * @port: pointer to struct sas_port of interest
  186. *
  187. * Devices directly attached to a HA port, have no parent. This is
  188. * how we know they are (domain) "root" devices. All other devices
  189. * do, and should have their "parent" pointer set appropriately as
  190. * soon as a child device is discovered.
  191. */
  192. static int sas_get_port_device(struct asd_sas_port *port)
  193. {
  194. unsigned long flags;
  195. struct asd_sas_phy *phy;
  196. struct sas_rphy *rphy;
  197. struct domain_device *dev;
  198. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  199. if (!dev)
  200. return -ENOMEM;
  201. spin_lock_irqsave(&port->phy_list_lock, flags);
  202. if (list_empty(&port->phy_list)) {
  203. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  204. kfree(dev);
  205. return -ENODEV;
  206. }
  207. phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
  208. spin_lock(&phy->frame_rcvd_lock);
  209. memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
  210. (size_t)phy->frame_rcvd_size));
  211. spin_unlock(&phy->frame_rcvd_lock);
  212. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  213. if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
  214. struct dev_to_host_fis *fis =
  215. (struct dev_to_host_fis *) dev->frame_rcvd;
  216. if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
  217. fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
  218. && (fis->device & ~0x10) == 0)
  219. dev->dev_type = SATA_PM;
  220. else
  221. dev->dev_type = SATA_DEV;
  222. dev->tproto = SATA_PROTO;
  223. } else {
  224. struct sas_identify_frame *id =
  225. (struct sas_identify_frame *) dev->frame_rcvd;
  226. dev->dev_type = id->dev_type;
  227. dev->iproto = id->initiator_bits;
  228. dev->tproto = id->target_bits;
  229. }
  230. sas_init_dev(dev);
  231. switch (dev->dev_type) {
  232. case SAS_END_DEV:
  233. rphy = sas_end_device_alloc(port->port);
  234. break;
  235. case EDGE_DEV:
  236. rphy = sas_expander_alloc(port->port,
  237. SAS_EDGE_EXPANDER_DEVICE);
  238. break;
  239. case FANOUT_DEV:
  240. rphy = sas_expander_alloc(port->port,
  241. SAS_FANOUT_EXPANDER_DEVICE);
  242. break;
  243. case SATA_DEV:
  244. default:
  245. printk("ERROR: Unidentified device type %d\n", dev->dev_type);
  246. rphy = NULL;
  247. break;
  248. }
  249. if (!rphy) {
  250. kfree(dev);
  251. return -ENODEV;
  252. }
  253. rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
  254. memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
  255. sas_fill_in_rphy(dev, rphy);
  256. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  257. port->port_dev = dev;
  258. dev->port = port;
  259. dev->linkrate = port->linkrate;
  260. dev->min_linkrate = port->linkrate;
  261. dev->max_linkrate = port->linkrate;
  262. dev->pathways = port->num_phys;
  263. memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
  264. memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
  265. memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
  266. port->disc.max_level = 0;
  267. dev->rphy = rphy;
  268. spin_lock(&port->dev_list_lock);
  269. list_add_tail(&dev->dev_list_node, &port->dev_list);
  270. spin_unlock(&port->dev_list_lock);
  271. return 0;
  272. }
  273. /* ---------- Discover and Revalidate ---------- */
  274. /* ---------- SATA ---------- */
  275. static void sas_get_ata_command_set(struct domain_device *dev)
  276. {
  277. struct dev_to_host_fis *fis =
  278. (struct dev_to_host_fis *) dev->frame_rcvd;
  279. if ((fis->sector_count == 1 && /* ATA */
  280. fis->lbal == 1 &&
  281. fis->lbam == 0 &&
  282. fis->lbah == 0 &&
  283. fis->device == 0)
  284. ||
  285. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  286. fis->lbal == 0 &&
  287. fis->lbam == 0xCE &&
  288. fis->lbah == 0xAA &&
  289. (fis->device & ~0x10) == 0))
  290. dev->sata_dev.command_set = ATA_COMMAND_SET;
  291. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  292. fis->lbal == 1 &&
  293. fis->byte_count_low == 0x14 &&
  294. fis->byte_count_high == 0xEB &&
  295. (fis->device & ~0x10) == 0))
  296. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  297. else if ((fis->sector_count == 1 && /* SEMB */
  298. fis->lbal == 1 &&
  299. fis->lbam == 0x3C &&
  300. fis->lbah == 0xC3 &&
  301. fis->device == 0)
  302. ||
  303. (fis->interrupt_reason == 1 && /* SATA PM */
  304. fis->lbal == 1 &&
  305. fis->byte_count_low == 0x69 &&
  306. fis->byte_count_high == 0x96 &&
  307. (fis->device & ~0x10) == 0))
  308. /* Treat it as a superset? */
  309. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  310. }
  311. /**
  312. * sas_issue_ata_cmd -- Basic SATA command processing for discovery
  313. * @dev: the device to send the command to
  314. * @command: the command register
  315. * @features: the features register
  316. * @buffer: pointer to buffer to do I/O
  317. * @size: size of @buffer
  318. * @pci_dma_dir: PCI_DMA_...
  319. */
  320. static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
  321. u8 features, void *buffer, int size,
  322. int pci_dma_dir)
  323. {
  324. int res = 0;
  325. struct sas_task *task;
  326. struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
  327. &dev->frame_rcvd[0];
  328. res = -ENOMEM;
  329. task = sas_alloc_task(GFP_KERNEL);
  330. if (!task)
  331. goto out;
  332. task->dev = dev;
  333. task->ata_task.fis.command = command;
  334. task->ata_task.fis.features = features;
  335. task->ata_task.fis.device = d2h_fis->device;
  336. task->ata_task.retry_count = 1;
  337. res = sas_execute_task(task, buffer, size, pci_dma_dir);
  338. sas_free_task(task);
  339. out:
  340. return res;
  341. }
  342. static void sas_sata_propagate_sas_addr(struct domain_device *dev)
  343. {
  344. unsigned long flags;
  345. struct asd_sas_port *port = dev->port;
  346. struct asd_sas_phy *phy;
  347. BUG_ON(dev->parent);
  348. memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  349. spin_lock_irqsave(&port->phy_list_lock, flags);
  350. list_for_each_entry(phy, &port->phy_list, port_phy_el)
  351. memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  352. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  353. }
  354. #define ATA_IDENTIFY_DEV 0xEC
  355. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  356. #define ATA_SET_FEATURES 0xEF
  357. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  358. /**
  359. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  360. * @dev: STP/SATA device of interest (ATA/ATAPI)
  361. *
  362. * The LLDD has already been notified of this device, so that we can
  363. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  364. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  365. * performance for this device.
  366. */
  367. static int sas_discover_sata_dev(struct domain_device *dev)
  368. {
  369. int res;
  370. __le16 *identify_x;
  371. u8 command;
  372. identify_x = kzalloc(512, GFP_KERNEL);
  373. if (!identify_x)
  374. return -ENOMEM;
  375. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  376. dev->sata_dev.identify_device = identify_x;
  377. command = ATA_IDENTIFY_DEV;
  378. } else {
  379. dev->sata_dev.identify_packet_device = identify_x;
  380. command = ATA_IDENTIFY_PACKET_DEV;
  381. }
  382. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  383. PCI_DMA_FROMDEVICE);
  384. if (res)
  385. goto out_err;
  386. /* lives on the media? */
  387. if (le16_to_cpu(identify_x[0]) & 4) {
  388. /* incomplete response */
  389. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  390. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  391. if (!le16_to_cpu(identify_x[83] & (1<<6)))
  392. goto cont1;
  393. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  394. ATA_FEATURE_PUP_STBY_SPIN_UP,
  395. NULL, 0, PCI_DMA_NONE);
  396. if (res)
  397. goto cont1;
  398. schedule_timeout_interruptible(5*HZ); /* More time? */
  399. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  400. PCI_DMA_FROMDEVICE);
  401. if (res)
  402. goto out_err;
  403. }
  404. cont1:
  405. /* Get WWN */
  406. if (dev->port->oob_mode != SATA_OOB_MODE) {
  407. memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
  408. SAS_ADDR_SIZE);
  409. } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
  410. (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
  411. == 0x5000) {
  412. int i;
  413. for (i = 0; i < 4; i++) {
  414. dev->sas_addr[2*i] =
  415. (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
  416. dev->sas_addr[2*i+1] =
  417. le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
  418. }
  419. }
  420. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  421. if (!dev->parent)
  422. sas_sata_propagate_sas_addr(dev);
  423. /* XXX Hint: register this SATA device with SATL.
  424. When this returns, dev->sata_dev->lu is alive and
  425. present.
  426. sas_satl_register_dev(dev);
  427. */
  428. return 0;
  429. out_err:
  430. dev->sata_dev.identify_packet_device = NULL;
  431. dev->sata_dev.identify_device = NULL;
  432. kfree(identify_x);
  433. return res;
  434. }
  435. static int sas_discover_sata_pm(struct domain_device *dev)
  436. {
  437. return -ENODEV;
  438. }
  439. int sas_notify_lldd_dev_found(struct domain_device *dev)
  440. {
  441. int res = 0;
  442. struct sas_ha_struct *sas_ha = dev->port->ha;
  443. struct Scsi_Host *shost = sas_ha->core.shost;
  444. struct sas_internal *i = to_sas_internal(shost->transportt);
  445. if (i->dft->lldd_dev_found) {
  446. res = i->dft->lldd_dev_found(dev);
  447. if (res) {
  448. printk("sas: driver on pcidev %s cannot handle "
  449. "device %llx, error:%d\n",
  450. pci_name(sas_ha->pcidev),
  451. SAS_ADDR(dev->sas_addr), res);
  452. }
  453. }
  454. return res;
  455. }
  456. void sas_notify_lldd_dev_gone(struct domain_device *dev)
  457. {
  458. struct sas_ha_struct *sas_ha = dev->port->ha;
  459. struct Scsi_Host *shost = sas_ha->core.shost;
  460. struct sas_internal *i = to_sas_internal(shost->transportt);
  461. if (i->dft->lldd_dev_gone)
  462. i->dft->lldd_dev_gone(dev);
  463. }
  464. /* ---------- Common/dispatchers ---------- */
  465. /**
  466. * sas_discover_sata -- discover an STP/SATA domain device
  467. * @dev: pointer to struct domain_device of interest
  468. *
  469. * First we notify the LLDD of this device, so we can send frames to
  470. * it. Then depending on the type of device we call the appropriate
  471. * discover functions. Once device discover is done, we notify the
  472. * LLDD so that it can fine-tune its parameters for the device, by
  473. * removing it and then adding it. That is, the second time around,
  474. * the driver would have certain fields, that it is looking at, set.
  475. * Finally we initialize the kobj so that the device can be added to
  476. * the system at registration time. Devices directly attached to a HA
  477. * port, have no parents. All other devices do, and should have their
  478. * "parent" pointer set appropriately before calling this function.
  479. */
  480. int sas_discover_sata(struct domain_device *dev)
  481. {
  482. int res;
  483. sas_get_ata_command_set(dev);
  484. res = sas_notify_lldd_dev_found(dev);
  485. if (res)
  486. return res;
  487. switch (dev->dev_type) {
  488. case SATA_DEV:
  489. res = sas_discover_sata_dev(dev);
  490. break;
  491. case SATA_PM:
  492. res = sas_discover_sata_pm(dev);
  493. break;
  494. default:
  495. break;
  496. }
  497. sas_notify_lldd_dev_gone(dev);
  498. if (!res) {
  499. sas_notify_lldd_dev_found(dev);
  500. }
  501. return res;
  502. }
  503. /**
  504. * sas_discover_end_dev -- discover an end device (SSP, etc)
  505. * @end: pointer to domain device of interest
  506. *
  507. * See comment in sas_discover_sata().
  508. */
  509. int sas_discover_end_dev(struct domain_device *dev)
  510. {
  511. int res;
  512. res = sas_notify_lldd_dev_found(dev);
  513. if (res)
  514. return res;
  515. res = sas_rphy_add(dev->rphy);
  516. if (res)
  517. goto out_err;
  518. /* do this to get the end device port attributes which will have
  519. * been scanned in sas_rphy_add */
  520. sas_notify_lldd_dev_gone(dev);
  521. sas_notify_lldd_dev_found(dev);
  522. return 0;
  523. out_err:
  524. sas_notify_lldd_dev_gone(dev);
  525. return res;
  526. }
  527. /* ---------- Device registration and unregistration ---------- */
  528. static inline void sas_unregister_common_dev(struct domain_device *dev)
  529. {
  530. sas_notify_lldd_dev_gone(dev);
  531. if (!dev->parent)
  532. dev->port->port_dev = NULL;
  533. else
  534. list_del_init(&dev->siblings);
  535. list_del_init(&dev->dev_list_node);
  536. }
  537. void sas_unregister_dev(struct domain_device *dev)
  538. {
  539. if (dev->rphy) {
  540. sas_remove_children(&dev->rphy->dev);
  541. sas_rphy_delete(dev->rphy);
  542. dev->rphy = NULL;
  543. }
  544. if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) {
  545. /* remove the phys and ports, everything else should be gone */
  546. kfree(dev->ex_dev.ex_phy);
  547. dev->ex_dev.ex_phy = NULL;
  548. }
  549. sas_unregister_common_dev(dev);
  550. }
  551. void sas_unregister_domain_devices(struct asd_sas_port *port)
  552. {
  553. struct domain_device *dev, *n;
  554. list_for_each_entry_safe_reverse(dev,n,&port->dev_list,dev_list_node)
  555. sas_unregister_dev(dev);
  556. port->port->rphy = NULL;
  557. }
  558. /* ---------- Discovery and Revalidation ---------- */
  559. /**
  560. * sas_discover_domain -- discover the domain
  561. * @port: port to the domain of interest
  562. *
  563. * NOTE: this process _must_ quit (return) as soon as any connection
  564. * errors are encountered. Connection recovery is done elsewhere.
  565. * Discover process only interrogates devices in order to discover the
  566. * domain.
  567. */
  568. static void sas_discover_domain(void *data)
  569. {
  570. int error = 0;
  571. struct asd_sas_port *port = data;
  572. sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
  573. &port->disc.pending);
  574. if (port->port_dev)
  575. return ;
  576. else {
  577. error = sas_get_port_device(port);
  578. if (error)
  579. return;
  580. }
  581. SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  582. current->pid);
  583. switch (port->port_dev->dev_type) {
  584. case SAS_END_DEV:
  585. error = sas_discover_end_dev(port->port_dev);
  586. break;
  587. case EDGE_DEV:
  588. case FANOUT_DEV:
  589. error = sas_discover_root_expander(port->port_dev);
  590. break;
  591. case SATA_DEV:
  592. case SATA_PM:
  593. error = sas_discover_sata(port->port_dev);
  594. break;
  595. default:
  596. SAS_DPRINTK("unhandled device %d\n", port->port_dev->dev_type);
  597. break;
  598. }
  599. if (error) {
  600. kfree(port->port_dev); /* not kobject_register-ed yet */
  601. port->port_dev = NULL;
  602. }
  603. SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  604. current->pid, error);
  605. }
  606. static void sas_revalidate_domain(void *data)
  607. {
  608. int res = 0;
  609. struct asd_sas_port *port = data;
  610. sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
  611. &port->disc.pending);
  612. SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  613. current->pid);
  614. if (port->port_dev)
  615. res = sas_ex_revalidate_domain(port->port_dev);
  616. SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  617. port->id, current->pid, res);
  618. }
  619. /* ---------- Events ---------- */
  620. int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  621. {
  622. struct sas_discovery *disc;
  623. if (!port)
  624. return 0;
  625. disc = &port->disc;
  626. BUG_ON(ev >= DISC_NUM_EVENTS);
  627. sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
  628. &disc->disc_work[ev], port->ha->core.shost);
  629. return 0;
  630. }
  631. /**
  632. * sas_init_disc -- initialize the discovery struct in the port
  633. * @port: pointer to struct port
  634. *
  635. * Called when the ports are being initialized.
  636. */
  637. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  638. {
  639. int i;
  640. static void (*sas_event_fns[DISC_NUM_EVENTS])(void *) = {
  641. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  642. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  643. };
  644. spin_lock_init(&disc->disc_event_lock);
  645. disc->pending = 0;
  646. for (i = 0; i < DISC_NUM_EVENTS; i++)
  647. INIT_WORK(&disc->disc_work[i], sas_event_fns[i], port);
  648. }