request.c 108 KB

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