request.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730
  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 "isci.h"
  56. #include "task.h"
  57. #include "request.h"
  58. #include "sata.h"
  59. #include "scu_completion_codes.h"
  60. #include "scu_event_codes.h"
  61. #include "sas.h"
  62. /**
  63. * This method returns the sgl element pair for the specificed sgl_pair index.
  64. * @sci_req: This parameter specifies the IO request for which to retrieve
  65. * the Scatter-Gather List element pair.
  66. * @sgl_pair_index: This parameter specifies the index into the SGL element
  67. * pair to be retrieved.
  68. *
  69. * This method returns a pointer to an struct scu_sgl_element_pair.
  70. */
  71. static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
  72. struct scic_sds_request *sci_req,
  73. u32 sgl_pair_index
  74. ) {
  75. struct scu_task_context *task_context;
  76. task_context = (struct scu_task_context *)sci_req->task_context_buffer;
  77. if (sgl_pair_index == 0) {
  78. return &task_context->sgl_pair_ab;
  79. } else if (sgl_pair_index == 1) {
  80. return &task_context->sgl_pair_cd;
  81. }
  82. return &sci_req->sg_table[sgl_pair_index - 2];
  83. }
  84. /**
  85. * This function will build the SGL list for an IO request.
  86. * @sci_req: This parameter specifies the IO request for which to build
  87. * the Scatter-Gather List.
  88. *
  89. */
  90. static void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
  91. {
  92. struct isci_request *isci_request = sci_req_to_ireq(sds_request);
  93. struct isci_host *isci_host = isci_request->isci_host;
  94. struct sas_task *task = isci_request_access_task(isci_request);
  95. struct scatterlist *sg = NULL;
  96. dma_addr_t dma_addr;
  97. u32 sg_idx = 0;
  98. struct scu_sgl_element_pair *scu_sg = NULL;
  99. struct scu_sgl_element_pair *prev_sg = NULL;
  100. if (task->num_scatter > 0) {
  101. sg = task->scatter;
  102. while (sg) {
  103. scu_sg = scic_sds_request_get_sgl_element_pair(
  104. sds_request,
  105. sg_idx);
  106. SCU_SGL_COPY(scu_sg->A, sg);
  107. sg = sg_next(sg);
  108. if (sg) {
  109. SCU_SGL_COPY(scu_sg->B, sg);
  110. sg = sg_next(sg);
  111. } else
  112. SCU_SGL_ZERO(scu_sg->B);
  113. if (prev_sg) {
  114. dma_addr =
  115. scic_io_request_get_dma_addr(
  116. sds_request,
  117. scu_sg);
  118. prev_sg->next_pair_upper =
  119. upper_32_bits(dma_addr);
  120. prev_sg->next_pair_lower =
  121. lower_32_bits(dma_addr);
  122. }
  123. prev_sg = scu_sg;
  124. sg_idx++;
  125. }
  126. } else { /* handle when no sg */
  127. scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
  128. sg_idx);
  129. dma_addr = dma_map_single(&isci_host->pdev->dev,
  130. task->scatter,
  131. task->total_xfer_len,
  132. task->data_dir);
  133. isci_request->zero_scatter_daddr = dma_addr;
  134. scu_sg->A.length = task->total_xfer_len;
  135. scu_sg->A.address_upper = upper_32_bits(dma_addr);
  136. scu_sg->A.address_lower = lower_32_bits(dma_addr);
  137. }
  138. if (scu_sg) {
  139. scu_sg->next_pair_upper = 0;
  140. scu_sg->next_pair_lower = 0;
  141. }
  142. }
  143. static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
  144. {
  145. struct ssp_cmd_iu *cmd_iu;
  146. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  147. struct sas_task *task = isci_request_access_task(ireq);
  148. cmd_iu = &sci_req->ssp.cmd;
  149. memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
  150. cmd_iu->add_cdb_len = 0;
  151. cmd_iu->_r_a = 0;
  152. cmd_iu->_r_b = 0;
  153. cmd_iu->en_fburst = 0; /* unsupported */
  154. cmd_iu->task_prio = task->ssp_task.task_prio;
  155. cmd_iu->task_attr = task->ssp_task.task_attr;
  156. cmd_iu->_r_c = 0;
  157. sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
  158. sizeof(task->ssp_task.cdb) / sizeof(u32));
  159. }
  160. static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
  161. {
  162. struct ssp_task_iu *task_iu;
  163. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  164. struct sas_task *task = isci_request_access_task(ireq);
  165. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  166. task_iu = &sci_req->ssp.tmf;
  167. memset(task_iu, 0, sizeof(struct ssp_task_iu));
  168. memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
  169. task_iu->task_func = isci_tmf->tmf_code;
  170. task_iu->task_tag =
  171. (ireq->ttype == tmf_task) ?
  172. isci_tmf->io_tag :
  173. SCI_CONTROLLER_INVALID_IO_TAG;
  174. }
  175. /**
  176. * This method is will fill in the SCU Task Context for any type of SSP request.
  177. * @sci_req:
  178. * @task_context:
  179. *
  180. */
  181. static void scu_ssp_reqeust_construct_task_context(
  182. struct scic_sds_request *sds_request,
  183. struct scu_task_context *task_context)
  184. {
  185. dma_addr_t dma_addr;
  186. struct scic_sds_remote_device *target_device;
  187. struct scic_sds_port *target_port;
  188. target_device = scic_sds_request_get_device(sds_request);
  189. target_port = scic_sds_request_get_port(sds_request);
  190. /* Fill in the TC with the its required data */
  191. task_context->abort = 0;
  192. task_context->priority = 0;
  193. task_context->initiator_request = 1;
  194. task_context->connection_rate = target_device->connection_rate;
  195. task_context->protocol_engine_index =
  196. scic_sds_controller_get_protocol_engine_group(controller);
  197. task_context->logical_port_index =
  198. scic_sds_port_get_index(target_port);
  199. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  200. task_context->valid = SCU_TASK_CONTEXT_VALID;
  201. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  202. task_context->remote_node_index =
  203. scic_sds_remote_device_get_index(sds_request->target_device);
  204. task_context->command_code = 0;
  205. task_context->link_layer_control = 0;
  206. task_context->do_not_dma_ssp_good_response = 1;
  207. task_context->strict_ordering = 0;
  208. task_context->control_frame = 0;
  209. task_context->timeout_enable = 0;
  210. task_context->block_guard_enable = 0;
  211. task_context->address_modifier = 0;
  212. /* task_context->type.ssp.tag = sci_req->io_tag; */
  213. task_context->task_phase = 0x01;
  214. if (sds_request->was_tag_assigned_by_user) {
  215. /*
  216. * Build the task context now since we have already read
  217. * the data
  218. */
  219. sds_request->post_context =
  220. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  221. (scic_sds_controller_get_protocol_engine_group(
  222. controller) <<
  223. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  224. (scic_sds_port_get_index(target_port) <<
  225. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  226. scic_sds_io_tag_get_index(sds_request->io_tag));
  227. } else {
  228. /*
  229. * Build the task context now since we have already read
  230. * the data
  231. *
  232. * I/O tag index is not assigned because we have to wait
  233. * until we get a TCi
  234. */
  235. sds_request->post_context =
  236. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  237. (scic_sds_controller_get_protocol_engine_group(
  238. owning_controller) <<
  239. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  240. (scic_sds_port_get_index(target_port) <<
  241. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  242. }
  243. /*
  244. * Copy the physical address for the command buffer to the
  245. * SCU Task Context
  246. */
  247. dma_addr = scic_io_request_get_dma_addr(sds_request,
  248. &sds_request->ssp.cmd);
  249. task_context->command_iu_upper = upper_32_bits(dma_addr);
  250. task_context->command_iu_lower = lower_32_bits(dma_addr);
  251. /*
  252. * Copy the physical address for the response buffer to the
  253. * SCU Task Context
  254. */
  255. dma_addr = scic_io_request_get_dma_addr(sds_request,
  256. &sds_request->ssp.rsp);
  257. task_context->response_iu_upper = upper_32_bits(dma_addr);
  258. task_context->response_iu_lower = lower_32_bits(dma_addr);
  259. }
  260. /**
  261. * This method is will fill in the SCU Task Context for a SSP IO request.
  262. * @sci_req:
  263. *
  264. */
  265. static void scu_ssp_io_request_construct_task_context(
  266. struct scic_sds_request *sci_req,
  267. enum dma_data_direction dir,
  268. u32 len)
  269. {
  270. struct scu_task_context *task_context;
  271. task_context = scic_sds_request_get_task_context(sci_req);
  272. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  273. task_context->ssp_command_iu_length =
  274. sizeof(struct ssp_cmd_iu) / sizeof(u32);
  275. task_context->type.ssp.frame_type = SSP_COMMAND;
  276. switch (dir) {
  277. case DMA_FROM_DEVICE:
  278. case DMA_NONE:
  279. default:
  280. task_context->task_type = SCU_TASK_TYPE_IOREAD;
  281. break;
  282. case DMA_TO_DEVICE:
  283. task_context->task_type = SCU_TASK_TYPE_IOWRITE;
  284. break;
  285. }
  286. task_context->transfer_length_bytes = len;
  287. if (task_context->transfer_length_bytes > 0)
  288. scic_sds_request_build_sgl(sci_req);
  289. }
  290. /**
  291. * This method will fill in the SCU Task Context for a SSP Task request. The
  292. * following important settings are utilized: -# priority ==
  293. * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
  294. * ahead of other task destined for the same Remote Node. -# task_type ==
  295. * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
  296. * (i.e. non-raw frame) is being utilized to perform task management. -#
  297. * control_frame == 1. This ensures that the proper endianess is set so
  298. * that the bytes are transmitted in the right order for a task frame.
  299. * @sci_req: This parameter specifies the task request object being
  300. * constructed.
  301. *
  302. */
  303. static void scu_ssp_task_request_construct_task_context(
  304. struct scic_sds_request *sci_req)
  305. {
  306. struct scu_task_context *task_context;
  307. task_context = scic_sds_request_get_task_context(sci_req);
  308. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  309. task_context->control_frame = 1;
  310. task_context->priority = SCU_TASK_PRIORITY_HIGH;
  311. task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
  312. task_context->transfer_length_bytes = 0;
  313. task_context->type.ssp.frame_type = SSP_TASK;
  314. task_context->ssp_command_iu_length =
  315. sizeof(struct ssp_task_iu) / sizeof(u32);
  316. }
  317. /**
  318. * This method is will fill in the SCU Task Context for any type of SATA
  319. * request. This is called from the various SATA constructors.
  320. * @sci_req: The general IO request object which is to be used in
  321. * constructing the SCU task context.
  322. * @task_context: The buffer pointer for the SCU task context which is being
  323. * constructed.
  324. *
  325. * The general io request construction is complete. The buffer assignment for
  326. * the command buffer is complete. none Revisit task context construction to
  327. * determine what is common for SSP/SMP/STP task context structures.
  328. */
  329. static void scu_sata_reqeust_construct_task_context(
  330. struct scic_sds_request *sci_req,
  331. struct scu_task_context *task_context)
  332. {
  333. dma_addr_t dma_addr;
  334. struct scic_sds_remote_device *target_device;
  335. struct scic_sds_port *target_port;
  336. target_device = scic_sds_request_get_device(sci_req);
  337. target_port = scic_sds_request_get_port(sci_req);
  338. /* Fill in the TC with the its required data */
  339. task_context->abort = 0;
  340. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  341. task_context->initiator_request = 1;
  342. task_context->connection_rate = target_device->connection_rate;
  343. task_context->protocol_engine_index =
  344. scic_sds_controller_get_protocol_engine_group(controller);
  345. task_context->logical_port_index =
  346. scic_sds_port_get_index(target_port);
  347. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
  348. task_context->valid = SCU_TASK_CONTEXT_VALID;
  349. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  350. task_context->remote_node_index =
  351. scic_sds_remote_device_get_index(sci_req->target_device);
  352. task_context->command_code = 0;
  353. task_context->link_layer_control = 0;
  354. task_context->do_not_dma_ssp_good_response = 1;
  355. task_context->strict_ordering = 0;
  356. task_context->control_frame = 0;
  357. task_context->timeout_enable = 0;
  358. task_context->block_guard_enable = 0;
  359. task_context->address_modifier = 0;
  360. task_context->task_phase = 0x01;
  361. task_context->ssp_command_iu_length =
  362. (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
  363. /* Set the first word of the H2D REG FIS */
  364. task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
  365. if (sci_req->was_tag_assigned_by_user) {
  366. /*
  367. * Build the task context now since we have already read
  368. * the data
  369. */
  370. sci_req->post_context =
  371. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  372. (scic_sds_controller_get_protocol_engine_group(
  373. controller) <<
  374. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  375. (scic_sds_port_get_index(target_port) <<
  376. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  377. scic_sds_io_tag_get_index(sci_req->io_tag));
  378. } else {
  379. /*
  380. * Build the task context now since we have already read
  381. * the data.
  382. * I/O tag index is not assigned because we have to wait
  383. * until we get a TCi.
  384. */
  385. sci_req->post_context =
  386. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  387. (scic_sds_controller_get_protocol_engine_group(
  388. controller) <<
  389. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  390. (scic_sds_port_get_index(target_port) <<
  391. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  392. }
  393. /*
  394. * Copy the physical address for the command buffer to the SCU Task
  395. * Context. We must offset the command buffer by 4 bytes because the
  396. * first 4 bytes are transfered in the body of the TC.
  397. */
  398. dma_addr = scic_io_request_get_dma_addr(sci_req,
  399. ((char *) &sci_req->stp.cmd) +
  400. sizeof(u32));
  401. task_context->command_iu_upper = upper_32_bits(dma_addr);
  402. task_context->command_iu_lower = lower_32_bits(dma_addr);
  403. /* SATA Requests do not have a response buffer */
  404. task_context->response_iu_upper = 0;
  405. task_context->response_iu_lower = 0;
  406. }
  407. /**
  408. * scu_stp_raw_request_construct_task_context -
  409. * @sci_req: This parameter specifies the STP request object for which to
  410. * construct a RAW command frame task context.
  411. * @task_context: This parameter specifies the SCU specific task context buffer
  412. * to construct.
  413. *
  414. * This method performs the operations common to all SATA/STP requests
  415. * utilizing the raw frame method. none
  416. */
  417. static void scu_stp_raw_request_construct_task_context(struct scic_sds_stp_request *stp_req,
  418. struct scu_task_context *task_context)
  419. {
  420. struct scic_sds_request *sci_req = to_sci_req(stp_req);
  421. scu_sata_reqeust_construct_task_context(sci_req, task_context);
  422. task_context->control_frame = 0;
  423. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  424. task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
  425. task_context->type.stp.fis_type = FIS_REGH2D;
  426. task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
  427. }
  428. static enum sci_status
  429. scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
  430. bool copy_rx_frame)
  431. {
  432. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  433. struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;
  434. scu_stp_raw_request_construct_task_context(stp_req,
  435. sci_req->task_context_buffer);
  436. pio->current_transfer_bytes = 0;
  437. pio->ending_error = 0;
  438. pio->ending_status = 0;
  439. pio->request_current.sgl_offset = 0;
  440. pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;
  441. if (copy_rx_frame) {
  442. scic_sds_request_build_sgl(sci_req);
  443. /* Since the IO request copy of the TC contains the same data as
  444. * the actual TC this pointer is vaild for either.
  445. */
  446. pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
  447. } else {
  448. /* The user does not want the data copied to the SGL buffer location */
  449. pio->request_current.sgl_pair = NULL;
  450. }
  451. return SCI_SUCCESS;
  452. }
  453. /**
  454. *
  455. * @sci_req: This parameter specifies the request to be constructed as an
  456. * optimized request.
  457. * @optimized_task_type: This parameter specifies whether the request is to be
  458. * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
  459. * value of 1 indicates NCQ.
  460. *
  461. * This method will perform request construction common to all types of STP
  462. * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
  463. * returns an indication as to whether the construction was successful.
  464. */
  465. static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
  466. u8 optimized_task_type,
  467. u32 len,
  468. enum dma_data_direction dir)
  469. {
  470. struct scu_task_context *task_context = sci_req->task_context_buffer;
  471. /* Build the STP task context structure */
  472. scu_sata_reqeust_construct_task_context(sci_req, task_context);
  473. /* Copy over the SGL elements */
  474. scic_sds_request_build_sgl(sci_req);
  475. /* Copy over the number of bytes to be transfered */
  476. task_context->transfer_length_bytes = len;
  477. if (dir == DMA_TO_DEVICE) {
  478. /*
  479. * The difference between the DMA IN and DMA OUT request task type
  480. * values are consistent with the difference between FPDMA READ
  481. * and FPDMA WRITE values. Add the supplied task type parameter
  482. * to this difference to set the task type properly for this
  483. * DATA OUT (WRITE) case. */
  484. task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
  485. - SCU_TASK_TYPE_DMA_IN);
  486. } else {
  487. /*
  488. * For the DATA IN (READ) case, simply save the supplied
  489. * optimized task type. */
  490. task_context->task_type = optimized_task_type;
  491. }
  492. }
  493. static enum sci_status
  494. scic_io_request_construct_sata(struct scic_sds_request *sci_req,
  495. u32 len,
  496. enum dma_data_direction dir,
  497. bool copy)
  498. {
  499. enum sci_status status = SCI_SUCCESS;
  500. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  501. struct sas_task *task = isci_request_access_task(ireq);
  502. /* check for management protocols */
  503. if (ireq->ttype == tmf_task) {
  504. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  505. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  506. tmf->tmf_code == isci_tmf_sata_srst_low) {
  507. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  508. sci_req->task_context_buffer);
  509. return SCI_SUCCESS;
  510. } else {
  511. dev_err(scic_to_dev(sci_req->owning_controller),
  512. "%s: Request 0x%p received un-handled SAT "
  513. "management protocol 0x%x.\n",
  514. __func__, sci_req, tmf->tmf_code);
  515. return SCI_FAILURE;
  516. }
  517. }
  518. if (!sas_protocol_ata(task->task_proto)) {
  519. dev_err(scic_to_dev(sci_req->owning_controller),
  520. "%s: Non-ATA protocol in SATA path: 0x%x\n",
  521. __func__,
  522. task->task_proto);
  523. return SCI_FAILURE;
  524. }
  525. /* non data */
  526. if (task->data_dir == DMA_NONE) {
  527. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  528. sci_req->task_context_buffer);
  529. return SCI_SUCCESS;
  530. }
  531. /* NCQ */
  532. if (task->ata_task.use_ncq) {
  533. scic_sds_stp_optimized_request_construct(sci_req,
  534. SCU_TASK_TYPE_FPDMAQ_READ,
  535. len, dir);
  536. return SCI_SUCCESS;
  537. }
  538. /* DMA */
  539. if (task->ata_task.dma_xfer) {
  540. scic_sds_stp_optimized_request_construct(sci_req,
  541. SCU_TASK_TYPE_DMA_IN,
  542. len, dir);
  543. return SCI_SUCCESS;
  544. } else /* PIO */
  545. return scic_sds_stp_pio_request_construct(sci_req, copy);
  546. return status;
  547. }
  548. static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
  549. {
  550. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  551. struct sas_task *task = isci_request_access_task(ireq);
  552. sci_req->protocol = SCIC_SSP_PROTOCOL;
  553. scu_ssp_io_request_construct_task_context(sci_req,
  554. task->data_dir,
  555. task->total_xfer_len);
  556. scic_sds_io_request_build_ssp_command_iu(sci_req);
  557. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  558. return SCI_SUCCESS;
  559. }
  560. enum sci_status scic_task_request_construct_ssp(
  561. struct scic_sds_request *sci_req)
  562. {
  563. /* Construct the SSP Task SCU Task Context */
  564. scu_ssp_task_request_construct_task_context(sci_req);
  565. /* Fill in the SSP Task IU */
  566. scic_sds_task_request_build_ssp_task_iu(sci_req);
  567. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  568. return SCI_SUCCESS;
  569. }
  570. static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
  571. {
  572. enum sci_status status;
  573. bool copy = false;
  574. struct isci_request *isci_request = sci_req_to_ireq(sci_req);
  575. struct sas_task *task = isci_request_access_task(isci_request);
  576. sci_req->protocol = SCIC_STP_PROTOCOL;
  577. copy = (task->data_dir == DMA_NONE) ? false : true;
  578. status = scic_io_request_construct_sata(sci_req,
  579. task->total_xfer_len,
  580. task->data_dir,
  581. copy);
  582. if (status == SCI_SUCCESS)
  583. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  584. return status;
  585. }
  586. enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
  587. {
  588. enum sci_status status = SCI_SUCCESS;
  589. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  590. /* check for management protocols */
  591. if (ireq->ttype == tmf_task) {
  592. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  593. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  594. tmf->tmf_code == isci_tmf_sata_srst_low) {
  595. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  596. sci_req->task_context_buffer);
  597. } else {
  598. dev_err(scic_to_dev(sci_req->owning_controller),
  599. "%s: Request 0x%p received un-handled SAT "
  600. "Protocol 0x%x.\n",
  601. __func__, sci_req, tmf->tmf_code);
  602. return SCI_FAILURE;
  603. }
  604. }
  605. if (status != SCI_SUCCESS)
  606. return status;
  607. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  608. return status;
  609. }
  610. /**
  611. * sci_req_tx_bytes - bytes transferred when reply underruns request
  612. * @sci_req: request that was terminated early
  613. */
  614. #define SCU_TASK_CONTEXT_SRAM 0x200000
  615. static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
  616. {
  617. struct scic_sds_controller *scic = sci_req->owning_controller;
  618. u32 ret_val = 0;
  619. if (readl(&scic->smu_registers->address_modifier) == 0) {
  620. void __iomem *scu_reg_base = scic->scu_registers;
  621. /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
  622. * BAR1 is the scu_registers
  623. * 0x20002C = 0x200000 + 0x2c
  624. * = start of task context SRAM + offset of (type.ssp.data_offset)
  625. * TCi is the io_tag of struct scic_sds_request
  626. */
  627. ret_val = readl(scu_reg_base +
  628. (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
  629. ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(sci_req->io_tag)));
  630. }
  631. return ret_val;
  632. }
  633. enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req)
  634. {
  635. struct scic_sds_controller *scic = sci_req->owning_controller;
  636. struct scu_task_context *task_context;
  637. enum sci_base_request_states state;
  638. if (sci_req->device_sequence !=
  639. scic_sds_remote_device_get_sequence(sci_req->target_device))
  640. return SCI_FAILURE;
  641. state = sci_req->sm.current_state_id;
  642. if (state != SCI_REQ_CONSTRUCTED) {
  643. dev_warn(scic_to_dev(scic),
  644. "%s: SCIC IO Request requested to start while in wrong "
  645. "state %d\n", __func__, state);
  646. return SCI_FAILURE_INVALID_STATE;
  647. }
  648. /* if necessary, allocate a TCi for the io request object and then will,
  649. * if necessary, copy the constructed TC data into the actual TC buffer.
  650. * If everything is successful the post context field is updated with
  651. * the TCi so the controller can post the request to the hardware.
  652. */
  653. if (sci_req->io_tag == SCI_CONTROLLER_INVALID_IO_TAG)
  654. sci_req->io_tag = scic_controller_allocate_io_tag(scic);
  655. /* Record the IO Tag in the request */
  656. if (sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  657. task_context = sci_req->task_context_buffer;
  658. task_context->task_index = scic_sds_io_tag_get_index(sci_req->io_tag);
  659. switch (task_context->protocol_type) {
  660. case SCU_TASK_CONTEXT_PROTOCOL_SMP:
  661. case SCU_TASK_CONTEXT_PROTOCOL_SSP:
  662. /* SSP/SMP Frame */
  663. task_context->type.ssp.tag = sci_req->io_tag;
  664. task_context->type.ssp.target_port_transfer_tag =
  665. 0xFFFF;
  666. break;
  667. case SCU_TASK_CONTEXT_PROTOCOL_STP:
  668. /* STP/SATA Frame
  669. * task_context->type.stp.ncq_tag = sci_req->ncq_tag;
  670. */
  671. break;
  672. case SCU_TASK_CONTEXT_PROTOCOL_NONE:
  673. /* / @todo When do we set no protocol type? */
  674. break;
  675. default:
  676. /* This should never happen since we build the IO
  677. * requests */
  678. break;
  679. }
  680. /*
  681. * Check to see if we need to copy the task context buffer
  682. * or have been building into the task context buffer */
  683. if (sci_req->was_tag_assigned_by_user == false)
  684. scic_sds_controller_copy_task_context(scic, sci_req);
  685. /* Add to the post_context the io tag value */
  686. sci_req->post_context |= scic_sds_io_tag_get_index(sci_req->io_tag);
  687. /* Everything is good go ahead and change state */
  688. sci_change_state(&sci_req->sm, SCI_REQ_STARTED);
  689. return SCI_SUCCESS;
  690. }
  691. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  692. }
  693. enum sci_status
  694. scic_sds_io_request_terminate(struct scic_sds_request *sci_req)
  695. {
  696. enum sci_base_request_states state;
  697. state = sci_req->sm.current_state_id;
  698. switch (state) {
  699. case SCI_REQ_CONSTRUCTED:
  700. scic_sds_request_set_status(sci_req,
  701. SCU_TASK_DONE_TASK_ABORT,
  702. SCI_FAILURE_IO_TERMINATED);
  703. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  704. return SCI_SUCCESS;
  705. case SCI_REQ_STARTED:
  706. case SCI_REQ_TASK_WAIT_TC_COMP:
  707. case SCI_REQ_SMP_WAIT_RESP:
  708. case SCI_REQ_SMP_WAIT_TC_COMP:
  709. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  710. case SCI_REQ_STP_UDMA_WAIT_D2H:
  711. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  712. case SCI_REQ_STP_NON_DATA_WAIT_D2H:
  713. case SCI_REQ_STP_PIO_WAIT_H2D:
  714. case SCI_REQ_STP_PIO_WAIT_FRAME:
  715. case SCI_REQ_STP_PIO_DATA_IN:
  716. case SCI_REQ_STP_PIO_DATA_OUT:
  717. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  718. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  719. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
  720. sci_change_state(&sci_req->sm, SCI_REQ_ABORTING);
  721. return SCI_SUCCESS;
  722. case SCI_REQ_TASK_WAIT_TC_RESP:
  723. sci_change_state(&sci_req->sm, SCI_REQ_ABORTING);
  724. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  725. return SCI_SUCCESS;
  726. case SCI_REQ_ABORTING:
  727. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  728. return SCI_SUCCESS;
  729. case SCI_REQ_COMPLETED:
  730. default:
  731. dev_warn(scic_to_dev(sci_req->owning_controller),
  732. "%s: SCIC IO Request requested to abort while in wrong "
  733. "state %d\n",
  734. __func__,
  735. sci_req->sm.current_state_id);
  736. break;
  737. }
  738. return SCI_FAILURE_INVALID_STATE;
  739. }
  740. enum sci_status scic_sds_request_complete(struct scic_sds_request *sci_req)
  741. {
  742. enum sci_base_request_states state;
  743. struct scic_sds_controller *scic = sci_req->owning_controller;
  744. state = sci_req->sm.current_state_id;
  745. if (WARN_ONCE(state != SCI_REQ_COMPLETED,
  746. "isci: request completion from wrong state (%d)\n", state))
  747. return SCI_FAILURE_INVALID_STATE;
  748. if (!sci_req->was_tag_assigned_by_user)
  749. scic_controller_free_io_tag(scic, sci_req->io_tag);
  750. if (sci_req->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
  751. scic_sds_controller_release_frame(scic,
  752. sci_req->saved_rx_frame_index);
  753. /* XXX can we just stop the machine and remove the 'final' state? */
  754. sci_change_state(&sci_req->sm, SCI_REQ_FINAL);
  755. return SCI_SUCCESS;
  756. }
  757. enum sci_status scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
  758. u32 event_code)
  759. {
  760. enum sci_base_request_states state;
  761. struct scic_sds_controller *scic = sci_req->owning_controller;
  762. state = sci_req->sm.current_state_id;
  763. if (state != SCI_REQ_STP_PIO_DATA_IN) {
  764. dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
  765. __func__, event_code, state);
  766. return SCI_FAILURE_INVALID_STATE;
  767. }
  768. switch (scu_get_event_specifier(event_code)) {
  769. case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
  770. /* We are waiting for data and the SCU has R_ERR the data frame.
  771. * Go back to waiting for the D2H Register FIS
  772. */
  773. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  774. return SCI_SUCCESS;
  775. default:
  776. dev_err(scic_to_dev(scic),
  777. "%s: pio request unexpected event %#x\n",
  778. __func__, event_code);
  779. /* TODO Should we fail the PIO request when we get an
  780. * unexpected event?
  781. */
  782. return SCI_FAILURE;
  783. }
  784. }
  785. /*
  786. * This function copies response data for requests returning response data
  787. * instead of sense data.
  788. * @sci_req: This parameter specifies the request object for which to copy
  789. * the response data.
  790. */
  791. static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
  792. {
  793. void *resp_buf;
  794. u32 len;
  795. struct ssp_response_iu *ssp_response;
  796. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  797. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  798. ssp_response = &sci_req->ssp.rsp;
  799. resp_buf = &isci_tmf->resp.resp_iu;
  800. len = min_t(u32,
  801. SSP_RESP_IU_MAX_SIZE,
  802. be32_to_cpu(ssp_response->response_data_len));
  803. memcpy(resp_buf, ssp_response->resp_data, len);
  804. }
  805. static enum sci_status
  806. request_started_state_tc_event(struct scic_sds_request *sci_req,
  807. u32 completion_code)
  808. {
  809. struct ssp_response_iu *resp_iu;
  810. u8 datapres;
  811. /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
  812. * to determine SDMA status
  813. */
  814. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  815. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  816. scic_sds_request_set_status(sci_req,
  817. SCU_TASK_DONE_GOOD,
  818. SCI_SUCCESS);
  819. break;
  820. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
  821. /* There are times when the SCU hardware will return an early
  822. * response because the io request specified more data than is
  823. * returned by the target device (mode pages, inquiry data,
  824. * etc.). We must check the response stats to see if this is
  825. * truly a failed request or a good request that just got
  826. * completed early.
  827. */
  828. struct ssp_response_iu *resp = &sci_req->ssp.rsp;
  829. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  830. sci_swab32_cpy(&sci_req->ssp.rsp,
  831. &sci_req->ssp.rsp,
  832. word_cnt);
  833. if (resp->status == 0) {
  834. scic_sds_request_set_status(sci_req,
  835. SCU_TASK_DONE_GOOD,
  836. SCI_SUCCESS_IO_DONE_EARLY);
  837. } else {
  838. scic_sds_request_set_status(sci_req,
  839. SCU_TASK_DONE_CHECK_RESPONSE,
  840. SCI_FAILURE_IO_RESPONSE_VALID);
  841. }
  842. break;
  843. }
  844. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
  845. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  846. sci_swab32_cpy(&sci_req->ssp.rsp,
  847. &sci_req->ssp.rsp,
  848. word_cnt);
  849. scic_sds_request_set_status(sci_req,
  850. SCU_TASK_DONE_CHECK_RESPONSE,
  851. SCI_FAILURE_IO_RESPONSE_VALID);
  852. break;
  853. }
  854. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
  855. /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
  856. * guaranteed to be received before this completion status is
  857. * posted?
  858. */
  859. resp_iu = &sci_req->ssp.rsp;
  860. datapres = resp_iu->datapres;
  861. if (datapres == 1 || datapres == 2) {
  862. scic_sds_request_set_status(sci_req,
  863. SCU_TASK_DONE_CHECK_RESPONSE,
  864. SCI_FAILURE_IO_RESPONSE_VALID);
  865. } else
  866. scic_sds_request_set_status(sci_req,
  867. SCU_TASK_DONE_GOOD,
  868. SCI_SUCCESS);
  869. break;
  870. /* only stp device gets suspended. */
  871. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  872. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
  873. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
  874. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
  875. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
  876. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
  877. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  878. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
  879. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
  880. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  881. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
  882. if (sci_req->protocol == SCIC_STP_PROTOCOL) {
  883. scic_sds_request_set_status(sci_req,
  884. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  885. SCU_COMPLETION_TL_STATUS_SHIFT,
  886. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  887. } else {
  888. scic_sds_request_set_status(sci_req,
  889. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  890. SCU_COMPLETION_TL_STATUS_SHIFT,
  891. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  892. }
  893. break;
  894. /* both stp/ssp device gets suspended */
  895. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
  896. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
  897. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
  898. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
  899. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
  900. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
  901. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
  902. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
  903. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
  904. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
  905. scic_sds_request_set_status(sci_req,
  906. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  907. SCU_COMPLETION_TL_STATUS_SHIFT,
  908. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  909. break;
  910. /* neither ssp nor stp gets suspended. */
  911. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
  912. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
  913. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
  914. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
  915. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
  916. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
  917. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  918. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  919. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  920. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  921. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
  922. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
  923. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
  924. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
  925. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
  926. default:
  927. scic_sds_request_set_status(
  928. sci_req,
  929. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  930. SCU_COMPLETION_TL_STATUS_SHIFT,
  931. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  932. break;
  933. }
  934. /*
  935. * TODO: This is probably wrong for ACK/NAK timeout conditions
  936. */
  937. /* In all cases we will treat this as the completion of the IO req. */
  938. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  939. return SCI_SUCCESS;
  940. }
  941. static enum sci_status
  942. request_aborting_state_tc_event(struct scic_sds_request *sci_req,
  943. u32 completion_code)
  944. {
  945. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  946. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  947. case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
  948. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_TASK_ABORT,
  949. SCI_FAILURE_IO_TERMINATED);
  950. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  951. break;
  952. default:
  953. /* Unless we get some strange error wait for the task abort to complete
  954. * TODO: Should there be a state change for this completion?
  955. */
  956. break;
  957. }
  958. return SCI_SUCCESS;
  959. }
  960. static enum sci_status ssp_task_request_await_tc_event(struct scic_sds_request *sci_req,
  961. u32 completion_code)
  962. {
  963. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  964. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  965. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  966. SCI_SUCCESS);
  967. sci_change_state(&sci_req->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  968. break;
  969. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  970. /* Currently, the decision is to simply allow the task request
  971. * to timeout if the task IU wasn't received successfully.
  972. * There is a potential for receiving multiple task responses if
  973. * we decide to send the task IU again.
  974. */
  975. dev_warn(scic_to_dev(sci_req->owning_controller),
  976. "%s: TaskRequest:0x%p CompletionCode:%x - "
  977. "ACK/NAK timeout\n", __func__, sci_req,
  978. completion_code);
  979. sci_change_state(&sci_req->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  980. break;
  981. default:
  982. /*
  983. * All other completion status cause the IO to be complete.
  984. * If a NAK was received, then it is up to the user to retry
  985. * the request.
  986. */
  987. scic_sds_request_set_status(sci_req,
  988. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  989. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  990. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  991. break;
  992. }
  993. return SCI_SUCCESS;
  994. }
  995. static enum sci_status
  996. smp_request_await_response_tc_event(struct scic_sds_request *sci_req,
  997. u32 completion_code)
  998. {
  999. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1000. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1001. /* In the AWAIT RESPONSE state, any TC completion is
  1002. * unexpected. but if the TC has success status, we
  1003. * complete the IO anyway.
  1004. */
  1005. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1006. SCI_SUCCESS);
  1007. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1008. break;
  1009. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  1010. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  1011. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  1012. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  1013. /* These status has been seen in a specific LSI
  1014. * expander, which sometimes is not able to send smp
  1015. * response within 2 ms. This causes our hardware break
  1016. * the connection and set TC completion with one of
  1017. * these SMP_XXX_XX_ERR status. For these type of error,
  1018. * we ask scic user to retry the request.
  1019. */
  1020. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_SMP_RESP_TO_ERR,
  1021. SCI_FAILURE_RETRY_REQUIRED);
  1022. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1023. break;
  1024. default:
  1025. /* All other completion status cause the IO to be complete. If a NAK
  1026. * was received, then it is up to the user to retry the request
  1027. */
  1028. scic_sds_request_set_status(sci_req,
  1029. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1030. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1031. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1032. break;
  1033. }
  1034. return SCI_SUCCESS;
  1035. }
  1036. static enum sci_status
  1037. smp_request_await_tc_event(struct scic_sds_request *sci_req,
  1038. u32 completion_code)
  1039. {
  1040. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1041. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1042. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1043. SCI_SUCCESS);
  1044. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1045. break;
  1046. default:
  1047. /* All other completion status cause the IO to be
  1048. * complete. If a NAK was received, then it is up to
  1049. * the user to retry the request.
  1050. */
  1051. scic_sds_request_set_status(sci_req,
  1052. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1053. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1054. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1055. break;
  1056. }
  1057. return SCI_SUCCESS;
  1058. }
  1059. void scic_stp_io_request_set_ncq_tag(struct scic_sds_request *req,
  1060. u16 ncq_tag)
  1061. {
  1062. /**
  1063. * @note This could be made to return an error to the user if the user
  1064. * attempts to set the NCQ tag in the wrong state.
  1065. */
  1066. req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
  1067. }
  1068. /**
  1069. *
  1070. * @sci_req:
  1071. *
  1072. * Get the next SGL element from the request. - Check on which SGL element pair
  1073. * we are working - if working on SLG pair element A - advance to element B -
  1074. * else - check to see if there are more SGL element pairs for this IO request
  1075. * - if there are more SGL element pairs - advance to the next pair and return
  1076. * element A struct scu_sgl_element*
  1077. */
  1078. static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
  1079. {
  1080. struct scu_sgl_element *current_sgl;
  1081. struct scic_sds_request *sci_req = to_sci_req(stp_req);
  1082. struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;
  1083. if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
  1084. if (pio_sgl->sgl_pair->B.address_lower == 0 &&
  1085. pio_sgl->sgl_pair->B.address_upper == 0) {
  1086. current_sgl = NULL;
  1087. } else {
  1088. pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
  1089. current_sgl = &pio_sgl->sgl_pair->B;
  1090. }
  1091. } else {
  1092. if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
  1093. pio_sgl->sgl_pair->next_pair_upper == 0) {
  1094. current_sgl = NULL;
  1095. } else {
  1096. u64 phys_addr;
  1097. phys_addr = pio_sgl->sgl_pair->next_pair_upper;
  1098. phys_addr <<= 32;
  1099. phys_addr |= pio_sgl->sgl_pair->next_pair_lower;
  1100. pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
  1101. pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
  1102. current_sgl = &pio_sgl->sgl_pair->A;
  1103. }
  1104. }
  1105. return current_sgl;
  1106. }
  1107. static enum sci_status
  1108. stp_request_non_data_await_h2d_tc_event(struct scic_sds_request *sci_req,
  1109. u32 completion_code)
  1110. {
  1111. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1112. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1113. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1114. SCI_SUCCESS);
  1115. sci_change_state(&sci_req->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
  1116. break;
  1117. default:
  1118. /* All other completion status cause the IO to be
  1119. * complete. If a NAK was received, then it is up to
  1120. * the user to retry the request.
  1121. */
  1122. scic_sds_request_set_status(sci_req,
  1123. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1124. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1125. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1126. break;
  1127. }
  1128. return SCI_SUCCESS;
  1129. }
  1130. #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
  1131. /* transmit DATA_FIS from (current sgl + offset) for input
  1132. * parameter length. current sgl and offset is alreay stored in the IO request
  1133. */
  1134. static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
  1135. struct scic_sds_request *sci_req,
  1136. u32 length)
  1137. {
  1138. struct scic_sds_controller *scic = sci_req->owning_controller;
  1139. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1140. struct scu_task_context *task_context;
  1141. struct scu_sgl_element *current_sgl;
  1142. /* Recycle the TC and reconstruct it for sending out DATA FIS containing
  1143. * for the data from current_sgl+offset for the input length
  1144. */
  1145. task_context = scic_sds_controller_get_task_context_buffer(scic,
  1146. sci_req->io_tag);
  1147. if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
  1148. current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
  1149. else
  1150. current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;
  1151. /* update the TC */
  1152. task_context->command_iu_upper = current_sgl->address_upper;
  1153. task_context->command_iu_lower = current_sgl->address_lower;
  1154. task_context->transfer_length_bytes = length;
  1155. task_context->type.stp.fis_type = FIS_DATA;
  1156. /* send the new TC out. */
  1157. return scic_controller_continue_io(sci_req);
  1158. }
  1159. static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
  1160. {
  1161. struct scu_sgl_element *current_sgl;
  1162. u32 sgl_offset;
  1163. u32 remaining_bytes_in_current_sgl = 0;
  1164. enum sci_status status = SCI_SUCCESS;
  1165. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1166. sgl_offset = stp_req->type.pio.request_current.sgl_offset;
  1167. if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
  1168. current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
  1169. remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
  1170. } else {
  1171. current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
  1172. remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
  1173. }
  1174. if (stp_req->type.pio.pio_transfer_bytes > 0) {
  1175. if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
  1176. /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
  1177. status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
  1178. if (status == SCI_SUCCESS) {
  1179. stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;
  1180. /* update the current sgl, sgl_offset and save for future */
  1181. current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
  1182. sgl_offset = 0;
  1183. }
  1184. } else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
  1185. /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
  1186. scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);
  1187. if (status == SCI_SUCCESS) {
  1188. /* Sgl offset will be adjusted and saved for future */
  1189. sgl_offset += stp_req->type.pio.pio_transfer_bytes;
  1190. current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
  1191. stp_req->type.pio.pio_transfer_bytes = 0;
  1192. }
  1193. }
  1194. }
  1195. if (status == SCI_SUCCESS) {
  1196. stp_req->type.pio.request_current.sgl_offset = sgl_offset;
  1197. }
  1198. return status;
  1199. }
  1200. /**
  1201. *
  1202. * @stp_request: The request that is used for the SGL processing.
  1203. * @data_buffer: The buffer of data to be copied.
  1204. * @length: The length of the data transfer.
  1205. *
  1206. * Copy the data from the buffer for the length specified to the IO reqeust SGL
  1207. * specified data region. enum sci_status
  1208. */
  1209. static enum sci_status
  1210. scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
  1211. u8 *data_buf, u32 len)
  1212. {
  1213. struct scic_sds_request *sci_req;
  1214. struct isci_request *ireq;
  1215. u8 *src_addr;
  1216. int copy_len;
  1217. struct sas_task *task;
  1218. struct scatterlist *sg;
  1219. void *kaddr;
  1220. int total_len = len;
  1221. sci_req = to_sci_req(stp_req);
  1222. ireq = sci_req_to_ireq(sci_req);
  1223. task = isci_request_access_task(ireq);
  1224. src_addr = data_buf;
  1225. if (task->num_scatter > 0) {
  1226. sg = task->scatter;
  1227. while (total_len > 0) {
  1228. struct page *page = sg_page(sg);
  1229. copy_len = min_t(int, total_len, sg_dma_len(sg));
  1230. kaddr = kmap_atomic(page, KM_IRQ0);
  1231. memcpy(kaddr + sg->offset, src_addr, copy_len);
  1232. kunmap_atomic(kaddr, KM_IRQ0);
  1233. total_len -= copy_len;
  1234. src_addr += copy_len;
  1235. sg = sg_next(sg);
  1236. }
  1237. } else {
  1238. BUG_ON(task->total_xfer_len < total_len);
  1239. memcpy(task->scatter, src_addr, total_len);
  1240. }
  1241. return SCI_SUCCESS;
  1242. }
  1243. /**
  1244. *
  1245. * @sci_req: The PIO DATA IN request that is to receive the data.
  1246. * @data_buffer: The buffer to copy from.
  1247. *
  1248. * Copy the data buffer to the io request data region. enum sci_status
  1249. */
  1250. static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
  1251. struct scic_sds_stp_request *sci_req,
  1252. u8 *data_buffer)
  1253. {
  1254. enum sci_status status;
  1255. /*
  1256. * If there is less than 1K remaining in the transfer request
  1257. * copy just the data for the transfer */
  1258. if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
  1259. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1260. sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);
  1261. if (status == SCI_SUCCESS)
  1262. sci_req->type.pio.pio_transfer_bytes = 0;
  1263. } else {
  1264. /* We are transfering the whole frame so copy */
  1265. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1266. sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
  1267. if (status == SCI_SUCCESS)
  1268. sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
  1269. }
  1270. return status;
  1271. }
  1272. static enum sci_status
  1273. stp_request_pio_await_h2d_completion_tc_event(struct scic_sds_request *sci_req,
  1274. u32 completion_code)
  1275. {
  1276. enum sci_status status = SCI_SUCCESS;
  1277. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1278. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1279. scic_sds_request_set_status(sci_req,
  1280. SCU_TASK_DONE_GOOD,
  1281. SCI_SUCCESS);
  1282. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1283. break;
  1284. default:
  1285. /* All other completion status cause the IO to be
  1286. * complete. If a NAK was received, then it is up to
  1287. * the user to retry the request.
  1288. */
  1289. scic_sds_request_set_status(sci_req,
  1290. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1291. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1292. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1293. break;
  1294. }
  1295. return status;
  1296. }
  1297. static enum sci_status
  1298. pio_data_out_tx_done_tc_event(struct scic_sds_request *sci_req,
  1299. u32 completion_code)
  1300. {
  1301. enum sci_status status = SCI_SUCCESS;
  1302. bool all_frames_transferred = false;
  1303. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1304. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1305. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1306. /* Transmit data */
  1307. if (stp_req->type.pio.pio_transfer_bytes != 0) {
  1308. status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
  1309. if (status == SCI_SUCCESS) {
  1310. if (stp_req->type.pio.pio_transfer_bytes == 0)
  1311. all_frames_transferred = true;
  1312. }
  1313. } else if (stp_req->type.pio.pio_transfer_bytes == 0) {
  1314. /*
  1315. * this will happen if the all data is written at the
  1316. * first time after the pio setup fis is received
  1317. */
  1318. all_frames_transferred = true;
  1319. }
  1320. /* all data transferred. */
  1321. if (all_frames_transferred) {
  1322. /*
  1323. * Change the state to SCI_REQ_STP_PIO_DATA_IN
  1324. * and wait for PIO_SETUP fis / or D2H REg fis. */
  1325. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1326. }
  1327. break;
  1328. default:
  1329. /*
  1330. * All other completion status cause the IO to be complete.
  1331. * If a NAK was received, then it is up to the user to retry
  1332. * the request.
  1333. */
  1334. scic_sds_request_set_status(
  1335. sci_req,
  1336. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1337. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1338. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1339. break;
  1340. }
  1341. return status;
  1342. }
  1343. static void scic_sds_stp_request_udma_complete_request(
  1344. struct scic_sds_request *request,
  1345. u32 scu_status,
  1346. enum sci_status sci_status)
  1347. {
  1348. scic_sds_request_set_status(request, scu_status, sci_status);
  1349. sci_change_state(&request->sm, SCI_REQ_COMPLETED);
  1350. }
  1351. static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
  1352. u32 frame_index)
  1353. {
  1354. struct scic_sds_controller *scic = sci_req->owning_controller;
  1355. struct dev_to_host_fis *frame_header;
  1356. enum sci_status status;
  1357. u32 *frame_buffer;
  1358. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1359. frame_index,
  1360. (void **)&frame_header);
  1361. if ((status == SCI_SUCCESS) &&
  1362. (frame_header->fis_type == FIS_REGD2H)) {
  1363. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1364. frame_index,
  1365. (void **)&frame_buffer);
  1366. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1367. frame_header,
  1368. frame_buffer);
  1369. }
  1370. scic_sds_controller_release_frame(scic, frame_index);
  1371. return status;
  1372. }
  1373. enum sci_status
  1374. scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
  1375. u32 frame_index)
  1376. {
  1377. struct scic_sds_controller *scic = sci_req->owning_controller;
  1378. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1379. enum sci_base_request_states state;
  1380. enum sci_status status;
  1381. ssize_t word_cnt;
  1382. state = sci_req->sm.current_state_id;
  1383. switch (state) {
  1384. case SCI_REQ_STARTED: {
  1385. struct ssp_frame_hdr ssp_hdr;
  1386. void *frame_header;
  1387. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1388. frame_index,
  1389. &frame_header);
  1390. word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
  1391. sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
  1392. if (ssp_hdr.frame_type == SSP_RESPONSE) {
  1393. struct ssp_response_iu *resp_iu;
  1394. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  1395. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1396. frame_index,
  1397. (void **)&resp_iu);
  1398. sci_swab32_cpy(&sci_req->ssp.rsp, resp_iu, word_cnt);
  1399. resp_iu = &sci_req->ssp.rsp;
  1400. if (resp_iu->datapres == 0x01 ||
  1401. resp_iu->datapres == 0x02) {
  1402. scic_sds_request_set_status(sci_req,
  1403. SCU_TASK_DONE_CHECK_RESPONSE,
  1404. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1405. } else
  1406. scic_sds_request_set_status(sci_req,
  1407. SCU_TASK_DONE_GOOD,
  1408. SCI_SUCCESS);
  1409. } else {
  1410. /* not a response frame, why did it get forwarded? */
  1411. dev_err(scic_to_dev(scic),
  1412. "%s: SCIC IO Request 0x%p received unexpected "
  1413. "frame %d type 0x%02x\n", __func__, sci_req,
  1414. frame_index, ssp_hdr.frame_type);
  1415. }
  1416. /*
  1417. * In any case we are done with this frame buffer return it to
  1418. * the controller
  1419. */
  1420. scic_sds_controller_release_frame(scic, frame_index);
  1421. return SCI_SUCCESS;
  1422. }
  1423. case SCI_REQ_TASK_WAIT_TC_RESP:
  1424. scic_sds_io_request_copy_response(sci_req);
  1425. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1426. scic_sds_controller_release_frame(scic,frame_index);
  1427. return SCI_SUCCESS;
  1428. case SCI_REQ_SMP_WAIT_RESP: {
  1429. struct smp_resp *rsp_hdr = &sci_req->smp.rsp;
  1430. void *frame_header;
  1431. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1432. frame_index,
  1433. &frame_header);
  1434. /* byte swap the header. */
  1435. word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
  1436. sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
  1437. if (rsp_hdr->frame_type == SMP_RESPONSE) {
  1438. void *smp_resp;
  1439. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1440. frame_index,
  1441. &smp_resp);
  1442. word_cnt = (sizeof(struct smp_req) - SMP_RESP_HDR_SZ) /
  1443. sizeof(u32);
  1444. sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
  1445. smp_resp, word_cnt);
  1446. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1447. SCI_SUCCESS);
  1448. sci_change_state(&sci_req->sm, SCI_REQ_SMP_WAIT_TC_COMP);
  1449. } else {
  1450. /*
  1451. * This was not a response frame why did it get
  1452. * forwarded?
  1453. */
  1454. dev_err(scic_to_dev(scic),
  1455. "%s: SCIC SMP Request 0x%p received unexpected "
  1456. "frame %d type 0x%02x\n",
  1457. __func__,
  1458. sci_req,
  1459. frame_index,
  1460. rsp_hdr->frame_type);
  1461. scic_sds_request_set_status(sci_req,
  1462. SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
  1463. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1464. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1465. }
  1466. scic_sds_controller_release_frame(scic, frame_index);
  1467. return SCI_SUCCESS;
  1468. }
  1469. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1470. return scic_sds_stp_request_udma_general_frame_handler(sci_req,
  1471. frame_index);
  1472. case SCI_REQ_STP_UDMA_WAIT_D2H:
  1473. /* Use the general frame handler to copy the resposne data */
  1474. status = scic_sds_stp_request_udma_general_frame_handler(sci_req,
  1475. frame_index);
  1476. if (status != SCI_SUCCESS)
  1477. return status;
  1478. scic_sds_stp_request_udma_complete_request(sci_req,
  1479. SCU_TASK_DONE_CHECK_RESPONSE,
  1480. SCI_FAILURE_IO_RESPONSE_VALID);
  1481. return SCI_SUCCESS;
  1482. case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
  1483. struct dev_to_host_fis *frame_header;
  1484. u32 *frame_buffer;
  1485. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1486. frame_index,
  1487. (void **)&frame_header);
  1488. if (status != SCI_SUCCESS) {
  1489. dev_err(scic_to_dev(scic),
  1490. "%s: SCIC IO Request 0x%p could not get frame "
  1491. "header for frame index %d, status %x\n",
  1492. __func__,
  1493. stp_req,
  1494. frame_index,
  1495. status);
  1496. return status;
  1497. }
  1498. switch (frame_header->fis_type) {
  1499. case FIS_REGD2H:
  1500. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1501. frame_index,
  1502. (void **)&frame_buffer);
  1503. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1504. frame_header,
  1505. frame_buffer);
  1506. /* The command has completed with error */
  1507. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
  1508. SCI_FAILURE_IO_RESPONSE_VALID);
  1509. break;
  1510. default:
  1511. dev_warn(scic_to_dev(scic),
  1512. "%s: IO Request:0x%p Frame Id:%d protocol "
  1513. "violation occurred\n", __func__, stp_req,
  1514. frame_index);
  1515. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
  1516. SCI_FAILURE_PROTOCOL_VIOLATION);
  1517. break;
  1518. }
  1519. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1520. /* Frame has been decoded return it to the controller */
  1521. scic_sds_controller_release_frame(scic, frame_index);
  1522. return status;
  1523. }
  1524. case SCI_REQ_STP_PIO_WAIT_FRAME: {
  1525. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  1526. struct sas_task *task = isci_request_access_task(ireq);
  1527. struct dev_to_host_fis *frame_header;
  1528. u32 *frame_buffer;
  1529. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1530. frame_index,
  1531. (void **)&frame_header);
  1532. if (status != SCI_SUCCESS) {
  1533. dev_err(scic_to_dev(scic),
  1534. "%s: SCIC IO Request 0x%p could not get frame "
  1535. "header for frame index %d, status %x\n",
  1536. __func__, stp_req, frame_index, status);
  1537. return status;
  1538. }
  1539. switch (frame_header->fis_type) {
  1540. case FIS_PIO_SETUP:
  1541. /* Get from the frame buffer the PIO Setup Data */
  1542. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1543. frame_index,
  1544. (void **)&frame_buffer);
  1545. /* Get the data from the PIO Setup The SCU Hardware
  1546. * returns first word in the frame_header and the rest
  1547. * of the data is in the frame buffer so we need to
  1548. * back up one dword
  1549. */
  1550. /* transfer_count: first 16bits in the 4th dword */
  1551. stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;
  1552. /* ending_status: 4th byte in the 3rd dword */
  1553. stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;
  1554. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1555. frame_header,
  1556. frame_buffer);
  1557. sci_req->stp.rsp.status = stp_req->type.pio.ending_status;
  1558. /* The next state is dependent on whether the
  1559. * request was PIO Data-in or Data out
  1560. */
  1561. if (task->data_dir == DMA_FROM_DEVICE) {
  1562. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_DATA_IN);
  1563. } else if (task->data_dir == DMA_TO_DEVICE) {
  1564. /* Transmit data */
  1565. status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
  1566. if (status != SCI_SUCCESS)
  1567. break;
  1568. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_DATA_OUT);
  1569. }
  1570. break;
  1571. case FIS_SETDEVBITS:
  1572. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1573. break;
  1574. case FIS_REGD2H:
  1575. if (frame_header->status & ATA_BUSY) {
  1576. /*
  1577. * Now why is the drive sending a D2H Register
  1578. * FIS when it is still busy? Do nothing since
  1579. * we are still in the right state.
  1580. */
  1581. dev_dbg(scic_to_dev(scic),
  1582. "%s: SCIC PIO Request 0x%p received "
  1583. "D2H Register FIS with BSY status "
  1584. "0x%x\n",
  1585. __func__,
  1586. stp_req,
  1587. frame_header->status);
  1588. break;
  1589. }
  1590. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1591. frame_index,
  1592. (void **)&frame_buffer);
  1593. scic_sds_controller_copy_sata_response(&sci_req->stp.req,
  1594. frame_header,
  1595. frame_buffer);
  1596. scic_sds_request_set_status(sci_req,
  1597. SCU_TASK_DONE_CHECK_RESPONSE,
  1598. SCI_FAILURE_IO_RESPONSE_VALID);
  1599. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1600. break;
  1601. default:
  1602. /* FIXME: what do we do here? */
  1603. break;
  1604. }
  1605. /* Frame is decoded return it to the controller */
  1606. scic_sds_controller_release_frame(scic, frame_index);
  1607. return status;
  1608. }
  1609. case SCI_REQ_STP_PIO_DATA_IN: {
  1610. struct dev_to_host_fis *frame_header;
  1611. struct sata_fis_data *frame_buffer;
  1612. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1613. frame_index,
  1614. (void **)&frame_header);
  1615. if (status != SCI_SUCCESS) {
  1616. dev_err(scic_to_dev(scic),
  1617. "%s: SCIC IO Request 0x%p could not get frame "
  1618. "header for frame index %d, status %x\n",
  1619. __func__,
  1620. stp_req,
  1621. frame_index,
  1622. status);
  1623. return status;
  1624. }
  1625. if (frame_header->fis_type != FIS_DATA) {
  1626. dev_err(scic_to_dev(scic),
  1627. "%s: SCIC PIO Request 0x%p received frame %d "
  1628. "with fis type 0x%02x when expecting a data "
  1629. "fis.\n",
  1630. __func__,
  1631. stp_req,
  1632. frame_index,
  1633. frame_header->fis_type);
  1634. scic_sds_request_set_status(sci_req,
  1635. SCU_TASK_DONE_GOOD,
  1636. SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
  1637. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1638. /* Frame is decoded return it to the controller */
  1639. scic_sds_controller_release_frame(scic, frame_index);
  1640. return status;
  1641. }
  1642. if (stp_req->type.pio.request_current.sgl_pair == NULL) {
  1643. sci_req->saved_rx_frame_index = frame_index;
  1644. stp_req->type.pio.pio_transfer_bytes = 0;
  1645. } else {
  1646. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1647. frame_index,
  1648. (void **)&frame_buffer);
  1649. status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
  1650. (u8 *)frame_buffer);
  1651. /* Frame is decoded return it to the controller */
  1652. scic_sds_controller_release_frame(scic, frame_index);
  1653. }
  1654. /* Check for the end of the transfer, are there more
  1655. * bytes remaining for this data transfer
  1656. */
  1657. if (status != SCI_SUCCESS ||
  1658. stp_req->type.pio.pio_transfer_bytes != 0)
  1659. return status;
  1660. if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
  1661. scic_sds_request_set_status(sci_req,
  1662. SCU_TASK_DONE_CHECK_RESPONSE,
  1663. SCI_FAILURE_IO_RESPONSE_VALID);
  1664. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1665. } else {
  1666. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1667. }
  1668. return status;
  1669. }
  1670. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
  1671. struct dev_to_host_fis *frame_header;
  1672. u32 *frame_buffer;
  1673. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1674. frame_index,
  1675. (void **)&frame_header);
  1676. if (status != SCI_SUCCESS) {
  1677. dev_err(scic_to_dev(scic),
  1678. "%s: SCIC IO Request 0x%p could not get frame "
  1679. "header for frame index %d, status %x\n",
  1680. __func__,
  1681. stp_req,
  1682. frame_index,
  1683. status);
  1684. return status;
  1685. }
  1686. switch (frame_header->fis_type) {
  1687. case FIS_REGD2H:
  1688. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1689. frame_index,
  1690. (void **)&frame_buffer);
  1691. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1692. frame_header,
  1693. frame_buffer);
  1694. /* The command has completed with error */
  1695. scic_sds_request_set_status(sci_req,
  1696. SCU_TASK_DONE_CHECK_RESPONSE,
  1697. SCI_FAILURE_IO_RESPONSE_VALID);
  1698. break;
  1699. default:
  1700. dev_warn(scic_to_dev(scic),
  1701. "%s: IO Request:0x%p Frame Id:%d protocol "
  1702. "violation occurred\n",
  1703. __func__,
  1704. stp_req,
  1705. frame_index);
  1706. scic_sds_request_set_status(sci_req,
  1707. SCU_TASK_DONE_UNEXP_FIS,
  1708. SCI_FAILURE_PROTOCOL_VIOLATION);
  1709. break;
  1710. }
  1711. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1712. /* Frame has been decoded return it to the controller */
  1713. scic_sds_controller_release_frame(scic, frame_index);
  1714. return status;
  1715. }
  1716. case SCI_REQ_ABORTING:
  1717. /*
  1718. * TODO: Is it even possible to get an unsolicited frame in the
  1719. * aborting state?
  1720. */
  1721. scic_sds_controller_release_frame(scic, frame_index);
  1722. return SCI_SUCCESS;
  1723. default:
  1724. dev_warn(scic_to_dev(scic),
  1725. "%s: SCIC IO Request given unexpected frame %x while "
  1726. "in state %d\n",
  1727. __func__,
  1728. frame_index,
  1729. state);
  1730. scic_sds_controller_release_frame(scic, frame_index);
  1731. return SCI_FAILURE_INVALID_STATE;
  1732. }
  1733. }
  1734. static enum sci_status stp_request_udma_await_tc_event(struct scic_sds_request *sci_req,
  1735. u32 completion_code)
  1736. {
  1737. enum sci_status status = SCI_SUCCESS;
  1738. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1739. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1740. scic_sds_stp_request_udma_complete_request(sci_req,
  1741. SCU_TASK_DONE_GOOD,
  1742. SCI_SUCCESS);
  1743. break;
  1744. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
  1745. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  1746. /* We must check ther response buffer to see if the D2H
  1747. * Register FIS was received before we got the TC
  1748. * completion.
  1749. */
  1750. if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
  1751. scic_sds_remote_device_suspend(sci_req->target_device,
  1752. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1753. scic_sds_stp_request_udma_complete_request(sci_req,
  1754. SCU_TASK_DONE_CHECK_RESPONSE,
  1755. SCI_FAILURE_IO_RESPONSE_VALID);
  1756. } else {
  1757. /* If we have an error completion status for the
  1758. * TC then we can expect a D2H register FIS from
  1759. * the device so we must change state to wait
  1760. * for it
  1761. */
  1762. sci_change_state(&sci_req->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
  1763. }
  1764. break;
  1765. /* TODO Check to see if any of these completion status need to
  1766. * wait for the device to host register fis.
  1767. */
  1768. /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
  1769. * - this comes only for B0
  1770. */
  1771. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
  1772. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  1773. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
  1774. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
  1775. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
  1776. scic_sds_remote_device_suspend(sci_req->target_device,
  1777. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1778. /* Fall through to the default case */
  1779. default:
  1780. /* All other completion status cause the IO to be complete. */
  1781. scic_sds_stp_request_udma_complete_request(sci_req,
  1782. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1783. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1784. break;
  1785. }
  1786. return status;
  1787. }
  1788. static enum sci_status
  1789. stp_request_soft_reset_await_h2d_asserted_tc_event(struct scic_sds_request *sci_req,
  1790. u32 completion_code)
  1791. {
  1792. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1793. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1794. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1795. SCI_SUCCESS);
  1796. sci_change_state(&sci_req->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
  1797. break;
  1798. default:
  1799. /*
  1800. * All other completion status cause the IO to be complete.
  1801. * If a NAK was received, then it is up to the user to retry
  1802. * the request.
  1803. */
  1804. scic_sds_request_set_status(sci_req,
  1805. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1806. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1807. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1808. break;
  1809. }
  1810. return SCI_SUCCESS;
  1811. }
  1812. static enum sci_status
  1813. stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct scic_sds_request *sci_req,
  1814. u32 completion_code)
  1815. {
  1816. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1817. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1818. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1819. SCI_SUCCESS);
  1820. sci_change_state(&sci_req->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
  1821. break;
  1822. default:
  1823. /* All other completion status cause the IO to be complete. If
  1824. * a NAK was received, then it is up to the user to retry the
  1825. * request.
  1826. */
  1827. scic_sds_request_set_status(sci_req,
  1828. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1829. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1830. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1831. break;
  1832. }
  1833. return SCI_SUCCESS;
  1834. }
  1835. enum sci_status
  1836. scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req,
  1837. u32 completion_code)
  1838. {
  1839. enum sci_base_request_states state;
  1840. struct scic_sds_controller *scic = sci_req->owning_controller;
  1841. state = sci_req->sm.current_state_id;
  1842. switch (state) {
  1843. case SCI_REQ_STARTED:
  1844. return request_started_state_tc_event(sci_req, completion_code);
  1845. case SCI_REQ_TASK_WAIT_TC_COMP:
  1846. return ssp_task_request_await_tc_event(sci_req,
  1847. completion_code);
  1848. case SCI_REQ_SMP_WAIT_RESP:
  1849. return smp_request_await_response_tc_event(sci_req,
  1850. completion_code);
  1851. case SCI_REQ_SMP_WAIT_TC_COMP:
  1852. return smp_request_await_tc_event(sci_req, completion_code);
  1853. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1854. return stp_request_udma_await_tc_event(sci_req,
  1855. completion_code);
  1856. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  1857. return stp_request_non_data_await_h2d_tc_event(sci_req,
  1858. completion_code);
  1859. case SCI_REQ_STP_PIO_WAIT_H2D:
  1860. return stp_request_pio_await_h2d_completion_tc_event(sci_req,
  1861. completion_code);
  1862. case SCI_REQ_STP_PIO_DATA_OUT:
  1863. return pio_data_out_tx_done_tc_event(sci_req, completion_code);
  1864. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  1865. return stp_request_soft_reset_await_h2d_asserted_tc_event(sci_req,
  1866. completion_code);
  1867. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  1868. return stp_request_soft_reset_await_h2d_diagnostic_tc_event(sci_req,
  1869. completion_code);
  1870. case SCI_REQ_ABORTING:
  1871. return request_aborting_state_tc_event(sci_req,
  1872. completion_code);
  1873. default:
  1874. dev_warn(scic_to_dev(scic),
  1875. "%s: SCIC IO Request given task completion "
  1876. "notification %x while in wrong state %d\n",
  1877. __func__,
  1878. completion_code,
  1879. state);
  1880. return SCI_FAILURE_INVALID_STATE;
  1881. }
  1882. }
  1883. /**
  1884. * isci_request_process_response_iu() - This function sets the status and
  1885. * response iu, in the task struct, from the request object for the upper
  1886. * layer driver.
  1887. * @sas_task: This parameter is the task struct from the upper layer driver.
  1888. * @resp_iu: This parameter points to the response iu of the completed request.
  1889. * @dev: This parameter specifies the linux device struct.
  1890. *
  1891. * none.
  1892. */
  1893. static void isci_request_process_response_iu(
  1894. struct sas_task *task,
  1895. struct ssp_response_iu *resp_iu,
  1896. struct device *dev)
  1897. {
  1898. dev_dbg(dev,
  1899. "%s: resp_iu = %p "
  1900. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  1901. "resp_iu->response_data_len = %x, "
  1902. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  1903. __func__,
  1904. resp_iu,
  1905. resp_iu->status,
  1906. resp_iu->datapres,
  1907. resp_iu->response_data_len,
  1908. resp_iu->sense_data_len);
  1909. task->task_status.stat = resp_iu->status;
  1910. /* libsas updates the task status fields based on the response iu. */
  1911. sas_ssp_task_response(dev, task, resp_iu);
  1912. }
  1913. /**
  1914. * isci_request_set_open_reject_status() - This function prepares the I/O
  1915. * completion for OPEN_REJECT conditions.
  1916. * @request: This parameter is the completed isci_request object.
  1917. * @response_ptr: This parameter specifies the service response for the I/O.
  1918. * @status_ptr: This parameter specifies the exec status for the I/O.
  1919. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1920. * the LLDD with respect to completing this request or forcing an abort
  1921. * condition on the I/O.
  1922. * @open_rej_reason: This parameter specifies the encoded reason for the
  1923. * abandon-class reject.
  1924. *
  1925. * none.
  1926. */
  1927. static void isci_request_set_open_reject_status(
  1928. struct isci_request *request,
  1929. struct sas_task *task,
  1930. enum service_response *response_ptr,
  1931. enum exec_status *status_ptr,
  1932. enum isci_completion_selection *complete_to_host_ptr,
  1933. enum sas_open_rej_reason open_rej_reason)
  1934. {
  1935. /* Task in the target is done. */
  1936. request->complete_in_target = true;
  1937. *response_ptr = SAS_TASK_UNDELIVERED;
  1938. *status_ptr = SAS_OPEN_REJECT;
  1939. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1940. task->task_status.open_rej_reason = open_rej_reason;
  1941. }
  1942. /**
  1943. * isci_request_handle_controller_specific_errors() - This function decodes
  1944. * controller-specific I/O completion error conditions.
  1945. * @request: This parameter is the completed isci_request object.
  1946. * @response_ptr: This parameter specifies the service response for the I/O.
  1947. * @status_ptr: This parameter specifies the exec status for the I/O.
  1948. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1949. * the LLDD with respect to completing this request or forcing an abort
  1950. * condition on the I/O.
  1951. *
  1952. * none.
  1953. */
  1954. static void isci_request_handle_controller_specific_errors(
  1955. struct isci_remote_device *isci_device,
  1956. struct isci_request *request,
  1957. struct sas_task *task,
  1958. enum service_response *response_ptr,
  1959. enum exec_status *status_ptr,
  1960. enum isci_completion_selection *complete_to_host_ptr)
  1961. {
  1962. unsigned int cstatus;
  1963. cstatus = request->sci.scu_status;
  1964. dev_dbg(&request->isci_host->pdev->dev,
  1965. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  1966. "- controller status = 0x%x\n",
  1967. __func__, request, cstatus);
  1968. /* Decode the controller-specific errors; most
  1969. * important is to recognize those conditions in which
  1970. * the target may still have a task outstanding that
  1971. * must be aborted.
  1972. *
  1973. * Note that there are SCU completion codes being
  1974. * named in the decode below for which SCIC has already
  1975. * done work to handle them in a way other than as
  1976. * a controller-specific completion code; these are left
  1977. * in the decode below for completeness sake.
  1978. */
  1979. switch (cstatus) {
  1980. case SCU_TASK_DONE_DMASETUP_DIRERR:
  1981. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  1982. case SCU_TASK_DONE_XFERCNT_ERR:
  1983. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  1984. if (task->task_proto == SAS_PROTOCOL_SMP) {
  1985. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  1986. *response_ptr = SAS_TASK_COMPLETE;
  1987. /* See if the device has been/is being stopped. Note
  1988. * that we ignore the quiesce state, since we are
  1989. * concerned about the actual device state.
  1990. */
  1991. if ((isci_device->status == isci_stopping) ||
  1992. (isci_device->status == isci_stopped))
  1993. *status_ptr = SAS_DEVICE_UNKNOWN;
  1994. else
  1995. *status_ptr = SAS_ABORTED_TASK;
  1996. request->complete_in_target = true;
  1997. *complete_to_host_ptr =
  1998. isci_perform_normal_io_completion;
  1999. } else {
  2000. /* Task in the target is not done. */
  2001. *response_ptr = SAS_TASK_UNDELIVERED;
  2002. if ((isci_device->status == isci_stopping) ||
  2003. (isci_device->status == isci_stopped))
  2004. *status_ptr = SAS_DEVICE_UNKNOWN;
  2005. else
  2006. *status_ptr = SAM_STAT_TASK_ABORTED;
  2007. request->complete_in_target = false;
  2008. *complete_to_host_ptr =
  2009. isci_perform_error_io_completion;
  2010. }
  2011. break;
  2012. case SCU_TASK_DONE_CRC_ERR:
  2013. case SCU_TASK_DONE_NAK_CMD_ERR:
  2014. case SCU_TASK_DONE_EXCESS_DATA:
  2015. case SCU_TASK_DONE_UNEXP_FIS:
  2016. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  2017. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  2018. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  2019. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  2020. /* These are conditions in which the target
  2021. * has completed the task, so that no cleanup
  2022. * is necessary.
  2023. */
  2024. *response_ptr = SAS_TASK_COMPLETE;
  2025. /* See if the device has been/is being stopped. Note
  2026. * that we ignore the quiesce state, since we are
  2027. * concerned about the actual device state.
  2028. */
  2029. if ((isci_device->status == isci_stopping) ||
  2030. (isci_device->status == isci_stopped))
  2031. *status_ptr = SAS_DEVICE_UNKNOWN;
  2032. else
  2033. *status_ptr = SAS_ABORTED_TASK;
  2034. request->complete_in_target = true;
  2035. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2036. break;
  2037. /* Note that the only open reject completion codes seen here will be
  2038. * abandon-class codes; all others are automatically retried in the SCU.
  2039. */
  2040. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  2041. isci_request_set_open_reject_status(
  2042. request, task, response_ptr, status_ptr,
  2043. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  2044. break;
  2045. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  2046. /* Note - the return of AB0 will change when
  2047. * libsas implements detection of zone violations.
  2048. */
  2049. isci_request_set_open_reject_status(
  2050. request, task, response_ptr, status_ptr,
  2051. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  2052. break;
  2053. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  2054. isci_request_set_open_reject_status(
  2055. request, task, response_ptr, status_ptr,
  2056. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  2057. break;
  2058. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  2059. isci_request_set_open_reject_status(
  2060. request, task, response_ptr, status_ptr,
  2061. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  2062. break;
  2063. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  2064. isci_request_set_open_reject_status(
  2065. request, task, response_ptr, status_ptr,
  2066. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  2067. break;
  2068. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  2069. isci_request_set_open_reject_status(
  2070. request, task, response_ptr, status_ptr,
  2071. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  2072. break;
  2073. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  2074. isci_request_set_open_reject_status(
  2075. request, task, response_ptr, status_ptr,
  2076. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  2077. break;
  2078. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  2079. isci_request_set_open_reject_status(
  2080. request, task, response_ptr, status_ptr,
  2081. complete_to_host_ptr, SAS_OREJ_EPROTO);
  2082. break;
  2083. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  2084. isci_request_set_open_reject_status(
  2085. request, task, response_ptr, status_ptr,
  2086. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  2087. break;
  2088. case SCU_TASK_DONE_LL_R_ERR:
  2089. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  2090. case SCU_TASK_DONE_LL_PERR:
  2091. case SCU_TASK_DONE_LL_SY_TERM:
  2092. /* Also SCU_TASK_DONE_NAK_ERR:*/
  2093. case SCU_TASK_DONE_LL_LF_TERM:
  2094. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  2095. case SCU_TASK_DONE_LL_ABORT_ERR:
  2096. case SCU_TASK_DONE_SEQ_INV_TYPE:
  2097. /* Also SCU_TASK_DONE_UNEXP_XR: */
  2098. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  2099. case SCU_TASK_DONE_INV_FIS_LEN:
  2100. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  2101. case SCU_TASK_DONE_SDMA_ERR:
  2102. case SCU_TASK_DONE_OFFSET_ERR:
  2103. case SCU_TASK_DONE_MAX_PLD_ERR:
  2104. case SCU_TASK_DONE_LF_ERR:
  2105. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  2106. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  2107. case SCU_TASK_DONE_UNEXP_DATA:
  2108. case SCU_TASK_DONE_UNEXP_SDBFIS:
  2109. case SCU_TASK_DONE_REG_ERR:
  2110. case SCU_TASK_DONE_SDB_ERR:
  2111. case SCU_TASK_DONE_TASK_ABORT:
  2112. default:
  2113. /* Task in the target is not done. */
  2114. *response_ptr = SAS_TASK_UNDELIVERED;
  2115. *status_ptr = SAM_STAT_TASK_ABORTED;
  2116. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2117. request->complete_in_target = true;
  2118. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2119. } else {
  2120. request->complete_in_target = false;
  2121. *complete_to_host_ptr = isci_perform_error_io_completion;
  2122. }
  2123. break;
  2124. }
  2125. }
  2126. /**
  2127. * isci_task_save_for_upper_layer_completion() - This function saves the
  2128. * request for later completion to the upper layer driver.
  2129. * @host: This parameter is a pointer to the host on which the the request
  2130. * should be queued (either as an error or success).
  2131. * @request: This parameter is the completed request.
  2132. * @response: This parameter is the response code for the completed task.
  2133. * @status: This parameter is the status code for the completed task.
  2134. *
  2135. * none.
  2136. */
  2137. static void isci_task_save_for_upper_layer_completion(
  2138. struct isci_host *host,
  2139. struct isci_request *request,
  2140. enum service_response response,
  2141. enum exec_status status,
  2142. enum isci_completion_selection task_notification_selection)
  2143. {
  2144. struct sas_task *task = isci_request_access_task(request);
  2145. task_notification_selection
  2146. = isci_task_set_completion_status(task, response, status,
  2147. task_notification_selection);
  2148. /* Tasks aborted specifically by a call to the lldd_abort_task
  2149. * function should not be completed to the host in the regular path.
  2150. */
  2151. switch (task_notification_selection) {
  2152. case isci_perform_normal_io_completion:
  2153. /* Normal notification (task_done) */
  2154. dev_dbg(&host->pdev->dev,
  2155. "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
  2156. __func__,
  2157. task,
  2158. task->task_status.resp, response,
  2159. task->task_status.stat, status);
  2160. /* Add to the completed list. */
  2161. list_add(&request->completed_node,
  2162. &host->requests_to_complete);
  2163. /* Take the request off the device's pending request list. */
  2164. list_del_init(&request->dev_node);
  2165. break;
  2166. case isci_perform_aborted_io_completion:
  2167. /* No notification to libsas because this request is
  2168. * already in the abort path.
  2169. */
  2170. dev_warn(&host->pdev->dev,
  2171. "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
  2172. __func__,
  2173. task,
  2174. task->task_status.resp, response,
  2175. task->task_status.stat, status);
  2176. /* Wake up whatever process was waiting for this
  2177. * request to complete.
  2178. */
  2179. WARN_ON(request->io_request_completion == NULL);
  2180. if (request->io_request_completion != NULL) {
  2181. /* Signal whoever is waiting that this
  2182. * request is complete.
  2183. */
  2184. complete(request->io_request_completion);
  2185. }
  2186. break;
  2187. case isci_perform_error_io_completion:
  2188. /* Use sas_task_abort */
  2189. dev_warn(&host->pdev->dev,
  2190. "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
  2191. __func__,
  2192. task,
  2193. task->task_status.resp, response,
  2194. task->task_status.stat, status);
  2195. /* Add to the aborted list. */
  2196. list_add(&request->completed_node,
  2197. &host->requests_to_errorback);
  2198. break;
  2199. default:
  2200. dev_warn(&host->pdev->dev,
  2201. "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
  2202. __func__,
  2203. task,
  2204. task->task_status.resp, response,
  2205. task->task_status.stat, status);
  2206. /* Add to the error to libsas list. */
  2207. list_add(&request->completed_node,
  2208. &host->requests_to_errorback);
  2209. break;
  2210. }
  2211. }
  2212. static void isci_request_io_request_complete(struct isci_host *isci_host,
  2213. struct isci_request *request,
  2214. enum sci_io_status completion_status)
  2215. {
  2216. struct sas_task *task = isci_request_access_task(request);
  2217. struct ssp_response_iu *resp_iu;
  2218. void *resp_buf;
  2219. unsigned long task_flags;
  2220. struct isci_remote_device *isci_device = request->isci_device;
  2221. enum service_response response = SAS_TASK_UNDELIVERED;
  2222. enum exec_status status = SAS_ABORTED_TASK;
  2223. enum isci_request_status request_status;
  2224. enum isci_completion_selection complete_to_host
  2225. = isci_perform_normal_io_completion;
  2226. dev_dbg(&isci_host->pdev->dev,
  2227. "%s: request = %p, task = %p,\n"
  2228. "task->data_dir = %d completion_status = 0x%x\n",
  2229. __func__,
  2230. request,
  2231. task,
  2232. task->data_dir,
  2233. completion_status);
  2234. spin_lock(&request->state_lock);
  2235. request_status = isci_request_get_state(request);
  2236. /* Decode the request status. Note that if the request has been
  2237. * aborted by a task management function, we don't care
  2238. * what the status is.
  2239. */
  2240. switch (request_status) {
  2241. case aborted:
  2242. /* "aborted" indicates that the request was aborted by a task
  2243. * management function, since once a task management request is
  2244. * perfomed by the device, the request only completes because
  2245. * of the subsequent driver terminate.
  2246. *
  2247. * Aborted also means an external thread is explicitly managing
  2248. * this request, so that we do not complete it up the stack.
  2249. *
  2250. * The target is still there (since the TMF was successful).
  2251. */
  2252. request->complete_in_target = true;
  2253. response = SAS_TASK_COMPLETE;
  2254. /* See if the device has been/is being stopped. Note
  2255. * that we ignore the quiesce state, since we are
  2256. * concerned about the actual device state.
  2257. */
  2258. if ((isci_device->status == isci_stopping)
  2259. || (isci_device->status == isci_stopped)
  2260. )
  2261. status = SAS_DEVICE_UNKNOWN;
  2262. else
  2263. status = SAS_ABORTED_TASK;
  2264. complete_to_host = isci_perform_aborted_io_completion;
  2265. /* This was an aborted request. */
  2266. spin_unlock(&request->state_lock);
  2267. break;
  2268. case aborting:
  2269. /* aborting means that the task management function tried and
  2270. * failed to abort the request. We need to note the request
  2271. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  2272. * target as down.
  2273. *
  2274. * Aborting also means an external thread is explicitly managing
  2275. * this request, so that we do not complete it up the stack.
  2276. */
  2277. request->complete_in_target = true;
  2278. response = SAS_TASK_UNDELIVERED;
  2279. if ((isci_device->status == isci_stopping) ||
  2280. (isci_device->status == isci_stopped))
  2281. /* The device has been /is being stopped. Note that
  2282. * we ignore the quiesce state, since we are
  2283. * concerned about the actual device state.
  2284. */
  2285. status = SAS_DEVICE_UNKNOWN;
  2286. else
  2287. status = SAS_PHY_DOWN;
  2288. complete_to_host = isci_perform_aborted_io_completion;
  2289. /* This was an aborted request. */
  2290. spin_unlock(&request->state_lock);
  2291. break;
  2292. case terminating:
  2293. /* This was an terminated request. This happens when
  2294. * the I/O is being terminated because of an action on
  2295. * the device (reset, tear down, etc.), and the I/O needs
  2296. * to be completed up the stack.
  2297. */
  2298. request->complete_in_target = true;
  2299. response = SAS_TASK_UNDELIVERED;
  2300. /* See if the device has been/is being stopped. Note
  2301. * that we ignore the quiesce state, since we are
  2302. * concerned about the actual device state.
  2303. */
  2304. if ((isci_device->status == isci_stopping) ||
  2305. (isci_device->status == isci_stopped))
  2306. status = SAS_DEVICE_UNKNOWN;
  2307. else
  2308. status = SAS_ABORTED_TASK;
  2309. complete_to_host = isci_perform_aborted_io_completion;
  2310. /* This was a terminated request. */
  2311. spin_unlock(&request->state_lock);
  2312. break;
  2313. default:
  2314. /* The request is done from an SCU HW perspective. */
  2315. request->status = completed;
  2316. spin_unlock(&request->state_lock);
  2317. /* This is an active request being completed from the core. */
  2318. switch (completion_status) {
  2319. case SCI_IO_FAILURE_RESPONSE_VALID:
  2320. dev_dbg(&isci_host->pdev->dev,
  2321. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  2322. __func__,
  2323. request,
  2324. task);
  2325. if (sas_protocol_ata(task->task_proto)) {
  2326. resp_buf = &request->sci.stp.rsp;
  2327. isci_request_process_stp_response(task,
  2328. resp_buf);
  2329. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  2330. /* crack the iu response buffer. */
  2331. resp_iu = &request->sci.ssp.rsp;
  2332. isci_request_process_response_iu(task, resp_iu,
  2333. &isci_host->pdev->dev);
  2334. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  2335. dev_err(&isci_host->pdev->dev,
  2336. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  2337. "SAS_PROTOCOL_SMP protocol\n",
  2338. __func__);
  2339. } else
  2340. dev_err(&isci_host->pdev->dev,
  2341. "%s: unknown protocol\n", __func__);
  2342. /* use the task status set in the task struct by the
  2343. * isci_request_process_response_iu call.
  2344. */
  2345. request->complete_in_target = true;
  2346. response = task->task_status.resp;
  2347. status = task->task_status.stat;
  2348. break;
  2349. case SCI_IO_SUCCESS:
  2350. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  2351. response = SAS_TASK_COMPLETE;
  2352. status = SAM_STAT_GOOD;
  2353. request->complete_in_target = true;
  2354. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2355. void *rsp = &request->sci.smp.rsp;
  2356. dev_dbg(&isci_host->pdev->dev,
  2357. "%s: SMP protocol completion\n",
  2358. __func__);
  2359. sg_copy_from_buffer(
  2360. &task->smp_task.smp_resp, 1,
  2361. rsp, sizeof(struct smp_resp));
  2362. } else if (completion_status
  2363. == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  2364. /* This was an SSP / STP / SATA transfer.
  2365. * There is a possibility that less data than
  2366. * the maximum was transferred.
  2367. */
  2368. u32 transferred_length = sci_req_tx_bytes(&request->sci);
  2369. task->task_status.residual
  2370. = task->total_xfer_len - transferred_length;
  2371. /* If there were residual bytes, call this an
  2372. * underrun.
  2373. */
  2374. if (task->task_status.residual != 0)
  2375. status = SAS_DATA_UNDERRUN;
  2376. dev_dbg(&isci_host->pdev->dev,
  2377. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  2378. __func__,
  2379. status);
  2380. } else
  2381. dev_dbg(&isci_host->pdev->dev,
  2382. "%s: SCI_IO_SUCCESS\n",
  2383. __func__);
  2384. break;
  2385. case SCI_IO_FAILURE_TERMINATED:
  2386. dev_dbg(&isci_host->pdev->dev,
  2387. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  2388. __func__,
  2389. request,
  2390. task);
  2391. /* The request was terminated explicitly. No handling
  2392. * is needed in the SCSI error handler path.
  2393. */
  2394. request->complete_in_target = true;
  2395. response = SAS_TASK_UNDELIVERED;
  2396. /* See if the device has been/is being stopped. Note
  2397. * that we ignore the quiesce state, since we are
  2398. * concerned about the actual device state.
  2399. */
  2400. if ((isci_device->status == isci_stopping) ||
  2401. (isci_device->status == isci_stopped))
  2402. status = SAS_DEVICE_UNKNOWN;
  2403. else
  2404. status = SAS_ABORTED_TASK;
  2405. complete_to_host = isci_perform_normal_io_completion;
  2406. break;
  2407. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  2408. isci_request_handle_controller_specific_errors(
  2409. isci_device, request, task, &response, &status,
  2410. &complete_to_host);
  2411. break;
  2412. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  2413. /* This is a special case, in that the I/O completion
  2414. * is telling us that the device needs a reset.
  2415. * In order for the device reset condition to be
  2416. * noticed, the I/O has to be handled in the error
  2417. * handler. Set the reset flag and cause the
  2418. * SCSI error thread to be scheduled.
  2419. */
  2420. spin_lock_irqsave(&task->task_state_lock, task_flags);
  2421. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2422. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  2423. /* Fail the I/O. */
  2424. response = SAS_TASK_UNDELIVERED;
  2425. status = SAM_STAT_TASK_ABORTED;
  2426. complete_to_host = isci_perform_error_io_completion;
  2427. request->complete_in_target = false;
  2428. break;
  2429. case SCI_FAILURE_RETRY_REQUIRED:
  2430. /* Fail the I/O so it can be retried. */
  2431. response = SAS_TASK_UNDELIVERED;
  2432. if ((isci_device->status == isci_stopping) ||
  2433. (isci_device->status == isci_stopped))
  2434. status = SAS_DEVICE_UNKNOWN;
  2435. else
  2436. status = SAS_ABORTED_TASK;
  2437. complete_to_host = isci_perform_normal_io_completion;
  2438. request->complete_in_target = true;
  2439. break;
  2440. default:
  2441. /* Catch any otherwise unhandled error codes here. */
  2442. dev_warn(&isci_host->pdev->dev,
  2443. "%s: invalid completion code: 0x%x - "
  2444. "isci_request = %p\n",
  2445. __func__, completion_status, request);
  2446. response = SAS_TASK_UNDELIVERED;
  2447. /* See if the device has been/is being stopped. Note
  2448. * that we ignore the quiesce state, since we are
  2449. * concerned about the actual device state.
  2450. */
  2451. if ((isci_device->status == isci_stopping) ||
  2452. (isci_device->status == isci_stopped))
  2453. status = SAS_DEVICE_UNKNOWN;
  2454. else
  2455. status = SAS_ABORTED_TASK;
  2456. if (SAS_PROTOCOL_SMP == task->task_proto) {
  2457. request->complete_in_target = true;
  2458. complete_to_host = isci_perform_normal_io_completion;
  2459. } else {
  2460. request->complete_in_target = false;
  2461. complete_to_host = isci_perform_error_io_completion;
  2462. }
  2463. break;
  2464. }
  2465. break;
  2466. }
  2467. isci_request_unmap_sgl(request, isci_host->pdev);
  2468. /* Put the completed request on the correct list */
  2469. isci_task_save_for_upper_layer_completion(isci_host, request, response,
  2470. status, complete_to_host
  2471. );
  2472. /* complete the io request to the core. */
  2473. scic_controller_complete_io(&isci_host->sci,
  2474. &isci_device->sci,
  2475. &request->sci);
  2476. /* set terminated handle so it cannot be completed or
  2477. * terminated again, and to cause any calls into abort
  2478. * task to recognize the already completed case.
  2479. */
  2480. request->terminated = true;
  2481. isci_host_can_dequeue(isci_host, 1);
  2482. }
  2483. static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
  2484. {
  2485. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2486. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  2487. struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
  2488. struct sas_task *task;
  2489. /* XXX as hch said always creating an internal sas_task for tmf
  2490. * requests would simplify the driver
  2491. */
  2492. task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
  2493. /* all unaccelerated request types (non ssp or ncq) handled with
  2494. * substates
  2495. */
  2496. if (!task && dev->dev_type == SAS_END_DEV) {
  2497. sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
  2498. } else if (!task &&
  2499. (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
  2500. isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
  2501. sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
  2502. } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
  2503. sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
  2504. } else if (task && sas_protocol_ata(task->task_proto) &&
  2505. !task->ata_task.use_ncq) {
  2506. u32 state;
  2507. if (task->data_dir == DMA_NONE)
  2508. state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
  2509. else if (task->ata_task.dma_xfer)
  2510. state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
  2511. else /* PIO */
  2512. state = SCI_REQ_STP_PIO_WAIT_H2D;
  2513. sci_change_state(sm, state);
  2514. }
  2515. }
  2516. static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
  2517. {
  2518. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2519. struct scic_sds_controller *scic = sci_req->owning_controller;
  2520. struct isci_host *ihost = scic_to_ihost(scic);
  2521. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  2522. /* Tell the SCI_USER that the IO request is complete */
  2523. if (sci_req->is_task_management_request == false)
  2524. isci_request_io_request_complete(ihost, ireq,
  2525. sci_req->sci_status);
  2526. else
  2527. isci_task_request_complete(ihost, ireq, sci_req->sci_status);
  2528. }
  2529. static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
  2530. {
  2531. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2532. /* Setting the abort bit in the Task Context is required by the silicon. */
  2533. sci_req->task_context_buffer->abort = 1;
  2534. }
  2535. static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2536. {
  2537. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2538. scic_sds_remote_device_set_working_request(sci_req->target_device,
  2539. sci_req);
  2540. }
  2541. static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2542. {
  2543. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2544. scic_sds_remote_device_set_working_request(sci_req->target_device,
  2545. sci_req);
  2546. }
  2547. static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
  2548. {
  2549. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2550. scic_sds_remote_device_set_working_request(sci_req->target_device,
  2551. sci_req);
  2552. }
  2553. static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
  2554. {
  2555. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2556. struct scu_task_context *task_context;
  2557. struct host_to_dev_fis *h2d_fis;
  2558. enum sci_status status;
  2559. /* Clear the SRST bit */
  2560. h2d_fis = &sci_req->stp.cmd;
  2561. h2d_fis->control = 0;
  2562. /* Clear the TC control bit */
  2563. task_context = scic_sds_controller_get_task_context_buffer(
  2564. sci_req->owning_controller, sci_req->io_tag);
  2565. task_context->control_frame = 0;
  2566. status = scic_controller_continue_io(sci_req);
  2567. WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
  2568. }
  2569. static const struct sci_base_state scic_sds_request_state_table[] = {
  2570. [SCI_REQ_INIT] = { },
  2571. [SCI_REQ_CONSTRUCTED] = { },
  2572. [SCI_REQ_STARTED] = {
  2573. .enter_state = scic_sds_request_started_state_enter,
  2574. },
  2575. [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
  2576. .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
  2577. },
  2578. [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
  2579. [SCI_REQ_STP_PIO_WAIT_H2D] = {
  2580. .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
  2581. },
  2582. [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
  2583. [SCI_REQ_STP_PIO_DATA_IN] = { },
  2584. [SCI_REQ_STP_PIO_DATA_OUT] = { },
  2585. [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
  2586. [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
  2587. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
  2588. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
  2589. },
  2590. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
  2591. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
  2592. },
  2593. [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
  2594. [SCI_REQ_TASK_WAIT_TC_COMP] = { },
  2595. [SCI_REQ_TASK_WAIT_TC_RESP] = { },
  2596. [SCI_REQ_SMP_WAIT_RESP] = { },
  2597. [SCI_REQ_SMP_WAIT_TC_COMP] = { },
  2598. [SCI_REQ_COMPLETED] = {
  2599. .enter_state = scic_sds_request_completed_state_enter,
  2600. },
  2601. [SCI_REQ_ABORTING] = {
  2602. .enter_state = scic_sds_request_aborting_state_enter,
  2603. },
  2604. [SCI_REQ_FINAL] = { },
  2605. };
  2606. static void
  2607. scic_sds_general_request_construct(struct scic_sds_controller *scic,
  2608. struct scic_sds_remote_device *sci_dev,
  2609. u16 io_tag,
  2610. struct scic_sds_request *sci_req)
  2611. {
  2612. sci_init_sm(&sci_req->sm, scic_sds_request_state_table, SCI_REQ_INIT);
  2613. sci_req->io_tag = io_tag;
  2614. sci_req->owning_controller = scic;
  2615. sci_req->target_device = sci_dev;
  2616. sci_req->protocol = SCIC_NO_PROTOCOL;
  2617. sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
  2618. sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);
  2619. sci_req->sci_status = SCI_SUCCESS;
  2620. sci_req->scu_status = 0;
  2621. sci_req->post_context = 0xFFFFFFFF;
  2622. sci_req->is_task_management_request = false;
  2623. if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  2624. sci_req->was_tag_assigned_by_user = false;
  2625. sci_req->task_context_buffer = &sci_req->tc;
  2626. } else {
  2627. sci_req->was_tag_assigned_by_user = true;
  2628. sci_req->task_context_buffer =
  2629. scic_sds_controller_get_task_context_buffer(scic, io_tag);
  2630. }
  2631. }
  2632. static enum sci_status
  2633. scic_io_request_construct(struct scic_sds_controller *scic,
  2634. struct scic_sds_remote_device *sci_dev,
  2635. u16 io_tag, struct scic_sds_request *sci_req)
  2636. {
  2637. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2638. enum sci_status status = SCI_SUCCESS;
  2639. /* Build the common part of the request */
  2640. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  2641. if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  2642. return SCI_FAILURE_INVALID_REMOTE_DEVICE;
  2643. if (dev->dev_type == SAS_END_DEV)
  2644. /* pass */;
  2645. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  2646. memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
  2647. else if (dev_is_expander(dev))
  2648. memset(&sci_req->smp.cmd, 0, sizeof(sci_req->smp.cmd));
  2649. else
  2650. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2651. memset(sci_req->task_context_buffer, 0,
  2652. offsetof(struct scu_task_context, sgl_pair_ab));
  2653. return status;
  2654. }
  2655. enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
  2656. struct scic_sds_remote_device *sci_dev,
  2657. u16 io_tag, struct scic_sds_request *sci_req)
  2658. {
  2659. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2660. enum sci_status status = SCI_SUCCESS;
  2661. /* Build the common part of the request */
  2662. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  2663. if (dev->dev_type == SAS_END_DEV ||
  2664. dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  2665. sci_req->is_task_management_request = true;
  2666. memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
  2667. } else
  2668. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2669. return status;
  2670. }
  2671. static enum sci_status isci_request_ssp_request_construct(
  2672. struct isci_request *request)
  2673. {
  2674. enum sci_status status;
  2675. dev_dbg(&request->isci_host->pdev->dev,
  2676. "%s: request = %p\n",
  2677. __func__,
  2678. request);
  2679. status = scic_io_request_construct_basic_ssp(&request->sci);
  2680. return status;
  2681. }
  2682. static enum sci_status isci_request_stp_request_construct(
  2683. struct isci_request *request)
  2684. {
  2685. struct sas_task *task = isci_request_access_task(request);
  2686. enum sci_status status;
  2687. struct host_to_dev_fis *register_fis;
  2688. dev_dbg(&request->isci_host->pdev->dev,
  2689. "%s: request = %p\n",
  2690. __func__,
  2691. request);
  2692. /* Get the host_to_dev_fis from the core and copy
  2693. * the fis from the task into it.
  2694. */
  2695. register_fis = isci_sata_task_to_fis_copy(task);
  2696. status = scic_io_request_construct_basic_sata(&request->sci);
  2697. /* Set the ncq tag in the fis, from the queue
  2698. * command in the task.
  2699. */
  2700. if (isci_sata_is_task_ncq(task)) {
  2701. isci_sata_set_ncq_tag(
  2702. register_fis,
  2703. task
  2704. );
  2705. }
  2706. return status;
  2707. }
  2708. /*
  2709. * This function will fill in the SCU Task Context for a SMP request. The
  2710. * following important settings are utilized: -# task_type ==
  2711. * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
  2712. * (i.e. non-raw frame) is being utilized to perform task management. -#
  2713. * control_frame == 1. This ensures that the proper endianess is set so
  2714. * that the bytes are transmitted in the right order for a smp request frame.
  2715. * @sci_req: This parameter specifies the smp request object being
  2716. * constructed.
  2717. *
  2718. */
  2719. static void
  2720. scu_smp_request_construct_task_context(struct scic_sds_request *sci_req,
  2721. ssize_t req_len)
  2722. {
  2723. dma_addr_t dma_addr;
  2724. struct scic_sds_remote_device *sci_dev;
  2725. struct scic_sds_port *sci_port;
  2726. struct scu_task_context *task_context;
  2727. ssize_t word_cnt = sizeof(struct smp_req) / sizeof(u32);
  2728. /* byte swap the smp request. */
  2729. sci_swab32_cpy(&sci_req->smp.cmd, &sci_req->smp.cmd,
  2730. word_cnt);
  2731. task_context = scic_sds_request_get_task_context(sci_req);
  2732. sci_dev = scic_sds_request_get_device(sci_req);
  2733. sci_port = scic_sds_request_get_port(sci_req);
  2734. /*
  2735. * Fill in the TC with the its required data
  2736. * 00h
  2737. */
  2738. task_context->priority = 0;
  2739. task_context->initiator_request = 1;
  2740. task_context->connection_rate = sci_dev->connection_rate;
  2741. task_context->protocol_engine_index =
  2742. scic_sds_controller_get_protocol_engine_group(scic);
  2743. task_context->logical_port_index = scic_sds_port_get_index(sci_port);
  2744. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
  2745. task_context->abort = 0;
  2746. task_context->valid = SCU_TASK_CONTEXT_VALID;
  2747. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  2748. /* 04h */
  2749. task_context->remote_node_index = sci_dev->rnc.remote_node_index;
  2750. task_context->command_code = 0;
  2751. task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
  2752. /* 08h */
  2753. task_context->link_layer_control = 0;
  2754. task_context->do_not_dma_ssp_good_response = 1;
  2755. task_context->strict_ordering = 0;
  2756. task_context->control_frame = 1;
  2757. task_context->timeout_enable = 0;
  2758. task_context->block_guard_enable = 0;
  2759. /* 0ch */
  2760. task_context->address_modifier = 0;
  2761. /* 10h */
  2762. task_context->ssp_command_iu_length = req_len;
  2763. /* 14h */
  2764. task_context->transfer_length_bytes = 0;
  2765. /*
  2766. * 18h ~ 30h, protocol specific
  2767. * since commandIU has been build by framework at this point, we just
  2768. * copy the frist DWord from command IU to this location. */
  2769. memcpy(&task_context->type.smp, &sci_req->smp.cmd, sizeof(u32));
  2770. /*
  2771. * 40h
  2772. * "For SMP you could program it to zero. We would prefer that way
  2773. * so that done code will be consistent." - Venki
  2774. */
  2775. task_context->task_phase = 0;
  2776. if (sci_req->was_tag_assigned_by_user) {
  2777. /*
  2778. * Build the task context now since we have already read
  2779. * the data
  2780. */
  2781. sci_req->post_context =
  2782. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  2783. (scic_sds_controller_get_protocol_engine_group(scic) <<
  2784. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  2785. (scic_sds_port_get_index(sci_port) <<
  2786. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  2787. scic_sds_io_tag_get_index(sci_req->io_tag));
  2788. } else {
  2789. /*
  2790. * Build the task context now since we have already read
  2791. * the data.
  2792. * I/O tag index is not assigned because we have to wait
  2793. * until we get a TCi.
  2794. */
  2795. sci_req->post_context =
  2796. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  2797. (scic_sds_controller_get_protocol_engine_group(scic) <<
  2798. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  2799. (scic_sds_port_get_index(sci_port) <<
  2800. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  2801. }
  2802. /*
  2803. * Copy the physical address for the command buffer to the SCU Task
  2804. * Context command buffer should not contain command header.
  2805. */
  2806. dma_addr = scic_io_request_get_dma_addr(sci_req,
  2807. ((char *) &sci_req->smp.cmd) +
  2808. sizeof(u32));
  2809. task_context->command_iu_upper = upper_32_bits(dma_addr);
  2810. task_context->command_iu_lower = lower_32_bits(dma_addr);
  2811. /* SMP response comes as UF, so no need to set response IU address. */
  2812. task_context->response_iu_upper = 0;
  2813. task_context->response_iu_lower = 0;
  2814. }
  2815. static enum sci_status
  2816. scic_io_request_construct_smp(struct scic_sds_request *sci_req)
  2817. {
  2818. struct smp_req *smp_req = &sci_req->smp.cmd;
  2819. sci_req->protocol = SCIC_SMP_PROTOCOL;
  2820. /*
  2821. * Look at the SMP requests' header fields; for certain SAS 1.x SMP
  2822. * functions under SAS 2.0, a zero request length really indicates
  2823. * a non-zero default length.
  2824. */
  2825. if (smp_req->req_len == 0) {
  2826. switch (smp_req->func) {
  2827. case SMP_DISCOVER:
  2828. case SMP_REPORT_PHY_ERR_LOG:
  2829. case SMP_REPORT_PHY_SATA:
  2830. case SMP_REPORT_ROUTE_INFO:
  2831. smp_req->req_len = 2;
  2832. break;
  2833. case SMP_CONF_ROUTE_INFO:
  2834. case SMP_PHY_CONTROL:
  2835. case SMP_PHY_TEST_FUNCTION:
  2836. smp_req->req_len = 9;
  2837. break;
  2838. /* Default - zero is a valid default for 2.0. */
  2839. }
  2840. }
  2841. scu_smp_request_construct_task_context(sci_req, smp_req->req_len);
  2842. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  2843. return SCI_SUCCESS;
  2844. }
  2845. /*
  2846. * isci_smp_request_build() - This function builds the smp request.
  2847. * @ireq: This parameter points to the isci_request allocated in the
  2848. * request construct function.
  2849. *
  2850. * SCI_SUCCESS on successfull completion, or specific failure code.
  2851. */
  2852. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  2853. {
  2854. enum sci_status status = SCI_FAILURE;
  2855. struct sas_task *task = isci_request_access_task(ireq);
  2856. struct scic_sds_request *sci_req = &ireq->sci;
  2857. dev_dbg(&ireq->isci_host->pdev->dev,
  2858. "%s: request = %p\n", __func__, ireq);
  2859. dev_dbg(&ireq->isci_host->pdev->dev,
  2860. "%s: smp_req len = %d\n",
  2861. __func__,
  2862. task->smp_task.smp_req.length);
  2863. /* copy the smp_command to the address; */
  2864. sg_copy_to_buffer(&task->smp_task.smp_req, 1,
  2865. &sci_req->smp.cmd,
  2866. sizeof(struct smp_req));
  2867. status = scic_io_request_construct_smp(sci_req);
  2868. if (status != SCI_SUCCESS)
  2869. dev_warn(&ireq->isci_host->pdev->dev,
  2870. "%s: failed with status = %d\n",
  2871. __func__,
  2872. status);
  2873. return status;
  2874. }
  2875. /**
  2876. * isci_io_request_build() - This function builds the io request object.
  2877. * @isci_host: This parameter specifies the ISCI host object
  2878. * @request: This parameter points to the isci_request object allocated in the
  2879. * request construct function.
  2880. * @sci_device: This parameter is the handle for the sci core's remote device
  2881. * object that is the destination for this request.
  2882. *
  2883. * SCI_SUCCESS on successfull completion, or specific failure code.
  2884. */
  2885. static enum sci_status isci_io_request_build(
  2886. struct isci_host *isci_host,
  2887. struct isci_request *request,
  2888. struct isci_remote_device *isci_device)
  2889. {
  2890. enum sci_status status = SCI_SUCCESS;
  2891. struct sas_task *task = isci_request_access_task(request);
  2892. struct scic_sds_remote_device *sci_device = &isci_device->sci;
  2893. dev_dbg(&isci_host->pdev->dev,
  2894. "%s: isci_device = 0x%p; request = %p, "
  2895. "num_scatter = %d\n",
  2896. __func__,
  2897. isci_device,
  2898. request,
  2899. task->num_scatter);
  2900. /* map the sgl addresses, if present.
  2901. * libata does the mapping for sata devices
  2902. * before we get the request.
  2903. */
  2904. if (task->num_scatter &&
  2905. !sas_protocol_ata(task->task_proto) &&
  2906. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  2907. request->num_sg_entries = dma_map_sg(
  2908. &isci_host->pdev->dev,
  2909. task->scatter,
  2910. task->num_scatter,
  2911. task->data_dir
  2912. );
  2913. if (request->num_sg_entries == 0)
  2914. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2915. }
  2916. /* build the common request object. For now,
  2917. * we will let the core allocate the IO tag.
  2918. */
  2919. status = scic_io_request_construct(&isci_host->sci, sci_device,
  2920. SCI_CONTROLLER_INVALID_IO_TAG,
  2921. &request->sci);
  2922. if (status != SCI_SUCCESS) {
  2923. dev_warn(&isci_host->pdev->dev,
  2924. "%s: failed request construct\n",
  2925. __func__);
  2926. return SCI_FAILURE;
  2927. }
  2928. switch (task->task_proto) {
  2929. case SAS_PROTOCOL_SMP:
  2930. status = isci_smp_request_build(request);
  2931. break;
  2932. case SAS_PROTOCOL_SSP:
  2933. status = isci_request_ssp_request_construct(request);
  2934. break;
  2935. case SAS_PROTOCOL_SATA:
  2936. case SAS_PROTOCOL_STP:
  2937. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  2938. status = isci_request_stp_request_construct(request);
  2939. break;
  2940. default:
  2941. dev_warn(&isci_host->pdev->dev,
  2942. "%s: unknown protocol\n", __func__);
  2943. return SCI_FAILURE;
  2944. }
  2945. return SCI_SUCCESS;
  2946. }
  2947. /**
  2948. * isci_request_alloc_core() - This function gets the request object from the
  2949. * isci_host dma cache.
  2950. * @isci_host: This parameter specifies the ISCI host object
  2951. * @isci_request: This parameter will contain the pointer to the new
  2952. * isci_request object.
  2953. * @isci_device: This parameter is the pointer to the isci remote device object
  2954. * that is the destination for this request.
  2955. * @gfp_flags: This parameter specifies the os allocation flags.
  2956. *
  2957. * SCI_SUCCESS on successfull completion, or specific failure code.
  2958. */
  2959. static int isci_request_alloc_core(
  2960. struct isci_host *isci_host,
  2961. struct isci_request **isci_request,
  2962. struct isci_remote_device *isci_device,
  2963. gfp_t gfp_flags)
  2964. {
  2965. int ret = 0;
  2966. dma_addr_t handle;
  2967. struct isci_request *request;
  2968. /* get pointer to dma memory. This actually points
  2969. * to both the isci_remote_device object and the
  2970. * sci object. The isci object is at the beginning
  2971. * of the memory allocated here.
  2972. */
  2973. request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
  2974. if (!request) {
  2975. dev_warn(&isci_host->pdev->dev,
  2976. "%s: dma_pool_alloc returned NULL\n", __func__);
  2977. return -ENOMEM;
  2978. }
  2979. /* initialize the request object. */
  2980. spin_lock_init(&request->state_lock);
  2981. request->request_daddr = handle;
  2982. request->isci_host = isci_host;
  2983. request->isci_device = isci_device;
  2984. request->io_request_completion = NULL;
  2985. request->terminated = false;
  2986. request->num_sg_entries = 0;
  2987. request->complete_in_target = false;
  2988. INIT_LIST_HEAD(&request->completed_node);
  2989. INIT_LIST_HEAD(&request->dev_node);
  2990. *isci_request = request;
  2991. isci_request_change_state(request, allocated);
  2992. return ret;
  2993. }
  2994. static int isci_request_alloc_io(
  2995. struct isci_host *isci_host,
  2996. struct sas_task *task,
  2997. struct isci_request **isci_request,
  2998. struct isci_remote_device *isci_device,
  2999. gfp_t gfp_flags)
  3000. {
  3001. int retval = isci_request_alloc_core(isci_host, isci_request,
  3002. isci_device, gfp_flags);
  3003. if (!retval) {
  3004. (*isci_request)->ttype_ptr.io_task_ptr = task;
  3005. (*isci_request)->ttype = io_task;
  3006. task->lldd_task = *isci_request;
  3007. }
  3008. return retval;
  3009. }
  3010. /**
  3011. * isci_request_alloc_tmf() - This function gets the request object from the
  3012. * isci_host dma cache and initializes the relevant fields as a sas_task.
  3013. * @isci_host: This parameter specifies the ISCI host object
  3014. * @sas_task: This parameter is the task struct from the upper layer driver.
  3015. * @isci_request: This parameter will contain the pointer to the new
  3016. * isci_request object.
  3017. * @isci_device: This parameter is the pointer to the isci remote device object
  3018. * that is the destination for this request.
  3019. * @gfp_flags: This parameter specifies the os allocation flags.
  3020. *
  3021. * SCI_SUCCESS on successfull completion, or specific failure code.
  3022. */
  3023. int isci_request_alloc_tmf(
  3024. struct isci_host *isci_host,
  3025. struct isci_tmf *isci_tmf,
  3026. struct isci_request **isci_request,
  3027. struct isci_remote_device *isci_device,
  3028. gfp_t gfp_flags)
  3029. {
  3030. int retval = isci_request_alloc_core(isci_host, isci_request,
  3031. isci_device, gfp_flags);
  3032. if (!retval) {
  3033. (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
  3034. (*isci_request)->ttype = tmf_task;
  3035. }
  3036. return retval;
  3037. }
  3038. /**
  3039. * isci_request_execute() - This function allocates the isci_request object,
  3040. * all fills in some common fields.
  3041. * @isci_host: This parameter specifies the ISCI host object
  3042. * @sas_task: This parameter is the task struct from the upper layer driver.
  3043. * @isci_request: This parameter will contain the pointer to the new
  3044. * isci_request object.
  3045. * @gfp_flags: This parameter specifies the os allocation flags.
  3046. *
  3047. * SCI_SUCCESS on successfull completion, or specific failure code.
  3048. */
  3049. int isci_request_execute(
  3050. struct isci_host *isci_host,
  3051. struct sas_task *task,
  3052. struct isci_request **isci_request,
  3053. gfp_t gfp_flags)
  3054. {
  3055. int ret = 0;
  3056. struct scic_sds_remote_device *sci_device;
  3057. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  3058. struct isci_remote_device *isci_device;
  3059. struct isci_request *request;
  3060. unsigned long flags;
  3061. isci_device = task->dev->lldd_dev;
  3062. sci_device = &isci_device->sci;
  3063. /* do common allocation and init of request object. */
  3064. ret = isci_request_alloc_io(
  3065. isci_host,
  3066. task,
  3067. &request,
  3068. isci_device,
  3069. gfp_flags
  3070. );
  3071. if (ret)
  3072. goto out;
  3073. status = isci_io_request_build(isci_host, request, isci_device);
  3074. if (status != SCI_SUCCESS) {
  3075. dev_warn(&isci_host->pdev->dev,
  3076. "%s: request_construct failed - status = 0x%x\n",
  3077. __func__,
  3078. status);
  3079. goto out;
  3080. }
  3081. spin_lock_irqsave(&isci_host->scic_lock, flags);
  3082. /* send the request, let the core assign the IO TAG. */
  3083. status = scic_controller_start_io(&isci_host->sci, sci_device,
  3084. &request->sci,
  3085. SCI_CONTROLLER_INVALID_IO_TAG);
  3086. if (status != SCI_SUCCESS &&
  3087. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3088. dev_warn(&isci_host->pdev->dev,
  3089. "%s: failed request start (0x%x)\n",
  3090. __func__, status);
  3091. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  3092. goto out;
  3093. }
  3094. /* Either I/O started OK, or the core has signaled that
  3095. * the device needs a target reset.
  3096. *
  3097. * In either case, hold onto the I/O for later.
  3098. *
  3099. * Update it's status and add it to the list in the
  3100. * remote device object.
  3101. */
  3102. list_add(&request->dev_node, &isci_device->reqs_in_process);
  3103. if (status == SCI_SUCCESS) {
  3104. /* Save the tag for possible task mgmt later. */
  3105. request->io_tag = request->sci.io_tag;
  3106. isci_request_change_state(request, started);
  3107. } else {
  3108. /* The request did not really start in the
  3109. * hardware, so clear the request handle
  3110. * here so no terminations will be done.
  3111. */
  3112. request->terminated = true;
  3113. isci_request_change_state(request, completed);
  3114. }
  3115. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  3116. if (status ==
  3117. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3118. /* Signal libsas that we need the SCSI error
  3119. * handler thread to work on this I/O and that
  3120. * we want a device reset.
  3121. */
  3122. spin_lock_irqsave(&task->task_state_lock, flags);
  3123. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  3124. spin_unlock_irqrestore(&task->task_state_lock, flags);
  3125. /* Cause this task to be scheduled in the SCSI error
  3126. * handler thread.
  3127. */
  3128. isci_execpath_callback(isci_host, task,
  3129. sas_task_abort);
  3130. /* Change the status, since we are holding
  3131. * the I/O until it is managed by the SCSI
  3132. * error handler.
  3133. */
  3134. status = SCI_SUCCESS;
  3135. }
  3136. out:
  3137. if (status != SCI_SUCCESS) {
  3138. /* release dma memory on failure. */
  3139. isci_request_free(isci_host, request);
  3140. request = NULL;
  3141. ret = SCI_FAILURE;
  3142. }
  3143. *isci_request = request;
  3144. return ret;
  3145. }