sas_expander.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  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 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 int 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 0;
  752. }
  753. }
  754. return -ENODEV;
  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. res = sas_ex_join_wide_port(dev, phy_id);
  882. if (!res) {
  883. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  884. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  885. return res;
  886. }
  887. switch (ex_phy->attached_dev_type) {
  888. case SAS_END_DEV:
  889. case SATA_PENDING:
  890. child = sas_ex_discover_end_dev(dev, phy_id);
  891. break;
  892. case FANOUT_DEV:
  893. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  894. SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
  895. "attached to ex %016llx phy 0x%x\n",
  896. SAS_ADDR(ex_phy->attached_sas_addr),
  897. ex_phy->attached_phy_id,
  898. SAS_ADDR(dev->sas_addr),
  899. phy_id);
  900. sas_ex_disable_phy(dev, phy_id);
  901. break;
  902. } else
  903. memcpy(dev->port->disc.fanout_sas_addr,
  904. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  905. /* fallthrough */
  906. case EDGE_DEV:
  907. child = sas_ex_discover_expander(dev, phy_id);
  908. break;
  909. default:
  910. break;
  911. }
  912. if (child) {
  913. int i;
  914. for (i = 0; i < ex->num_phys; i++) {
  915. if (ex->ex_phy[i].phy_state == PHY_VACANT ||
  916. ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
  917. continue;
  918. /*
  919. * Due to races, the phy might not get added to the
  920. * wide port, so we add the phy to the wide port here.
  921. */
  922. if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
  923. SAS_ADDR(child->sas_addr)) {
  924. ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
  925. res = sas_ex_join_wide_port(dev, i);
  926. if (!res)
  927. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  928. i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
  929. }
  930. }
  931. }
  932. return res;
  933. }
  934. static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
  935. {
  936. struct expander_device *ex = &dev->ex_dev;
  937. int i;
  938. for (i = 0; i < ex->num_phys; i++) {
  939. struct ex_phy *phy = &ex->ex_phy[i];
  940. if (phy->phy_state == PHY_VACANT ||
  941. phy->phy_state == PHY_NOT_PRESENT)
  942. continue;
  943. if ((phy->attached_dev_type == EDGE_DEV ||
  944. phy->attached_dev_type == FANOUT_DEV) &&
  945. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  946. memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
  947. return 1;
  948. }
  949. }
  950. return 0;
  951. }
  952. static int sas_check_level_subtractive_boundary(struct domain_device *dev)
  953. {
  954. struct expander_device *ex = &dev->ex_dev;
  955. struct domain_device *child;
  956. u8 sub_addr[8] = {0, };
  957. list_for_each_entry(child, &ex->children, siblings) {
  958. if (child->dev_type != EDGE_DEV &&
  959. child->dev_type != FANOUT_DEV)
  960. continue;
  961. if (sub_addr[0] == 0) {
  962. sas_find_sub_addr(child, sub_addr);
  963. continue;
  964. } else {
  965. u8 s2[8];
  966. if (sas_find_sub_addr(child, s2) &&
  967. (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
  968. SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
  969. "diverges from subtractive "
  970. "boundary %016llx\n",
  971. SAS_ADDR(dev->sas_addr),
  972. SAS_ADDR(child->sas_addr),
  973. SAS_ADDR(s2),
  974. SAS_ADDR(sub_addr));
  975. sas_ex_disable_port(child, s2);
  976. }
  977. }
  978. }
  979. return 0;
  980. }
  981. /**
  982. * sas_ex_discover_devices -- discover devices attached to this expander
  983. * dev: pointer to the expander domain device
  984. * single: if you want to do a single phy, else set to -1;
  985. *
  986. * Configure this expander for use with its devices and register the
  987. * devices of this expander.
  988. */
  989. static int sas_ex_discover_devices(struct domain_device *dev, int single)
  990. {
  991. struct expander_device *ex = &dev->ex_dev;
  992. int i = 0, end = ex->num_phys;
  993. int res = 0;
  994. if (0 <= single && single < end) {
  995. i = single;
  996. end = i+1;
  997. }
  998. for ( ; i < end; i++) {
  999. struct ex_phy *ex_phy = &ex->ex_phy[i];
  1000. if (ex_phy->phy_state == PHY_VACANT ||
  1001. ex_phy->phy_state == PHY_NOT_PRESENT ||
  1002. ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
  1003. continue;
  1004. switch (ex_phy->linkrate) {
  1005. case SAS_PHY_DISABLED:
  1006. case SAS_PHY_RESET_PROBLEM:
  1007. case SAS_SATA_PORT_SELECTOR:
  1008. continue;
  1009. default:
  1010. res = sas_ex_discover_dev(dev, i);
  1011. if (res)
  1012. break;
  1013. continue;
  1014. }
  1015. }
  1016. if (!res)
  1017. sas_check_level_subtractive_boundary(dev);
  1018. return res;
  1019. }
  1020. static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
  1021. {
  1022. struct expander_device *ex = &dev->ex_dev;
  1023. int i;
  1024. u8 *sub_sas_addr = NULL;
  1025. if (dev->dev_type != EDGE_DEV)
  1026. return 0;
  1027. for (i = 0; i < ex->num_phys; i++) {
  1028. struct ex_phy *phy = &ex->ex_phy[i];
  1029. if (phy->phy_state == PHY_VACANT ||
  1030. phy->phy_state == PHY_NOT_PRESENT)
  1031. continue;
  1032. if ((phy->attached_dev_type == FANOUT_DEV ||
  1033. phy->attached_dev_type == EDGE_DEV) &&
  1034. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1035. if (!sub_sas_addr)
  1036. sub_sas_addr = &phy->attached_sas_addr[0];
  1037. else if (SAS_ADDR(sub_sas_addr) !=
  1038. SAS_ADDR(phy->attached_sas_addr)) {
  1039. SAS_DPRINTK("ex %016llx phy 0x%x "
  1040. "diverges(%016llx) on subtractive "
  1041. "boundary(%016llx). Disabled\n",
  1042. SAS_ADDR(dev->sas_addr), i,
  1043. SAS_ADDR(phy->attached_sas_addr),
  1044. SAS_ADDR(sub_sas_addr));
  1045. sas_ex_disable_phy(dev, i);
  1046. }
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. static void sas_print_parent_topology_bug(struct domain_device *child,
  1052. struct ex_phy *parent_phy,
  1053. struct ex_phy *child_phy)
  1054. {
  1055. static const char *ex_type[] = {
  1056. [EDGE_DEV] = "edge",
  1057. [FANOUT_DEV] = "fanout",
  1058. };
  1059. struct domain_device *parent = child->parent;
  1060. sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
  1061. "phy 0x%x has %c:%c routing link!\n",
  1062. ex_type[parent->dev_type],
  1063. SAS_ADDR(parent->sas_addr),
  1064. parent_phy->phy_id,
  1065. ex_type[child->dev_type],
  1066. SAS_ADDR(child->sas_addr),
  1067. child_phy->phy_id,
  1068. sas_route_char(parent, parent_phy),
  1069. sas_route_char(child, child_phy));
  1070. }
  1071. static int sas_check_eeds(struct domain_device *child,
  1072. struct ex_phy *parent_phy,
  1073. struct ex_phy *child_phy)
  1074. {
  1075. int res = 0;
  1076. struct domain_device *parent = child->parent;
  1077. if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
  1078. res = -ENODEV;
  1079. SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
  1080. "phy S:0x%x, while there is a fanout ex %016llx\n",
  1081. SAS_ADDR(parent->sas_addr),
  1082. parent_phy->phy_id,
  1083. SAS_ADDR(child->sas_addr),
  1084. child_phy->phy_id,
  1085. SAS_ADDR(parent->port->disc.fanout_sas_addr));
  1086. } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
  1087. memcpy(parent->port->disc.eeds_a, parent->sas_addr,
  1088. SAS_ADDR_SIZE);
  1089. memcpy(parent->port->disc.eeds_b, child->sas_addr,
  1090. SAS_ADDR_SIZE);
  1091. } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
  1092. SAS_ADDR(parent->sas_addr)) ||
  1093. (SAS_ADDR(parent->port->disc.eeds_a) ==
  1094. SAS_ADDR(child->sas_addr)))
  1095. &&
  1096. ((SAS_ADDR(parent->port->disc.eeds_b) ==
  1097. SAS_ADDR(parent->sas_addr)) ||
  1098. (SAS_ADDR(parent->port->disc.eeds_b) ==
  1099. SAS_ADDR(child->sas_addr))))
  1100. ;
  1101. else {
  1102. res = -ENODEV;
  1103. SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
  1104. "phy 0x%x link forms a third EEDS!\n",
  1105. SAS_ADDR(parent->sas_addr),
  1106. parent_phy->phy_id,
  1107. SAS_ADDR(child->sas_addr),
  1108. child_phy->phy_id);
  1109. }
  1110. return res;
  1111. }
  1112. /* Here we spill over 80 columns. It is intentional.
  1113. */
  1114. static int sas_check_parent_topology(struct domain_device *child)
  1115. {
  1116. struct expander_device *child_ex = &child->ex_dev;
  1117. struct expander_device *parent_ex;
  1118. int i;
  1119. int res = 0;
  1120. if (!child->parent)
  1121. return 0;
  1122. if (child->parent->dev_type != EDGE_DEV &&
  1123. child->parent->dev_type != FANOUT_DEV)
  1124. return 0;
  1125. parent_ex = &child->parent->ex_dev;
  1126. for (i = 0; i < parent_ex->num_phys; i++) {
  1127. struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
  1128. struct ex_phy *child_phy;
  1129. if (parent_phy->phy_state == PHY_VACANT ||
  1130. parent_phy->phy_state == PHY_NOT_PRESENT)
  1131. continue;
  1132. if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
  1133. continue;
  1134. child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
  1135. switch (child->parent->dev_type) {
  1136. case EDGE_DEV:
  1137. if (child->dev_type == FANOUT_DEV) {
  1138. if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
  1139. child_phy->routing_attr != TABLE_ROUTING) {
  1140. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1141. res = -ENODEV;
  1142. }
  1143. } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1144. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1145. res = sas_check_eeds(child, parent_phy, child_phy);
  1146. } else if (child_phy->routing_attr != TABLE_ROUTING) {
  1147. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1148. res = -ENODEV;
  1149. }
  1150. } else if (parent_phy->routing_attr == TABLE_ROUTING) {
  1151. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
  1152. (child_phy->routing_attr == TABLE_ROUTING &&
  1153. child_ex->t2t_supp && parent_ex->t2t_supp)) {
  1154. /* All good */;
  1155. } else {
  1156. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1157. res = -ENODEV;
  1158. }
  1159. }
  1160. break;
  1161. case FANOUT_DEV:
  1162. if (parent_phy->routing_attr != TABLE_ROUTING ||
  1163. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1164. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1165. res = -ENODEV;
  1166. }
  1167. break;
  1168. default:
  1169. break;
  1170. }
  1171. }
  1172. return res;
  1173. }
  1174. #define RRI_REQ_SIZE 16
  1175. #define RRI_RESP_SIZE 44
  1176. static int sas_configure_present(struct domain_device *dev, int phy_id,
  1177. u8 *sas_addr, int *index, int *present)
  1178. {
  1179. int i, res = 0;
  1180. struct expander_device *ex = &dev->ex_dev;
  1181. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1182. u8 *rri_req;
  1183. u8 *rri_resp;
  1184. *present = 0;
  1185. *index = 0;
  1186. rri_req = alloc_smp_req(RRI_REQ_SIZE);
  1187. if (!rri_req)
  1188. return -ENOMEM;
  1189. rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
  1190. if (!rri_resp) {
  1191. kfree(rri_req);
  1192. return -ENOMEM;
  1193. }
  1194. rri_req[1] = SMP_REPORT_ROUTE_INFO;
  1195. rri_req[9] = phy_id;
  1196. for (i = 0; i < ex->max_route_indexes ; i++) {
  1197. *(__be16 *)(rri_req+6) = cpu_to_be16(i);
  1198. res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
  1199. RRI_RESP_SIZE);
  1200. if (res)
  1201. goto out;
  1202. res = rri_resp[2];
  1203. if (res == SMP_RESP_NO_INDEX) {
  1204. SAS_DPRINTK("overflow of indexes: dev %016llx "
  1205. "phy 0x%x index 0x%x\n",
  1206. SAS_ADDR(dev->sas_addr), phy_id, i);
  1207. goto out;
  1208. } else if (res != SMP_RESP_FUNC_ACC) {
  1209. SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
  1210. "result 0x%x\n", __func__,
  1211. SAS_ADDR(dev->sas_addr), phy_id, i, res);
  1212. goto out;
  1213. }
  1214. if (SAS_ADDR(sas_addr) != 0) {
  1215. if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
  1216. *index = i;
  1217. if ((rri_resp[12] & 0x80) == 0x80)
  1218. *present = 0;
  1219. else
  1220. *present = 1;
  1221. goto out;
  1222. } else if (SAS_ADDR(rri_resp+16) == 0) {
  1223. *index = i;
  1224. *present = 0;
  1225. goto out;
  1226. }
  1227. } else if (SAS_ADDR(rri_resp+16) == 0 &&
  1228. phy->last_da_index < i) {
  1229. phy->last_da_index = i;
  1230. *index = i;
  1231. *present = 0;
  1232. goto out;
  1233. }
  1234. }
  1235. res = -1;
  1236. out:
  1237. kfree(rri_req);
  1238. kfree(rri_resp);
  1239. return res;
  1240. }
  1241. #define CRI_REQ_SIZE 44
  1242. #define CRI_RESP_SIZE 8
  1243. static int sas_configure_set(struct domain_device *dev, int phy_id,
  1244. u8 *sas_addr, int index, int include)
  1245. {
  1246. int res;
  1247. u8 *cri_req;
  1248. u8 *cri_resp;
  1249. cri_req = alloc_smp_req(CRI_REQ_SIZE);
  1250. if (!cri_req)
  1251. return -ENOMEM;
  1252. cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
  1253. if (!cri_resp) {
  1254. kfree(cri_req);
  1255. return -ENOMEM;
  1256. }
  1257. cri_req[1] = SMP_CONF_ROUTE_INFO;
  1258. *(__be16 *)(cri_req+6) = cpu_to_be16(index);
  1259. cri_req[9] = phy_id;
  1260. if (SAS_ADDR(sas_addr) == 0 || !include)
  1261. cri_req[12] |= 0x80;
  1262. memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
  1263. res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
  1264. CRI_RESP_SIZE);
  1265. if (res)
  1266. goto out;
  1267. res = cri_resp[2];
  1268. if (res == SMP_RESP_NO_INDEX) {
  1269. SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
  1270. "index 0x%x\n",
  1271. SAS_ADDR(dev->sas_addr), phy_id, index);
  1272. }
  1273. out:
  1274. kfree(cri_req);
  1275. kfree(cri_resp);
  1276. return res;
  1277. }
  1278. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  1279. u8 *sas_addr, int include)
  1280. {
  1281. int index;
  1282. int present;
  1283. int res;
  1284. res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
  1285. if (res)
  1286. return res;
  1287. if (include ^ present)
  1288. return sas_configure_set(dev, phy_id, sas_addr, index,include);
  1289. return res;
  1290. }
  1291. /**
  1292. * sas_configure_parent -- configure routing table of parent
  1293. * parent: parent expander
  1294. * child: child expander
  1295. * sas_addr: SAS port identifier of device directly attached to child
  1296. */
  1297. static int sas_configure_parent(struct domain_device *parent,
  1298. struct domain_device *child,
  1299. u8 *sas_addr, int include)
  1300. {
  1301. struct expander_device *ex_parent = &parent->ex_dev;
  1302. int res = 0;
  1303. int i;
  1304. if (parent->parent) {
  1305. res = sas_configure_parent(parent->parent, parent, sas_addr,
  1306. include);
  1307. if (res)
  1308. return res;
  1309. }
  1310. if (ex_parent->conf_route_table == 0) {
  1311. SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
  1312. SAS_ADDR(parent->sas_addr));
  1313. return 0;
  1314. }
  1315. for (i = 0; i < ex_parent->num_phys; i++) {
  1316. struct ex_phy *phy = &ex_parent->ex_phy[i];
  1317. if ((phy->routing_attr == TABLE_ROUTING) &&
  1318. (SAS_ADDR(phy->attached_sas_addr) ==
  1319. SAS_ADDR(child->sas_addr))) {
  1320. res = sas_configure_phy(parent, i, sas_addr, include);
  1321. if (res)
  1322. return res;
  1323. }
  1324. }
  1325. return res;
  1326. }
  1327. /**
  1328. * sas_configure_routing -- configure routing
  1329. * dev: expander device
  1330. * sas_addr: port identifier of device directly attached to the expander device
  1331. */
  1332. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
  1333. {
  1334. if (dev->parent)
  1335. return sas_configure_parent(dev->parent, dev, sas_addr, 1);
  1336. return 0;
  1337. }
  1338. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
  1339. {
  1340. if (dev->parent)
  1341. return sas_configure_parent(dev->parent, dev, sas_addr, 0);
  1342. return 0;
  1343. }
  1344. /**
  1345. * sas_discover_expander -- expander discovery
  1346. * @ex: pointer to expander domain device
  1347. *
  1348. * See comment in sas_discover_sata().
  1349. */
  1350. static int sas_discover_expander(struct domain_device *dev)
  1351. {
  1352. int res;
  1353. res = sas_notify_lldd_dev_found(dev);
  1354. if (res)
  1355. return res;
  1356. res = sas_ex_general(dev);
  1357. if (res)
  1358. goto out_err;
  1359. res = sas_ex_manuf_info(dev);
  1360. if (res)
  1361. goto out_err;
  1362. res = sas_expander_discover(dev);
  1363. if (res) {
  1364. SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
  1365. SAS_ADDR(dev->sas_addr), res);
  1366. goto out_err;
  1367. }
  1368. sas_check_ex_subtractive_boundary(dev);
  1369. res = sas_check_parent_topology(dev);
  1370. if (res)
  1371. goto out_err;
  1372. return 0;
  1373. out_err:
  1374. sas_notify_lldd_dev_gone(dev);
  1375. return res;
  1376. }
  1377. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1378. {
  1379. int res = 0;
  1380. struct domain_device *dev;
  1381. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1382. if (dev->dev_type == EDGE_DEV ||
  1383. dev->dev_type == FANOUT_DEV) {
  1384. struct sas_expander_device *ex =
  1385. rphy_to_expander_device(dev->rphy);
  1386. if (level == ex->level)
  1387. res = sas_ex_discover_devices(dev, -1);
  1388. else if (level > 0)
  1389. res = sas_ex_discover_devices(port->port_dev, -1);
  1390. }
  1391. }
  1392. return res;
  1393. }
  1394. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1395. {
  1396. int res;
  1397. int level;
  1398. do {
  1399. level = port->disc.max_level;
  1400. res = sas_ex_level_discovery(port, level);
  1401. mb();
  1402. } while (level < port->disc.max_level);
  1403. return res;
  1404. }
  1405. int sas_discover_root_expander(struct domain_device *dev)
  1406. {
  1407. int res;
  1408. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1409. res = sas_rphy_add(dev->rphy);
  1410. if (res)
  1411. goto out_err;
  1412. ex->level = dev->port->disc.max_level; /* 0 */
  1413. res = sas_discover_expander(dev);
  1414. if (res)
  1415. goto out_err2;
  1416. sas_ex_bfs_disc(dev->port);
  1417. return res;
  1418. out_err2:
  1419. sas_rphy_remove(dev->rphy);
  1420. out_err:
  1421. return res;
  1422. }
  1423. /* ---------- Domain revalidation ---------- */
  1424. static int sas_get_phy_discover(struct domain_device *dev,
  1425. int phy_id, struct smp_resp *disc_resp)
  1426. {
  1427. int res;
  1428. u8 *disc_req;
  1429. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1430. if (!disc_req)
  1431. return -ENOMEM;
  1432. disc_req[1] = SMP_DISCOVER;
  1433. disc_req[9] = phy_id;
  1434. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1435. disc_resp, DISCOVER_RESP_SIZE);
  1436. if (res)
  1437. goto out;
  1438. else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
  1439. res = disc_resp->result;
  1440. goto out;
  1441. }
  1442. out:
  1443. kfree(disc_req);
  1444. return res;
  1445. }
  1446. static int sas_get_phy_change_count(struct domain_device *dev,
  1447. int phy_id, int *pcc)
  1448. {
  1449. int res;
  1450. struct smp_resp *disc_resp;
  1451. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1452. if (!disc_resp)
  1453. return -ENOMEM;
  1454. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1455. if (!res)
  1456. *pcc = disc_resp->disc.change_count;
  1457. kfree(disc_resp);
  1458. return res;
  1459. }
  1460. static int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
  1461. u8 *sas_addr, enum sas_dev_type *type)
  1462. {
  1463. int res;
  1464. struct smp_resp *disc_resp;
  1465. struct discover_resp *dr;
  1466. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1467. if (!disc_resp)
  1468. return -ENOMEM;
  1469. dr = &disc_resp->disc;
  1470. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1471. if (res == 0) {
  1472. memcpy(sas_addr, disc_resp->disc.attached_sas_addr, 8);
  1473. *type = to_dev_type(dr);
  1474. if (*type == 0)
  1475. memset(sas_addr, 0, 8);
  1476. }
  1477. kfree(disc_resp);
  1478. return res;
  1479. }
  1480. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1481. int from_phy, bool update)
  1482. {
  1483. struct expander_device *ex = &dev->ex_dev;
  1484. int res = 0;
  1485. int i;
  1486. for (i = from_phy; i < ex->num_phys; i++) {
  1487. int phy_change_count = 0;
  1488. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1489. switch (res) {
  1490. case SMP_RESP_PHY_VACANT:
  1491. case SMP_RESP_NO_PHY:
  1492. continue;
  1493. case SMP_RESP_FUNC_ACC:
  1494. break;
  1495. default:
  1496. return res;
  1497. }
  1498. if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1499. if (update)
  1500. ex->ex_phy[i].phy_change_count =
  1501. phy_change_count;
  1502. *phy_id = i;
  1503. return 0;
  1504. }
  1505. }
  1506. return 0;
  1507. }
  1508. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1509. {
  1510. int res;
  1511. u8 *rg_req;
  1512. struct smp_resp *rg_resp;
  1513. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1514. if (!rg_req)
  1515. return -ENOMEM;
  1516. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1517. if (!rg_resp) {
  1518. kfree(rg_req);
  1519. return -ENOMEM;
  1520. }
  1521. rg_req[1] = SMP_REPORT_GENERAL;
  1522. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1523. RG_RESP_SIZE);
  1524. if (res)
  1525. goto out;
  1526. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1527. res = rg_resp->result;
  1528. goto out;
  1529. }
  1530. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1531. out:
  1532. kfree(rg_resp);
  1533. kfree(rg_req);
  1534. return res;
  1535. }
  1536. /**
  1537. * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
  1538. * @dev:domain device to be detect.
  1539. * @src_dev: the device which originated BROADCAST(CHANGE).
  1540. *
  1541. * Add self-configuration expander suport. Suppose two expander cascading,
  1542. * when the first level expander is self-configuring, hotplug the disks in
  1543. * second level expander, BROADCAST(CHANGE) will not only be originated
  1544. * in the second level expander, but also be originated in the first level
  1545. * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
  1546. * expander changed count in two level expanders will all increment at least
  1547. * once, but the phy which chang count has changed is the source device which
  1548. * we concerned.
  1549. */
  1550. static int sas_find_bcast_dev(struct domain_device *dev,
  1551. struct domain_device **src_dev)
  1552. {
  1553. struct expander_device *ex = &dev->ex_dev;
  1554. int ex_change_count = -1;
  1555. int phy_id = -1;
  1556. int res;
  1557. struct domain_device *ch;
  1558. res = sas_get_ex_change_count(dev, &ex_change_count);
  1559. if (res)
  1560. goto out;
  1561. if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
  1562. /* Just detect if this expander phys phy change count changed,
  1563. * in order to determine if this expander originate BROADCAST,
  1564. * and do not update phy change count field in our structure.
  1565. */
  1566. res = sas_find_bcast_phy(dev, &phy_id, 0, false);
  1567. if (phy_id != -1) {
  1568. *src_dev = dev;
  1569. ex->ex_change_count = ex_change_count;
  1570. SAS_DPRINTK("Expander phy change count has changed\n");
  1571. return res;
  1572. } else
  1573. SAS_DPRINTK("Expander phys DID NOT change\n");
  1574. }
  1575. list_for_each_entry(ch, &ex->children, siblings) {
  1576. if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
  1577. res = sas_find_bcast_dev(ch, src_dev);
  1578. if (*src_dev)
  1579. return res;
  1580. }
  1581. }
  1582. out:
  1583. return res;
  1584. }
  1585. static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
  1586. {
  1587. struct expander_device *ex = &dev->ex_dev;
  1588. struct domain_device *child, *n;
  1589. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1590. set_bit(SAS_DEV_GONE, &child->state);
  1591. if (child->dev_type == EDGE_DEV ||
  1592. child->dev_type == FANOUT_DEV)
  1593. sas_unregister_ex_tree(port, child);
  1594. else
  1595. sas_unregister_dev(port, child);
  1596. }
  1597. sas_unregister_dev(port, dev);
  1598. }
  1599. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1600. int phy_id, bool last)
  1601. {
  1602. struct expander_device *ex_dev = &parent->ex_dev;
  1603. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1604. struct domain_device *child, *n, *found = NULL;
  1605. if (last) {
  1606. list_for_each_entry_safe(child, n,
  1607. &ex_dev->children, siblings) {
  1608. if (SAS_ADDR(child->sas_addr) ==
  1609. SAS_ADDR(phy->attached_sas_addr)) {
  1610. set_bit(SAS_DEV_GONE, &child->state);
  1611. if (child->dev_type == EDGE_DEV ||
  1612. child->dev_type == FANOUT_DEV)
  1613. sas_unregister_ex_tree(parent->port, child);
  1614. else
  1615. sas_unregister_dev(parent->port, child);
  1616. found = child;
  1617. break;
  1618. }
  1619. }
  1620. sas_disable_routing(parent, phy->attached_sas_addr);
  1621. }
  1622. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1623. if (phy->port) {
  1624. sas_port_delete_phy(phy->port, phy->phy);
  1625. sas_device_set_phy(found, phy->port);
  1626. if (phy->port->num_phys == 0)
  1627. sas_port_delete(phy->port);
  1628. phy->port = NULL;
  1629. }
  1630. }
  1631. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1632. const int level)
  1633. {
  1634. struct expander_device *ex_root = &root->ex_dev;
  1635. struct domain_device *child;
  1636. int res = 0;
  1637. list_for_each_entry(child, &ex_root->children, siblings) {
  1638. if (child->dev_type == EDGE_DEV ||
  1639. child->dev_type == FANOUT_DEV) {
  1640. struct sas_expander_device *ex =
  1641. rphy_to_expander_device(child->rphy);
  1642. if (level > ex->level)
  1643. res = sas_discover_bfs_by_root_level(child,
  1644. level);
  1645. else if (level == ex->level)
  1646. res = sas_ex_discover_devices(child, -1);
  1647. }
  1648. }
  1649. return res;
  1650. }
  1651. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1652. {
  1653. int res;
  1654. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1655. int level = ex->level+1;
  1656. res = sas_ex_discover_devices(dev, -1);
  1657. if (res)
  1658. goto out;
  1659. do {
  1660. res = sas_discover_bfs_by_root_level(dev, level);
  1661. mb();
  1662. level += 1;
  1663. } while (level <= dev->port->disc.max_level);
  1664. out:
  1665. return res;
  1666. }
  1667. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1668. {
  1669. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1670. struct domain_device *child;
  1671. bool found = false;
  1672. int res, i;
  1673. SAS_DPRINTK("ex %016llx phy%d new device attached\n",
  1674. SAS_ADDR(dev->sas_addr), phy_id);
  1675. res = sas_ex_phy_discover(dev, phy_id);
  1676. if (res)
  1677. goto out;
  1678. /* to support the wide port inserted */
  1679. for (i = 0; i < dev->ex_dev.num_phys; i++) {
  1680. struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
  1681. if (i == phy_id)
  1682. continue;
  1683. if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
  1684. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1685. found = true;
  1686. break;
  1687. }
  1688. }
  1689. if (found) {
  1690. sas_ex_join_wide_port(dev, phy_id);
  1691. return 0;
  1692. }
  1693. res = sas_ex_discover_devices(dev, phy_id);
  1694. if (!res)
  1695. goto out;
  1696. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1697. if (SAS_ADDR(child->sas_addr) ==
  1698. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1699. if (child->dev_type == EDGE_DEV ||
  1700. child->dev_type == FANOUT_DEV)
  1701. res = sas_discover_bfs_by_root(child);
  1702. break;
  1703. }
  1704. }
  1705. out:
  1706. return res;
  1707. }
  1708. static bool dev_type_flutter(enum sas_dev_type new, enum sas_dev_type old)
  1709. {
  1710. if (old == new)
  1711. return true;
  1712. /* treat device directed resets as flutter, if we went
  1713. * SAS_END_DEV to SATA_PENDING the link needs recovery
  1714. */
  1715. if ((old == SATA_PENDING && new == SAS_END_DEV) ||
  1716. (old == SAS_END_DEV && new == SATA_PENDING))
  1717. return true;
  1718. return false;
  1719. }
  1720. static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
  1721. {
  1722. struct expander_device *ex = &dev->ex_dev;
  1723. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1724. enum sas_dev_type type = NO_DEVICE;
  1725. u8 sas_addr[8];
  1726. int res;
  1727. memset(sas_addr, 0, 8);
  1728. res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
  1729. switch (res) {
  1730. case SMP_RESP_NO_PHY:
  1731. phy->phy_state = PHY_NOT_PRESENT;
  1732. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1733. return res;
  1734. case SMP_RESP_PHY_VACANT:
  1735. phy->phy_state = PHY_VACANT;
  1736. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1737. return res;
  1738. case SMP_RESP_FUNC_ACC:
  1739. break;
  1740. case -ECOMM:
  1741. break;
  1742. default:
  1743. return res;
  1744. }
  1745. if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) {
  1746. phy->phy_state = PHY_EMPTY;
  1747. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1748. return res;
  1749. } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
  1750. dev_type_flutter(type, phy->attached_dev_type)) {
  1751. struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
  1752. char *action = "";
  1753. sas_ex_phy_discover(dev, phy_id);
  1754. if (ata_dev && phy->attached_dev_type == SATA_PENDING)
  1755. action = ", needs recovery";
  1756. SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter%s\n",
  1757. SAS_ADDR(dev->sas_addr), phy_id, action);
  1758. return res;
  1759. }
  1760. /* delete the old link */
  1761. if (SAS_ADDR(phy->attached_sas_addr) &&
  1762. SAS_ADDR(sas_addr) != SAS_ADDR(phy->attached_sas_addr)) {
  1763. SAS_DPRINTK("ex %016llx phy 0x%x replace %016llx\n",
  1764. SAS_ADDR(dev->sas_addr), phy_id,
  1765. SAS_ADDR(phy->attached_sas_addr));
  1766. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1767. }
  1768. return sas_discover_new(dev, phy_id);
  1769. }
  1770. /**
  1771. * sas_rediscover - revalidate the domain.
  1772. * @dev:domain device to be detect.
  1773. * @phy_id: the phy id will be detected.
  1774. *
  1775. * NOTE: this process _must_ quit (return) as soon as any connection
  1776. * errors are encountered. Connection recovery is done elsewhere.
  1777. * Discover process only interrogates devices in order to discover the
  1778. * domain.For plugging out, we un-register the device only when it is
  1779. * the last phy in the port, for other phys in this port, we just delete it
  1780. * from the port.For inserting, we do discovery when it is the
  1781. * first phy,for other phys in this port, we add it to the port to
  1782. * forming the wide-port.
  1783. */
  1784. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1785. {
  1786. struct expander_device *ex = &dev->ex_dev;
  1787. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1788. int res = 0;
  1789. int i;
  1790. bool last = true; /* is this the last phy of the port */
  1791. SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
  1792. SAS_ADDR(dev->sas_addr), phy_id);
  1793. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1794. for (i = 0; i < ex->num_phys; i++) {
  1795. struct ex_phy *phy = &ex->ex_phy[i];
  1796. if (i == phy_id)
  1797. continue;
  1798. if (SAS_ADDR(phy->attached_sas_addr) ==
  1799. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1800. SAS_DPRINTK("phy%d part of wide port with "
  1801. "phy%d\n", phy_id, i);
  1802. last = false;
  1803. break;
  1804. }
  1805. }
  1806. res = sas_rediscover_dev(dev, phy_id, last);
  1807. } else
  1808. res = sas_discover_new(dev, phy_id);
  1809. return res;
  1810. }
  1811. /**
  1812. * sas_revalidate_domain -- revalidate the domain
  1813. * @port: port to the domain of interest
  1814. *
  1815. * NOTE: this process _must_ quit (return) as soon as any connection
  1816. * errors are encountered. Connection recovery is done elsewhere.
  1817. * Discover process only interrogates devices in order to discover the
  1818. * domain.
  1819. */
  1820. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1821. {
  1822. int res;
  1823. struct domain_device *dev = NULL;
  1824. res = sas_find_bcast_dev(port_dev, &dev);
  1825. while (res == 0 && dev) {
  1826. struct expander_device *ex = &dev->ex_dev;
  1827. int i = 0, phy_id;
  1828. do {
  1829. phy_id = -1;
  1830. res = sas_find_bcast_phy(dev, &phy_id, i, true);
  1831. if (phy_id == -1)
  1832. break;
  1833. res = sas_rediscover(dev, phy_id);
  1834. i = phy_id + 1;
  1835. } while (i < ex->num_phys);
  1836. dev = NULL;
  1837. res = sas_find_bcast_dev(port_dev, &dev);
  1838. }
  1839. return res;
  1840. }
  1841. int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1842. struct request *req)
  1843. {
  1844. struct domain_device *dev;
  1845. int ret, type;
  1846. struct request *rsp = req->next_rq;
  1847. if (!rsp) {
  1848. printk("%s: space for a smp response is missing\n",
  1849. __func__);
  1850. return -EINVAL;
  1851. }
  1852. /* no rphy means no smp target support (ie aic94xx host) */
  1853. if (!rphy)
  1854. return sas_smp_host_handler(shost, req, rsp);
  1855. type = rphy->identify.device_type;
  1856. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1857. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1858. printk("%s: can we send a smp request to a device?\n",
  1859. __func__);
  1860. return -EINVAL;
  1861. }
  1862. dev = sas_find_dev_by_rphy(rphy);
  1863. if (!dev) {
  1864. printk("%s: fail to find a domain_device?\n", __func__);
  1865. return -EINVAL;
  1866. }
  1867. /* do we need to support multiple segments? */
  1868. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1869. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1870. __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
  1871. rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1872. return -EINVAL;
  1873. }
  1874. ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
  1875. bio_data(rsp->bio), blk_rq_bytes(rsp));
  1876. if (ret > 0) {
  1877. /* positive number is the untransferred residual */
  1878. rsp->resid_len = ret;
  1879. req->resid_len = 0;
  1880. ret = 0;
  1881. } else if (ret == 0) {
  1882. rsp->resid_len = 0;
  1883. req->resid_len = 0;
  1884. }
  1885. return ret;
  1886. }