request.c 100 KB

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