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