sas_expander.c 53 KB

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