sas_expander.c 57 KB

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