sas_expander.c 54 KB

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