host.c 101 KB

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