request.c 100 KB

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