sas_expander.c 54 KB

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