sas_expander.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. /*
  2. * Serial Attached SCSI (SAS) Expander discovery and configuration
  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/scatterlist.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/slab.h>
  27. #include "sas_internal.h"
  28. #include <scsi/sas_ata.h>
  29. #include <scsi/scsi_transport.h>
  30. #include <scsi/scsi_transport_sas.h>
  31. #include "../scsi_sas_internal.h"
  32. static int sas_discover_expander(struct domain_device *dev);
  33. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
  34. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  35. u8 *sas_addr, int include);
  36. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
  37. /* ---------- SMP task management ---------- */
  38. static void smp_task_timedout(unsigned long _task)
  39. {
  40. struct sas_task *task = (void *) _task;
  41. unsigned long flags;
  42. spin_lock_irqsave(&task->task_state_lock, flags);
  43. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  44. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  45. spin_unlock_irqrestore(&task->task_state_lock, flags);
  46. complete(&task->completion);
  47. }
  48. static void smp_task_done(struct sas_task *task)
  49. {
  50. if (!del_timer(&task->timer))
  51. return;
  52. complete(&task->completion);
  53. }
  54. /* Give it some long enough timeout. In seconds. */
  55. #define SMP_TIMEOUT 10
  56. static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
  57. void *resp, int resp_size)
  58. {
  59. int res, retry;
  60. struct sas_task *task = NULL;
  61. struct sas_internal *i =
  62. to_sas_internal(dev->port->ha->core.shost->transportt);
  63. mutex_lock(&dev->ex_dev.cmd_mutex);
  64. for (retry = 0; retry < 3; retry++) {
  65. if (test_bit(SAS_DEV_GONE, &dev->state)) {
  66. res = -ECOMM;
  67. break;
  68. }
  69. task = sas_alloc_task(GFP_KERNEL);
  70. if (!task) {
  71. res = -ENOMEM;
  72. break;
  73. }
  74. task->dev = dev;
  75. task->task_proto = dev->tproto;
  76. sg_init_one(&task->smp_task.smp_req, req, req_size);
  77. sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
  78. task->task_done = smp_task_done;
  79. task->timer.data = (unsigned long) task;
  80. task->timer.function = smp_task_timedout;
  81. task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
  82. add_timer(&task->timer);
  83. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  84. if (res) {
  85. del_timer(&task->timer);
  86. SAS_DPRINTK("executing SMP task failed:%d\n", res);
  87. break;
  88. }
  89. wait_for_completion(&task->completion);
  90. res = -ECOMM;
  91. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  92. SAS_DPRINTK("smp task timed out or aborted\n");
  93. i->dft->lldd_abort_task(task);
  94. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  95. SAS_DPRINTK("SMP task aborted and not done\n");
  96. break;
  97. }
  98. }
  99. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  100. task->task_status.stat == SAM_STAT_GOOD) {
  101. res = 0;
  102. break;
  103. }
  104. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  105. task->task_status.stat == SAS_DATA_UNDERRUN) {
  106. /* no error, but return the number of bytes of
  107. * underrun */
  108. res = task->task_status.residual;
  109. break;
  110. }
  111. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  112. task->task_status.stat == SAS_DATA_OVERRUN) {
  113. res = -EMSGSIZE;
  114. break;
  115. }
  116. if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
  117. task->task_status.stat == SAS_DEVICE_UNKNOWN)
  118. break;
  119. else {
  120. SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
  121. "status 0x%x\n", __func__,
  122. SAS_ADDR(dev->sas_addr),
  123. task->task_status.resp,
  124. task->task_status.stat);
  125. sas_free_task(task);
  126. task = NULL;
  127. }
  128. }
  129. mutex_unlock(&dev->ex_dev.cmd_mutex);
  130. BUG_ON(retry == 3 && task != NULL);
  131. sas_free_task(task);
  132. return res;
  133. }
  134. /* ---------- Allocations ---------- */
  135. static inline void *alloc_smp_req(int size)
  136. {
  137. u8 *p = kzalloc(size, GFP_KERNEL);
  138. if (p)
  139. p[0] = SMP_REQUEST;
  140. return p;
  141. }
  142. static inline void *alloc_smp_resp(int size)
  143. {
  144. return kzalloc(size, GFP_KERNEL);
  145. }
  146. static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
  147. {
  148. switch (phy->routing_attr) {
  149. case TABLE_ROUTING:
  150. if (dev->ex_dev.t2t_supp)
  151. return 'U';
  152. else
  153. return 'T';
  154. case DIRECT_ROUTING:
  155. return 'D';
  156. case SUBTRACTIVE_ROUTING:
  157. return 'S';
  158. default:
  159. return '?';
  160. }
  161. }
  162. static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
  163. void *disc_resp)
  164. {
  165. struct expander_device *ex = &dev->ex_dev;
  166. struct ex_phy *phy = &ex->ex_phy[phy_id];
  167. struct smp_resp *resp = disc_resp;
  168. struct discover_resp *dr = &resp->disc;
  169. struct sas_rphy *rphy = dev->rphy;
  170. bool new_phy = !phy->phy;
  171. char *type;
  172. if (new_phy) {
  173. phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
  174. /* FIXME: error_handling */
  175. BUG_ON(!phy->phy);
  176. }
  177. switch (resp->result) {
  178. case SMP_RESP_PHY_VACANT:
  179. phy->phy_state = PHY_VACANT;
  180. break;
  181. default:
  182. phy->phy_state = PHY_NOT_PRESENT;
  183. break;
  184. case SMP_RESP_FUNC_ACC:
  185. phy->phy_state = PHY_EMPTY; /* do not know yet */
  186. break;
  187. }
  188. phy->phy_id = phy_id;
  189. phy->attached_dev_type = dr->attached_dev_type;
  190. phy->linkrate = dr->linkrate;
  191. phy->attached_sata_host = dr->attached_sata_host;
  192. phy->attached_sata_dev = dr->attached_sata_dev;
  193. phy->attached_sata_ps = dr->attached_sata_ps;
  194. phy->attached_iproto = dr->iproto << 1;
  195. phy->attached_tproto = dr->tproto << 1;
  196. memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
  197. phy->attached_phy_id = dr->attached_phy_id;
  198. phy->phy_change_count = dr->change_count;
  199. phy->routing_attr = dr->routing_attr;
  200. phy->virtual = dr->virtual;
  201. phy->last_da_index = -1;
  202. phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
  203. phy->phy->identify.device_type = phy->attached_dev_type;
  204. phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
  205. phy->phy->identify.target_port_protocols = phy->attached_tproto;
  206. phy->phy->identify.phy_identifier = phy_id;
  207. phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
  208. phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
  209. phy->phy->minimum_linkrate = dr->pmin_linkrate;
  210. phy->phy->maximum_linkrate = dr->pmax_linkrate;
  211. phy->phy->negotiated_linkrate = phy->linkrate;
  212. if (new_phy)
  213. if (sas_phy_add(phy->phy)) {
  214. sas_phy_free(phy->phy);
  215. return;
  216. }
  217. switch (phy->attached_dev_type) {
  218. case NO_DEVICE:
  219. type = "no device";
  220. break;
  221. case SAS_END_DEV:
  222. if (phy->attached_iproto) {
  223. if (phy->attached_tproto)
  224. type = "host+target";
  225. else
  226. type = "host";
  227. } else {
  228. if (dr->attached_sata_dev)
  229. type = "stp";
  230. else
  231. type = "ssp";
  232. }
  233. break;
  234. case EDGE_DEV:
  235. case FANOUT_DEV:
  236. type = "smp";
  237. break;
  238. default:
  239. type = "unknown";
  240. }
  241. SAS_DPRINTK("ex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
  242. SAS_ADDR(dev->sas_addr), phy->phy_id,
  243. sas_route_char(dev, phy), phy->linkrate,
  244. SAS_ADDR(phy->attached_sas_addr), type);
  245. }
  246. /* check if we have an existing attached ata device on this expander phy */
  247. struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
  248. {
  249. struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
  250. struct domain_device *dev;
  251. struct sas_rphy *rphy;
  252. if (!ex_phy->port)
  253. return NULL;
  254. rphy = ex_phy->port->rphy;
  255. if (!rphy)
  256. return NULL;
  257. dev = sas_find_dev_by_rphy(rphy);
  258. if (dev && dev_is_sata(dev))
  259. return dev;
  260. return NULL;
  261. }
  262. #define DISCOVER_REQ_SIZE 16
  263. #define DISCOVER_RESP_SIZE 56
  264. static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
  265. u8 *disc_resp, int single)
  266. {
  267. struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
  268. int i, res;
  269. disc_req[9] = single;
  270. for (i = 1 ; i < 3; i++) {
  271. struct discover_resp *dr;
  272. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  273. disc_resp, DISCOVER_RESP_SIZE);
  274. if (res)
  275. return res;
  276. dr = &((struct smp_resp *)disc_resp)->disc;
  277. if (memcmp(dev->sas_addr, dr->attached_sas_addr,
  278. SAS_ADDR_SIZE) == 0) {
  279. sas_printk("Found loopback topology, just ignore it!\n");
  280. return 0;
  281. }
  282. /* This is detecting a failure to transmit initial
  283. * dev to host FIS as described in section J.5 of
  284. * sas-2 r16
  285. */
  286. if (!(dr->attached_dev_type == 0 &&
  287. dr->attached_sata_dev))
  288. break;
  289. /* In order to generate the dev to host FIS, we send a
  290. * link reset to the expander port. If a device was
  291. * previously detected on this port we ask libata to
  292. * manage the reset and link recovery.
  293. */
  294. if (ata_dev) {
  295. sas_ata_schedule_reset(ata_dev);
  296. break;
  297. }
  298. sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
  299. /* Wait for the reset to trigger the negotiation */
  300. msleep(500);
  301. }
  302. sas_set_ex_phy(dev, single, disc_resp);
  303. return 0;
  304. }
  305. static int sas_ex_phy_discover(struct domain_device *dev, int single)
  306. {
  307. struct expander_device *ex = &dev->ex_dev;
  308. int res = 0;
  309. u8 *disc_req;
  310. u8 *disc_resp;
  311. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  312. if (!disc_req)
  313. return -ENOMEM;
  314. disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
  315. if (!disc_resp) {
  316. kfree(disc_req);
  317. return -ENOMEM;
  318. }
  319. disc_req[1] = SMP_DISCOVER;
  320. if (0 <= single && single < ex->num_phys) {
  321. res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
  322. } else {
  323. int i;
  324. for (i = 0; i < ex->num_phys; i++) {
  325. res = sas_ex_phy_discover_helper(dev, disc_req,
  326. disc_resp, i);
  327. if (res)
  328. goto out_err;
  329. }
  330. }
  331. out_err:
  332. kfree(disc_resp);
  333. kfree(disc_req);
  334. return res;
  335. }
  336. static int sas_expander_discover(struct domain_device *dev)
  337. {
  338. struct expander_device *ex = &dev->ex_dev;
  339. int res = -ENOMEM;
  340. ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
  341. if (!ex->ex_phy)
  342. return -ENOMEM;
  343. res = sas_ex_phy_discover(dev, -1);
  344. if (res)
  345. goto out_err;
  346. return 0;
  347. out_err:
  348. kfree(ex->ex_phy);
  349. ex->ex_phy = NULL;
  350. return res;
  351. }
  352. #define MAX_EXPANDER_PHYS 128
  353. static void ex_assign_report_general(struct domain_device *dev,
  354. struct smp_resp *resp)
  355. {
  356. struct report_general_resp *rg = &resp->rg;
  357. dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
  358. dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
  359. dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
  360. dev->ex_dev.t2t_supp = rg->t2t_supp;
  361. dev->ex_dev.conf_route_table = rg->conf_route_table;
  362. dev->ex_dev.configuring = rg->configuring;
  363. memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
  364. }
  365. #define RG_REQ_SIZE 8
  366. #define RG_RESP_SIZE 32
  367. static int sas_ex_general(struct domain_device *dev)
  368. {
  369. u8 *rg_req;
  370. struct smp_resp *rg_resp;
  371. int res;
  372. int i;
  373. rg_req = alloc_smp_req(RG_REQ_SIZE);
  374. if (!rg_req)
  375. return -ENOMEM;
  376. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  377. if (!rg_resp) {
  378. kfree(rg_req);
  379. return -ENOMEM;
  380. }
  381. rg_req[1] = SMP_REPORT_GENERAL;
  382. for (i = 0; i < 5; i++) {
  383. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  384. RG_RESP_SIZE);
  385. if (res) {
  386. SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
  387. SAS_ADDR(dev->sas_addr), res);
  388. goto out;
  389. } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  390. SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
  391. SAS_ADDR(dev->sas_addr), rg_resp->result);
  392. res = rg_resp->result;
  393. goto out;
  394. }
  395. ex_assign_report_general(dev, rg_resp);
  396. if (dev->ex_dev.configuring) {
  397. SAS_DPRINTK("RG: ex %llx self-configuring...\n",
  398. SAS_ADDR(dev->sas_addr));
  399. schedule_timeout_interruptible(5*HZ);
  400. } else
  401. break;
  402. }
  403. out:
  404. kfree(rg_req);
  405. kfree(rg_resp);
  406. return res;
  407. }
  408. static void ex_assign_manuf_info(struct domain_device *dev, void
  409. *_mi_resp)
  410. {
  411. u8 *mi_resp = _mi_resp;
  412. struct sas_rphy *rphy = dev->rphy;
  413. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  414. memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
  415. memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
  416. memcpy(edev->product_rev, mi_resp + 36,
  417. SAS_EXPANDER_PRODUCT_REV_LEN);
  418. if (mi_resp[8] & 1) {
  419. memcpy(edev->component_vendor_id, mi_resp + 40,
  420. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  421. edev->component_id = mi_resp[48] << 8 | mi_resp[49];
  422. edev->component_revision_id = mi_resp[50];
  423. }
  424. }
  425. #define MI_REQ_SIZE 8
  426. #define MI_RESP_SIZE 64
  427. static int sas_ex_manuf_info(struct domain_device *dev)
  428. {
  429. u8 *mi_req;
  430. u8 *mi_resp;
  431. int res;
  432. mi_req = alloc_smp_req(MI_REQ_SIZE);
  433. if (!mi_req)
  434. return -ENOMEM;
  435. mi_resp = alloc_smp_resp(MI_RESP_SIZE);
  436. if (!mi_resp) {
  437. kfree(mi_req);
  438. return -ENOMEM;
  439. }
  440. mi_req[1] = SMP_REPORT_MANUF_INFO;
  441. res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
  442. if (res) {
  443. SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
  444. SAS_ADDR(dev->sas_addr), res);
  445. goto out;
  446. } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
  447. SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
  448. SAS_ADDR(dev->sas_addr), mi_resp[2]);
  449. goto out;
  450. }
  451. ex_assign_manuf_info(dev, mi_resp);
  452. out:
  453. kfree(mi_req);
  454. kfree(mi_resp);
  455. return res;
  456. }
  457. #define PC_REQ_SIZE 44
  458. #define PC_RESP_SIZE 8
  459. int sas_smp_phy_control(struct domain_device *dev, int phy_id,
  460. enum phy_func phy_func,
  461. struct sas_phy_linkrates *rates)
  462. {
  463. u8 *pc_req;
  464. u8 *pc_resp;
  465. int res;
  466. pc_req = alloc_smp_req(PC_REQ_SIZE);
  467. if (!pc_req)
  468. return -ENOMEM;
  469. pc_resp = alloc_smp_resp(PC_RESP_SIZE);
  470. if (!pc_resp) {
  471. kfree(pc_req);
  472. return -ENOMEM;
  473. }
  474. pc_req[1] = SMP_PHY_CONTROL;
  475. pc_req[9] = phy_id;
  476. pc_req[10]= phy_func;
  477. if (rates) {
  478. pc_req[32] = rates->minimum_linkrate << 4;
  479. pc_req[33] = rates->maximum_linkrate << 4;
  480. }
  481. res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
  482. kfree(pc_resp);
  483. kfree(pc_req);
  484. return res;
  485. }
  486. static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
  487. {
  488. struct expander_device *ex = &dev->ex_dev;
  489. struct ex_phy *phy = &ex->ex_phy[phy_id];
  490. sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
  491. phy->linkrate = SAS_PHY_DISABLED;
  492. }
  493. static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
  494. {
  495. struct expander_device *ex = &dev->ex_dev;
  496. int i;
  497. for (i = 0; i < ex->num_phys; i++) {
  498. struct ex_phy *phy = &ex->ex_phy[i];
  499. if (phy->phy_state == PHY_VACANT ||
  500. phy->phy_state == PHY_NOT_PRESENT)
  501. continue;
  502. if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
  503. sas_ex_disable_phy(dev, i);
  504. }
  505. }
  506. static int sas_dev_present_in_domain(struct asd_sas_port *port,
  507. u8 *sas_addr)
  508. {
  509. struct domain_device *dev;
  510. if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
  511. return 1;
  512. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  513. if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
  514. return 1;
  515. }
  516. return 0;
  517. }
  518. #define RPEL_REQ_SIZE 16
  519. #define RPEL_RESP_SIZE 32
  520. int sas_smp_get_phy_events(struct sas_phy *phy)
  521. {
  522. int res;
  523. u8 *req;
  524. u8 *resp;
  525. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  526. struct domain_device *dev = sas_find_dev_by_rphy(rphy);
  527. req = alloc_smp_req(RPEL_REQ_SIZE);
  528. if (!req)
  529. return -ENOMEM;
  530. resp = alloc_smp_resp(RPEL_RESP_SIZE);
  531. if (!resp) {
  532. kfree(req);
  533. return -ENOMEM;
  534. }
  535. req[1] = SMP_REPORT_PHY_ERR_LOG;
  536. req[9] = phy->number;
  537. res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
  538. resp, RPEL_RESP_SIZE);
  539. if (!res)
  540. goto out;
  541. phy->invalid_dword_count = scsi_to_u32(&resp[12]);
  542. phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
  543. phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
  544. phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
  545. out:
  546. kfree(resp);
  547. return res;
  548. }
  549. #ifdef CONFIG_SCSI_SAS_ATA
  550. #define RPS_REQ_SIZE 16
  551. #define RPS_RESP_SIZE 60
  552. static int sas_get_report_phy_sata(struct domain_device *dev,
  553. int phy_id,
  554. struct smp_resp *rps_resp)
  555. {
  556. int res;
  557. u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
  558. u8 *resp = (u8 *)rps_resp;
  559. if (!rps_req)
  560. return -ENOMEM;
  561. rps_req[1] = SMP_REPORT_PHY_SATA;
  562. rps_req[9] = phy_id;
  563. res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
  564. rps_resp, RPS_RESP_SIZE);
  565. /* 0x34 is the FIS type for the D2H fis. There's a potential
  566. * standards cockup here. sas-2 explicitly specifies the FIS
  567. * should be encoded so that FIS type is in resp[24].
  568. * However, some expanders endian reverse this. Undo the
  569. * reversal here */
  570. if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
  571. int i;
  572. for (i = 0; i < 5; i++) {
  573. int j = 24 + (i*4);
  574. u8 a, b;
  575. a = resp[j + 0];
  576. b = resp[j + 1];
  577. resp[j + 0] = resp[j + 3];
  578. resp[j + 1] = resp[j + 2];
  579. resp[j + 2] = b;
  580. resp[j + 3] = a;
  581. }
  582. }
  583. kfree(rps_req);
  584. return res;
  585. }
  586. #endif
  587. static void sas_ex_get_linkrate(struct domain_device *parent,
  588. struct domain_device *child,
  589. struct ex_phy *parent_phy)
  590. {
  591. struct expander_device *parent_ex = &parent->ex_dev;
  592. struct sas_port *port;
  593. int i;
  594. child->pathways = 0;
  595. port = parent_phy->port;
  596. for (i = 0; i < parent_ex->num_phys; i++) {
  597. struct ex_phy *phy = &parent_ex->ex_phy[i];
  598. if (phy->phy_state == PHY_VACANT ||
  599. phy->phy_state == PHY_NOT_PRESENT)
  600. continue;
  601. if (SAS_ADDR(phy->attached_sas_addr) ==
  602. SAS_ADDR(child->sas_addr)) {
  603. child->min_linkrate = min(parent->min_linkrate,
  604. phy->linkrate);
  605. child->max_linkrate = max(parent->max_linkrate,
  606. phy->linkrate);
  607. child->pathways++;
  608. sas_port_add_phy(port, phy->phy);
  609. }
  610. }
  611. child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
  612. child->pathways = min(child->pathways, parent->pathways);
  613. }
  614. static struct domain_device *sas_ex_discover_end_dev(
  615. struct domain_device *parent, int phy_id)
  616. {
  617. struct expander_device *parent_ex = &parent->ex_dev;
  618. struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
  619. struct domain_device *child = NULL;
  620. struct sas_rphy *rphy;
  621. int res;
  622. if (phy->attached_sata_host || phy->attached_sata_ps)
  623. return NULL;
  624. child = sas_alloc_device();
  625. if (!child)
  626. return NULL;
  627. kref_get(&parent->kref);
  628. child->parent = parent;
  629. child->port = parent->port;
  630. child->iproto = phy->attached_iproto;
  631. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  632. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  633. if (!phy->port) {
  634. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  635. if (unlikely(!phy->port))
  636. goto out_err;
  637. if (unlikely(sas_port_add(phy->port) != 0)) {
  638. sas_port_free(phy->port);
  639. goto out_err;
  640. }
  641. }
  642. sas_ex_get_linkrate(parent, child, phy);
  643. sas_device_set_phy(child, phy->port);
  644. #ifdef CONFIG_SCSI_SAS_ATA
  645. if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
  646. child->dev_type = SATA_DEV;
  647. if (phy->attached_tproto & SAS_PROTOCOL_STP)
  648. child->tproto = phy->attached_tproto;
  649. if (phy->attached_sata_dev)
  650. child->tproto |= SATA_DEV;
  651. res = sas_get_report_phy_sata(parent, phy_id,
  652. &child->sata_dev.rps_resp);
  653. if (res) {
  654. SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
  655. "0x%x\n", SAS_ADDR(parent->sas_addr),
  656. phy_id, res);
  657. goto out_free;
  658. }
  659. memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
  660. sizeof(struct dev_to_host_fis));
  661. rphy = sas_end_device_alloc(phy->port);
  662. if (unlikely(!rphy))
  663. goto out_free;
  664. sas_init_dev(child);
  665. child->rphy = rphy;
  666. list_add_tail(&child->disco_list_node, &parent->port->disco_list);
  667. res = sas_discover_sata(child);
  668. if (res) {
  669. SAS_DPRINTK("sas_discover_sata() for device %16llx at "
  670. "%016llx:0x%x returned 0x%x\n",
  671. SAS_ADDR(child->sas_addr),
  672. SAS_ADDR(parent->sas_addr), phy_id, res);
  673. goto out_list_del;
  674. }
  675. } else
  676. #endif
  677. if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
  678. child->dev_type = SAS_END_DEV;
  679. rphy = sas_end_device_alloc(phy->port);
  680. /* FIXME: error handling */
  681. if (unlikely(!rphy))
  682. goto out_free;
  683. child->tproto = phy->attached_tproto;
  684. sas_init_dev(child);
  685. child->rphy = rphy;
  686. sas_fill_in_rphy(child, rphy);
  687. spin_lock_irq(&parent->port->dev_list_lock);
  688. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  689. spin_unlock_irq(&parent->port->dev_list_lock);
  690. res = sas_discover_end_dev(child);
  691. if (res) {
  692. SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
  693. "at %016llx:0x%x returned 0x%x\n",
  694. SAS_ADDR(child->sas_addr),
  695. SAS_ADDR(parent->sas_addr), phy_id, res);
  696. goto out_list_del;
  697. }
  698. } else {
  699. SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
  700. phy->attached_tproto, SAS_ADDR(parent->sas_addr),
  701. phy_id);
  702. goto out_free;
  703. }
  704. list_add_tail(&child->siblings, &parent_ex->children);
  705. return child;
  706. out_list_del:
  707. sas_rphy_free(child->rphy);
  708. child->rphy = NULL;
  709. list_del(&child->disco_list_node);
  710. spin_lock_irq(&parent->port->dev_list_lock);
  711. list_del(&child->dev_list_node);
  712. spin_unlock_irq(&parent->port->dev_list_lock);
  713. out_free:
  714. sas_port_delete(phy->port);
  715. out_err:
  716. phy->port = NULL;
  717. sas_put_device(child);
  718. return NULL;
  719. }
  720. /* See if this phy is part of a wide port */
  721. static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
  722. {
  723. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  724. int i;
  725. for (i = 0; i < parent->ex_dev.num_phys; i++) {
  726. struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
  727. if (ephy == phy)
  728. continue;
  729. if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
  730. SAS_ADDR_SIZE) && ephy->port) {
  731. sas_port_add_phy(ephy->port, phy->phy);
  732. phy->port = ephy->port;
  733. phy->phy_state = PHY_DEVICE_DISCOVERED;
  734. return 0;
  735. }
  736. }
  737. return -ENODEV;
  738. }
  739. static struct domain_device *sas_ex_discover_expander(
  740. struct domain_device *parent, int phy_id)
  741. {
  742. struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
  743. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  744. struct domain_device *child = NULL;
  745. struct sas_rphy *rphy;
  746. struct sas_expander_device *edev;
  747. struct asd_sas_port *port;
  748. int res;
  749. if (phy->routing_attr == DIRECT_ROUTING) {
  750. SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
  751. "allowed\n",
  752. SAS_ADDR(parent->sas_addr), phy_id,
  753. SAS_ADDR(phy->attached_sas_addr),
  754. phy->attached_phy_id);
  755. return NULL;
  756. }
  757. child = sas_alloc_device();
  758. if (!child)
  759. return NULL;
  760. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  761. /* FIXME: better error handling */
  762. BUG_ON(sas_port_add(phy->port) != 0);
  763. switch (phy->attached_dev_type) {
  764. case EDGE_DEV:
  765. rphy = sas_expander_alloc(phy->port,
  766. SAS_EDGE_EXPANDER_DEVICE);
  767. break;
  768. case FANOUT_DEV:
  769. rphy = sas_expander_alloc(phy->port,
  770. SAS_FANOUT_EXPANDER_DEVICE);
  771. break;
  772. default:
  773. rphy = NULL; /* shut gcc up */
  774. BUG();
  775. }
  776. port = parent->port;
  777. child->rphy = rphy;
  778. edev = rphy_to_expander_device(rphy);
  779. child->dev_type = phy->attached_dev_type;
  780. kref_get(&parent->kref);
  781. child->parent = parent;
  782. child->port = port;
  783. child->iproto = phy->attached_iproto;
  784. child->tproto = phy->attached_tproto;
  785. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  786. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  787. sas_ex_get_linkrate(parent, child, phy);
  788. edev->level = parent_ex->level + 1;
  789. parent->port->disc.max_level = max(parent->port->disc.max_level,
  790. edev->level);
  791. sas_init_dev(child);
  792. sas_fill_in_rphy(child, rphy);
  793. sas_rphy_add(rphy);
  794. spin_lock_irq(&parent->port->dev_list_lock);
  795. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  796. spin_unlock_irq(&parent->port->dev_list_lock);
  797. res = sas_discover_expander(child);
  798. if (res) {
  799. spin_lock_irq(&parent->port->dev_list_lock);
  800. list_del(&child->dev_list_node);
  801. spin_unlock_irq(&parent->port->dev_list_lock);
  802. sas_put_device(child);
  803. return NULL;
  804. }
  805. list_add_tail(&child->siblings, &parent->ex_dev.children);
  806. return child;
  807. }
  808. static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
  809. {
  810. struct expander_device *ex = &dev->ex_dev;
  811. struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
  812. struct domain_device *child = NULL;
  813. int res = 0;
  814. /* Phy state */
  815. if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
  816. if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
  817. res = sas_ex_phy_discover(dev, phy_id);
  818. if (res)
  819. return res;
  820. }
  821. /* Parent and domain coherency */
  822. if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  823. SAS_ADDR(dev->port->sas_addr))) {
  824. sas_add_parent_port(dev, phy_id);
  825. return 0;
  826. }
  827. if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  828. SAS_ADDR(dev->parent->sas_addr))) {
  829. sas_add_parent_port(dev, phy_id);
  830. if (ex_phy->routing_attr == TABLE_ROUTING)
  831. sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
  832. return 0;
  833. }
  834. if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
  835. sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
  836. if (ex_phy->attached_dev_type == NO_DEVICE) {
  837. if (ex_phy->routing_attr == DIRECT_ROUTING) {
  838. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  839. sas_configure_routing(dev, ex_phy->attached_sas_addr);
  840. }
  841. return 0;
  842. } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
  843. return 0;
  844. if (ex_phy->attached_dev_type != SAS_END_DEV &&
  845. ex_phy->attached_dev_type != FANOUT_DEV &&
  846. ex_phy->attached_dev_type != EDGE_DEV) {
  847. SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
  848. "phy 0x%x\n", ex_phy->attached_dev_type,
  849. SAS_ADDR(dev->sas_addr),
  850. phy_id);
  851. return 0;
  852. }
  853. res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
  854. if (res) {
  855. SAS_DPRINTK("configure routing for dev %016llx "
  856. "reported 0x%x. Forgotten\n",
  857. SAS_ADDR(ex_phy->attached_sas_addr), res);
  858. sas_disable_routing(dev, ex_phy->attached_sas_addr);
  859. return res;
  860. }
  861. res = sas_ex_join_wide_port(dev, phy_id);
  862. if (!res) {
  863. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  864. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  865. return res;
  866. }
  867. switch (ex_phy->attached_dev_type) {
  868. case SAS_END_DEV:
  869. child = sas_ex_discover_end_dev(dev, phy_id);
  870. break;
  871. case FANOUT_DEV:
  872. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  873. SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
  874. "attached to ex %016llx phy 0x%x\n",
  875. SAS_ADDR(ex_phy->attached_sas_addr),
  876. ex_phy->attached_phy_id,
  877. SAS_ADDR(dev->sas_addr),
  878. phy_id);
  879. sas_ex_disable_phy(dev, phy_id);
  880. break;
  881. } else
  882. memcpy(dev->port->disc.fanout_sas_addr,
  883. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  884. /* fallthrough */
  885. case EDGE_DEV:
  886. child = sas_ex_discover_expander(dev, phy_id);
  887. break;
  888. default:
  889. break;
  890. }
  891. if (child) {
  892. int i;
  893. for (i = 0; i < ex->num_phys; i++) {
  894. if (ex->ex_phy[i].phy_state == PHY_VACANT ||
  895. ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
  896. continue;
  897. /*
  898. * Due to races, the phy might not get added to the
  899. * wide port, so we add the phy to the wide port here.
  900. */
  901. if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
  902. SAS_ADDR(child->sas_addr)) {
  903. ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
  904. res = sas_ex_join_wide_port(dev, i);
  905. if (!res)
  906. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  907. i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
  908. }
  909. }
  910. }
  911. return res;
  912. }
  913. static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
  914. {
  915. struct expander_device *ex = &dev->ex_dev;
  916. int i;
  917. for (i = 0; i < ex->num_phys; i++) {
  918. struct ex_phy *phy = &ex->ex_phy[i];
  919. if (phy->phy_state == PHY_VACANT ||
  920. phy->phy_state == PHY_NOT_PRESENT)
  921. continue;
  922. if ((phy->attached_dev_type == EDGE_DEV ||
  923. phy->attached_dev_type == FANOUT_DEV) &&
  924. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  925. memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
  926. return 1;
  927. }
  928. }
  929. return 0;
  930. }
  931. static int sas_check_level_subtractive_boundary(struct domain_device *dev)
  932. {
  933. struct expander_device *ex = &dev->ex_dev;
  934. struct domain_device *child;
  935. u8 sub_addr[8] = {0, };
  936. list_for_each_entry(child, &ex->children, siblings) {
  937. if (child->dev_type != EDGE_DEV &&
  938. child->dev_type != FANOUT_DEV)
  939. continue;
  940. if (sub_addr[0] == 0) {
  941. sas_find_sub_addr(child, sub_addr);
  942. continue;
  943. } else {
  944. u8 s2[8];
  945. if (sas_find_sub_addr(child, s2) &&
  946. (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
  947. SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
  948. "diverges from subtractive "
  949. "boundary %016llx\n",
  950. SAS_ADDR(dev->sas_addr),
  951. SAS_ADDR(child->sas_addr),
  952. SAS_ADDR(s2),
  953. SAS_ADDR(sub_addr));
  954. sas_ex_disable_port(child, s2);
  955. }
  956. }
  957. }
  958. return 0;
  959. }
  960. /**
  961. * sas_ex_discover_devices -- discover devices attached to this expander
  962. * dev: pointer to the expander domain device
  963. * single: if you want to do a single phy, else set to -1;
  964. *
  965. * Configure this expander for use with its devices and register the
  966. * devices of this expander.
  967. */
  968. static int sas_ex_discover_devices(struct domain_device *dev, int single)
  969. {
  970. struct expander_device *ex = &dev->ex_dev;
  971. int i = 0, end = ex->num_phys;
  972. int res = 0;
  973. if (0 <= single && single < end) {
  974. i = single;
  975. end = i+1;
  976. }
  977. for ( ; i < end; i++) {
  978. struct ex_phy *ex_phy = &ex->ex_phy[i];
  979. if (ex_phy->phy_state == PHY_VACANT ||
  980. ex_phy->phy_state == PHY_NOT_PRESENT ||
  981. ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
  982. continue;
  983. switch (ex_phy->linkrate) {
  984. case SAS_PHY_DISABLED:
  985. case SAS_PHY_RESET_PROBLEM:
  986. case SAS_SATA_PORT_SELECTOR:
  987. continue;
  988. default:
  989. res = sas_ex_discover_dev(dev, i);
  990. if (res)
  991. break;
  992. continue;
  993. }
  994. }
  995. if (!res)
  996. sas_check_level_subtractive_boundary(dev);
  997. return res;
  998. }
  999. static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
  1000. {
  1001. struct expander_device *ex = &dev->ex_dev;
  1002. int i;
  1003. u8 *sub_sas_addr = NULL;
  1004. if (dev->dev_type != EDGE_DEV)
  1005. return 0;
  1006. for (i = 0; i < ex->num_phys; i++) {
  1007. struct ex_phy *phy = &ex->ex_phy[i];
  1008. if (phy->phy_state == PHY_VACANT ||
  1009. phy->phy_state == PHY_NOT_PRESENT)
  1010. continue;
  1011. if ((phy->attached_dev_type == FANOUT_DEV ||
  1012. phy->attached_dev_type == EDGE_DEV) &&
  1013. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1014. if (!sub_sas_addr)
  1015. sub_sas_addr = &phy->attached_sas_addr[0];
  1016. else if (SAS_ADDR(sub_sas_addr) !=
  1017. SAS_ADDR(phy->attached_sas_addr)) {
  1018. SAS_DPRINTK("ex %016llx phy 0x%x "
  1019. "diverges(%016llx) on subtractive "
  1020. "boundary(%016llx). Disabled\n",
  1021. SAS_ADDR(dev->sas_addr), i,
  1022. SAS_ADDR(phy->attached_sas_addr),
  1023. SAS_ADDR(sub_sas_addr));
  1024. sas_ex_disable_phy(dev, i);
  1025. }
  1026. }
  1027. }
  1028. return 0;
  1029. }
  1030. static void sas_print_parent_topology_bug(struct domain_device *child,
  1031. struct ex_phy *parent_phy,
  1032. struct ex_phy *child_phy)
  1033. {
  1034. static const char *ex_type[] = {
  1035. [EDGE_DEV] = "edge",
  1036. [FANOUT_DEV] = "fanout",
  1037. };
  1038. struct domain_device *parent = child->parent;
  1039. sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
  1040. "phy 0x%x has %c:%c routing link!\n",
  1041. ex_type[parent->dev_type],
  1042. SAS_ADDR(parent->sas_addr),
  1043. parent_phy->phy_id,
  1044. ex_type[child->dev_type],
  1045. SAS_ADDR(child->sas_addr),
  1046. child_phy->phy_id,
  1047. sas_route_char(parent, parent_phy),
  1048. sas_route_char(child, child_phy));
  1049. }
  1050. static int sas_check_eeds(struct domain_device *child,
  1051. struct ex_phy *parent_phy,
  1052. struct ex_phy *child_phy)
  1053. {
  1054. int res = 0;
  1055. struct domain_device *parent = child->parent;
  1056. if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
  1057. res = -ENODEV;
  1058. SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
  1059. "phy S:0x%x, while there is a fanout ex %016llx\n",
  1060. SAS_ADDR(parent->sas_addr),
  1061. parent_phy->phy_id,
  1062. SAS_ADDR(child->sas_addr),
  1063. child_phy->phy_id,
  1064. SAS_ADDR(parent->port->disc.fanout_sas_addr));
  1065. } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
  1066. memcpy(parent->port->disc.eeds_a, parent->sas_addr,
  1067. SAS_ADDR_SIZE);
  1068. memcpy(parent->port->disc.eeds_b, child->sas_addr,
  1069. SAS_ADDR_SIZE);
  1070. } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
  1071. SAS_ADDR(parent->sas_addr)) ||
  1072. (SAS_ADDR(parent->port->disc.eeds_a) ==
  1073. SAS_ADDR(child->sas_addr)))
  1074. &&
  1075. ((SAS_ADDR(parent->port->disc.eeds_b) ==
  1076. SAS_ADDR(parent->sas_addr)) ||
  1077. (SAS_ADDR(parent->port->disc.eeds_b) ==
  1078. SAS_ADDR(child->sas_addr))))
  1079. ;
  1080. else {
  1081. res = -ENODEV;
  1082. SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
  1083. "phy 0x%x link forms a third EEDS!\n",
  1084. SAS_ADDR(parent->sas_addr),
  1085. parent_phy->phy_id,
  1086. SAS_ADDR(child->sas_addr),
  1087. child_phy->phy_id);
  1088. }
  1089. return res;
  1090. }
  1091. /* Here we spill over 80 columns. It is intentional.
  1092. */
  1093. static int sas_check_parent_topology(struct domain_device *child)
  1094. {
  1095. struct expander_device *child_ex = &child->ex_dev;
  1096. struct expander_device *parent_ex;
  1097. int i;
  1098. int res = 0;
  1099. if (!child->parent)
  1100. return 0;
  1101. if (child->parent->dev_type != EDGE_DEV &&
  1102. child->parent->dev_type != FANOUT_DEV)
  1103. return 0;
  1104. parent_ex = &child->parent->ex_dev;
  1105. for (i = 0; i < parent_ex->num_phys; i++) {
  1106. struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
  1107. struct ex_phy *child_phy;
  1108. if (parent_phy->phy_state == PHY_VACANT ||
  1109. parent_phy->phy_state == PHY_NOT_PRESENT)
  1110. continue;
  1111. if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
  1112. continue;
  1113. child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
  1114. switch (child->parent->dev_type) {
  1115. case EDGE_DEV:
  1116. if (child->dev_type == FANOUT_DEV) {
  1117. if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
  1118. child_phy->routing_attr != TABLE_ROUTING) {
  1119. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1120. res = -ENODEV;
  1121. }
  1122. } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1123. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1124. res = sas_check_eeds(child, parent_phy, child_phy);
  1125. } else if (child_phy->routing_attr != TABLE_ROUTING) {
  1126. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1127. res = -ENODEV;
  1128. }
  1129. } else if (parent_phy->routing_attr == TABLE_ROUTING) {
  1130. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
  1131. (child_phy->routing_attr == TABLE_ROUTING &&
  1132. child_ex->t2t_supp && parent_ex->t2t_supp)) {
  1133. /* All good */;
  1134. } else {
  1135. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1136. res = -ENODEV;
  1137. }
  1138. }
  1139. break;
  1140. case FANOUT_DEV:
  1141. if (parent_phy->routing_attr != TABLE_ROUTING ||
  1142. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1143. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1144. res = -ENODEV;
  1145. }
  1146. break;
  1147. default:
  1148. break;
  1149. }
  1150. }
  1151. return res;
  1152. }
  1153. #define RRI_REQ_SIZE 16
  1154. #define RRI_RESP_SIZE 44
  1155. static int sas_configure_present(struct domain_device *dev, int phy_id,
  1156. u8 *sas_addr, int *index, int *present)
  1157. {
  1158. int i, res = 0;
  1159. struct expander_device *ex = &dev->ex_dev;
  1160. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1161. u8 *rri_req;
  1162. u8 *rri_resp;
  1163. *present = 0;
  1164. *index = 0;
  1165. rri_req = alloc_smp_req(RRI_REQ_SIZE);
  1166. if (!rri_req)
  1167. return -ENOMEM;
  1168. rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
  1169. if (!rri_resp) {
  1170. kfree(rri_req);
  1171. return -ENOMEM;
  1172. }
  1173. rri_req[1] = SMP_REPORT_ROUTE_INFO;
  1174. rri_req[9] = phy_id;
  1175. for (i = 0; i < ex->max_route_indexes ; i++) {
  1176. *(__be16 *)(rri_req+6) = cpu_to_be16(i);
  1177. res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
  1178. RRI_RESP_SIZE);
  1179. if (res)
  1180. goto out;
  1181. res = rri_resp[2];
  1182. if (res == SMP_RESP_NO_INDEX) {
  1183. SAS_DPRINTK("overflow of indexes: dev %016llx "
  1184. "phy 0x%x index 0x%x\n",
  1185. SAS_ADDR(dev->sas_addr), phy_id, i);
  1186. goto out;
  1187. } else if (res != SMP_RESP_FUNC_ACC) {
  1188. SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
  1189. "result 0x%x\n", __func__,
  1190. SAS_ADDR(dev->sas_addr), phy_id, i, res);
  1191. goto out;
  1192. }
  1193. if (SAS_ADDR(sas_addr) != 0) {
  1194. if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
  1195. *index = i;
  1196. if ((rri_resp[12] & 0x80) == 0x80)
  1197. *present = 0;
  1198. else
  1199. *present = 1;
  1200. goto out;
  1201. } else if (SAS_ADDR(rri_resp+16) == 0) {
  1202. *index = i;
  1203. *present = 0;
  1204. goto out;
  1205. }
  1206. } else if (SAS_ADDR(rri_resp+16) == 0 &&
  1207. phy->last_da_index < i) {
  1208. phy->last_da_index = i;
  1209. *index = i;
  1210. *present = 0;
  1211. goto out;
  1212. }
  1213. }
  1214. res = -1;
  1215. out:
  1216. kfree(rri_req);
  1217. kfree(rri_resp);
  1218. return res;
  1219. }
  1220. #define CRI_REQ_SIZE 44
  1221. #define CRI_RESP_SIZE 8
  1222. static int sas_configure_set(struct domain_device *dev, int phy_id,
  1223. u8 *sas_addr, int index, int include)
  1224. {
  1225. int res;
  1226. u8 *cri_req;
  1227. u8 *cri_resp;
  1228. cri_req = alloc_smp_req(CRI_REQ_SIZE);
  1229. if (!cri_req)
  1230. return -ENOMEM;
  1231. cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
  1232. if (!cri_resp) {
  1233. kfree(cri_req);
  1234. return -ENOMEM;
  1235. }
  1236. cri_req[1] = SMP_CONF_ROUTE_INFO;
  1237. *(__be16 *)(cri_req+6) = cpu_to_be16(index);
  1238. cri_req[9] = phy_id;
  1239. if (SAS_ADDR(sas_addr) == 0 || !include)
  1240. cri_req[12] |= 0x80;
  1241. memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
  1242. res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
  1243. CRI_RESP_SIZE);
  1244. if (res)
  1245. goto out;
  1246. res = cri_resp[2];
  1247. if (res == SMP_RESP_NO_INDEX) {
  1248. SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
  1249. "index 0x%x\n",
  1250. SAS_ADDR(dev->sas_addr), phy_id, index);
  1251. }
  1252. out:
  1253. kfree(cri_req);
  1254. kfree(cri_resp);
  1255. return res;
  1256. }
  1257. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  1258. u8 *sas_addr, int include)
  1259. {
  1260. int index;
  1261. int present;
  1262. int res;
  1263. res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
  1264. if (res)
  1265. return res;
  1266. if (include ^ present)
  1267. return sas_configure_set(dev, phy_id, sas_addr, index,include);
  1268. return res;
  1269. }
  1270. /**
  1271. * sas_configure_parent -- configure routing table of parent
  1272. * parent: parent expander
  1273. * child: child expander
  1274. * sas_addr: SAS port identifier of device directly attached to child
  1275. */
  1276. static int sas_configure_parent(struct domain_device *parent,
  1277. struct domain_device *child,
  1278. u8 *sas_addr, int include)
  1279. {
  1280. struct expander_device *ex_parent = &parent->ex_dev;
  1281. int res = 0;
  1282. int i;
  1283. if (parent->parent) {
  1284. res = sas_configure_parent(parent->parent, parent, sas_addr,
  1285. include);
  1286. if (res)
  1287. return res;
  1288. }
  1289. if (ex_parent->conf_route_table == 0) {
  1290. SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
  1291. SAS_ADDR(parent->sas_addr));
  1292. return 0;
  1293. }
  1294. for (i = 0; i < ex_parent->num_phys; i++) {
  1295. struct ex_phy *phy = &ex_parent->ex_phy[i];
  1296. if ((phy->routing_attr == TABLE_ROUTING) &&
  1297. (SAS_ADDR(phy->attached_sas_addr) ==
  1298. SAS_ADDR(child->sas_addr))) {
  1299. res = sas_configure_phy(parent, i, sas_addr, include);
  1300. if (res)
  1301. return res;
  1302. }
  1303. }
  1304. return res;
  1305. }
  1306. /**
  1307. * sas_configure_routing -- configure routing
  1308. * dev: expander device
  1309. * sas_addr: port identifier of device directly attached to the expander device
  1310. */
  1311. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
  1312. {
  1313. if (dev->parent)
  1314. return sas_configure_parent(dev->parent, dev, sas_addr, 1);
  1315. return 0;
  1316. }
  1317. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
  1318. {
  1319. if (dev->parent)
  1320. return sas_configure_parent(dev->parent, dev, sas_addr, 0);
  1321. return 0;
  1322. }
  1323. /**
  1324. * sas_discover_expander -- expander discovery
  1325. * @ex: pointer to expander domain device
  1326. *
  1327. * See comment in sas_discover_sata().
  1328. */
  1329. static int sas_discover_expander(struct domain_device *dev)
  1330. {
  1331. int res;
  1332. res = sas_notify_lldd_dev_found(dev);
  1333. if (res)
  1334. return res;
  1335. res = sas_ex_general(dev);
  1336. if (res)
  1337. goto out_err;
  1338. res = sas_ex_manuf_info(dev);
  1339. if (res)
  1340. goto out_err;
  1341. res = sas_expander_discover(dev);
  1342. if (res) {
  1343. SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
  1344. SAS_ADDR(dev->sas_addr), res);
  1345. goto out_err;
  1346. }
  1347. sas_check_ex_subtractive_boundary(dev);
  1348. res = sas_check_parent_topology(dev);
  1349. if (res)
  1350. goto out_err;
  1351. return 0;
  1352. out_err:
  1353. sas_notify_lldd_dev_gone(dev);
  1354. return res;
  1355. }
  1356. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1357. {
  1358. int res = 0;
  1359. struct domain_device *dev;
  1360. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1361. if (dev->dev_type == EDGE_DEV ||
  1362. dev->dev_type == FANOUT_DEV) {
  1363. struct sas_expander_device *ex =
  1364. rphy_to_expander_device(dev->rphy);
  1365. if (level == ex->level)
  1366. res = sas_ex_discover_devices(dev, -1);
  1367. else if (level > 0)
  1368. res = sas_ex_discover_devices(port->port_dev, -1);
  1369. }
  1370. }
  1371. return res;
  1372. }
  1373. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1374. {
  1375. int res;
  1376. int level;
  1377. do {
  1378. level = port->disc.max_level;
  1379. res = sas_ex_level_discovery(port, level);
  1380. mb();
  1381. } while (level < port->disc.max_level);
  1382. return res;
  1383. }
  1384. int sas_discover_root_expander(struct domain_device *dev)
  1385. {
  1386. int res;
  1387. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1388. res = sas_rphy_add(dev->rphy);
  1389. if (res)
  1390. goto out_err;
  1391. ex->level = dev->port->disc.max_level; /* 0 */
  1392. res = sas_discover_expander(dev);
  1393. if (res)
  1394. goto out_err2;
  1395. sas_ex_bfs_disc(dev->port);
  1396. return res;
  1397. out_err2:
  1398. sas_rphy_remove(dev->rphy);
  1399. out_err:
  1400. return res;
  1401. }
  1402. /* ---------- Domain revalidation ---------- */
  1403. static int sas_get_phy_discover(struct domain_device *dev,
  1404. int phy_id, struct smp_resp *disc_resp)
  1405. {
  1406. int res;
  1407. u8 *disc_req;
  1408. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1409. if (!disc_req)
  1410. return -ENOMEM;
  1411. disc_req[1] = SMP_DISCOVER;
  1412. disc_req[9] = phy_id;
  1413. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1414. disc_resp, DISCOVER_RESP_SIZE);
  1415. if (res)
  1416. goto out;
  1417. else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
  1418. res = disc_resp->result;
  1419. goto out;
  1420. }
  1421. out:
  1422. kfree(disc_req);
  1423. return res;
  1424. }
  1425. static int sas_get_phy_change_count(struct domain_device *dev,
  1426. int phy_id, int *pcc)
  1427. {
  1428. int res;
  1429. struct smp_resp *disc_resp;
  1430. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1431. if (!disc_resp)
  1432. return -ENOMEM;
  1433. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1434. if (!res)
  1435. *pcc = disc_resp->disc.change_count;
  1436. kfree(disc_resp);
  1437. return res;
  1438. }
  1439. int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
  1440. u8 *attached_sas_addr)
  1441. {
  1442. int res;
  1443. struct smp_resp *disc_resp;
  1444. struct discover_resp *dr;
  1445. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1446. if (!disc_resp)
  1447. return -ENOMEM;
  1448. dr = &disc_resp->disc;
  1449. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1450. if (!res) {
  1451. memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
  1452. if (dr->attached_dev_type == 0)
  1453. memset(attached_sas_addr, 0, 8);
  1454. }
  1455. kfree(disc_resp);
  1456. return res;
  1457. }
  1458. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1459. int from_phy, bool update)
  1460. {
  1461. struct expander_device *ex = &dev->ex_dev;
  1462. int res = 0;
  1463. int i;
  1464. for (i = from_phy; i < ex->num_phys; i++) {
  1465. int phy_change_count = 0;
  1466. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1467. if (res)
  1468. goto out;
  1469. else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1470. if (update)
  1471. ex->ex_phy[i].phy_change_count =
  1472. phy_change_count;
  1473. *phy_id = i;
  1474. return 0;
  1475. }
  1476. }
  1477. out:
  1478. return res;
  1479. }
  1480. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1481. {
  1482. int res;
  1483. u8 *rg_req;
  1484. struct smp_resp *rg_resp;
  1485. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1486. if (!rg_req)
  1487. return -ENOMEM;
  1488. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1489. if (!rg_resp) {
  1490. kfree(rg_req);
  1491. return -ENOMEM;
  1492. }
  1493. rg_req[1] = SMP_REPORT_GENERAL;
  1494. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1495. RG_RESP_SIZE);
  1496. if (res)
  1497. goto out;
  1498. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1499. res = rg_resp->result;
  1500. goto out;
  1501. }
  1502. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1503. out:
  1504. kfree(rg_resp);
  1505. kfree(rg_req);
  1506. return res;
  1507. }
  1508. /**
  1509. * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
  1510. * @dev:domain device to be detect.
  1511. * @src_dev: the device which originated BROADCAST(CHANGE).
  1512. *
  1513. * Add self-configuration expander suport. Suppose two expander cascading,
  1514. * when the first level expander is self-configuring, hotplug the disks in
  1515. * second level expander, BROADCAST(CHANGE) will not only be originated
  1516. * in the second level expander, but also be originated in the first level
  1517. * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
  1518. * expander changed count in two level expanders will all increment at least
  1519. * once, but the phy which chang count has changed is the source device which
  1520. * we concerned.
  1521. */
  1522. static int sas_find_bcast_dev(struct domain_device *dev,
  1523. struct domain_device **src_dev)
  1524. {
  1525. struct expander_device *ex = &dev->ex_dev;
  1526. int ex_change_count = -1;
  1527. int phy_id = -1;
  1528. int res;
  1529. struct domain_device *ch;
  1530. res = sas_get_ex_change_count(dev, &ex_change_count);
  1531. if (res)
  1532. goto out;
  1533. if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
  1534. /* Just detect if this expander phys phy change count changed,
  1535. * in order to determine if this expander originate BROADCAST,
  1536. * and do not update phy change count field in our structure.
  1537. */
  1538. res = sas_find_bcast_phy(dev, &phy_id, 0, false);
  1539. if (phy_id != -1) {
  1540. *src_dev = dev;
  1541. ex->ex_change_count = ex_change_count;
  1542. SAS_DPRINTK("Expander phy change count has changed\n");
  1543. return res;
  1544. } else
  1545. SAS_DPRINTK("Expander phys DID NOT change\n");
  1546. }
  1547. list_for_each_entry(ch, &ex->children, siblings) {
  1548. if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
  1549. res = sas_find_bcast_dev(ch, src_dev);
  1550. if (*src_dev)
  1551. return res;
  1552. }
  1553. }
  1554. out:
  1555. return res;
  1556. }
  1557. static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
  1558. {
  1559. struct expander_device *ex = &dev->ex_dev;
  1560. struct domain_device *child, *n;
  1561. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1562. set_bit(SAS_DEV_GONE, &child->state);
  1563. if (child->dev_type == EDGE_DEV ||
  1564. child->dev_type == FANOUT_DEV)
  1565. sas_unregister_ex_tree(port, child);
  1566. else
  1567. sas_unregister_dev(port, child);
  1568. }
  1569. sas_unregister_dev(port, dev);
  1570. }
  1571. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1572. int phy_id, bool last)
  1573. {
  1574. struct expander_device *ex_dev = &parent->ex_dev;
  1575. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1576. struct domain_device *child, *n, *found = NULL;
  1577. if (last) {
  1578. list_for_each_entry_safe(child, n,
  1579. &ex_dev->children, siblings) {
  1580. if (SAS_ADDR(child->sas_addr) ==
  1581. SAS_ADDR(phy->attached_sas_addr)) {
  1582. set_bit(SAS_DEV_GONE, &child->state);
  1583. if (child->dev_type == EDGE_DEV ||
  1584. child->dev_type == FANOUT_DEV)
  1585. sas_unregister_ex_tree(parent->port, child);
  1586. else
  1587. sas_unregister_dev(parent->port, child);
  1588. found = child;
  1589. break;
  1590. }
  1591. }
  1592. sas_disable_routing(parent, phy->attached_sas_addr);
  1593. }
  1594. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1595. if (phy->port) {
  1596. sas_port_delete_phy(phy->port, phy->phy);
  1597. sas_device_set_phy(found, phy->port);
  1598. if (phy->port->num_phys == 0)
  1599. sas_port_delete(phy->port);
  1600. phy->port = NULL;
  1601. }
  1602. }
  1603. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1604. const int level)
  1605. {
  1606. struct expander_device *ex_root = &root->ex_dev;
  1607. struct domain_device *child;
  1608. int res = 0;
  1609. list_for_each_entry(child, &ex_root->children, siblings) {
  1610. if (child->dev_type == EDGE_DEV ||
  1611. child->dev_type == FANOUT_DEV) {
  1612. struct sas_expander_device *ex =
  1613. rphy_to_expander_device(child->rphy);
  1614. if (level > ex->level)
  1615. res = sas_discover_bfs_by_root_level(child,
  1616. level);
  1617. else if (level == ex->level)
  1618. res = sas_ex_discover_devices(child, -1);
  1619. }
  1620. }
  1621. return res;
  1622. }
  1623. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1624. {
  1625. int res;
  1626. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1627. int level = ex->level+1;
  1628. res = sas_ex_discover_devices(dev, -1);
  1629. if (res)
  1630. goto out;
  1631. do {
  1632. res = sas_discover_bfs_by_root_level(dev, level);
  1633. mb();
  1634. level += 1;
  1635. } while (level <= dev->port->disc.max_level);
  1636. out:
  1637. return res;
  1638. }
  1639. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1640. {
  1641. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1642. struct domain_device *child;
  1643. bool found = false;
  1644. int res, i;
  1645. SAS_DPRINTK("ex %016llx phy%d new device attached\n",
  1646. SAS_ADDR(dev->sas_addr), phy_id);
  1647. res = sas_ex_phy_discover(dev, phy_id);
  1648. if (res)
  1649. goto out;
  1650. /* to support the wide port inserted */
  1651. for (i = 0; i < dev->ex_dev.num_phys; i++) {
  1652. struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
  1653. if (i == phy_id)
  1654. continue;
  1655. if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
  1656. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1657. found = true;
  1658. break;
  1659. }
  1660. }
  1661. if (found) {
  1662. sas_ex_join_wide_port(dev, phy_id);
  1663. return 0;
  1664. }
  1665. res = sas_ex_discover_devices(dev, phy_id);
  1666. if (!res)
  1667. goto out;
  1668. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1669. if (SAS_ADDR(child->sas_addr) ==
  1670. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1671. if (child->dev_type == EDGE_DEV ||
  1672. child->dev_type == FANOUT_DEV)
  1673. res = sas_discover_bfs_by_root(child);
  1674. break;
  1675. }
  1676. }
  1677. out:
  1678. return res;
  1679. }
  1680. static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
  1681. {
  1682. struct expander_device *ex = &dev->ex_dev;
  1683. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1684. u8 attached_sas_addr[8];
  1685. int res;
  1686. res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
  1687. switch (res) {
  1688. case SMP_RESP_NO_PHY:
  1689. phy->phy_state = PHY_NOT_PRESENT;
  1690. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1691. goto out; break;
  1692. case SMP_RESP_PHY_VACANT:
  1693. phy->phy_state = PHY_VACANT;
  1694. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1695. goto out; break;
  1696. case SMP_RESP_FUNC_ACC:
  1697. break;
  1698. }
  1699. if (SAS_ADDR(attached_sas_addr) == 0) {
  1700. phy->phy_state = PHY_EMPTY;
  1701. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1702. } else if (SAS_ADDR(attached_sas_addr) ==
  1703. SAS_ADDR(phy->attached_sas_addr)) {
  1704. SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
  1705. SAS_ADDR(dev->sas_addr), phy_id);
  1706. sas_ex_phy_discover(dev, phy_id);
  1707. } else
  1708. res = sas_discover_new(dev, phy_id);
  1709. out:
  1710. return res;
  1711. }
  1712. /**
  1713. * sas_rediscover - revalidate the domain.
  1714. * @dev:domain device to be detect.
  1715. * @phy_id: the phy id will be detected.
  1716. *
  1717. * NOTE: this process _must_ quit (return) as soon as any connection
  1718. * errors are encountered. Connection recovery is done elsewhere.
  1719. * Discover process only interrogates devices in order to discover the
  1720. * domain.For plugging out, we un-register the device only when it is
  1721. * the last phy in the port, for other phys in this port, we just delete it
  1722. * from the port.For inserting, we do discovery when it is the
  1723. * first phy,for other phys in this port, we add it to the port to
  1724. * forming the wide-port.
  1725. */
  1726. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1727. {
  1728. struct expander_device *ex = &dev->ex_dev;
  1729. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1730. int res = 0;
  1731. int i;
  1732. bool last = true; /* is this the last phy of the port */
  1733. SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
  1734. SAS_ADDR(dev->sas_addr), phy_id);
  1735. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1736. for (i = 0; i < ex->num_phys; i++) {
  1737. struct ex_phy *phy = &ex->ex_phy[i];
  1738. if (i == phy_id)
  1739. continue;
  1740. if (SAS_ADDR(phy->attached_sas_addr) ==
  1741. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1742. SAS_DPRINTK("phy%d part of wide port with "
  1743. "phy%d\n", phy_id, i);
  1744. last = false;
  1745. break;
  1746. }
  1747. }
  1748. res = sas_rediscover_dev(dev, phy_id, last);
  1749. } else
  1750. res = sas_discover_new(dev, phy_id);
  1751. return res;
  1752. }
  1753. /**
  1754. * sas_revalidate_domain -- revalidate the domain
  1755. * @port: port to the domain of interest
  1756. *
  1757. * NOTE: this process _must_ quit (return) as soon as any connection
  1758. * errors are encountered. Connection recovery is done elsewhere.
  1759. * Discover process only interrogates devices in order to discover the
  1760. * domain.
  1761. */
  1762. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1763. {
  1764. int res;
  1765. struct domain_device *dev = NULL;
  1766. res = sas_find_bcast_dev(port_dev, &dev);
  1767. if (res)
  1768. goto out;
  1769. if (dev) {
  1770. struct expander_device *ex = &dev->ex_dev;
  1771. int i = 0, phy_id;
  1772. do {
  1773. phy_id = -1;
  1774. res = sas_find_bcast_phy(dev, &phy_id, i, true);
  1775. if (phy_id == -1)
  1776. break;
  1777. res = sas_rediscover(dev, phy_id);
  1778. i = phy_id + 1;
  1779. } while (i < ex->num_phys);
  1780. }
  1781. out:
  1782. return res;
  1783. }
  1784. int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1785. struct request *req)
  1786. {
  1787. struct domain_device *dev;
  1788. int ret, type;
  1789. struct request *rsp = req->next_rq;
  1790. if (!rsp) {
  1791. printk("%s: space for a smp response is missing\n",
  1792. __func__);
  1793. return -EINVAL;
  1794. }
  1795. /* no rphy means no smp target support (ie aic94xx host) */
  1796. if (!rphy)
  1797. return sas_smp_host_handler(shost, req, rsp);
  1798. type = rphy->identify.device_type;
  1799. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1800. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1801. printk("%s: can we send a smp request to a device?\n",
  1802. __func__);
  1803. return -EINVAL;
  1804. }
  1805. dev = sas_find_dev_by_rphy(rphy);
  1806. if (!dev) {
  1807. printk("%s: fail to find a domain_device?\n", __func__);
  1808. return -EINVAL;
  1809. }
  1810. /* do we need to support multiple segments? */
  1811. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1812. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1813. __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
  1814. rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1815. return -EINVAL;
  1816. }
  1817. ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
  1818. bio_data(rsp->bio), blk_rq_bytes(rsp));
  1819. if (ret > 0) {
  1820. /* positive number is the untransferred residual */
  1821. rsp->resid_len = ret;
  1822. req->resid_len = 0;
  1823. ret = 0;
  1824. } else if (ret == 0) {
  1825. rsp->resid_len = 0;
  1826. req->resid_len = 0;
  1827. }
  1828. return ret;
  1829. }