request.c 106 KB

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