sas_expander.c 55 KB

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