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