sas_discover.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. case SATA_DEV:
  234. rphy = sas_end_device_alloc(port->port);
  235. break;
  236. case EDGE_DEV:
  237. rphy = sas_expander_alloc(port->port,
  238. SAS_EDGE_EXPANDER_DEVICE);
  239. break;
  240. case FANOUT_DEV:
  241. rphy = sas_expander_alloc(port->port,
  242. SAS_FANOUT_EXPANDER_DEVICE);
  243. break;
  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.fis_type = 0x27;
  334. task->ata_task.fis.command = command;
  335. task->ata_task.fis.features = features;
  336. task->ata_task.fis.device = d2h_fis->device;
  337. task->ata_task.retry_count = 1;
  338. res = sas_execute_task(task, buffer, size, pci_dma_dir);
  339. sas_free_task(task);
  340. out:
  341. return res;
  342. }
  343. static void sas_sata_propagate_sas_addr(struct domain_device *dev)
  344. {
  345. unsigned long flags;
  346. struct asd_sas_port *port = dev->port;
  347. struct asd_sas_phy *phy;
  348. BUG_ON(dev->parent);
  349. memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  350. spin_lock_irqsave(&port->phy_list_lock, flags);
  351. list_for_each_entry(phy, &port->phy_list, port_phy_el)
  352. memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  353. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  354. }
  355. #define ATA_IDENTIFY_DEV 0xEC
  356. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  357. #define ATA_SET_FEATURES 0xEF
  358. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  359. /**
  360. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  361. * @dev: STP/SATA device of interest (ATA/ATAPI)
  362. *
  363. * The LLDD has already been notified of this device, so that we can
  364. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  365. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  366. * performance for this device.
  367. */
  368. static int sas_discover_sata_dev(struct domain_device *dev)
  369. {
  370. int res;
  371. __le16 *identify_x;
  372. u8 command;
  373. identify_x = kzalloc(512, GFP_KERNEL);
  374. if (!identify_x)
  375. return -ENOMEM;
  376. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  377. dev->sata_dev.identify_device = identify_x;
  378. command = ATA_IDENTIFY_DEV;
  379. } else {
  380. dev->sata_dev.identify_packet_device = identify_x;
  381. command = ATA_IDENTIFY_PACKET_DEV;
  382. }
  383. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  384. PCI_DMA_FROMDEVICE);
  385. if (res)
  386. goto out_err;
  387. /* lives on the media? */
  388. if (le16_to_cpu(identify_x[0]) & 4) {
  389. /* incomplete response */
  390. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  391. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  392. if (!le16_to_cpu(identify_x[83] & (1<<6)))
  393. goto cont1;
  394. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  395. ATA_FEATURE_PUP_STBY_SPIN_UP,
  396. NULL, 0, PCI_DMA_NONE);
  397. if (res)
  398. goto cont1;
  399. schedule_timeout_interruptible(5*HZ); /* More time? */
  400. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  401. PCI_DMA_FROMDEVICE);
  402. if (res)
  403. goto out_err;
  404. }
  405. cont1:
  406. /* Get WWN */
  407. if (dev->port->oob_mode != SATA_OOB_MODE) {
  408. memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
  409. SAS_ADDR_SIZE);
  410. } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
  411. (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
  412. == 0x5000) {
  413. int i;
  414. for (i = 0; i < 4; i++) {
  415. dev->sas_addr[2*i] =
  416. (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
  417. dev->sas_addr[2*i+1] =
  418. le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
  419. }
  420. }
  421. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  422. if (!dev->parent)
  423. sas_sata_propagate_sas_addr(dev);
  424. /* XXX Hint: register this SATA device with SATL.
  425. When this returns, dev->sata_dev->lu is alive and
  426. present.
  427. sas_satl_register_dev(dev);
  428. */
  429. sas_fill_in_rphy(dev, dev->rphy);
  430. return 0;
  431. out_err:
  432. dev->sata_dev.identify_packet_device = NULL;
  433. dev->sata_dev.identify_device = NULL;
  434. kfree(identify_x);
  435. return res;
  436. }
  437. static int sas_discover_sata_pm(struct domain_device *dev)
  438. {
  439. return -ENODEV;
  440. }
  441. int sas_notify_lldd_dev_found(struct domain_device *dev)
  442. {
  443. int res = 0;
  444. struct sas_ha_struct *sas_ha = dev->port->ha;
  445. struct Scsi_Host *shost = sas_ha->core.shost;
  446. struct sas_internal *i = to_sas_internal(shost->transportt);
  447. if (i->dft->lldd_dev_found) {
  448. res = i->dft->lldd_dev_found(dev);
  449. if (res) {
  450. printk("sas: driver on pcidev %s cannot handle "
  451. "device %llx, error:%d\n",
  452. pci_name(sas_ha->pcidev),
  453. SAS_ADDR(dev->sas_addr), res);
  454. }
  455. }
  456. return res;
  457. }
  458. void sas_notify_lldd_dev_gone(struct domain_device *dev)
  459. {
  460. struct sas_ha_struct *sas_ha = dev->port->ha;
  461. struct Scsi_Host *shost = sas_ha->core.shost;
  462. struct sas_internal *i = to_sas_internal(shost->transportt);
  463. if (i->dft->lldd_dev_gone)
  464. i->dft->lldd_dev_gone(dev);
  465. }
  466. /* ---------- Common/dispatchers ---------- */
  467. /**
  468. * sas_discover_sata -- discover an STP/SATA domain device
  469. * @dev: pointer to struct domain_device of interest
  470. *
  471. * First we notify the LLDD of this device, so we can send frames to
  472. * it. Then depending on the type of device we call the appropriate
  473. * discover functions. Once device discover is done, we notify the
  474. * LLDD so that it can fine-tune its parameters for the device, by
  475. * removing it and then adding it. That is, the second time around,
  476. * the driver would have certain fields, that it is looking at, set.
  477. * Finally we initialize the kobj so that the device can be added to
  478. * the system at registration time. Devices directly attached to a HA
  479. * port, have no parents. All other devices do, and should have their
  480. * "parent" pointer set appropriately before calling this function.
  481. */
  482. int sas_discover_sata(struct domain_device *dev)
  483. {
  484. int res;
  485. sas_get_ata_command_set(dev);
  486. res = sas_notify_lldd_dev_found(dev);
  487. if (res)
  488. return res;
  489. switch (dev->dev_type) {
  490. case SATA_DEV:
  491. res = sas_discover_sata_dev(dev);
  492. break;
  493. case SATA_PM:
  494. res = sas_discover_sata_pm(dev);
  495. break;
  496. default:
  497. break;
  498. }
  499. sas_notify_lldd_dev_gone(dev);
  500. if (!res) {
  501. sas_notify_lldd_dev_found(dev);
  502. res = sas_rphy_add(dev->rphy);
  503. }
  504. return res;
  505. }
  506. /**
  507. * sas_discover_end_dev -- discover an end device (SSP, etc)
  508. * @end: pointer to domain device of interest
  509. *
  510. * See comment in sas_discover_sata().
  511. */
  512. int sas_discover_end_dev(struct domain_device *dev)
  513. {
  514. int res;
  515. res = sas_notify_lldd_dev_found(dev);
  516. if (res)
  517. goto out_err2;
  518. res = sas_rphy_add(dev->rphy);
  519. if (res)
  520. goto out_err;
  521. return 0;
  522. out_err:
  523. sas_notify_lldd_dev_gone(dev);
  524. out_err2:
  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(struct work_struct *work)
  569. {
  570. struct domain_device *dev;
  571. int error = 0;
  572. struct sas_discovery_event *ev =
  573. container_of(work, struct sas_discovery_event, work);
  574. struct asd_sas_port *port = ev->port;
  575. sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
  576. &port->disc.pending);
  577. if (port->port_dev)
  578. return;
  579. error = sas_get_port_device(port);
  580. if (error)
  581. return;
  582. dev = port->port_dev;
  583. SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  584. current->pid);
  585. switch (dev->dev_type) {
  586. case SAS_END_DEV:
  587. error = sas_discover_end_dev(dev);
  588. break;
  589. case EDGE_DEV:
  590. case FANOUT_DEV:
  591. error = sas_discover_root_expander(dev);
  592. break;
  593. case SATA_DEV:
  594. case SATA_PM:
  595. error = sas_discover_sata(dev);
  596. break;
  597. default:
  598. SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
  599. break;
  600. }
  601. if (error) {
  602. sas_rphy_free(dev->rphy);
  603. dev->rphy = NULL;
  604. spin_lock(&port->dev_list_lock);
  605. list_del_init(&dev->dev_list_node);
  606. spin_unlock(&port->dev_list_lock);
  607. kfree(dev); /* not kobject_register-ed yet */
  608. port->port_dev = NULL;
  609. }
  610. SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  611. current->pid, error);
  612. }
  613. static void sas_revalidate_domain(struct work_struct *work)
  614. {
  615. int res = 0;
  616. struct sas_discovery_event *ev =
  617. container_of(work, struct sas_discovery_event, work);
  618. struct asd_sas_port *port = ev->port;
  619. sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
  620. &port->disc.pending);
  621. SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  622. current->pid);
  623. if (port->port_dev)
  624. res = sas_ex_revalidate_domain(port->port_dev);
  625. SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  626. port->id, current->pid, res);
  627. }
  628. /* ---------- Events ---------- */
  629. int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  630. {
  631. struct sas_discovery *disc;
  632. if (!port)
  633. return 0;
  634. disc = &port->disc;
  635. BUG_ON(ev >= DISC_NUM_EVENTS);
  636. sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
  637. &disc->disc_work[ev].work, port->ha);
  638. return 0;
  639. }
  640. /**
  641. * sas_init_disc -- initialize the discovery struct in the port
  642. * @port: pointer to struct port
  643. *
  644. * Called when the ports are being initialized.
  645. */
  646. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  647. {
  648. int i;
  649. static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
  650. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  651. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  652. };
  653. spin_lock_init(&disc->disc_event_lock);
  654. disc->pending = 0;
  655. for (i = 0; i < DISC_NUM_EVENTS; i++) {
  656. INIT_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
  657. disc->disc_work[i].port = port;
  658. }
  659. }