host.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <linux/device.h>
  56. #include <scsi/sas.h>
  57. #include "host.h"
  58. #include "isci.h"
  59. #include "port.h"
  60. #include "host.h"
  61. #include "probe_roms.h"
  62. #include "remote_device.h"
  63. #include "request.h"
  64. #include "scic_io_request.h"
  65. #include "scic_sds_port_configuration_agent.h"
  66. #include "sci_util.h"
  67. #include "scu_completion_codes.h"
  68. #include "scu_event_codes.h"
  69. #include "registers.h"
  70. #include "scu_remote_node_context.h"
  71. #include "scu_task_context.h"
  72. #include "scu_unsolicited_frame.h"
  73. #include "timers.h"
  74. #define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
  75. /**
  76. * smu_dcc_get_max_ports() -
  77. *
  78. * This macro returns the maximum number of logical ports supported by the
  79. * hardware. The caller passes in the value read from the device context
  80. * capacity register and this macro will mash and shift the value appropriately.
  81. */
  82. #define smu_dcc_get_max_ports(dcc_value) \
  83. (\
  84. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
  85. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
  86. )
  87. /**
  88. * smu_dcc_get_max_task_context() -
  89. *
  90. * This macro returns the maximum number of task contexts supported by the
  91. * hardware. The caller passes in the value read from the device context
  92. * capacity register and this macro will mash and shift the value appropriately.
  93. */
  94. #define smu_dcc_get_max_task_context(dcc_value) \
  95. (\
  96. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
  97. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
  98. )
  99. /**
  100. * smu_dcc_get_max_remote_node_context() -
  101. *
  102. * This macro returns the maximum number of remote node contexts supported by
  103. * the hardware. The caller passes in the value read from the device context
  104. * capacity register and this macro will mash and shift the value appropriately.
  105. */
  106. #define smu_dcc_get_max_remote_node_context(dcc_value) \
  107. (\
  108. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
  109. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
  110. )
  111. #define SCIC_SDS_CONTROLLER_MIN_TIMER_COUNT 3
  112. #define SCIC_SDS_CONTROLLER_MAX_TIMER_COUNT 3
  113. /**
  114. *
  115. *
  116. * The number of milliseconds to wait for a phy to start.
  117. */
  118. #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
  119. /**
  120. *
  121. *
  122. * The number of milliseconds to wait while a given phy is consuming power
  123. * before allowing another set of phys to consume power. Ultimately, this will
  124. * be specified by OEM parameter.
  125. */
  126. #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
  127. /**
  128. * NORMALIZE_PUT_POINTER() -
  129. *
  130. * This macro will normalize the completion queue put pointer so its value can
  131. * be used as an array inde
  132. */
  133. #define NORMALIZE_PUT_POINTER(x) \
  134. ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
  135. /**
  136. * NORMALIZE_EVENT_POINTER() -
  137. *
  138. * This macro will normalize the completion queue event entry so its value can
  139. * be used as an index.
  140. */
  141. #define NORMALIZE_EVENT_POINTER(x) \
  142. (\
  143. ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
  144. >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
  145. )
  146. /**
  147. * INCREMENT_COMPLETION_QUEUE_GET() -
  148. *
  149. * This macro will increment the controllers completion queue index value and
  150. * possibly toggle the cycle bit if the completion queue index wraps back to 0.
  151. */
  152. #define INCREMENT_COMPLETION_QUEUE_GET(controller, index, cycle) \
  153. INCREMENT_QUEUE_GET(\
  154. (index), \
  155. (cycle), \
  156. (controller)->completion_queue_entries, \
  157. SMU_CQGR_CYCLE_BIT \
  158. )
  159. /**
  160. * INCREMENT_EVENT_QUEUE_GET() -
  161. *
  162. * This macro will increment the controllers event queue index value and
  163. * possibly toggle the event cycle bit if the event queue index wraps back to 0.
  164. */
  165. #define INCREMENT_EVENT_QUEUE_GET(controller, index, cycle) \
  166. INCREMENT_QUEUE_GET(\
  167. (index), \
  168. (cycle), \
  169. (controller)->completion_event_entries, \
  170. SMU_CQGR_EVENT_CYCLE_BIT \
  171. )
  172. /**
  173. * NORMALIZE_GET_POINTER() -
  174. *
  175. * This macro will normalize the completion queue get pointer so its value can
  176. * be used as an index into an array
  177. */
  178. #define NORMALIZE_GET_POINTER(x) \
  179. ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
  180. /**
  181. * NORMALIZE_GET_POINTER_CYCLE_BIT() -
  182. *
  183. * This macro will normalize the completion queue cycle pointer so it matches
  184. * the completion queue cycle bit
  185. */
  186. #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
  187. ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
  188. /**
  189. * COMPLETION_QUEUE_CYCLE_BIT() -
  190. *
  191. * This macro will return the cycle bit of the completion queue entry
  192. */
  193. #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
  194. static bool scic_sds_controller_completion_queue_has_entries(
  195. struct scic_sds_controller *scic)
  196. {
  197. u32 get_value = scic->completion_queue_get;
  198. u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
  199. if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
  200. COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
  201. return true;
  202. return false;
  203. }
  204. static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
  205. {
  206. if (scic_sds_controller_completion_queue_has_entries(scic)) {
  207. return true;
  208. } else {
  209. /*
  210. * we have a spurious interrupt it could be that we have already
  211. * emptied the completion queue from a previous interrupt */
  212. writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
  213. /*
  214. * There is a race in the hardware that could cause us not to be notified
  215. * of an interrupt completion if we do not take this step. We will mask
  216. * then unmask the interrupts so if there is another interrupt pending
  217. * the clearing of the interrupt source we get the next interrupt message. */
  218. writel(0xFF000000, &scic->smu_registers->interrupt_mask);
  219. writel(0, &scic->smu_registers->interrupt_mask);
  220. }
  221. return false;
  222. }
  223. irqreturn_t isci_msix_isr(int vec, void *data)
  224. {
  225. struct isci_host *ihost = data;
  226. if (scic_sds_controller_isr(&ihost->sci))
  227. tasklet_schedule(&ihost->completion_tasklet);
  228. return IRQ_HANDLED;
  229. }
  230. static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
  231. {
  232. u32 interrupt_status;
  233. interrupt_status =
  234. readl(&scic->smu_registers->interrupt_status);
  235. interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
  236. if (interrupt_status != 0) {
  237. /*
  238. * There is an error interrupt pending so let it through and handle
  239. * in the callback */
  240. return true;
  241. }
  242. /*
  243. * There is a race in the hardware that could cause us not to be notified
  244. * of an interrupt completion if we do not take this step. We will mask
  245. * then unmask the error interrupts so if there was another interrupt
  246. * pending we will be notified.
  247. * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
  248. writel(0xff, &scic->smu_registers->interrupt_mask);
  249. writel(0, &scic->smu_registers->interrupt_mask);
  250. return false;
  251. }
  252. static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
  253. u32 completion_entry)
  254. {
  255. u32 index;
  256. struct scic_sds_request *io_request;
  257. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  258. io_request = scic->io_request_table[index];
  259. /* Make sure that we really want to process this IO request */
  260. if (
  261. (io_request != NULL)
  262. && (io_request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG)
  263. && (
  264. scic_sds_io_tag_get_sequence(io_request->io_tag)
  265. == scic->io_request_sequence[index]
  266. )
  267. ) {
  268. /* Yep this is a valid io request pass it along to the io request handler */
  269. scic_sds_io_request_tc_completion(io_request, completion_entry);
  270. }
  271. }
  272. static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
  273. u32 completion_entry)
  274. {
  275. u32 index;
  276. struct scic_sds_request *io_request;
  277. struct scic_sds_remote_device *device;
  278. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  279. switch (scu_get_command_request_type(completion_entry)) {
  280. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
  281. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
  282. io_request = scic->io_request_table[index];
  283. dev_warn(scic_to_dev(scic),
  284. "%s: SCIC SDS Completion type SDMA %x for io request "
  285. "%p\n",
  286. __func__,
  287. completion_entry,
  288. io_request);
  289. /* @todo For a post TC operation we need to fail the IO
  290. * request
  291. */
  292. break;
  293. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
  294. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
  295. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
  296. device = scic->device_table[index];
  297. dev_warn(scic_to_dev(scic),
  298. "%s: SCIC SDS Completion type SDMA %x for remote "
  299. "device %p\n",
  300. __func__,
  301. completion_entry,
  302. device);
  303. /* @todo For a port RNC operation we need to fail the
  304. * device
  305. */
  306. break;
  307. default:
  308. dev_warn(scic_to_dev(scic),
  309. "%s: SCIC SDS Completion unknown SDMA completion "
  310. "type %x\n",
  311. __func__,
  312. completion_entry);
  313. break;
  314. }
  315. }
  316. static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
  317. u32 completion_entry)
  318. {
  319. u32 index;
  320. u32 frame_index;
  321. struct isci_host *ihost = scic_to_ihost(scic);
  322. struct scu_unsolicited_frame_header *frame_header;
  323. struct scic_sds_phy *phy;
  324. struct scic_sds_remote_device *device;
  325. enum sci_status result = SCI_FAILURE;
  326. frame_index = SCU_GET_FRAME_INDEX(completion_entry);
  327. frame_header = scic->uf_control.buffers.array[frame_index].header;
  328. scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
  329. if (SCU_GET_FRAME_ERROR(completion_entry)) {
  330. /*
  331. * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
  332. * / this cause a problem? We expect the phy initialization will
  333. * / fail if there is an error in the frame. */
  334. scic_sds_controller_release_frame(scic, frame_index);
  335. return;
  336. }
  337. if (frame_header->is_address_frame) {
  338. index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
  339. phy = &ihost->phys[index].sci;
  340. result = scic_sds_phy_frame_handler(phy, frame_index);
  341. } else {
  342. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  343. if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  344. /*
  345. * This is a signature fis or a frame from a direct attached SATA
  346. * device that has not yet been created. In either case forwared
  347. * the frame to the PE and let it take care of the frame data. */
  348. index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
  349. phy = &ihost->phys[index].sci;
  350. result = scic_sds_phy_frame_handler(phy, frame_index);
  351. } else {
  352. if (index < scic->remote_node_entries)
  353. device = scic->device_table[index];
  354. else
  355. device = NULL;
  356. if (device != NULL)
  357. result = scic_sds_remote_device_frame_handler(device, frame_index);
  358. else
  359. scic_sds_controller_release_frame(scic, frame_index);
  360. }
  361. }
  362. if (result != SCI_SUCCESS) {
  363. /*
  364. * / @todo Is there any reason to report some additional error message
  365. * / when we get this failure notifiction? */
  366. }
  367. }
  368. static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
  369. u32 completion_entry)
  370. {
  371. struct isci_host *ihost = scic_to_ihost(scic);
  372. struct scic_sds_request *io_request;
  373. struct scic_sds_remote_device *device;
  374. struct scic_sds_phy *phy;
  375. u32 index;
  376. index = SCU_GET_COMPLETION_INDEX(completion_entry);
  377. switch (scu_get_event_type(completion_entry)) {
  378. case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
  379. /* / @todo The driver did something wrong and we need to fix the condtion. */
  380. dev_err(scic_to_dev(scic),
  381. "%s: SCIC Controller 0x%p received SMU command error "
  382. "0x%x\n",
  383. __func__,
  384. scic,
  385. completion_entry);
  386. break;
  387. case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
  388. case SCU_EVENT_TYPE_SMU_ERROR:
  389. case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
  390. /*
  391. * / @todo This is a hardware failure and its likely that we want to
  392. * / reset the controller. */
  393. dev_err(scic_to_dev(scic),
  394. "%s: SCIC Controller 0x%p received fatal controller "
  395. "event 0x%x\n",
  396. __func__,
  397. scic,
  398. completion_entry);
  399. break;
  400. case SCU_EVENT_TYPE_TRANSPORT_ERROR:
  401. io_request = scic->io_request_table[index];
  402. scic_sds_io_request_event_handler(io_request, completion_entry);
  403. break;
  404. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  405. switch (scu_get_event_specifier(completion_entry)) {
  406. case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
  407. case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
  408. io_request = scic->io_request_table[index];
  409. if (io_request != NULL)
  410. scic_sds_io_request_event_handler(io_request, completion_entry);
  411. else
  412. dev_warn(scic_to_dev(scic),
  413. "%s: SCIC Controller 0x%p received "
  414. "event 0x%x for io request object "
  415. "that doesnt exist.\n",
  416. __func__,
  417. scic,
  418. completion_entry);
  419. break;
  420. case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
  421. device = scic->device_table[index];
  422. if (device != NULL)
  423. scic_sds_remote_device_event_handler(device, completion_entry);
  424. else
  425. dev_warn(scic_to_dev(scic),
  426. "%s: SCIC Controller 0x%p received "
  427. "event 0x%x for remote device object "
  428. "that doesnt exist.\n",
  429. __func__,
  430. scic,
  431. completion_entry);
  432. break;
  433. }
  434. break;
  435. case SCU_EVENT_TYPE_BROADCAST_CHANGE:
  436. /*
  437. * direct the broadcast change event to the phy first and then let
  438. * the phy redirect the broadcast change to the port object */
  439. case SCU_EVENT_TYPE_ERR_CNT_EVENT:
  440. /*
  441. * direct error counter event to the phy object since that is where
  442. * we get the event notification. This is a type 4 event. */
  443. case SCU_EVENT_TYPE_OSSP_EVENT:
  444. index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
  445. phy = &ihost->phys[index].sci;
  446. scic_sds_phy_event_handler(phy, completion_entry);
  447. break;
  448. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  449. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  450. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  451. if (index < scic->remote_node_entries) {
  452. device = scic->device_table[index];
  453. if (device != NULL)
  454. scic_sds_remote_device_event_handler(device, completion_entry);
  455. } else
  456. dev_err(scic_to_dev(scic),
  457. "%s: SCIC Controller 0x%p received event 0x%x "
  458. "for remote device object 0x%0x that doesnt "
  459. "exist.\n",
  460. __func__,
  461. scic,
  462. completion_entry,
  463. index);
  464. break;
  465. default:
  466. dev_warn(scic_to_dev(scic),
  467. "%s: SCIC Controller received unknown event code %x\n",
  468. __func__,
  469. completion_entry);
  470. break;
  471. }
  472. }
  473. static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
  474. {
  475. u32 completion_count = 0;
  476. u32 completion_entry;
  477. u32 get_index;
  478. u32 get_cycle;
  479. u32 event_index;
  480. u32 event_cycle;
  481. dev_dbg(scic_to_dev(scic),
  482. "%s: completion queue begining get:0x%08x\n",
  483. __func__,
  484. scic->completion_queue_get);
  485. /* Get the component parts of the completion queue */
  486. get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
  487. get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
  488. event_index = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
  489. event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
  490. while (
  491. NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
  492. == COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
  493. ) {
  494. completion_count++;
  495. completion_entry = scic->completion_queue[get_index];
  496. INCREMENT_COMPLETION_QUEUE_GET(scic, get_index, get_cycle);
  497. dev_dbg(scic_to_dev(scic),
  498. "%s: completion queue entry:0x%08x\n",
  499. __func__,
  500. completion_entry);
  501. switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
  502. case SCU_COMPLETION_TYPE_TASK:
  503. scic_sds_controller_task_completion(scic, completion_entry);
  504. break;
  505. case SCU_COMPLETION_TYPE_SDMA:
  506. scic_sds_controller_sdma_completion(scic, completion_entry);
  507. break;
  508. case SCU_COMPLETION_TYPE_UFI:
  509. scic_sds_controller_unsolicited_frame(scic, completion_entry);
  510. break;
  511. case SCU_COMPLETION_TYPE_EVENT:
  512. INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
  513. scic_sds_controller_event_completion(scic, completion_entry);
  514. break;
  515. case SCU_COMPLETION_TYPE_NOTIFY:
  516. /*
  517. * Presently we do the same thing with a notify event that we do with the
  518. * other event codes. */
  519. INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
  520. scic_sds_controller_event_completion(scic, completion_entry);
  521. break;
  522. default:
  523. dev_warn(scic_to_dev(scic),
  524. "%s: SCIC Controller received unknown "
  525. "completion type %x\n",
  526. __func__,
  527. completion_entry);
  528. break;
  529. }
  530. }
  531. /* Update the get register if we completed one or more entries */
  532. if (completion_count > 0) {
  533. scic->completion_queue_get =
  534. SMU_CQGR_GEN_BIT(ENABLE) |
  535. SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
  536. event_cycle |
  537. SMU_CQGR_GEN_VAL(EVENT_POINTER, event_index) |
  538. get_cycle |
  539. SMU_CQGR_GEN_VAL(POINTER, get_index);
  540. writel(scic->completion_queue_get,
  541. &scic->smu_registers->completion_queue_get);
  542. }
  543. dev_dbg(scic_to_dev(scic),
  544. "%s: completion queue ending get:0x%08x\n",
  545. __func__,
  546. scic->completion_queue_get);
  547. }
  548. static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
  549. {
  550. u32 interrupt_status;
  551. interrupt_status =
  552. readl(&scic->smu_registers->interrupt_status);
  553. if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
  554. scic_sds_controller_completion_queue_has_entries(scic)) {
  555. scic_sds_controller_process_completions(scic);
  556. writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
  557. } else {
  558. dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
  559. interrupt_status);
  560. sci_base_state_machine_change_state(&scic->state_machine,
  561. SCI_BASE_CONTROLLER_STATE_FAILED);
  562. return;
  563. }
  564. /* If we dont process any completions I am not sure that we want to do this.
  565. * We are in the middle of a hardware fault and should probably be reset.
  566. */
  567. writel(0, &scic->smu_registers->interrupt_mask);
  568. }
  569. irqreturn_t isci_intx_isr(int vec, void *data)
  570. {
  571. irqreturn_t ret = IRQ_NONE;
  572. struct isci_host *ihost = data;
  573. struct scic_sds_controller *scic = &ihost->sci;
  574. if (scic_sds_controller_isr(scic)) {
  575. writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
  576. tasklet_schedule(&ihost->completion_tasklet);
  577. ret = IRQ_HANDLED;
  578. } else if (scic_sds_controller_error_isr(scic)) {
  579. spin_lock(&ihost->scic_lock);
  580. scic_sds_controller_error_handler(scic);
  581. spin_unlock(&ihost->scic_lock);
  582. ret = IRQ_HANDLED;
  583. }
  584. return ret;
  585. }
  586. irqreturn_t isci_error_isr(int vec, void *data)
  587. {
  588. struct isci_host *ihost = data;
  589. if (scic_sds_controller_error_isr(&ihost->sci))
  590. scic_sds_controller_error_handler(&ihost->sci);
  591. return IRQ_HANDLED;
  592. }
  593. /**
  594. * isci_host_start_complete() - This function is called by the core library,
  595. * through the ISCI Module, to indicate controller start status.
  596. * @isci_host: This parameter specifies the ISCI host object
  597. * @completion_status: This parameter specifies the completion status from the
  598. * core library.
  599. *
  600. */
  601. static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
  602. {
  603. if (completion_status != SCI_SUCCESS)
  604. dev_info(&ihost->pdev->dev,
  605. "controller start timed out, continuing...\n");
  606. isci_host_change_state(ihost, isci_ready);
  607. clear_bit(IHOST_START_PENDING, &ihost->flags);
  608. wake_up(&ihost->eventq);
  609. }
  610. int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
  611. {
  612. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  613. if (test_bit(IHOST_START_PENDING, &ihost->flags))
  614. return 0;
  615. /* todo: use sas_flush_discovery once it is upstream */
  616. scsi_flush_work(shost);
  617. scsi_flush_work(shost);
  618. dev_dbg(&ihost->pdev->dev,
  619. "%s: ihost->status = %d, time = %ld\n",
  620. __func__, isci_host_get_state(ihost), time);
  621. return 1;
  622. }
  623. /**
  624. * scic_controller_get_suggested_start_timeout() - This method returns the
  625. * suggested scic_controller_start() timeout amount. The user is free to
  626. * use any timeout value, but this method provides the suggested minimum
  627. * start timeout value. The returned value is based upon empirical
  628. * information determined as a result of interoperability testing.
  629. * @controller: the handle to the controller object for which to return the
  630. * suggested start timeout.
  631. *
  632. * This method returns the number of milliseconds for the suggested start
  633. * operation timeout.
  634. */
  635. static u32 scic_controller_get_suggested_start_timeout(
  636. struct scic_sds_controller *sc)
  637. {
  638. /* Validate the user supplied parameters. */
  639. if (sc == NULL)
  640. return 0;
  641. /*
  642. * The suggested minimum timeout value for a controller start operation:
  643. *
  644. * Signature FIS Timeout
  645. * + Phy Start Timeout
  646. * + Number of Phy Spin Up Intervals
  647. * ---------------------------------
  648. * Number of milliseconds for the controller start operation.
  649. *
  650. * NOTE: The number of phy spin up intervals will be equivalent
  651. * to the number of phys divided by the number phys allowed
  652. * per interval - 1 (once OEM parameters are supported).
  653. * Currently we assume only 1 phy per interval. */
  654. return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  655. + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
  656. + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  657. }
  658. static void scic_controller_enable_interrupts(
  659. struct scic_sds_controller *scic)
  660. {
  661. BUG_ON(scic->smu_registers == NULL);
  662. writel(0, &scic->smu_registers->interrupt_mask);
  663. }
  664. void scic_controller_disable_interrupts(
  665. struct scic_sds_controller *scic)
  666. {
  667. BUG_ON(scic->smu_registers == NULL);
  668. writel(0xffffffff, &scic->smu_registers->interrupt_mask);
  669. }
  670. static void scic_sds_controller_enable_port_task_scheduler(
  671. struct scic_sds_controller *scic)
  672. {
  673. u32 port_task_scheduler_value;
  674. port_task_scheduler_value =
  675. readl(&scic->scu_registers->peg0.ptsg.control);
  676. port_task_scheduler_value |=
  677. (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
  678. SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
  679. writel(port_task_scheduler_value,
  680. &scic->scu_registers->peg0.ptsg.control);
  681. }
  682. static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
  683. {
  684. u32 task_assignment;
  685. /*
  686. * Assign all the TCs to function 0
  687. * TODO: Do we actually need to read this register to write it back?
  688. */
  689. task_assignment =
  690. readl(&scic->smu_registers->task_context_assignment[0]);
  691. task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
  692. (SMU_TCA_GEN_VAL(ENDING, scic->task_context_entries - 1)) |
  693. (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
  694. writel(task_assignment,
  695. &scic->smu_registers->task_context_assignment[0]);
  696. }
  697. static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
  698. {
  699. u32 index;
  700. u32 completion_queue_control_value;
  701. u32 completion_queue_get_value;
  702. u32 completion_queue_put_value;
  703. scic->completion_queue_get = 0;
  704. completion_queue_control_value = (
  705. SMU_CQC_QUEUE_LIMIT_SET(scic->completion_queue_entries - 1)
  706. | SMU_CQC_EVENT_LIMIT_SET(scic->completion_event_entries - 1)
  707. );
  708. writel(completion_queue_control_value,
  709. &scic->smu_registers->completion_queue_control);
  710. /* Set the completion queue get pointer and enable the queue */
  711. completion_queue_get_value = (
  712. (SMU_CQGR_GEN_VAL(POINTER, 0))
  713. | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
  714. | (SMU_CQGR_GEN_BIT(ENABLE))
  715. | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
  716. );
  717. writel(completion_queue_get_value,
  718. &scic->smu_registers->completion_queue_get);
  719. /* Set the completion queue put pointer */
  720. completion_queue_put_value = (
  721. (SMU_CQPR_GEN_VAL(POINTER, 0))
  722. | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
  723. );
  724. writel(completion_queue_put_value,
  725. &scic->smu_registers->completion_queue_put);
  726. /* Initialize the cycle bit of the completion queue entries */
  727. for (index = 0; index < scic->completion_queue_entries; index++) {
  728. /*
  729. * If get.cycle_bit != completion_queue.cycle_bit
  730. * its not a valid completion queue entry
  731. * so at system start all entries are invalid */
  732. scic->completion_queue[index] = 0x80000000;
  733. }
  734. }
  735. static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
  736. {
  737. u32 frame_queue_control_value;
  738. u32 frame_queue_get_value;
  739. u32 frame_queue_put_value;
  740. /* Write the queue size */
  741. frame_queue_control_value =
  742. SCU_UFQC_GEN_VAL(QUEUE_SIZE,
  743. scic->uf_control.address_table.count);
  744. writel(frame_queue_control_value,
  745. &scic->scu_registers->sdma.unsolicited_frame_queue_control);
  746. /* Setup the get pointer for the unsolicited frame queue */
  747. frame_queue_get_value = (
  748. SCU_UFQGP_GEN_VAL(POINTER, 0)
  749. | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
  750. );
  751. writel(frame_queue_get_value,
  752. &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
  753. /* Setup the put pointer for the unsolicited frame queue */
  754. frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
  755. writel(frame_queue_put_value,
  756. &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
  757. }
  758. /**
  759. * This method will attempt to transition into the ready state for the
  760. * controller and indicate that the controller start operation has completed
  761. * if all criteria are met.
  762. * @scic: This parameter indicates the controller object for which
  763. * to transition to ready.
  764. * @status: This parameter indicates the status value to be pass into the call
  765. * to scic_cb_controller_start_complete().
  766. *
  767. * none.
  768. */
  769. static void scic_sds_controller_transition_to_ready(
  770. struct scic_sds_controller *scic,
  771. enum sci_status status)
  772. {
  773. struct isci_host *ihost = scic_to_ihost(scic);
  774. if (scic->state_machine.current_state_id ==
  775. SCI_BASE_CONTROLLER_STATE_STARTING) {
  776. /*
  777. * We move into the ready state, because some of the phys/ports
  778. * may be up and operational.
  779. */
  780. sci_base_state_machine_change_state(&scic->state_machine,
  781. SCI_BASE_CONTROLLER_STATE_READY);
  782. isci_host_start_complete(ihost, status);
  783. }
  784. }
  785. static void scic_sds_controller_phy_timer_stop(struct scic_sds_controller *scic)
  786. {
  787. isci_timer_stop(scic->phy_startup_timer);
  788. scic->phy_startup_timer_pending = false;
  789. }
  790. static void scic_sds_controller_phy_timer_start(struct scic_sds_controller *scic)
  791. {
  792. isci_timer_start(scic->phy_startup_timer,
  793. SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
  794. scic->phy_startup_timer_pending = true;
  795. }
  796. /**
  797. * scic_sds_controller_start_next_phy - start phy
  798. * @scic: controller
  799. *
  800. * If all the phys have been started, then attempt to transition the
  801. * controller to the READY state and inform the user
  802. * (scic_cb_controller_start_complete()).
  803. */
  804. static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
  805. {
  806. struct isci_host *ihost = scic_to_ihost(scic);
  807. struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
  808. struct scic_sds_phy *sci_phy;
  809. enum sci_status status;
  810. status = SCI_SUCCESS;
  811. if (scic->phy_startup_timer_pending)
  812. return status;
  813. if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
  814. bool is_controller_start_complete = true;
  815. u32 state;
  816. u8 index;
  817. for (index = 0; index < SCI_MAX_PHYS; index++) {
  818. sci_phy = &ihost->phys[index].sci;
  819. state = sci_phy->state_machine.current_state_id;
  820. if (!scic_sds_phy_get_port(sci_phy))
  821. continue;
  822. /* The controller start operation is complete iff:
  823. * - all links have been given an opportunity to start
  824. * - have no indication of a connected device
  825. * - have an indication of a connected device and it has
  826. * finished the link training process.
  827. */
  828. if ((sci_phy->is_in_link_training == false &&
  829. state == SCI_BASE_PHY_STATE_INITIAL) ||
  830. (sci_phy->is_in_link_training == false &&
  831. state == SCI_BASE_PHY_STATE_STOPPED) ||
  832. (sci_phy->is_in_link_training == true &&
  833. state == SCI_BASE_PHY_STATE_STARTING)) {
  834. is_controller_start_complete = false;
  835. break;
  836. }
  837. }
  838. /*
  839. * The controller has successfully finished the start process.
  840. * Inform the SCI Core user and transition to the READY state. */
  841. if (is_controller_start_complete == true) {
  842. scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
  843. scic_sds_controller_phy_timer_stop(scic);
  844. }
  845. } else {
  846. sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
  847. if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  848. if (scic_sds_phy_get_port(sci_phy) == NULL) {
  849. scic->next_phy_to_start++;
  850. /* Caution recursion ahead be forwarned
  851. *
  852. * The PHY was never added to a PORT in MPC mode
  853. * so start the next phy in sequence This phy
  854. * will never go link up and will not draw power
  855. * the OEM parameters either configured the phy
  856. * incorrectly for the PORT or it was never
  857. * assigned to a PORT
  858. */
  859. return scic_sds_controller_start_next_phy(scic);
  860. }
  861. }
  862. status = scic_sds_phy_start(sci_phy);
  863. if (status == SCI_SUCCESS) {
  864. scic_sds_controller_phy_timer_start(scic);
  865. } else {
  866. dev_warn(scic_to_dev(scic),
  867. "%s: Controller stop operation failed "
  868. "to stop phy %d because of status "
  869. "%d.\n",
  870. __func__,
  871. ihost->phys[scic->next_phy_to_start].sci.phy_index,
  872. status);
  873. }
  874. scic->next_phy_to_start++;
  875. }
  876. return status;
  877. }
  878. static void scic_sds_controller_phy_startup_timeout_handler(void *_scic)
  879. {
  880. struct scic_sds_controller *scic = _scic;
  881. enum sci_status status;
  882. scic->phy_startup_timer_pending = false;
  883. status = SCI_FAILURE;
  884. while (status != SCI_SUCCESS)
  885. status = scic_sds_controller_start_next_phy(scic);
  886. }
  887. static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
  888. u32 timeout)
  889. {
  890. struct isci_host *ihost = scic_to_ihost(scic);
  891. enum sci_status result;
  892. u16 index;
  893. if (scic->state_machine.current_state_id !=
  894. SCI_BASE_CONTROLLER_STATE_INITIALIZED) {
  895. dev_warn(scic_to_dev(scic),
  896. "SCIC Controller start operation requested in "
  897. "invalid state\n");
  898. return SCI_FAILURE_INVALID_STATE;
  899. }
  900. /* Build the TCi free pool */
  901. sci_pool_initialize(scic->tci_pool);
  902. for (index = 0; index < scic->task_context_entries; index++)
  903. sci_pool_put(scic->tci_pool, index);
  904. /* Build the RNi free pool */
  905. scic_sds_remote_node_table_initialize(
  906. &scic->available_remote_nodes,
  907. scic->remote_node_entries);
  908. /*
  909. * Before anything else lets make sure we will not be
  910. * interrupted by the hardware.
  911. */
  912. scic_controller_disable_interrupts(scic);
  913. /* Enable the port task scheduler */
  914. scic_sds_controller_enable_port_task_scheduler(scic);
  915. /* Assign all the task entries to scic physical function */
  916. scic_sds_controller_assign_task_entries(scic);
  917. /* Now initialize the completion queue */
  918. scic_sds_controller_initialize_completion_queue(scic);
  919. /* Initialize the unsolicited frame queue for use */
  920. scic_sds_controller_initialize_unsolicited_frame_queue(scic);
  921. /* Start all of the ports on this controller */
  922. for (index = 0; index < scic->logical_port_entries; index++) {
  923. struct scic_sds_port *sci_port = &ihost->ports[index].sci;
  924. result = sci_port->state_handlers->start_handler(sci_port);
  925. if (result)
  926. return result;
  927. }
  928. scic_sds_controller_start_next_phy(scic);
  929. isci_timer_start(scic->timeout_timer, timeout);
  930. sci_base_state_machine_change_state(&scic->state_machine,
  931. SCI_BASE_CONTROLLER_STATE_STARTING);
  932. return SCI_SUCCESS;
  933. }
  934. void isci_host_scan_start(struct Scsi_Host *shost)
  935. {
  936. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  937. unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
  938. set_bit(IHOST_START_PENDING, &ihost->flags);
  939. spin_lock_irq(&ihost->scic_lock);
  940. scic_controller_start(&ihost->sci, tmo);
  941. scic_controller_enable_interrupts(&ihost->sci);
  942. spin_unlock_irq(&ihost->scic_lock);
  943. }
  944. static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
  945. {
  946. isci_host_change_state(ihost, isci_stopped);
  947. scic_controller_disable_interrupts(&ihost->sci);
  948. clear_bit(IHOST_STOP_PENDING, &ihost->flags);
  949. wake_up(&ihost->eventq);
  950. }
  951. static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
  952. {
  953. /* Empty out the completion queue */
  954. if (scic_sds_controller_completion_queue_has_entries(scic))
  955. scic_sds_controller_process_completions(scic);
  956. /* Clear the interrupt and enable all interrupts again */
  957. writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
  958. /* Could we write the value of SMU_ISR_COMPLETION? */
  959. writel(0xFF000000, &scic->smu_registers->interrupt_mask);
  960. writel(0, &scic->smu_registers->interrupt_mask);
  961. }
  962. /**
  963. * isci_host_completion_routine() - This function is the delayed service
  964. * routine that calls the sci core library's completion handler. It's
  965. * scheduled as a tasklet from the interrupt service routine when interrupts
  966. * in use, or set as the timeout function in polled mode.
  967. * @data: This parameter specifies the ISCI host object
  968. *
  969. */
  970. static void isci_host_completion_routine(unsigned long data)
  971. {
  972. struct isci_host *isci_host = (struct isci_host *)data;
  973. struct list_head completed_request_list;
  974. struct list_head errored_request_list;
  975. struct list_head *current_position;
  976. struct list_head *next_position;
  977. struct isci_request *request;
  978. struct isci_request *next_request;
  979. struct sas_task *task;
  980. INIT_LIST_HEAD(&completed_request_list);
  981. INIT_LIST_HEAD(&errored_request_list);
  982. spin_lock_irq(&isci_host->scic_lock);
  983. scic_sds_controller_completion_handler(&isci_host->sci);
  984. /* Take the lists of completed I/Os from the host. */
  985. list_splice_init(&isci_host->requests_to_complete,
  986. &completed_request_list);
  987. /* Take the list of errored I/Os from the host. */
  988. list_splice_init(&isci_host->requests_to_errorback,
  989. &errored_request_list);
  990. spin_unlock_irq(&isci_host->scic_lock);
  991. /* Process any completions in the lists. */
  992. list_for_each_safe(current_position, next_position,
  993. &completed_request_list) {
  994. request = list_entry(current_position, struct isci_request,
  995. completed_node);
  996. task = isci_request_access_task(request);
  997. /* Normal notification (task_done) */
  998. dev_dbg(&isci_host->pdev->dev,
  999. "%s: Normal - request/task = %p/%p\n",
  1000. __func__,
  1001. request,
  1002. task);
  1003. /* Return the task to libsas */
  1004. if (task != NULL) {
  1005. task->lldd_task = NULL;
  1006. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1007. /* If the task is already in the abort path,
  1008. * the task_done callback cannot be called.
  1009. */
  1010. task->task_done(task);
  1011. }
  1012. }
  1013. /* Free the request object. */
  1014. isci_request_free(isci_host, request);
  1015. }
  1016. list_for_each_entry_safe(request, next_request, &errored_request_list,
  1017. completed_node) {
  1018. task = isci_request_access_task(request);
  1019. /* Use sas_task_abort */
  1020. dev_warn(&isci_host->pdev->dev,
  1021. "%s: Error - request/task = %p/%p\n",
  1022. __func__,
  1023. request,
  1024. task);
  1025. if (task != NULL) {
  1026. /* Put the task into the abort path if it's not there
  1027. * already.
  1028. */
  1029. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
  1030. sas_task_abort(task);
  1031. } else {
  1032. /* This is a case where the request has completed with a
  1033. * status such that it needed further target servicing,
  1034. * but the sas_task reference has already been removed
  1035. * from the request. Since it was errored, it was not
  1036. * being aborted, so there is nothing to do except free
  1037. * it.
  1038. */
  1039. spin_lock_irq(&isci_host->scic_lock);
  1040. /* Remove the request from the remote device's list
  1041. * of pending requests.
  1042. */
  1043. list_del_init(&request->dev_node);
  1044. spin_unlock_irq(&isci_host->scic_lock);
  1045. /* Free the request object. */
  1046. isci_request_free(isci_host, request);
  1047. }
  1048. }
  1049. }
  1050. /**
  1051. * scic_controller_stop() - This method will stop an individual controller
  1052. * object.This method will invoke the associated user callback upon
  1053. * completion. The completion callback is called when the following
  1054. * conditions are met: -# the method return status is SCI_SUCCESS. -# the
  1055. * controller has been quiesced. This method will ensure that all IO
  1056. * requests are quiesced, phys are stopped, and all additional operation by
  1057. * the hardware is halted.
  1058. * @controller: the handle to the controller object to stop.
  1059. * @timeout: This parameter specifies the number of milliseconds in which the
  1060. * stop operation should complete.
  1061. *
  1062. * The controller must be in the STARTED or STOPPED state. Indicate if the
  1063. * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
  1064. * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
  1065. * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
  1066. * controller is not either in the STARTED or STOPPED states.
  1067. */
  1068. static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
  1069. u32 timeout)
  1070. {
  1071. if (scic->state_machine.current_state_id !=
  1072. SCI_BASE_CONTROLLER_STATE_READY) {
  1073. dev_warn(scic_to_dev(scic),
  1074. "SCIC Controller stop operation requested in "
  1075. "invalid state\n");
  1076. return SCI_FAILURE_INVALID_STATE;
  1077. }
  1078. isci_timer_start(scic->timeout_timer, timeout);
  1079. sci_base_state_machine_change_state(&scic->state_machine,
  1080. SCI_BASE_CONTROLLER_STATE_STOPPING);
  1081. return SCI_SUCCESS;
  1082. }
  1083. /**
  1084. * scic_controller_reset() - This method will reset the supplied core
  1085. * controller regardless of the state of said controller. This operation is
  1086. * considered destructive. In other words, all current operations are wiped
  1087. * out. No IO completions for outstanding devices occur. Outstanding IO
  1088. * requests are not aborted or completed at the actual remote device.
  1089. * @controller: the handle to the controller object to reset.
  1090. *
  1091. * Indicate if the controller reset method succeeded or failed in some way.
  1092. * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
  1093. * the controller reset operation is unable to complete.
  1094. */
  1095. static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
  1096. {
  1097. switch (scic->state_machine.current_state_id) {
  1098. case SCI_BASE_CONTROLLER_STATE_RESET:
  1099. case SCI_BASE_CONTROLLER_STATE_READY:
  1100. case SCI_BASE_CONTROLLER_STATE_STOPPED:
  1101. case SCI_BASE_CONTROLLER_STATE_FAILED:
  1102. /*
  1103. * The reset operation is not a graceful cleanup, just
  1104. * perform the state transition.
  1105. */
  1106. sci_base_state_machine_change_state(&scic->state_machine,
  1107. SCI_BASE_CONTROLLER_STATE_RESETTING);
  1108. return SCI_SUCCESS;
  1109. default:
  1110. dev_warn(scic_to_dev(scic),
  1111. "SCIC Controller reset operation requested in "
  1112. "invalid state\n");
  1113. return SCI_FAILURE_INVALID_STATE;
  1114. }
  1115. }
  1116. void isci_host_deinit(struct isci_host *ihost)
  1117. {
  1118. int i;
  1119. isci_host_change_state(ihost, isci_stopping);
  1120. for (i = 0; i < SCI_MAX_PORTS; i++) {
  1121. struct isci_port *iport = &ihost->ports[i];
  1122. struct isci_remote_device *idev, *d;
  1123. list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
  1124. isci_remote_device_change_state(idev, isci_stopping);
  1125. isci_remote_device_stop(ihost, idev);
  1126. }
  1127. }
  1128. set_bit(IHOST_STOP_PENDING, &ihost->flags);
  1129. spin_lock_irq(&ihost->scic_lock);
  1130. scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
  1131. spin_unlock_irq(&ihost->scic_lock);
  1132. wait_for_stop(ihost);
  1133. scic_controller_reset(&ihost->sci);
  1134. isci_timer_list_destroy(ihost);
  1135. }
  1136. static void __iomem *scu_base(struct isci_host *isci_host)
  1137. {
  1138. struct pci_dev *pdev = isci_host->pdev;
  1139. int id = isci_host->id;
  1140. return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
  1141. }
  1142. static void __iomem *smu_base(struct isci_host *isci_host)
  1143. {
  1144. struct pci_dev *pdev = isci_host->pdev;
  1145. int id = isci_host->id;
  1146. return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
  1147. }
  1148. static void isci_user_parameters_get(
  1149. struct isci_host *isci_host,
  1150. union scic_user_parameters *scic_user_params)
  1151. {
  1152. struct scic_sds_user_parameters *u = &scic_user_params->sds1;
  1153. int i;
  1154. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1155. struct sci_phy_user_params *u_phy = &u->phys[i];
  1156. u_phy->max_speed_generation = phy_gen;
  1157. /* we are not exporting these for now */
  1158. u_phy->align_insertion_frequency = 0x7f;
  1159. u_phy->in_connection_align_insertion_frequency = 0xff;
  1160. u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
  1161. }
  1162. u->stp_inactivity_timeout = stp_inactive_to;
  1163. u->ssp_inactivity_timeout = ssp_inactive_to;
  1164. u->stp_max_occupancy_timeout = stp_max_occ_to;
  1165. u->ssp_max_occupancy_timeout = ssp_max_occ_to;
  1166. u->no_outbound_task_timeout = no_outbound_task_to;
  1167. u->max_number_concurrent_device_spin_up = max_concurr_spinup;
  1168. }
  1169. static void scic_sds_controller_initial_state_enter(void *object)
  1170. {
  1171. struct scic_sds_controller *scic = object;
  1172. sci_base_state_machine_change_state(&scic->state_machine,
  1173. SCI_BASE_CONTROLLER_STATE_RESET);
  1174. }
  1175. static inline void scic_sds_controller_starting_state_exit(void *object)
  1176. {
  1177. struct scic_sds_controller *scic = object;
  1178. isci_timer_stop(scic->timeout_timer);
  1179. }
  1180. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
  1181. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
  1182. #define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
  1183. #define INTERRUPT_COALESCE_NUMBER_MAX 256
  1184. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
  1185. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
  1186. /**
  1187. * scic_controller_set_interrupt_coalescence() - This method allows the user to
  1188. * configure the interrupt coalescence.
  1189. * @controller: This parameter represents the handle to the controller object
  1190. * for which its interrupt coalesce register is overridden.
  1191. * @coalesce_number: Used to control the number of entries in the Completion
  1192. * Queue before an interrupt is generated. If the number of entries exceed
  1193. * this number, an interrupt will be generated. The valid range of the input
  1194. * is [0, 256]. A setting of 0 results in coalescing being disabled.
  1195. * @coalesce_timeout: Timeout value in microseconds. The valid range of the
  1196. * input is [0, 2700000] . A setting of 0 is allowed and results in no
  1197. * interrupt coalescing timeout.
  1198. *
  1199. * Indicate if the user successfully set the interrupt coalesce parameters.
  1200. * SCI_SUCCESS The user successfully updated the interrutp coalescence.
  1201. * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
  1202. */
  1203. static enum sci_status scic_controller_set_interrupt_coalescence(
  1204. struct scic_sds_controller *scic_controller,
  1205. u32 coalesce_number,
  1206. u32 coalesce_timeout)
  1207. {
  1208. u8 timeout_encode = 0;
  1209. u32 min = 0;
  1210. u32 max = 0;
  1211. /* Check if the input parameters fall in the range. */
  1212. if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
  1213. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1214. /*
  1215. * Defined encoding for interrupt coalescing timeout:
  1216. * Value Min Max Units
  1217. * ----- --- --- -----
  1218. * 0 - - Disabled
  1219. * 1 13.3 20.0 ns
  1220. * 2 26.7 40.0
  1221. * 3 53.3 80.0
  1222. * 4 106.7 160.0
  1223. * 5 213.3 320.0
  1224. * 6 426.7 640.0
  1225. * 7 853.3 1280.0
  1226. * 8 1.7 2.6 us
  1227. * 9 3.4 5.1
  1228. * 10 6.8 10.2
  1229. * 11 13.7 20.5
  1230. * 12 27.3 41.0
  1231. * 13 54.6 81.9
  1232. * 14 109.2 163.8
  1233. * 15 218.5 327.7
  1234. * 16 436.9 655.4
  1235. * 17 873.8 1310.7
  1236. * 18 1.7 2.6 ms
  1237. * 19 3.5 5.2
  1238. * 20 7.0 10.5
  1239. * 21 14.0 21.0
  1240. * 22 28.0 41.9
  1241. * 23 55.9 83.9
  1242. * 24 111.8 167.8
  1243. * 25 223.7 335.5
  1244. * 26 447.4 671.1
  1245. * 27 894.8 1342.2
  1246. * 28 1.8 2.7 s
  1247. * Others Undefined */
  1248. /*
  1249. * Use the table above to decide the encode of interrupt coalescing timeout
  1250. * value for register writing. */
  1251. if (coalesce_timeout == 0)
  1252. timeout_encode = 0;
  1253. else{
  1254. /* make the timeout value in unit of (10 ns). */
  1255. coalesce_timeout = coalesce_timeout * 100;
  1256. min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
  1257. max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
  1258. /* get the encode of timeout for register writing. */
  1259. for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
  1260. timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
  1261. timeout_encode++) {
  1262. if (min <= coalesce_timeout && max > coalesce_timeout)
  1263. break;
  1264. else if (coalesce_timeout >= max && coalesce_timeout < min * 2
  1265. && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
  1266. if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
  1267. break;
  1268. else{
  1269. timeout_encode++;
  1270. break;
  1271. }
  1272. } else {
  1273. max = max * 2;
  1274. min = min * 2;
  1275. }
  1276. }
  1277. if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
  1278. /* the value is out of range. */
  1279. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1280. }
  1281. writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
  1282. SMU_ICC_GEN_VAL(TIMER, timeout_encode),
  1283. &scic_controller->smu_registers->interrupt_coalesce_control);
  1284. scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
  1285. scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
  1286. return SCI_SUCCESS;
  1287. }
  1288. static void scic_sds_controller_ready_state_enter(void *object)
  1289. {
  1290. struct scic_sds_controller *scic = object;
  1291. /* set the default interrupt coalescence number and timeout value. */
  1292. scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
  1293. }
  1294. static void scic_sds_controller_ready_state_exit(void *object)
  1295. {
  1296. struct scic_sds_controller *scic = object;
  1297. /* disable interrupt coalescence. */
  1298. scic_controller_set_interrupt_coalescence(scic, 0, 0);
  1299. }
  1300. static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
  1301. {
  1302. u32 index;
  1303. enum sci_status status;
  1304. enum sci_status phy_status;
  1305. struct isci_host *ihost = scic_to_ihost(scic);
  1306. status = SCI_SUCCESS;
  1307. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1308. phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
  1309. if (phy_status != SCI_SUCCESS &&
  1310. phy_status != SCI_FAILURE_INVALID_STATE) {
  1311. status = SCI_FAILURE;
  1312. dev_warn(scic_to_dev(scic),
  1313. "%s: Controller stop operation failed to stop "
  1314. "phy %d because of status %d.\n",
  1315. __func__,
  1316. ihost->phys[index].sci.phy_index, phy_status);
  1317. }
  1318. }
  1319. return status;
  1320. }
  1321. static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
  1322. {
  1323. u32 index;
  1324. enum sci_status port_status;
  1325. enum sci_status status = SCI_SUCCESS;
  1326. struct isci_host *ihost = scic_to_ihost(scic);
  1327. for (index = 0; index < scic->logical_port_entries; index++) {
  1328. struct scic_sds_port *sci_port = &ihost->ports[index].sci;
  1329. scic_sds_port_handler_t stop;
  1330. stop = sci_port->state_handlers->stop_handler;
  1331. port_status = stop(sci_port);
  1332. if ((port_status != SCI_SUCCESS) &&
  1333. (port_status != SCI_FAILURE_INVALID_STATE)) {
  1334. status = SCI_FAILURE;
  1335. dev_warn(scic_to_dev(scic),
  1336. "%s: Controller stop operation failed to "
  1337. "stop port %d because of status %d.\n",
  1338. __func__,
  1339. sci_port->logical_port_index,
  1340. port_status);
  1341. }
  1342. }
  1343. return status;
  1344. }
  1345. static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
  1346. {
  1347. u32 index;
  1348. enum sci_status status;
  1349. enum sci_status device_status;
  1350. status = SCI_SUCCESS;
  1351. for (index = 0; index < scic->remote_node_entries; index++) {
  1352. if (scic->device_table[index] != NULL) {
  1353. /* / @todo What timeout value do we want to provide to this request? */
  1354. device_status = scic_remote_device_stop(scic->device_table[index], 0);
  1355. if ((device_status != SCI_SUCCESS) &&
  1356. (device_status != SCI_FAILURE_INVALID_STATE)) {
  1357. dev_warn(scic_to_dev(scic),
  1358. "%s: Controller stop operation failed "
  1359. "to stop device 0x%p because of "
  1360. "status %d.\n",
  1361. __func__,
  1362. scic->device_table[index], device_status);
  1363. }
  1364. }
  1365. }
  1366. return status;
  1367. }
  1368. static void scic_sds_controller_stopping_state_enter(void *object)
  1369. {
  1370. struct scic_sds_controller *scic = object;
  1371. /* Stop all of the components for this controller */
  1372. scic_sds_controller_stop_phys(scic);
  1373. scic_sds_controller_stop_ports(scic);
  1374. scic_sds_controller_stop_devices(scic);
  1375. }
  1376. static void scic_sds_controller_stopping_state_exit(void *object)
  1377. {
  1378. struct scic_sds_controller *scic = object;
  1379. isci_timer_stop(scic->timeout_timer);
  1380. }
  1381. /**
  1382. * scic_sds_controller_reset_hardware() -
  1383. *
  1384. * This method will reset the controller hardware.
  1385. */
  1386. static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
  1387. {
  1388. /* Disable interrupts so we dont take any spurious interrupts */
  1389. scic_controller_disable_interrupts(scic);
  1390. /* Reset the SCU */
  1391. writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
  1392. /* Delay for 1ms to before clearing the CQP and UFQPR. */
  1393. udelay(1000);
  1394. /* The write to the CQGR clears the CQP */
  1395. writel(0x00000000, &scic->smu_registers->completion_queue_get);
  1396. /* The write to the UFQGP clears the UFQPR */
  1397. writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
  1398. }
  1399. static void scic_sds_controller_resetting_state_enter(void *object)
  1400. {
  1401. struct scic_sds_controller *scic = object;
  1402. scic_sds_controller_reset_hardware(scic);
  1403. sci_base_state_machine_change_state(&scic->state_machine,
  1404. SCI_BASE_CONTROLLER_STATE_RESET);
  1405. }
  1406. static const struct sci_base_state scic_sds_controller_state_table[] = {
  1407. [SCI_BASE_CONTROLLER_STATE_INITIAL] = {
  1408. .enter_state = scic_sds_controller_initial_state_enter,
  1409. },
  1410. [SCI_BASE_CONTROLLER_STATE_RESET] = {},
  1411. [SCI_BASE_CONTROLLER_STATE_INITIALIZING] = {},
  1412. [SCI_BASE_CONTROLLER_STATE_INITIALIZED] = {},
  1413. [SCI_BASE_CONTROLLER_STATE_STARTING] = {
  1414. .exit_state = scic_sds_controller_starting_state_exit,
  1415. },
  1416. [SCI_BASE_CONTROLLER_STATE_READY] = {
  1417. .enter_state = scic_sds_controller_ready_state_enter,
  1418. .exit_state = scic_sds_controller_ready_state_exit,
  1419. },
  1420. [SCI_BASE_CONTROLLER_STATE_RESETTING] = {
  1421. .enter_state = scic_sds_controller_resetting_state_enter,
  1422. },
  1423. [SCI_BASE_CONTROLLER_STATE_STOPPING] = {
  1424. .enter_state = scic_sds_controller_stopping_state_enter,
  1425. .exit_state = scic_sds_controller_stopping_state_exit,
  1426. },
  1427. [SCI_BASE_CONTROLLER_STATE_STOPPED] = {},
  1428. [SCI_BASE_CONTROLLER_STATE_FAILED] = {}
  1429. };
  1430. static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
  1431. {
  1432. /* these defaults are overridden by the platform / firmware */
  1433. struct isci_host *ihost = scic_to_ihost(scic);
  1434. u16 index;
  1435. /* Default to APC mode. */
  1436. scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
  1437. /* Default to APC mode. */
  1438. scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
  1439. /* Default to no SSC operation. */
  1440. scic->oem_parameters.sds1.controller.do_enable_ssc = false;
  1441. /* Initialize all of the port parameter information to narrow ports. */
  1442. for (index = 0; index < SCI_MAX_PORTS; index++) {
  1443. scic->oem_parameters.sds1.ports[index].phy_mask = 0;
  1444. }
  1445. /* Initialize all of the phy parameter information. */
  1446. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1447. /* Default to 6G (i.e. Gen 3) for now. */
  1448. scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
  1449. /* the frequencies cannot be 0 */
  1450. scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
  1451. scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
  1452. scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
  1453. /*
  1454. * Previous Vitesse based expanders had a arbitration issue that
  1455. * is worked around by having the upper 32-bits of SAS address
  1456. * with a value greater then the Vitesse company identifier.
  1457. * Hence, usage of 0x5FCFFFFF. */
  1458. scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
  1459. scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
  1460. }
  1461. scic->user_parameters.sds1.stp_inactivity_timeout = 5;
  1462. scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
  1463. scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
  1464. scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
  1465. scic->user_parameters.sds1.no_outbound_task_timeout = 20;
  1466. }
  1467. /**
  1468. * scic_controller_construct() - This method will attempt to construct a
  1469. * controller object utilizing the supplied parameter information.
  1470. * @c: This parameter specifies the controller to be constructed.
  1471. * @scu_base: mapped base address of the scu registers
  1472. * @smu_base: mapped base address of the smu registers
  1473. *
  1474. * Indicate if the controller was successfully constructed or if it failed in
  1475. * some way. SCI_SUCCESS This value is returned if the controller was
  1476. * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
  1477. * if the interrupt coalescence timer may cause SAS compliance issues for SMP
  1478. * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
  1479. * This value is returned if the controller does not support the supplied type.
  1480. * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
  1481. * controller does not support the supplied initialization data version.
  1482. */
  1483. static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
  1484. void __iomem *scu_base,
  1485. void __iomem *smu_base)
  1486. {
  1487. struct isci_host *ihost = scic_to_ihost(scic);
  1488. u8 i;
  1489. sci_base_state_machine_construct(&scic->state_machine,
  1490. scic, scic_sds_controller_state_table,
  1491. SCI_BASE_CONTROLLER_STATE_INITIAL);
  1492. sci_base_state_machine_start(&scic->state_machine);
  1493. scic->scu_registers = scu_base;
  1494. scic->smu_registers = smu_base;
  1495. scic_sds_port_configuration_agent_construct(&scic->port_agent);
  1496. /* Construct the ports for this controller */
  1497. for (i = 0; i < SCI_MAX_PORTS; i++)
  1498. scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
  1499. scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
  1500. /* Construct the phys for this controller */
  1501. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1502. /* Add all the PHYs to the dummy port */
  1503. scic_sds_phy_construct(&ihost->phys[i].sci,
  1504. &ihost->ports[SCI_MAX_PORTS].sci, i);
  1505. }
  1506. scic->invalid_phy_mask = 0;
  1507. /* Set the default maximum values */
  1508. scic->completion_event_entries = SCU_EVENT_COUNT;
  1509. scic->completion_queue_entries = SCU_COMPLETION_QUEUE_COUNT;
  1510. scic->remote_node_entries = SCI_MAX_REMOTE_DEVICES;
  1511. scic->logical_port_entries = SCI_MAX_PORTS;
  1512. scic->task_context_entries = SCU_IO_REQUEST_COUNT;
  1513. scic->uf_control.buffers.count = SCU_UNSOLICITED_FRAME_COUNT;
  1514. scic->uf_control.address_table.count = SCU_UNSOLICITED_FRAME_COUNT;
  1515. /* Initialize the User and OEM parameters to default values. */
  1516. scic_sds_controller_set_default_config_parameters(scic);
  1517. return scic_controller_reset(scic);
  1518. }
  1519. int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
  1520. {
  1521. int i;
  1522. for (i = 0; i < SCI_MAX_PORTS; i++)
  1523. if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
  1524. return -EINVAL;
  1525. for (i = 0; i < SCI_MAX_PHYS; i++)
  1526. if (oem->phys[i].sas_address.high == 0 &&
  1527. oem->phys[i].sas_address.low == 0)
  1528. return -EINVAL;
  1529. if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
  1530. for (i = 0; i < SCI_MAX_PHYS; i++)
  1531. if (oem->ports[i].phy_mask != 0)
  1532. return -EINVAL;
  1533. } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  1534. u8 phy_mask = 0;
  1535. for (i = 0; i < SCI_MAX_PHYS; i++)
  1536. phy_mask |= oem->ports[i].phy_mask;
  1537. if (phy_mask == 0)
  1538. return -EINVAL;
  1539. } else
  1540. return -EINVAL;
  1541. if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
  1542. return -EINVAL;
  1543. return 0;
  1544. }
  1545. static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
  1546. union scic_oem_parameters *scic_parms)
  1547. {
  1548. u32 state = scic->state_machine.current_state_id;
  1549. if (state == SCI_BASE_CONTROLLER_STATE_RESET ||
  1550. state == SCI_BASE_CONTROLLER_STATE_INITIALIZING ||
  1551. state == SCI_BASE_CONTROLLER_STATE_INITIALIZED) {
  1552. if (scic_oem_parameters_validate(&scic_parms->sds1))
  1553. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1554. scic->oem_parameters.sds1 = scic_parms->sds1;
  1555. return SCI_SUCCESS;
  1556. }
  1557. return SCI_FAILURE_INVALID_STATE;
  1558. }
  1559. void scic_oem_parameters_get(
  1560. struct scic_sds_controller *scic,
  1561. union scic_oem_parameters *scic_parms)
  1562. {
  1563. memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
  1564. }
  1565. static void scic_sds_controller_timeout_handler(void *_scic)
  1566. {
  1567. struct scic_sds_controller *scic = _scic;
  1568. struct isci_host *ihost = scic_to_ihost(scic);
  1569. struct sci_base_state_machine *sm = &scic->state_machine;
  1570. if (sm->current_state_id == SCI_BASE_CONTROLLER_STATE_STARTING)
  1571. scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
  1572. else if (sm->current_state_id == SCI_BASE_CONTROLLER_STATE_STOPPING) {
  1573. sci_base_state_machine_change_state(sm, SCI_BASE_CONTROLLER_STATE_FAILED);
  1574. isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
  1575. } else /* / @todo Now what do we want to do in this case? */
  1576. dev_err(scic_to_dev(scic),
  1577. "%s: Controller timer fired when controller was not "
  1578. "in a state being timed.\n",
  1579. __func__);
  1580. }
  1581. static enum sci_status scic_sds_controller_initialize_phy_startup(struct scic_sds_controller *scic)
  1582. {
  1583. struct isci_host *ihost = scic_to_ihost(scic);
  1584. scic->phy_startup_timer = isci_timer_create(ihost,
  1585. scic,
  1586. scic_sds_controller_phy_startup_timeout_handler);
  1587. if (scic->phy_startup_timer == NULL)
  1588. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  1589. else {
  1590. scic->next_phy_to_start = 0;
  1591. scic->phy_startup_timer_pending = false;
  1592. }
  1593. return SCI_SUCCESS;
  1594. }
  1595. static void scic_sds_controller_power_control_timer_start(struct scic_sds_controller *scic)
  1596. {
  1597. isci_timer_start(scic->power_control.timer,
  1598. SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1599. scic->power_control.timer_started = true;
  1600. }
  1601. static void scic_sds_controller_power_control_timer_stop(struct scic_sds_controller *scic)
  1602. {
  1603. if (scic->power_control.timer_started) {
  1604. isci_timer_stop(scic->power_control.timer);
  1605. scic->power_control.timer_started = false;
  1606. }
  1607. }
  1608. static void scic_sds_controller_power_control_timer_restart(struct scic_sds_controller *scic)
  1609. {
  1610. scic_sds_controller_power_control_timer_stop(scic);
  1611. scic_sds_controller_power_control_timer_start(scic);
  1612. }
  1613. static void scic_sds_controller_power_control_timer_handler(
  1614. void *controller)
  1615. {
  1616. struct scic_sds_controller *scic;
  1617. scic = (struct scic_sds_controller *)controller;
  1618. scic->power_control.phys_granted_power = 0;
  1619. if (scic->power_control.phys_waiting == 0) {
  1620. scic->power_control.timer_started = false;
  1621. } else {
  1622. struct scic_sds_phy *sci_phy = NULL;
  1623. u8 i;
  1624. for (i = 0;
  1625. (i < SCI_MAX_PHYS)
  1626. && (scic->power_control.phys_waiting != 0);
  1627. i++) {
  1628. if (scic->power_control.requesters[i] != NULL) {
  1629. if (scic->power_control.phys_granted_power <
  1630. scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
  1631. sci_phy = scic->power_control.requesters[i];
  1632. scic->power_control.requesters[i] = NULL;
  1633. scic->power_control.phys_waiting--;
  1634. scic->power_control.phys_granted_power++;
  1635. scic_sds_phy_consume_power_handler(sci_phy);
  1636. } else {
  1637. break;
  1638. }
  1639. }
  1640. }
  1641. /*
  1642. * It doesn't matter if the power list is empty, we need to start the
  1643. * timer in case another phy becomes ready.
  1644. */
  1645. scic_sds_controller_power_control_timer_start(scic);
  1646. }
  1647. }
  1648. /**
  1649. * This method inserts the phy in the stagger spinup control queue.
  1650. * @scic:
  1651. *
  1652. *
  1653. */
  1654. void scic_sds_controller_power_control_queue_insert(
  1655. struct scic_sds_controller *scic,
  1656. struct scic_sds_phy *sci_phy)
  1657. {
  1658. BUG_ON(sci_phy == NULL);
  1659. if (scic->power_control.phys_granted_power <
  1660. scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
  1661. scic->power_control.phys_granted_power++;
  1662. scic_sds_phy_consume_power_handler(sci_phy);
  1663. /*
  1664. * stop and start the power_control timer. When the timer fires, the
  1665. * no_of_phys_granted_power will be set to 0
  1666. */
  1667. scic_sds_controller_power_control_timer_restart(scic);
  1668. } else {
  1669. /* Add the phy in the waiting list */
  1670. scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
  1671. scic->power_control.phys_waiting++;
  1672. }
  1673. }
  1674. /**
  1675. * This method removes the phy from the stagger spinup control queue.
  1676. * @scic:
  1677. *
  1678. *
  1679. */
  1680. void scic_sds_controller_power_control_queue_remove(
  1681. struct scic_sds_controller *scic,
  1682. struct scic_sds_phy *sci_phy)
  1683. {
  1684. BUG_ON(sci_phy == NULL);
  1685. if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
  1686. scic->power_control.phys_waiting--;
  1687. }
  1688. scic->power_control.requesters[sci_phy->phy_index] = NULL;
  1689. }
  1690. #define AFE_REGISTER_WRITE_DELAY 10
  1691. /* Initialize the AFE for this phy index. We need to read the AFE setup from
  1692. * the OEM parameters
  1693. */
  1694. static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
  1695. {
  1696. const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
  1697. u32 afe_status;
  1698. u32 phy_id;
  1699. /* Clear DFX Status registers */
  1700. writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
  1701. udelay(AFE_REGISTER_WRITE_DELAY);
  1702. if (is_b0()) {
  1703. /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
  1704. * Timer, PM Stagger Timer */
  1705. writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
  1706. udelay(AFE_REGISTER_WRITE_DELAY);
  1707. }
  1708. /* Configure bias currents to normal */
  1709. if (is_a0())
  1710. writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
  1711. else if (is_a2())
  1712. writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
  1713. else if (is_b0())
  1714. writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
  1715. udelay(AFE_REGISTER_WRITE_DELAY);
  1716. /* Enable PLL */
  1717. if (is_b0())
  1718. writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
  1719. else
  1720. writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
  1721. udelay(AFE_REGISTER_WRITE_DELAY);
  1722. /* Wait for the PLL to lock */
  1723. do {
  1724. afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
  1725. udelay(AFE_REGISTER_WRITE_DELAY);
  1726. } while ((afe_status & 0x00001000) == 0);
  1727. if (is_a0() || is_a2()) {
  1728. /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
  1729. writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
  1730. udelay(AFE_REGISTER_WRITE_DELAY);
  1731. }
  1732. for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
  1733. const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
  1734. if (is_b0()) {
  1735. /* Configure transmitter SSC parameters */
  1736. writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
  1737. udelay(AFE_REGISTER_WRITE_DELAY);
  1738. } else {
  1739. /*
  1740. * All defaults, except the Receive Word Alignament/Comma Detect
  1741. * Enable....(0xe800) */
  1742. writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
  1743. udelay(AFE_REGISTER_WRITE_DELAY);
  1744. writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
  1745. udelay(AFE_REGISTER_WRITE_DELAY);
  1746. }
  1747. /*
  1748. * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
  1749. * & increase TX int & ext bias 20%....(0xe85c) */
  1750. if (is_a0())
  1751. writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1752. else if (is_a2())
  1753. writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1754. else {
  1755. /* Power down TX and RX (PWRDNTX and PWRDNRX) */
  1756. writel(0x000003d7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1757. udelay(AFE_REGISTER_WRITE_DELAY);
  1758. /*
  1759. * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
  1760. * & increase TX int & ext bias 20%....(0xe85c) */
  1761. writel(0x000003d4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
  1762. }
  1763. udelay(AFE_REGISTER_WRITE_DELAY);
  1764. if (is_a0() || is_a2()) {
  1765. /* Enable TX equalization (0xe824) */
  1766. writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
  1767. udelay(AFE_REGISTER_WRITE_DELAY);
  1768. }
  1769. /*
  1770. * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
  1771. * RDD=0x0(RX Detect Enabled) ....(0xe800) */
  1772. writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
  1773. udelay(AFE_REGISTER_WRITE_DELAY);
  1774. /* Leave DFE/FFE on */
  1775. if (is_a0())
  1776. writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1777. else if (is_a2())
  1778. writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1779. else {
  1780. writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
  1781. udelay(AFE_REGISTER_WRITE_DELAY);
  1782. /* Enable TX equalization (0xe824) */
  1783. writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
  1784. }
  1785. udelay(AFE_REGISTER_WRITE_DELAY);
  1786. writel(oem_phy->afe_tx_amp_control0,
  1787. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
  1788. udelay(AFE_REGISTER_WRITE_DELAY);
  1789. writel(oem_phy->afe_tx_amp_control1,
  1790. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
  1791. udelay(AFE_REGISTER_WRITE_DELAY);
  1792. writel(oem_phy->afe_tx_amp_control2,
  1793. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
  1794. udelay(AFE_REGISTER_WRITE_DELAY);
  1795. writel(oem_phy->afe_tx_amp_control3,
  1796. &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
  1797. udelay(AFE_REGISTER_WRITE_DELAY);
  1798. }
  1799. /* Transfer control to the PEs */
  1800. writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
  1801. udelay(AFE_REGISTER_WRITE_DELAY);
  1802. }
  1803. static enum sci_status scic_controller_set_mode(struct scic_sds_controller *scic,
  1804. enum sci_controller_mode operating_mode)
  1805. {
  1806. enum sci_status status = SCI_SUCCESS;
  1807. if ((scic->state_machine.current_state_id ==
  1808. SCI_BASE_CONTROLLER_STATE_INITIALIZING) ||
  1809. (scic->state_machine.current_state_id ==
  1810. SCI_BASE_CONTROLLER_STATE_INITIALIZED)) {
  1811. switch (operating_mode) {
  1812. case SCI_MODE_SPEED:
  1813. scic->remote_node_entries = SCI_MAX_REMOTE_DEVICES;
  1814. scic->task_context_entries = SCU_IO_REQUEST_COUNT;
  1815. scic->uf_control.buffers.count =
  1816. SCU_UNSOLICITED_FRAME_COUNT;
  1817. scic->completion_event_entries = SCU_EVENT_COUNT;
  1818. scic->completion_queue_entries =
  1819. SCU_COMPLETION_QUEUE_COUNT;
  1820. break;
  1821. case SCI_MODE_SIZE:
  1822. scic->remote_node_entries = SCI_MIN_REMOTE_DEVICES;
  1823. scic->task_context_entries = SCI_MIN_IO_REQUESTS;
  1824. scic->uf_control.buffers.count =
  1825. SCU_MIN_UNSOLICITED_FRAMES;
  1826. scic->completion_event_entries = SCU_MIN_EVENTS;
  1827. scic->completion_queue_entries =
  1828. SCU_MIN_COMPLETION_QUEUE_ENTRIES;
  1829. break;
  1830. default:
  1831. status = SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1832. break;
  1833. }
  1834. } else
  1835. status = SCI_FAILURE_INVALID_STATE;
  1836. return status;
  1837. }
  1838. static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
  1839. {
  1840. struct isci_host *ihost = scic_to_ihost(scic);
  1841. scic->power_control.timer = isci_timer_create(ihost,
  1842. scic,
  1843. scic_sds_controller_power_control_timer_handler);
  1844. memset(scic->power_control.requesters, 0,
  1845. sizeof(scic->power_control.requesters));
  1846. scic->power_control.phys_waiting = 0;
  1847. scic->power_control.phys_granted_power = 0;
  1848. }
  1849. static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
  1850. {
  1851. struct sci_base_state_machine *sm = &scic->state_machine;
  1852. enum sci_status result = SCI_SUCCESS;
  1853. struct isci_host *ihost = scic_to_ihost(scic);
  1854. u32 index, state;
  1855. if (scic->state_machine.current_state_id !=
  1856. SCI_BASE_CONTROLLER_STATE_RESET) {
  1857. dev_warn(scic_to_dev(scic),
  1858. "SCIC Controller initialize operation requested "
  1859. "in invalid state\n");
  1860. return SCI_FAILURE_INVALID_STATE;
  1861. }
  1862. sci_base_state_machine_change_state(sm, SCI_BASE_CONTROLLER_STATE_INITIALIZING);
  1863. scic->timeout_timer = isci_timer_create(ihost, scic,
  1864. scic_sds_controller_timeout_handler);
  1865. scic_sds_controller_initialize_phy_startup(scic);
  1866. scic_sds_controller_initialize_power_control(scic);
  1867. /*
  1868. * There is nothing to do here for B0 since we do not have to
  1869. * program the AFE registers.
  1870. * / @todo The AFE settings are supposed to be correct for the B0 but
  1871. * / presently they seem to be wrong. */
  1872. scic_sds_controller_afe_initialization(scic);
  1873. if (result == SCI_SUCCESS) {
  1874. u32 status;
  1875. u32 terminate_loop;
  1876. /* Take the hardware out of reset */
  1877. writel(0, &scic->smu_registers->soft_reset_control);
  1878. /*
  1879. * / @todo Provide meaningfull error code for hardware failure
  1880. * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
  1881. result = SCI_FAILURE;
  1882. terminate_loop = 100;
  1883. while (terminate_loop-- && (result != SCI_SUCCESS)) {
  1884. /* Loop until the hardware reports success */
  1885. udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
  1886. status = readl(&scic->smu_registers->control_status);
  1887. if ((status & SCU_RAM_INIT_COMPLETED) ==
  1888. SCU_RAM_INIT_COMPLETED)
  1889. result = SCI_SUCCESS;
  1890. }
  1891. }
  1892. if (result == SCI_SUCCESS) {
  1893. u32 max_supported_ports;
  1894. u32 max_supported_devices;
  1895. u32 max_supported_io_requests;
  1896. u32 device_context_capacity;
  1897. /*
  1898. * Determine what are the actaul device capacities that the
  1899. * hardware will support */
  1900. device_context_capacity =
  1901. readl(&scic->smu_registers->device_context_capacity);
  1902. max_supported_ports = smu_dcc_get_max_ports(device_context_capacity);
  1903. max_supported_devices = smu_dcc_get_max_remote_node_context(device_context_capacity);
  1904. max_supported_io_requests = smu_dcc_get_max_task_context(device_context_capacity);
  1905. /*
  1906. * Make all PEs that are unassigned match up with the
  1907. * logical ports
  1908. */
  1909. for (index = 0; index < max_supported_ports; index++) {
  1910. struct scu_port_task_scheduler_group_registers __iomem
  1911. *ptsg = &scic->scu_registers->peg0.ptsg;
  1912. writel(index, &ptsg->protocol_engine[index]);
  1913. }
  1914. /* Record the smaller of the two capacity values */
  1915. scic->logical_port_entries =
  1916. min(max_supported_ports, scic->logical_port_entries);
  1917. scic->task_context_entries =
  1918. min(max_supported_io_requests,
  1919. scic->task_context_entries);
  1920. scic->remote_node_entries =
  1921. min(max_supported_devices, scic->remote_node_entries);
  1922. /*
  1923. * Now that we have the correct hardware reported minimum values
  1924. * build the MDL for the controller. Default to a performance
  1925. * configuration.
  1926. */
  1927. scic_controller_set_mode(scic, SCI_MODE_SPEED);
  1928. }
  1929. /* Initialize hardware PCI Relaxed ordering in DMA engines */
  1930. if (result == SCI_SUCCESS) {
  1931. u32 dma_configuration;
  1932. /* Configure the payload DMA */
  1933. dma_configuration =
  1934. readl(&scic->scu_registers->sdma.pdma_configuration);
  1935. dma_configuration |=
  1936. SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1937. writel(dma_configuration,
  1938. &scic->scu_registers->sdma.pdma_configuration);
  1939. /* Configure the control DMA */
  1940. dma_configuration =
  1941. readl(&scic->scu_registers->sdma.cdma_configuration);
  1942. dma_configuration |=
  1943. SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1944. writel(dma_configuration,
  1945. &scic->scu_registers->sdma.cdma_configuration);
  1946. }
  1947. /*
  1948. * Initialize the PHYs before the PORTs because the PHY registers
  1949. * are accessed during the port initialization.
  1950. */
  1951. if (result == SCI_SUCCESS) {
  1952. /* Initialize the phys */
  1953. for (index = 0;
  1954. (result == SCI_SUCCESS) && (index < SCI_MAX_PHYS);
  1955. index++) {
  1956. result = scic_sds_phy_initialize(
  1957. &ihost->phys[index].sci,
  1958. &scic->scu_registers->peg0.pe[index].tl,
  1959. &scic->scu_registers->peg0.pe[index].ll);
  1960. }
  1961. }
  1962. if (result == SCI_SUCCESS) {
  1963. /* Initialize the logical ports */
  1964. for (index = 0;
  1965. (index < scic->logical_port_entries) &&
  1966. (result == SCI_SUCCESS);
  1967. index++) {
  1968. result = scic_sds_port_initialize(
  1969. &ihost->ports[index].sci,
  1970. &scic->scu_registers->peg0.ptsg.port[index],
  1971. &scic->scu_registers->peg0.ptsg.protocol_engine,
  1972. &scic->scu_registers->peg0.viit[index]);
  1973. }
  1974. }
  1975. if (result == SCI_SUCCESS)
  1976. result = scic_sds_port_configuration_agent_initialize(
  1977. scic,
  1978. &scic->port_agent);
  1979. /* Advance the controller state machine */
  1980. if (result == SCI_SUCCESS)
  1981. state = SCI_BASE_CONTROLLER_STATE_INITIALIZED;
  1982. else
  1983. state = SCI_BASE_CONTROLLER_STATE_FAILED;
  1984. sci_base_state_machine_change_state(sm, state);
  1985. return result;
  1986. }
  1987. static enum sci_status scic_user_parameters_set(
  1988. struct scic_sds_controller *scic,
  1989. union scic_user_parameters *scic_parms)
  1990. {
  1991. u32 state = scic->state_machine.current_state_id;
  1992. if (state == SCI_BASE_CONTROLLER_STATE_RESET ||
  1993. state == SCI_BASE_CONTROLLER_STATE_INITIALIZING ||
  1994. state == SCI_BASE_CONTROLLER_STATE_INITIALIZED) {
  1995. u16 index;
  1996. /*
  1997. * Validate the user parameters. If they are not legal, then
  1998. * return a failure.
  1999. */
  2000. for (index = 0; index < SCI_MAX_PHYS; index++) {
  2001. struct sci_phy_user_params *user_phy;
  2002. user_phy = &scic_parms->sds1.phys[index];
  2003. if (!((user_phy->max_speed_generation <=
  2004. SCIC_SDS_PARM_MAX_SPEED) &&
  2005. (user_phy->max_speed_generation >
  2006. SCIC_SDS_PARM_NO_SPEED)))
  2007. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  2008. if (user_phy->in_connection_align_insertion_frequency <
  2009. 3)
  2010. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  2011. if ((user_phy->in_connection_align_insertion_frequency <
  2012. 3) ||
  2013. (user_phy->align_insertion_frequency == 0) ||
  2014. (user_phy->
  2015. notify_enable_spin_up_insertion_frequency ==
  2016. 0))
  2017. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  2018. }
  2019. if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
  2020. (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
  2021. (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
  2022. (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
  2023. (scic_parms->sds1.no_outbound_task_timeout == 0))
  2024. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  2025. memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
  2026. return SCI_SUCCESS;
  2027. }
  2028. return SCI_FAILURE_INVALID_STATE;
  2029. }
  2030. static int scic_controller_mem_init(struct scic_sds_controller *scic)
  2031. {
  2032. struct device *dev = scic_to_dev(scic);
  2033. dma_addr_t dma_handle;
  2034. enum sci_status result;
  2035. scic->completion_queue = dmam_alloc_coherent(dev,
  2036. scic->completion_queue_entries * sizeof(u32),
  2037. &dma_handle, GFP_KERNEL);
  2038. if (!scic->completion_queue)
  2039. return -ENOMEM;
  2040. writel(lower_32_bits(dma_handle),
  2041. &scic->smu_registers->completion_queue_lower);
  2042. writel(upper_32_bits(dma_handle),
  2043. &scic->smu_registers->completion_queue_upper);
  2044. scic->remote_node_context_table = dmam_alloc_coherent(dev,
  2045. scic->remote_node_entries *
  2046. sizeof(union scu_remote_node_context),
  2047. &dma_handle, GFP_KERNEL);
  2048. if (!scic->remote_node_context_table)
  2049. return -ENOMEM;
  2050. writel(lower_32_bits(dma_handle),
  2051. &scic->smu_registers->remote_node_context_lower);
  2052. writel(upper_32_bits(dma_handle),
  2053. &scic->smu_registers->remote_node_context_upper);
  2054. scic->task_context_table = dmam_alloc_coherent(dev,
  2055. scic->task_context_entries *
  2056. sizeof(struct scu_task_context),
  2057. &dma_handle, GFP_KERNEL);
  2058. if (!scic->task_context_table)
  2059. return -ENOMEM;
  2060. writel(lower_32_bits(dma_handle),
  2061. &scic->smu_registers->host_task_table_lower);
  2062. writel(upper_32_bits(dma_handle),
  2063. &scic->smu_registers->host_task_table_upper);
  2064. result = scic_sds_unsolicited_frame_control_construct(scic);
  2065. if (result)
  2066. return result;
  2067. /*
  2068. * Inform the silicon as to the location of the UF headers and
  2069. * address table.
  2070. */
  2071. writel(lower_32_bits(scic->uf_control.headers.physical_address),
  2072. &scic->scu_registers->sdma.uf_header_base_address_lower);
  2073. writel(upper_32_bits(scic->uf_control.headers.physical_address),
  2074. &scic->scu_registers->sdma.uf_header_base_address_upper);
  2075. writel(lower_32_bits(scic->uf_control.address_table.physical_address),
  2076. &scic->scu_registers->sdma.uf_address_table_lower);
  2077. writel(upper_32_bits(scic->uf_control.address_table.physical_address),
  2078. &scic->scu_registers->sdma.uf_address_table_upper);
  2079. return 0;
  2080. }
  2081. int isci_host_init(struct isci_host *isci_host)
  2082. {
  2083. int err = 0, i;
  2084. enum sci_status status;
  2085. union scic_oem_parameters oem;
  2086. union scic_user_parameters scic_user_params;
  2087. struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
  2088. isci_timer_list_construct(isci_host);
  2089. spin_lock_init(&isci_host->state_lock);
  2090. spin_lock_init(&isci_host->scic_lock);
  2091. spin_lock_init(&isci_host->queue_lock);
  2092. init_waitqueue_head(&isci_host->eventq);
  2093. isci_host_change_state(isci_host, isci_starting);
  2094. isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
  2095. status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
  2096. smu_base(isci_host));
  2097. if (status != SCI_SUCCESS) {
  2098. dev_err(&isci_host->pdev->dev,
  2099. "%s: scic_controller_construct failed - status = %x\n",
  2100. __func__,
  2101. status);
  2102. return -ENODEV;
  2103. }
  2104. isci_host->sas_ha.dev = &isci_host->pdev->dev;
  2105. isci_host->sas_ha.lldd_ha = isci_host;
  2106. /*
  2107. * grab initial values stored in the controller object for OEM and USER
  2108. * parameters
  2109. */
  2110. isci_user_parameters_get(isci_host, &scic_user_params);
  2111. status = scic_user_parameters_set(&isci_host->sci,
  2112. &scic_user_params);
  2113. if (status != SCI_SUCCESS) {
  2114. dev_warn(&isci_host->pdev->dev,
  2115. "%s: scic_user_parameters_set failed\n",
  2116. __func__);
  2117. return -ENODEV;
  2118. }
  2119. scic_oem_parameters_get(&isci_host->sci, &oem);
  2120. /* grab any OEM parameters specified in orom */
  2121. if (pci_info->orom) {
  2122. status = isci_parse_oem_parameters(&oem,
  2123. pci_info->orom,
  2124. isci_host->id);
  2125. if (status != SCI_SUCCESS) {
  2126. dev_warn(&isci_host->pdev->dev,
  2127. "parsing firmware oem parameters failed\n");
  2128. return -EINVAL;
  2129. }
  2130. }
  2131. status = scic_oem_parameters_set(&isci_host->sci, &oem);
  2132. if (status != SCI_SUCCESS) {
  2133. dev_warn(&isci_host->pdev->dev,
  2134. "%s: scic_oem_parameters_set failed\n",
  2135. __func__);
  2136. return -ENODEV;
  2137. }
  2138. tasklet_init(&isci_host->completion_tasklet,
  2139. isci_host_completion_routine, (unsigned long)isci_host);
  2140. INIT_LIST_HEAD(&isci_host->requests_to_complete);
  2141. INIT_LIST_HEAD(&isci_host->requests_to_errorback);
  2142. spin_lock_irq(&isci_host->scic_lock);
  2143. status = scic_controller_initialize(&isci_host->sci);
  2144. spin_unlock_irq(&isci_host->scic_lock);
  2145. if (status != SCI_SUCCESS) {
  2146. dev_warn(&isci_host->pdev->dev,
  2147. "%s: scic_controller_initialize failed -"
  2148. " status = 0x%x\n",
  2149. __func__, status);
  2150. return -ENODEV;
  2151. }
  2152. err = scic_controller_mem_init(&isci_host->sci);
  2153. if (err)
  2154. return err;
  2155. isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
  2156. sizeof(struct isci_request),
  2157. SLAB_HWCACHE_ALIGN, 0);
  2158. if (!isci_host->dma_pool)
  2159. return -ENOMEM;
  2160. for (i = 0; i < SCI_MAX_PORTS; i++)
  2161. isci_port_init(&isci_host->ports[i], isci_host, i);
  2162. for (i = 0; i < SCI_MAX_PHYS; i++)
  2163. isci_phy_init(&isci_host->phys[i], isci_host, i);
  2164. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  2165. struct isci_remote_device *idev = &isci_host->devices[i];
  2166. INIT_LIST_HEAD(&idev->reqs_in_process);
  2167. INIT_LIST_HEAD(&idev->node);
  2168. spin_lock_init(&idev->state_lock);
  2169. }
  2170. return 0;
  2171. }
  2172. void scic_sds_controller_link_up(struct scic_sds_controller *scic,
  2173. struct scic_sds_port *port, struct scic_sds_phy *phy)
  2174. {
  2175. switch (scic->state_machine.current_state_id) {
  2176. case SCI_BASE_CONTROLLER_STATE_STARTING:
  2177. scic_sds_controller_phy_timer_stop(scic);
  2178. scic->port_agent.link_up_handler(scic, &scic->port_agent,
  2179. port, phy);
  2180. scic_sds_controller_start_next_phy(scic);
  2181. break;
  2182. case SCI_BASE_CONTROLLER_STATE_READY:
  2183. scic->port_agent.link_up_handler(scic, &scic->port_agent,
  2184. port, phy);
  2185. break;
  2186. default:
  2187. dev_dbg(scic_to_dev(scic),
  2188. "%s: SCIC Controller linkup event from phy %d in "
  2189. "unexpected state %d\n", __func__, phy->phy_index,
  2190. scic->state_machine.current_state_id);
  2191. }
  2192. }
  2193. void scic_sds_controller_link_down(struct scic_sds_controller *scic,
  2194. struct scic_sds_port *port, struct scic_sds_phy *phy)
  2195. {
  2196. switch (scic->state_machine.current_state_id) {
  2197. case SCI_BASE_CONTROLLER_STATE_STARTING:
  2198. case SCI_BASE_CONTROLLER_STATE_READY:
  2199. scic->port_agent.link_down_handler(scic, &scic->port_agent,
  2200. port, phy);
  2201. break;
  2202. default:
  2203. dev_dbg(scic_to_dev(scic),
  2204. "%s: SCIC Controller linkdown event from phy %d in "
  2205. "unexpected state %d\n",
  2206. __func__,
  2207. phy->phy_index,
  2208. scic->state_machine.current_state_id);
  2209. }
  2210. }
  2211. /**
  2212. * This is a helper method to determine if any remote devices on this
  2213. * controller are still in the stopping state.
  2214. *
  2215. */
  2216. static bool scic_sds_controller_has_remote_devices_stopping(
  2217. struct scic_sds_controller *controller)
  2218. {
  2219. u32 index;
  2220. for (index = 0; index < controller->remote_node_entries; index++) {
  2221. if ((controller->device_table[index] != NULL) &&
  2222. (controller->device_table[index]->state_machine.current_state_id
  2223. == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING))
  2224. return true;
  2225. }
  2226. return false;
  2227. }
  2228. /**
  2229. * This method is called by the remote device to inform the controller
  2230. * object that the remote device has stopped.
  2231. */
  2232. void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
  2233. struct scic_sds_remote_device *sci_dev)
  2234. {
  2235. if (scic->state_machine.current_state_id !=
  2236. SCI_BASE_CONTROLLER_STATE_STOPPING) {
  2237. dev_dbg(scic_to_dev(scic),
  2238. "SCIC Controller 0x%p remote device stopped event "
  2239. "from device 0x%p in unexpected state %d\n",
  2240. scic, sci_dev,
  2241. scic->state_machine.current_state_id);
  2242. return;
  2243. }
  2244. if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
  2245. sci_base_state_machine_change_state(&scic->state_machine,
  2246. SCI_BASE_CONTROLLER_STATE_STOPPED);
  2247. }
  2248. }
  2249. /**
  2250. * This method will write to the SCU PCP register the request value. The method
  2251. * is used to suspend/resume ports, devices, and phys.
  2252. * @scic:
  2253. *
  2254. *
  2255. */
  2256. void scic_sds_controller_post_request(
  2257. struct scic_sds_controller *scic,
  2258. u32 request)
  2259. {
  2260. dev_dbg(scic_to_dev(scic),
  2261. "%s: SCIC Controller 0x%p post request 0x%08x\n",
  2262. __func__,
  2263. scic,
  2264. request);
  2265. writel(request, &scic->smu_registers->post_context_port);
  2266. }
  2267. /**
  2268. * This method will copy the soft copy of the task context into the physical
  2269. * memory accessible by the controller.
  2270. * @scic: This parameter specifies the controller for which to copy
  2271. * the task context.
  2272. * @sci_req: This parameter specifies the request for which the task
  2273. * context is being copied.
  2274. *
  2275. * After this call is made the SCIC_SDS_IO_REQUEST object will always point to
  2276. * the physical memory version of the task context. Thus, all subsequent
  2277. * updates to the task context are performed in the TC table (i.e. DMAable
  2278. * memory). none
  2279. */
  2280. void scic_sds_controller_copy_task_context(
  2281. struct scic_sds_controller *scic,
  2282. struct scic_sds_request *sci_req)
  2283. {
  2284. struct scu_task_context *task_context_buffer;
  2285. task_context_buffer = scic_sds_controller_get_task_context_buffer(
  2286. scic, sci_req->io_tag);
  2287. memcpy(task_context_buffer,
  2288. sci_req->task_context_buffer,
  2289. offsetof(struct scu_task_context, sgl_snapshot_ac));
  2290. /*
  2291. * Now that the soft copy of the TC has been copied into the TC
  2292. * table accessible by the silicon. Thus, any further changes to
  2293. * the TC (e.g. TC termination) occur in the appropriate location. */
  2294. sci_req->task_context_buffer = task_context_buffer;
  2295. }
  2296. /**
  2297. * This method returns the task context buffer for the given io tag.
  2298. * @scic:
  2299. * @io_tag:
  2300. *
  2301. * struct scu_task_context*
  2302. */
  2303. struct scu_task_context *scic_sds_controller_get_task_context_buffer(
  2304. struct scic_sds_controller *scic,
  2305. u16 io_tag
  2306. ) {
  2307. u16 task_index = scic_sds_io_tag_get_index(io_tag);
  2308. if (task_index < scic->task_context_entries) {
  2309. return &scic->task_context_table[task_index];
  2310. }
  2311. return NULL;
  2312. }
  2313. struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic,
  2314. u16 io_tag)
  2315. {
  2316. u16 task_index;
  2317. u16 task_sequence;
  2318. task_index = scic_sds_io_tag_get_index(io_tag);
  2319. if (task_index < scic->task_context_entries) {
  2320. if (scic->io_request_table[task_index] != NULL) {
  2321. task_sequence = scic_sds_io_tag_get_sequence(io_tag);
  2322. if (task_sequence == scic->io_request_sequence[task_index]) {
  2323. return scic->io_request_table[task_index];
  2324. }
  2325. }
  2326. }
  2327. return NULL;
  2328. }
  2329. /**
  2330. * This method allocates remote node index and the reserves the remote node
  2331. * context space for use. This method can fail if there are no more remote
  2332. * node index available.
  2333. * @scic: This is the controller object which contains the set of
  2334. * free remote node ids
  2335. * @sci_dev: This is the device object which is requesting the a remote node
  2336. * id
  2337. * @node_id: This is the remote node id that is assinged to the device if one
  2338. * is available
  2339. *
  2340. * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
  2341. * node index available.
  2342. */
  2343. enum sci_status scic_sds_controller_allocate_remote_node_context(
  2344. struct scic_sds_controller *scic,
  2345. struct scic_sds_remote_device *sci_dev,
  2346. u16 *node_id)
  2347. {
  2348. u16 node_index;
  2349. u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
  2350. node_index = scic_sds_remote_node_table_allocate_remote_node(
  2351. &scic->available_remote_nodes, remote_node_count
  2352. );
  2353. if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  2354. scic->device_table[node_index] = sci_dev;
  2355. *node_id = node_index;
  2356. return SCI_SUCCESS;
  2357. }
  2358. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2359. }
  2360. /**
  2361. * This method frees the remote node index back to the available pool. Once
  2362. * this is done the remote node context buffer is no longer valid and can
  2363. * not be used.
  2364. * @scic:
  2365. * @sci_dev:
  2366. * @node_id:
  2367. *
  2368. */
  2369. void scic_sds_controller_free_remote_node_context(
  2370. struct scic_sds_controller *scic,
  2371. struct scic_sds_remote_device *sci_dev,
  2372. u16 node_id)
  2373. {
  2374. u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
  2375. if (scic->device_table[node_id] == sci_dev) {
  2376. scic->device_table[node_id] = NULL;
  2377. scic_sds_remote_node_table_release_remote_node_index(
  2378. &scic->available_remote_nodes, remote_node_count, node_id
  2379. );
  2380. }
  2381. }
  2382. /**
  2383. * This method returns the union scu_remote_node_context for the specified remote
  2384. * node id.
  2385. * @scic:
  2386. * @node_id:
  2387. *
  2388. * union scu_remote_node_context*
  2389. */
  2390. union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
  2391. struct scic_sds_controller *scic,
  2392. u16 node_id
  2393. ) {
  2394. if (
  2395. (node_id < scic->remote_node_entries)
  2396. && (scic->device_table[node_id] != NULL)
  2397. ) {
  2398. return &scic->remote_node_context_table[node_id];
  2399. }
  2400. return NULL;
  2401. }
  2402. /**
  2403. *
  2404. * @resposne_buffer: This is the buffer into which the D2H register FIS will be
  2405. * constructed.
  2406. * @frame_header: This is the frame header returned by the hardware.
  2407. * @frame_buffer: This is the frame buffer returned by the hardware.
  2408. *
  2409. * This method will combind the frame header and frame buffer to create a SATA
  2410. * D2H register FIS none
  2411. */
  2412. void scic_sds_controller_copy_sata_response(
  2413. void *response_buffer,
  2414. void *frame_header,
  2415. void *frame_buffer)
  2416. {
  2417. memcpy(response_buffer, frame_header, sizeof(u32));
  2418. memcpy(response_buffer + sizeof(u32),
  2419. frame_buffer,
  2420. sizeof(struct dev_to_host_fis) - sizeof(u32));
  2421. }
  2422. /**
  2423. * This method releases the frame once this is done the frame is available for
  2424. * re-use by the hardware. The data contained in the frame header and frame
  2425. * buffer is no longer valid. The UF queue get pointer is only updated if UF
  2426. * control indicates this is appropriate.
  2427. * @scic:
  2428. * @frame_index:
  2429. *
  2430. */
  2431. void scic_sds_controller_release_frame(
  2432. struct scic_sds_controller *scic,
  2433. u32 frame_index)
  2434. {
  2435. if (scic_sds_unsolicited_frame_control_release_frame(
  2436. &scic->uf_control, frame_index) == true)
  2437. writel(scic->uf_control.get,
  2438. &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
  2439. }
  2440. /**
  2441. * scic_controller_start_io() - This method is called by the SCI user to
  2442. * send/start an IO request. If the method invocation is successful, then
  2443. * the IO request has been queued to the hardware for processing.
  2444. * @controller: the handle to the controller object for which to start an IO
  2445. * request.
  2446. * @remote_device: the handle to the remote device object for which to start an
  2447. * IO request.
  2448. * @io_request: the handle to the io request object to start.
  2449. * @io_tag: This parameter specifies a previously allocated IO tag that the
  2450. * user desires to be utilized for this request. This parameter is optional.
  2451. * The user is allowed to supply SCI_CONTROLLER_INVALID_IO_TAG as the value
  2452. * for this parameter.
  2453. *
  2454. * - IO tags are a protected resource. It is incumbent upon the SCI Core user
  2455. * to ensure that each of the methods that may allocate or free available IO
  2456. * tags are handled in a mutually exclusive manner. This method is one of said
  2457. * methods requiring proper critical code section protection (e.g. semaphore,
  2458. * spin-lock, etc.). - For SATA, the user is required to manage NCQ tags. As a
  2459. * result, it is expected the user will have set the NCQ tag field in the host
  2460. * to device register FIS prior to calling this method. There is also a
  2461. * requirement for the user to call scic_stp_io_set_ncq_tag() prior to invoking
  2462. * the scic_controller_start_io() method. scic_controller_allocate_tag() for
  2463. * more information on allocating a tag. Indicate if the controller
  2464. * successfully started the IO request. SCI_SUCCESS if the IO request was
  2465. * successfully started. Determine the failure situations and return values.
  2466. */
  2467. enum sci_status scic_controller_start_io(
  2468. struct scic_sds_controller *scic,
  2469. struct scic_sds_remote_device *rdev,
  2470. struct scic_sds_request *req,
  2471. u16 io_tag)
  2472. {
  2473. enum sci_status status;
  2474. if (scic->state_machine.current_state_id !=
  2475. SCI_BASE_CONTROLLER_STATE_READY) {
  2476. dev_warn(scic_to_dev(scic), "invalid state to start I/O");
  2477. return SCI_FAILURE_INVALID_STATE;
  2478. }
  2479. status = scic_sds_remote_device_start_io(scic, rdev, req);
  2480. if (status != SCI_SUCCESS)
  2481. return status;
  2482. scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
  2483. scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
  2484. return SCI_SUCCESS;
  2485. }
  2486. /**
  2487. * scic_controller_terminate_request() - This method is called by the SCI Core
  2488. * user to terminate an ongoing (i.e. started) core IO request. This does
  2489. * not abort the IO request at the target, but rather removes the IO request
  2490. * from the host controller.
  2491. * @controller: the handle to the controller object for which to terminate a
  2492. * request.
  2493. * @remote_device: the handle to the remote device object for which to
  2494. * terminate a request.
  2495. * @request: the handle to the io or task management request object to
  2496. * terminate.
  2497. *
  2498. * Indicate if the controller successfully began the terminate process for the
  2499. * IO request. SCI_SUCCESS if the terminate process was successfully started
  2500. * for the request. Determine the failure situations and return values.
  2501. */
  2502. enum sci_status scic_controller_terminate_request(
  2503. struct scic_sds_controller *scic,
  2504. struct scic_sds_remote_device *rdev,
  2505. struct scic_sds_request *req)
  2506. {
  2507. enum sci_status status;
  2508. if (scic->state_machine.current_state_id !=
  2509. SCI_BASE_CONTROLLER_STATE_READY) {
  2510. dev_warn(scic_to_dev(scic),
  2511. "invalid state to terminate request\n");
  2512. return SCI_FAILURE_INVALID_STATE;
  2513. }
  2514. status = scic_sds_io_request_terminate(req);
  2515. if (status != SCI_SUCCESS)
  2516. return status;
  2517. /*
  2518. * Utilize the original post context command and or in the POST_TC_ABORT
  2519. * request sub-type.
  2520. */
  2521. scic_sds_controller_post_request(scic,
  2522. scic_sds_request_get_post_context(req) |
  2523. SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
  2524. return SCI_SUCCESS;
  2525. }
  2526. /**
  2527. * scic_controller_complete_io() - This method will perform core specific
  2528. * completion operations for an IO request. After this method is invoked,
  2529. * the user should consider the IO request as invalid until it is properly
  2530. * reused (i.e. re-constructed).
  2531. * @controller: The handle to the controller object for which to complete the
  2532. * IO request.
  2533. * @remote_device: The handle to the remote device object for which to complete
  2534. * the IO request.
  2535. * @io_request: the handle to the io request object to complete.
  2536. *
  2537. * - IO tags are a protected resource. It is incumbent upon the SCI Core user
  2538. * to ensure that each of the methods that may allocate or free available IO
  2539. * tags are handled in a mutually exclusive manner. This method is one of said
  2540. * methods requiring proper critical code section protection (e.g. semaphore,
  2541. * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
  2542. * Core user, using the scic_controller_allocate_io_tag() method, then it is
  2543. * the responsibility of the caller to invoke the scic_controller_free_io_tag()
  2544. * method to free the tag (i.e. this method will not free the IO tag). Indicate
  2545. * if the controller successfully completed the IO request. SCI_SUCCESS if the
  2546. * completion process was successful.
  2547. */
  2548. enum sci_status scic_controller_complete_io(
  2549. struct scic_sds_controller *scic,
  2550. struct scic_sds_remote_device *rdev,
  2551. struct scic_sds_request *request)
  2552. {
  2553. enum sci_status status;
  2554. u16 index;
  2555. switch (scic->state_machine.current_state_id) {
  2556. case SCI_BASE_CONTROLLER_STATE_STOPPING:
  2557. /* XXX: Implement this function */
  2558. return SCI_FAILURE;
  2559. case SCI_BASE_CONTROLLER_STATE_READY:
  2560. status = scic_sds_remote_device_complete_io(scic, rdev, request);
  2561. if (status != SCI_SUCCESS)
  2562. return status;
  2563. index = scic_sds_io_tag_get_index(request->io_tag);
  2564. scic->io_request_table[index] = NULL;
  2565. return SCI_SUCCESS;
  2566. default:
  2567. dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
  2568. return SCI_FAILURE_INVALID_STATE;
  2569. }
  2570. }
  2571. enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
  2572. {
  2573. struct scic_sds_controller *scic = sci_req->owning_controller;
  2574. if (scic->state_machine.current_state_id !=
  2575. SCI_BASE_CONTROLLER_STATE_READY) {
  2576. dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
  2577. return SCI_FAILURE_INVALID_STATE;
  2578. }
  2579. scic->io_request_table[scic_sds_io_tag_get_index(sci_req->io_tag)] = sci_req;
  2580. scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
  2581. return SCI_SUCCESS;
  2582. }
  2583. /**
  2584. * scic_controller_start_task() - This method is called by the SCIC user to
  2585. * send/start a framework task management request.
  2586. * @controller: the handle to the controller object for which to start the task
  2587. * management request.
  2588. * @remote_device: the handle to the remote device object for which to start
  2589. * the task management request.
  2590. * @task_request: the handle to the task request object to start.
  2591. * @io_tag: This parameter specifies a previously allocated IO tag that the
  2592. * user desires to be utilized for this request. Note this not the io_tag
  2593. * of the request being managed. It is to be utilized for the task request
  2594. * itself. This parameter is optional. The user is allowed to supply
  2595. * SCI_CONTROLLER_INVALID_IO_TAG as the value for this parameter.
  2596. *
  2597. * - IO tags are a protected resource. It is incumbent upon the SCI Core user
  2598. * to ensure that each of the methods that may allocate or free available IO
  2599. * tags are handled in a mutually exclusive manner. This method is one of said
  2600. * methods requiring proper critical code section protection (e.g. semaphore,
  2601. * spin-lock, etc.). - The user must synchronize this task with completion
  2602. * queue processing. If they are not synchronized then it is possible for the
  2603. * io requests that are being managed by the task request can complete before
  2604. * starting the task request. scic_controller_allocate_tag() for more
  2605. * information on allocating a tag. Indicate if the controller successfully
  2606. * started the IO request. SCI_TASK_SUCCESS if the task request was
  2607. * successfully started. SCI_TASK_FAILURE_REQUIRES_SCSI_ABORT This value is
  2608. * returned if there is/are task(s) outstanding that require termination or
  2609. * completion before this request can succeed.
  2610. */
  2611. enum sci_task_status scic_controller_start_task(
  2612. struct scic_sds_controller *scic,
  2613. struct scic_sds_remote_device *rdev,
  2614. struct scic_sds_request *req,
  2615. u16 task_tag)
  2616. {
  2617. enum sci_status status;
  2618. if (scic->state_machine.current_state_id !=
  2619. SCI_BASE_CONTROLLER_STATE_READY) {
  2620. dev_warn(scic_to_dev(scic),
  2621. "%s: SCIC Controller starting task from invalid "
  2622. "state\n",
  2623. __func__);
  2624. return SCI_TASK_FAILURE_INVALID_STATE;
  2625. }
  2626. status = scic_sds_remote_device_start_task(scic, rdev, req);
  2627. switch (status) {
  2628. case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
  2629. scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
  2630. /*
  2631. * We will let framework know this task request started successfully,
  2632. * although core is still woring on starting the request (to post tc when
  2633. * RNC is resumed.)
  2634. */
  2635. return SCI_SUCCESS;
  2636. case SCI_SUCCESS:
  2637. scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
  2638. scic_sds_controller_post_request(scic,
  2639. scic_sds_request_get_post_context(req));
  2640. break;
  2641. default:
  2642. break;
  2643. }
  2644. return status;
  2645. }
  2646. /**
  2647. * scic_controller_allocate_io_tag() - This method will allocate a tag from the
  2648. * pool of free IO tags. Direct allocation of IO tags by the SCI Core user
  2649. * is optional. The scic_controller_start_io() method will allocate an IO
  2650. * tag if this method is not utilized and the tag is not supplied to the IO
  2651. * construct routine. Direct allocation of IO tags may provide additional
  2652. * performance improvements in environments capable of supporting this usage
  2653. * model. Additionally, direct allocation of IO tags also provides
  2654. * additional flexibility to the SCI Core user. Specifically, the user may
  2655. * retain IO tags across the lives of multiple IO requests.
  2656. * @controller: the handle to the controller object for which to allocate the
  2657. * tag.
  2658. *
  2659. * IO tags are a protected resource. It is incumbent upon the SCI Core user to
  2660. * ensure that each of the methods that may allocate or free available IO tags
  2661. * are handled in a mutually exclusive manner. This method is one of said
  2662. * methods requiring proper critical code section protection (e.g. semaphore,
  2663. * spin-lock, etc.). An unsigned integer representing an available IO tag.
  2664. * SCI_CONTROLLER_INVALID_IO_TAG This value is returned if there are no
  2665. * currently available tags to be allocated. All return other values indicate a
  2666. * legitimate tag.
  2667. */
  2668. u16 scic_controller_allocate_io_tag(
  2669. struct scic_sds_controller *scic)
  2670. {
  2671. u16 task_context;
  2672. u16 sequence_count;
  2673. if (!sci_pool_empty(scic->tci_pool)) {
  2674. sci_pool_get(scic->tci_pool, task_context);
  2675. sequence_count = scic->io_request_sequence[task_context];
  2676. return scic_sds_io_tag_construct(sequence_count, task_context);
  2677. }
  2678. return SCI_CONTROLLER_INVALID_IO_TAG;
  2679. }
  2680. /**
  2681. * scic_controller_free_io_tag() - This method will free an IO tag to the pool
  2682. * of free IO tags. This method provides the SCI Core user more flexibility
  2683. * with regards to IO tags. The user may desire to keep an IO tag after an
  2684. * IO request has completed, because they plan on re-using the tag for a
  2685. * subsequent IO request. This method is only legal if the tag was
  2686. * allocated via scic_controller_allocate_io_tag().
  2687. * @controller: This parameter specifies the handle to the controller object
  2688. * for which to free/return the tag.
  2689. * @io_tag: This parameter represents the tag to be freed to the pool of
  2690. * available tags.
  2691. *
  2692. * - IO tags are a protected resource. It is incumbent upon the SCI Core user
  2693. * to ensure that each of the methods that may allocate or free available IO
  2694. * tags are handled in a mutually exclusive manner. This method is one of said
  2695. * methods requiring proper critical code section protection (e.g. semaphore,
  2696. * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
  2697. * Core user, using the scic_controller_allocate_io_tag() method, then it is
  2698. * the responsibility of the caller to invoke this method to free the tag. This
  2699. * method returns an indication of whether the tag was successfully put back
  2700. * (freed) to the pool of available tags. SCI_SUCCESS This return value
  2701. * indicates the tag was successfully placed into the pool of available IO
  2702. * tags. SCI_FAILURE_INVALID_IO_TAG This value is returned if the supplied tag
  2703. * is not a valid IO tag value.
  2704. */
  2705. enum sci_status scic_controller_free_io_tag(
  2706. struct scic_sds_controller *scic,
  2707. u16 io_tag)
  2708. {
  2709. u16 sequence;
  2710. u16 index;
  2711. BUG_ON(io_tag == SCI_CONTROLLER_INVALID_IO_TAG);
  2712. sequence = scic_sds_io_tag_get_sequence(io_tag);
  2713. index = scic_sds_io_tag_get_index(io_tag);
  2714. if (!sci_pool_full(scic->tci_pool)) {
  2715. if (sequence == scic->io_request_sequence[index]) {
  2716. scic_sds_io_sequence_increment(
  2717. scic->io_request_sequence[index]);
  2718. sci_pool_put(scic->tci_pool, index);
  2719. return SCI_SUCCESS;
  2720. }
  2721. }
  2722. return SCI_FAILURE_INVALID_IO_TAG;
  2723. }