mtip32xx.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277
  1. /*
  2. * Driver for the Micron P320 SSD
  3. * Copyright (C) 2011 Micron Technology, Inc.
  4. *
  5. * Portions of this code were derived from works subjected to the
  6. * following copyright:
  7. * Copyright (C) 2009 Integrated Device Technology, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/ata.h>
  23. #include <linux/delay.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/random.h>
  27. #include <linux/smp.h>
  28. #include <linux/compat.h>
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/genhd.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/bio.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/idr.h>
  36. #include <linux/kthread.h>
  37. #include <../drivers/ata/ahci.h>
  38. #include <linux/export.h>
  39. #include <linux/debugfs.h>
  40. #include "mtip32xx.h"
  41. #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
  42. #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
  43. #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
  44. #define HW_PORT_PRIV_DMA_SZ \
  45. (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
  46. #define HOST_CAP_NZDMA (1 << 19)
  47. #define HOST_HSORG 0xFC
  48. #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
  49. #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
  50. #define HSORG_HWREV 0xFF00
  51. #define HSORG_STYLE 0x8
  52. #define HSORG_SLOTGROUPS 0x7
  53. #define PORT_COMMAND_ISSUE 0x38
  54. #define PORT_SDBV 0x7C
  55. #define PORT_OFFSET 0x100
  56. #define PORT_MEM_SIZE 0x80
  57. #define PORT_IRQ_ERR \
  58. (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
  59. PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
  60. PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
  61. PORT_IRQ_OVERFLOW)
  62. #define PORT_IRQ_LEGACY \
  63. (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  64. #define PORT_IRQ_HANDLED \
  65. (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
  66. PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
  67. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
  68. #define DEF_PORT_IRQ \
  69. (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
  70. /* product numbers */
  71. #define MTIP_PRODUCT_UNKNOWN 0x00
  72. #define MTIP_PRODUCT_ASICFPGA 0x11
  73. /* Device instance number, incremented each time a device is probed. */
  74. static int instance;
  75. /*
  76. * Global variable used to hold the major block device number
  77. * allocated in mtip_init().
  78. */
  79. static int mtip_major;
  80. static struct dentry *dfs_parent;
  81. static DEFINE_SPINLOCK(rssd_index_lock);
  82. static DEFINE_IDA(rssd_index_ida);
  83. static int mtip_block_initialize(struct driver_data *dd);
  84. #ifdef CONFIG_COMPAT
  85. struct mtip_compat_ide_task_request_s {
  86. __u8 io_ports[8];
  87. __u8 hob_ports[8];
  88. ide_reg_valid_t out_flags;
  89. ide_reg_valid_t in_flags;
  90. int data_phase;
  91. int req_cmd;
  92. compat_ulong_t out_size;
  93. compat_ulong_t in_size;
  94. };
  95. #endif
  96. /*
  97. * This function check_for_surprise_removal is called
  98. * while card is removed from the system and it will
  99. * read the vendor id from the configration space
  100. *
  101. * @pdev Pointer to the pci_dev structure.
  102. *
  103. * return value
  104. * true if device removed, else false
  105. */
  106. static bool mtip_check_surprise_removal(struct pci_dev *pdev)
  107. {
  108. u16 vendor_id = 0;
  109. /* Read the vendorID from the configuration space */
  110. pci_read_config_word(pdev, 0x00, &vendor_id);
  111. if (vendor_id == 0xFFFF)
  112. return true; /* device removed */
  113. return false; /* device present */
  114. }
  115. /*
  116. * This function is called for clean the pending command in the
  117. * command slot during the surprise removal of device and return
  118. * error to the upper layer.
  119. *
  120. * @dd Pointer to the DRIVER_DATA structure.
  121. *
  122. * return value
  123. * None
  124. */
  125. static void mtip_command_cleanup(struct driver_data *dd)
  126. {
  127. int group = 0, commandslot = 0, commandindex = 0;
  128. struct mtip_cmd *command;
  129. struct mtip_port *port = dd->port;
  130. static int in_progress;
  131. if (in_progress)
  132. return;
  133. in_progress = 1;
  134. for (group = 0; group < 4; group++) {
  135. for (commandslot = 0; commandslot < 32; commandslot++) {
  136. if (!(port->allocated[group] & (1 << commandslot)))
  137. continue;
  138. commandindex = group << 5 | commandslot;
  139. command = &port->commands[commandindex];
  140. if (atomic_read(&command->active)
  141. && (command->async_callback)) {
  142. command->async_callback(command->async_data,
  143. -ENODEV);
  144. command->async_callback = NULL;
  145. command->async_data = NULL;
  146. }
  147. dma_unmap_sg(&port->dd->pdev->dev,
  148. command->sg,
  149. command->scatter_ents,
  150. command->direction);
  151. }
  152. }
  153. up(&port->cmd_slot);
  154. set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
  155. in_progress = 0;
  156. }
  157. /*
  158. * Obtain an empty command slot.
  159. *
  160. * This function needs to be reentrant since it could be called
  161. * at the same time on multiple CPUs. The allocation of the
  162. * command slot must be atomic.
  163. *
  164. * @port Pointer to the port data structure.
  165. *
  166. * return value
  167. * >= 0 Index of command slot obtained.
  168. * -1 No command slots available.
  169. */
  170. static int get_slot(struct mtip_port *port)
  171. {
  172. int slot, i;
  173. unsigned int num_command_slots = port->dd->slot_groups * 32;
  174. /*
  175. * Try 10 times, because there is a small race here.
  176. * that's ok, because it's still cheaper than a lock.
  177. *
  178. * Race: Since this section is not protected by lock, same bit
  179. * could be chosen by different process contexts running in
  180. * different processor. So instead of costly lock, we are going
  181. * with loop.
  182. */
  183. for (i = 0; i < 10; i++) {
  184. slot = find_next_zero_bit(port->allocated,
  185. num_command_slots, 1);
  186. if ((slot < num_command_slots) &&
  187. (!test_and_set_bit(slot, port->allocated)))
  188. return slot;
  189. }
  190. dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
  191. if (mtip_check_surprise_removal(port->dd->pdev)) {
  192. /* Device not present, clean outstanding commands */
  193. mtip_command_cleanup(port->dd);
  194. }
  195. return -1;
  196. }
  197. /*
  198. * Release a command slot.
  199. *
  200. * @port Pointer to the port data structure.
  201. * @tag Tag of command to release
  202. *
  203. * return value
  204. * None
  205. */
  206. static inline void release_slot(struct mtip_port *port, int tag)
  207. {
  208. smp_mb__before_clear_bit();
  209. clear_bit(tag, port->allocated);
  210. smp_mb__after_clear_bit();
  211. }
  212. /*
  213. * Reset the HBA (without sleeping)
  214. *
  215. * Just like hba_reset, except does not call sleep, so can be
  216. * run from interrupt/tasklet context.
  217. *
  218. * @dd Pointer to the driver data structure.
  219. *
  220. * return value
  221. * 0 The reset was successful.
  222. * -1 The HBA Reset bit did not clear.
  223. */
  224. static int hba_reset_nosleep(struct driver_data *dd)
  225. {
  226. unsigned long timeout;
  227. /* Chip quirk: quiesce any chip function */
  228. mdelay(10);
  229. /* Set the reset bit */
  230. writel(HOST_RESET, dd->mmio + HOST_CTL);
  231. /* Flush */
  232. readl(dd->mmio + HOST_CTL);
  233. /*
  234. * Wait 10ms then spin for up to 1 second
  235. * waiting for reset acknowledgement
  236. */
  237. timeout = jiffies + msecs_to_jiffies(1000);
  238. mdelay(10);
  239. while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
  240. && time_before(jiffies, timeout))
  241. mdelay(1);
  242. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
  243. return -1;
  244. if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
  245. return -1;
  246. return 0;
  247. }
  248. /*
  249. * Issue a command to the hardware.
  250. *
  251. * Set the appropriate bit in the s_active and Command Issue hardware
  252. * registers, causing hardware command processing to begin.
  253. *
  254. * @port Pointer to the port structure.
  255. * @tag The tag of the command to be issued.
  256. *
  257. * return value
  258. * None
  259. */
  260. static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
  261. {
  262. atomic_set(&port->commands[tag].active, 1);
  263. spin_lock(&port->cmd_issue_lock);
  264. writel((1 << MTIP_TAG_BIT(tag)),
  265. port->s_active[MTIP_TAG_INDEX(tag)]);
  266. writel((1 << MTIP_TAG_BIT(tag)),
  267. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  268. spin_unlock(&port->cmd_issue_lock);
  269. /* Set the command's timeout value.*/
  270. port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
  271. MTIP_NCQ_COMMAND_TIMEOUT_MS);
  272. }
  273. /*
  274. * Enable/disable the reception of FIS
  275. *
  276. * @port Pointer to the port data structure
  277. * @enable 1 to enable, 0 to disable
  278. *
  279. * return value
  280. * Previous state: 1 enabled, 0 disabled
  281. */
  282. static int mtip_enable_fis(struct mtip_port *port, int enable)
  283. {
  284. u32 tmp;
  285. /* enable FIS reception */
  286. tmp = readl(port->mmio + PORT_CMD);
  287. if (enable)
  288. writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  289. else
  290. writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  291. /* Flush */
  292. readl(port->mmio + PORT_CMD);
  293. return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
  294. }
  295. /*
  296. * Enable/disable the DMA engine
  297. *
  298. * @port Pointer to the port data structure
  299. * @enable 1 to enable, 0 to disable
  300. *
  301. * return value
  302. * Previous state: 1 enabled, 0 disabled.
  303. */
  304. static int mtip_enable_engine(struct mtip_port *port, int enable)
  305. {
  306. u32 tmp;
  307. /* enable FIS reception */
  308. tmp = readl(port->mmio + PORT_CMD);
  309. if (enable)
  310. writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
  311. else
  312. writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
  313. readl(port->mmio + PORT_CMD);
  314. return (((tmp & PORT_CMD_START) == PORT_CMD_START));
  315. }
  316. /*
  317. * Enables the port DMA engine and FIS reception.
  318. *
  319. * return value
  320. * None
  321. */
  322. static inline void mtip_start_port(struct mtip_port *port)
  323. {
  324. /* Enable FIS reception */
  325. mtip_enable_fis(port, 1);
  326. /* Enable the DMA engine */
  327. mtip_enable_engine(port, 1);
  328. }
  329. /*
  330. * Deinitialize a port by disabling port interrupts, the DMA engine,
  331. * and FIS reception.
  332. *
  333. * @port Pointer to the port structure
  334. *
  335. * return value
  336. * None
  337. */
  338. static inline void mtip_deinit_port(struct mtip_port *port)
  339. {
  340. /* Disable interrupts on this port */
  341. writel(0, port->mmio + PORT_IRQ_MASK);
  342. /* Disable the DMA engine */
  343. mtip_enable_engine(port, 0);
  344. /* Disable FIS reception */
  345. mtip_enable_fis(port, 0);
  346. }
  347. /*
  348. * Initialize a port.
  349. *
  350. * This function deinitializes the port by calling mtip_deinit_port() and
  351. * then initializes it by setting the command header and RX FIS addresses,
  352. * clearing the SError register and any pending port interrupts before
  353. * re-enabling the default set of port interrupts.
  354. *
  355. * @port Pointer to the port structure.
  356. *
  357. * return value
  358. * None
  359. */
  360. static void mtip_init_port(struct mtip_port *port)
  361. {
  362. int i;
  363. mtip_deinit_port(port);
  364. /* Program the command list base and FIS base addresses */
  365. if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
  366. writel((port->command_list_dma >> 16) >> 16,
  367. port->mmio + PORT_LST_ADDR_HI);
  368. writel((port->rxfis_dma >> 16) >> 16,
  369. port->mmio + PORT_FIS_ADDR_HI);
  370. }
  371. writel(port->command_list_dma & 0xFFFFFFFF,
  372. port->mmio + PORT_LST_ADDR);
  373. writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
  374. /* Clear SError */
  375. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  376. /* reset the completed registers.*/
  377. for (i = 0; i < port->dd->slot_groups; i++)
  378. writel(0xFFFFFFFF, port->completed[i]);
  379. /* Clear any pending interrupts for this port */
  380. writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  381. /* Clear any pending interrupts on the HBA. */
  382. writel(readl(port->dd->mmio + HOST_IRQ_STAT),
  383. port->dd->mmio + HOST_IRQ_STAT);
  384. /* Enable port interrupts */
  385. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  386. }
  387. /*
  388. * Restart a port
  389. *
  390. * @port Pointer to the port data structure.
  391. *
  392. * return value
  393. * None
  394. */
  395. static void mtip_restart_port(struct mtip_port *port)
  396. {
  397. unsigned long timeout;
  398. /* Disable the DMA engine */
  399. mtip_enable_engine(port, 0);
  400. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  401. timeout = jiffies + msecs_to_jiffies(500);
  402. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  403. && time_before(jiffies, timeout))
  404. ;
  405. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  406. return;
  407. /*
  408. * Chip quirk: escalate to hba reset if
  409. * PxCMD.CR not clear after 500 ms
  410. */
  411. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  412. dev_warn(&port->dd->pdev->dev,
  413. "PxCMD.CR not clear, escalating reset\n");
  414. if (hba_reset_nosleep(port->dd))
  415. dev_err(&port->dd->pdev->dev,
  416. "HBA reset escalation failed.\n");
  417. /* 30 ms delay before com reset to quiesce chip */
  418. mdelay(30);
  419. }
  420. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  421. /* Set PxSCTL.DET */
  422. writel(readl(port->mmio + PORT_SCR_CTL) |
  423. 1, port->mmio + PORT_SCR_CTL);
  424. readl(port->mmio + PORT_SCR_CTL);
  425. /* Wait 1 ms to quiesce chip function */
  426. timeout = jiffies + msecs_to_jiffies(1);
  427. while (time_before(jiffies, timeout))
  428. ;
  429. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  430. return;
  431. /* Clear PxSCTL.DET */
  432. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  433. port->mmio + PORT_SCR_CTL);
  434. readl(port->mmio + PORT_SCR_CTL);
  435. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  436. timeout = jiffies + msecs_to_jiffies(500);
  437. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  438. && time_before(jiffies, timeout))
  439. ;
  440. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  441. return;
  442. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  443. dev_warn(&port->dd->pdev->dev,
  444. "COM reset failed\n");
  445. mtip_init_port(port);
  446. mtip_start_port(port);
  447. }
  448. /*
  449. * Helper function for tag logging
  450. */
  451. static void print_tags(struct driver_data *dd,
  452. char *msg,
  453. unsigned long *tagbits,
  454. int cnt)
  455. {
  456. unsigned char tagmap[128];
  457. int group, tagmap_len = 0;
  458. memset(tagmap, 0, sizeof(tagmap));
  459. for (group = SLOTBITS_IN_LONGS; group > 0; group--)
  460. tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
  461. tagbits[group-1]);
  462. dev_warn(&dd->pdev->dev,
  463. "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
  464. }
  465. /*
  466. * Called periodically to see if any read/write commands are
  467. * taking too long to complete.
  468. *
  469. * @data Pointer to the PORT data structure.
  470. *
  471. * return value
  472. * None
  473. */
  474. static void mtip_timeout_function(unsigned long int data)
  475. {
  476. struct mtip_port *port = (struct mtip_port *) data;
  477. struct host_to_dev_fis *fis;
  478. struct mtip_cmd *command;
  479. int tag, cmdto_cnt = 0;
  480. unsigned int bit, group;
  481. unsigned int num_command_slots = port->dd->slot_groups * 32;
  482. unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
  483. if (unlikely(!port))
  484. return;
  485. if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
  486. mod_timer(&port->cmd_timer,
  487. jiffies + msecs_to_jiffies(30000));
  488. return;
  489. }
  490. /* clear the tag accumulator */
  491. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  492. for (tag = 0; tag < num_command_slots; tag++) {
  493. /*
  494. * Skip internal command slot as it has
  495. * its own timeout mechanism
  496. */
  497. if (tag == MTIP_TAG_INTERNAL)
  498. continue;
  499. if (atomic_read(&port->commands[tag].active) &&
  500. (time_after(jiffies, port->commands[tag].comp_time))) {
  501. group = tag >> 5;
  502. bit = tag & 0x1F;
  503. command = &port->commands[tag];
  504. fis = (struct host_to_dev_fis *) command->command;
  505. set_bit(tag, tagaccum);
  506. cmdto_cnt++;
  507. if (cmdto_cnt == 1)
  508. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  509. /*
  510. * Clear the completed bit. This should prevent
  511. * any interrupt handlers from trying to retire
  512. * the command.
  513. */
  514. writel(1 << bit, port->completed[group]);
  515. /* Call the async completion callback. */
  516. if (likely(command->async_callback))
  517. command->async_callback(command->async_data,
  518. -EIO);
  519. command->async_callback = NULL;
  520. command->comp_func = NULL;
  521. /* Unmap the DMA scatter list entries */
  522. dma_unmap_sg(&port->dd->pdev->dev,
  523. command->sg,
  524. command->scatter_ents,
  525. command->direction);
  526. /*
  527. * Clear the allocated bit and active tag for the
  528. * command.
  529. */
  530. atomic_set(&port->commands[tag].active, 0);
  531. release_slot(port, tag);
  532. up(&port->cmd_slot);
  533. }
  534. }
  535. if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  536. print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
  537. mtip_restart_port(port);
  538. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  539. wake_up_interruptible(&port->svc_wait);
  540. }
  541. if (port->ic_pause_timer) {
  542. to = port->ic_pause_timer + msecs_to_jiffies(1000);
  543. if (time_after(jiffies, to)) {
  544. if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  545. port->ic_pause_timer = 0;
  546. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  547. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  548. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  549. wake_up_interruptible(&port->svc_wait);
  550. }
  551. }
  552. }
  553. /* Restart the timer */
  554. mod_timer(&port->cmd_timer,
  555. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  556. }
  557. /*
  558. * IO completion function.
  559. *
  560. * This completion function is called by the driver ISR when a
  561. * command that was issued by the kernel completes. It first calls the
  562. * asynchronous completion function which normally calls back into the block
  563. * layer passing the asynchronous callback data, then unmaps the
  564. * scatter list associated with the completed command, and finally
  565. * clears the allocated bit associated with the completed command.
  566. *
  567. * @port Pointer to the port data structure.
  568. * @tag Tag of the command.
  569. * @data Pointer to driver_data.
  570. * @status Completion status.
  571. *
  572. * return value
  573. * None
  574. */
  575. static void mtip_async_complete(struct mtip_port *port,
  576. int tag,
  577. void *data,
  578. int status)
  579. {
  580. struct mtip_cmd *command;
  581. struct driver_data *dd = data;
  582. int cb_status = status ? -EIO : 0;
  583. if (unlikely(!dd) || unlikely(!port))
  584. return;
  585. command = &port->commands[tag];
  586. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  587. dev_warn(&port->dd->pdev->dev,
  588. "Command tag %d failed due to TFE\n", tag);
  589. }
  590. /* Upper layer callback */
  591. if (likely(command->async_callback))
  592. command->async_callback(command->async_data, cb_status);
  593. command->async_callback = NULL;
  594. command->comp_func = NULL;
  595. /* Unmap the DMA scatter list entries */
  596. dma_unmap_sg(&dd->pdev->dev,
  597. command->sg,
  598. command->scatter_ents,
  599. command->direction);
  600. /* Clear the allocated and active bits for the command */
  601. atomic_set(&port->commands[tag].active, 0);
  602. release_slot(port, tag);
  603. up(&port->cmd_slot);
  604. }
  605. /*
  606. * Internal command completion callback function.
  607. *
  608. * This function is normally called by the driver ISR when an internal
  609. * command completed. This function signals the command completion by
  610. * calling complete().
  611. *
  612. * @port Pointer to the port data structure.
  613. * @tag Tag of the command that has completed.
  614. * @data Pointer to a completion structure.
  615. * @status Completion status.
  616. *
  617. * return value
  618. * None
  619. */
  620. static void mtip_completion(struct mtip_port *port,
  621. int tag,
  622. void *data,
  623. int status)
  624. {
  625. struct mtip_cmd *command = &port->commands[tag];
  626. struct completion *waiting = data;
  627. if (unlikely(status == PORT_IRQ_TF_ERR))
  628. dev_warn(&port->dd->pdev->dev,
  629. "Internal command %d completed with TFE\n", tag);
  630. command->async_callback = NULL;
  631. command->comp_func = NULL;
  632. complete(waiting);
  633. }
  634. static void mtip_null_completion(struct mtip_port *port,
  635. int tag,
  636. void *data,
  637. int status)
  638. {
  639. return;
  640. }
  641. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  642. dma_addr_t buffer_dma, unsigned int sectors);
  643. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  644. struct smart_attr *attrib);
  645. /*
  646. * Handle an error.
  647. *
  648. * @dd Pointer to the DRIVER_DATA structure.
  649. *
  650. * return value
  651. * None
  652. */
  653. static void mtip_handle_tfe(struct driver_data *dd)
  654. {
  655. int group, tag, bit, reissue, rv;
  656. struct mtip_port *port;
  657. struct mtip_cmd *cmd;
  658. u32 completed;
  659. struct host_to_dev_fis *fis;
  660. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  661. unsigned int cmd_cnt = 0;
  662. unsigned char *buf;
  663. char *fail_reason = NULL;
  664. int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
  665. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  666. port = dd->port;
  667. /* Stop the timer to prevent command timeouts. */
  668. del_timer(&port->cmd_timer);
  669. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  670. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  671. test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
  672. cmd = &port->commands[MTIP_TAG_INTERNAL];
  673. dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
  674. atomic_inc(&cmd->active); /* active > 1 indicates error */
  675. if (cmd->comp_data && cmd->comp_func) {
  676. cmd->comp_func(port, MTIP_TAG_INTERNAL,
  677. cmd->comp_data, PORT_IRQ_TF_ERR);
  678. }
  679. goto handle_tfe_exit;
  680. }
  681. /* clear the tag accumulator */
  682. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  683. /* Loop through all the groups */
  684. for (group = 0; group < dd->slot_groups; group++) {
  685. completed = readl(port->completed[group]);
  686. /* clear completed status register in the hardware.*/
  687. writel(completed, port->completed[group]);
  688. /* Process successfully completed commands */
  689. for (bit = 0; bit < 32 && completed; bit++) {
  690. if (!(completed & (1<<bit)))
  691. continue;
  692. tag = (group << 5) + bit;
  693. /* Skip the internal command slot */
  694. if (tag == MTIP_TAG_INTERNAL)
  695. continue;
  696. cmd = &port->commands[tag];
  697. if (likely(cmd->comp_func)) {
  698. set_bit(tag, tagaccum);
  699. cmd_cnt++;
  700. atomic_set(&cmd->active, 0);
  701. cmd->comp_func(port,
  702. tag,
  703. cmd->comp_data,
  704. 0);
  705. } else {
  706. dev_err(&port->dd->pdev->dev,
  707. "Missing completion func for tag %d",
  708. tag);
  709. if (mtip_check_surprise_removal(dd->pdev)) {
  710. mtip_command_cleanup(dd);
  711. /* don't proceed further */
  712. return;
  713. }
  714. }
  715. }
  716. }
  717. print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
  718. /* Restart the port */
  719. mdelay(20);
  720. mtip_restart_port(port);
  721. /* Trying to determine the cause of the error */
  722. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  723. dd->port->log_buf,
  724. dd->port->log_buf_dma, 1);
  725. if (rv) {
  726. dev_warn(&dd->pdev->dev,
  727. "Error in READ LOG EXT (10h) command\n");
  728. /* non-critical error, don't fail the load */
  729. } else {
  730. buf = (unsigned char *)dd->port->log_buf;
  731. if (buf[259] & 0x1) {
  732. dev_info(&dd->pdev->dev,
  733. "Write protect bit is set.\n");
  734. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  735. fail_all_ncq_write = 1;
  736. fail_reason = "write protect";
  737. }
  738. if (buf[288] == 0xF7) {
  739. dev_info(&dd->pdev->dev,
  740. "Exceeded Tmax, drive in thermal shutdown.\n");
  741. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  742. fail_all_ncq_cmds = 1;
  743. fail_reason = "thermal shutdown";
  744. }
  745. if (buf[288] == 0xBF) {
  746. dev_info(&dd->pdev->dev,
  747. "Drive indicates rebuild has failed.\n");
  748. fail_all_ncq_cmds = 1;
  749. fail_reason = "rebuild failed";
  750. }
  751. }
  752. /* clear the tag accumulator */
  753. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  754. /* Loop through all the groups */
  755. for (group = 0; group < dd->slot_groups; group++) {
  756. for (bit = 0; bit < 32; bit++) {
  757. reissue = 1;
  758. tag = (group << 5) + bit;
  759. cmd = &port->commands[tag];
  760. /* If the active bit is set re-issue the command */
  761. if (atomic_read(&cmd->active) == 0)
  762. continue;
  763. fis = (struct host_to_dev_fis *)cmd->command;
  764. /* Should re-issue? */
  765. if (tag == MTIP_TAG_INTERNAL ||
  766. fis->command == ATA_CMD_SET_FEATURES)
  767. reissue = 0;
  768. else {
  769. if (fail_all_ncq_cmds ||
  770. (fail_all_ncq_write &&
  771. fis->command == ATA_CMD_FPDMA_WRITE)) {
  772. dev_warn(&dd->pdev->dev,
  773. " Fail: %s w/tag %d [%s].\n",
  774. fis->command == ATA_CMD_FPDMA_WRITE ?
  775. "write" : "read",
  776. tag,
  777. fail_reason != NULL ?
  778. fail_reason : "unknown");
  779. atomic_set(&cmd->active, 0);
  780. if (cmd->comp_func) {
  781. cmd->comp_func(port, tag,
  782. cmd->comp_data,
  783. -ENODATA);
  784. }
  785. continue;
  786. }
  787. }
  788. /*
  789. * First check if this command has
  790. * exceeded its retries.
  791. */
  792. if (reissue && (cmd->retries-- > 0)) {
  793. set_bit(tag, tagaccum);
  794. /* Re-issue the command. */
  795. mtip_issue_ncq_command(port, tag);
  796. continue;
  797. }
  798. /* Retire a command that will not be reissued */
  799. dev_warn(&port->dd->pdev->dev,
  800. "retiring tag %d\n", tag);
  801. atomic_set(&cmd->active, 0);
  802. if (cmd->comp_func)
  803. cmd->comp_func(
  804. port,
  805. tag,
  806. cmd->comp_data,
  807. PORT_IRQ_TF_ERR);
  808. else
  809. dev_warn(&port->dd->pdev->dev,
  810. "Bad completion for tag %d\n",
  811. tag);
  812. }
  813. }
  814. print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
  815. handle_tfe_exit:
  816. /* clear eh_active */
  817. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  818. wake_up_interruptible(&port->svc_wait);
  819. mod_timer(&port->cmd_timer,
  820. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  821. }
  822. /*
  823. * Handle a set device bits interrupt
  824. */
  825. static inline void mtip_process_sdbf(struct driver_data *dd)
  826. {
  827. struct mtip_port *port = dd->port;
  828. int group, tag, bit;
  829. u32 completed;
  830. struct mtip_cmd *command;
  831. /* walk all bits in all slot groups */
  832. for (group = 0; group < dd->slot_groups; group++) {
  833. completed = readl(port->completed[group]);
  834. if (!completed)
  835. continue;
  836. /* clear completed status register in the hardware.*/
  837. writel(completed, port->completed[group]);
  838. /* Process completed commands. */
  839. for (bit = 0;
  840. (bit < 32) && completed;
  841. bit++, completed >>= 1) {
  842. if (completed & 0x01) {
  843. tag = (group << 5) | bit;
  844. /* skip internal command slot. */
  845. if (unlikely(tag == MTIP_TAG_INTERNAL))
  846. continue;
  847. command = &port->commands[tag];
  848. /* make internal callback */
  849. if (likely(command->comp_func)) {
  850. command->comp_func(
  851. port,
  852. tag,
  853. command->comp_data,
  854. 0);
  855. } else {
  856. dev_warn(&dd->pdev->dev,
  857. "Null completion "
  858. "for tag %d",
  859. tag);
  860. if (mtip_check_surprise_removal(
  861. dd->pdev)) {
  862. mtip_command_cleanup(dd);
  863. return;
  864. }
  865. }
  866. }
  867. }
  868. }
  869. }
  870. /*
  871. * Process legacy pio and d2h interrupts
  872. */
  873. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  874. {
  875. struct mtip_port *port = dd->port;
  876. struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
  877. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  878. (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  879. & (1 << MTIP_TAG_INTERNAL))) {
  880. if (cmd->comp_func) {
  881. cmd->comp_func(port,
  882. MTIP_TAG_INTERNAL,
  883. cmd->comp_data,
  884. 0);
  885. return;
  886. }
  887. }
  888. return;
  889. }
  890. /*
  891. * Demux and handle errors
  892. */
  893. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  894. {
  895. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
  896. mtip_handle_tfe(dd);
  897. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  898. dev_warn(&dd->pdev->dev,
  899. "Clearing PxSERR.DIAG.x\n");
  900. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  901. }
  902. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  903. dev_warn(&dd->pdev->dev,
  904. "Clearing PxSERR.DIAG.n\n");
  905. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  906. }
  907. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  908. dev_warn(&dd->pdev->dev,
  909. "Port stat errors %x unhandled\n",
  910. (port_stat & ~PORT_IRQ_HANDLED));
  911. }
  912. }
  913. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  914. {
  915. struct driver_data *dd = (struct driver_data *) data;
  916. struct mtip_port *port = dd->port;
  917. u32 hba_stat, port_stat;
  918. int rv = IRQ_NONE;
  919. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  920. if (hba_stat) {
  921. rv = IRQ_HANDLED;
  922. /* Acknowledge the interrupt status on the port.*/
  923. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  924. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  925. /* Demux port status */
  926. if (likely(port_stat & PORT_IRQ_SDB_FIS))
  927. mtip_process_sdbf(dd);
  928. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  929. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  930. mtip_command_cleanup(dd);
  931. /* don't proceed further */
  932. return IRQ_HANDLED;
  933. }
  934. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  935. &dd->dd_flag))
  936. return rv;
  937. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  938. }
  939. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  940. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  941. }
  942. /* acknowledge interrupt */
  943. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  944. return rv;
  945. }
  946. /*
  947. * Wrapper for mtip_handle_irq
  948. * (ignores return code)
  949. */
  950. static void mtip_tasklet(unsigned long data)
  951. {
  952. mtip_handle_irq((struct driver_data *) data);
  953. }
  954. /*
  955. * HBA interrupt subroutine.
  956. *
  957. * @irq IRQ number.
  958. * @instance Pointer to the driver data structure.
  959. *
  960. * return value
  961. * IRQ_HANDLED A HBA interrupt was pending and handled.
  962. * IRQ_NONE This interrupt was not for the HBA.
  963. */
  964. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  965. {
  966. struct driver_data *dd = instance;
  967. tasklet_schedule(&dd->tasklet);
  968. return IRQ_HANDLED;
  969. }
  970. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  971. {
  972. atomic_set(&port->commands[tag].active, 1);
  973. writel(1 << MTIP_TAG_BIT(tag),
  974. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  975. }
  976. static bool mtip_pause_ncq(struct mtip_port *port,
  977. struct host_to_dev_fis *fis)
  978. {
  979. struct host_to_dev_fis *reply;
  980. unsigned long task_file_data;
  981. reply = port->rxfis + RX_FIS_D2H_REG;
  982. task_file_data = readl(port->mmio+PORT_TFDATA);
  983. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  984. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  985. if ((task_file_data & 1))
  986. return false;
  987. if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
  988. set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  989. set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  990. port->ic_pause_timer = jiffies;
  991. return true;
  992. } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
  993. (fis->features == 0x03)) {
  994. set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  995. port->ic_pause_timer = jiffies;
  996. return true;
  997. } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
  998. ((fis->command == 0xFC) &&
  999. (fis->features == 0x27 || fis->features == 0x72 ||
  1000. fis->features == 0x62 || fis->features == 0x26))) {
  1001. /* Com reset after secure erase or lowlevel format */
  1002. mtip_restart_port(port);
  1003. return false;
  1004. }
  1005. return false;
  1006. }
  1007. /*
  1008. * Wait for port to quiesce
  1009. *
  1010. * @port Pointer to port data structure
  1011. * @timeout Max duration to wait (ms)
  1012. *
  1013. * return value
  1014. * 0 Success
  1015. * -EBUSY Commands still active
  1016. */
  1017. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  1018. {
  1019. unsigned long to;
  1020. unsigned int n;
  1021. unsigned int active = 1;
  1022. to = jiffies + msecs_to_jiffies(timeout);
  1023. do {
  1024. if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
  1025. test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  1026. msleep(20);
  1027. continue; /* svc thd is actively issuing commands */
  1028. }
  1029. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1030. return -EFAULT;
  1031. /*
  1032. * Ignore s_active bit 0 of array element 0.
  1033. * This bit will always be set
  1034. */
  1035. active = readl(port->s_active[0]) & 0xFFFFFFFE;
  1036. for (n = 1; n < port->dd->slot_groups; n++)
  1037. active |= readl(port->s_active[n]);
  1038. if (!active)
  1039. break;
  1040. msleep(20);
  1041. } while (time_before(jiffies, to));
  1042. return active ? -EBUSY : 0;
  1043. }
  1044. /*
  1045. * Execute an internal command and wait for the completion.
  1046. *
  1047. * @port Pointer to the port data structure.
  1048. * @fis Pointer to the FIS that describes the command.
  1049. * @fis_len Length in WORDS of the FIS.
  1050. * @buffer DMA accessible for command data.
  1051. * @buf_len Length, in bytes, of the data buffer.
  1052. * @opts Command header options, excluding the FIS length
  1053. * and the number of PRD entries.
  1054. * @timeout Time in ms to wait for the command to complete.
  1055. *
  1056. * return value
  1057. * 0 Command completed successfully.
  1058. * -EFAULT The buffer address is not correctly aligned.
  1059. * -EBUSY Internal command or other IO in progress.
  1060. * -EAGAIN Time out waiting for command to complete.
  1061. */
  1062. static int mtip_exec_internal_command(struct mtip_port *port,
  1063. struct host_to_dev_fis *fis,
  1064. int fis_len,
  1065. dma_addr_t buffer,
  1066. int buf_len,
  1067. u32 opts,
  1068. gfp_t atomic,
  1069. unsigned long timeout)
  1070. {
  1071. struct mtip_cmd_sg *command_sg;
  1072. DECLARE_COMPLETION_ONSTACK(wait);
  1073. int rv = 0, ready2go = 1;
  1074. struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
  1075. unsigned long to;
  1076. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  1077. if (buffer & 0x00000007) {
  1078. dev_err(&port->dd->pdev->dev,
  1079. "SG buffer is not 8 byte aligned\n");
  1080. return -EFAULT;
  1081. }
  1082. to = jiffies + msecs_to_jiffies(timeout);
  1083. do {
  1084. ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
  1085. port->allocated);
  1086. if (ready2go)
  1087. break;
  1088. mdelay(100);
  1089. } while (time_before(jiffies, to));
  1090. if (!ready2go) {
  1091. dev_warn(&port->dd->pdev->dev,
  1092. "Internal cmd active. new cmd [%02X]\n", fis->command);
  1093. return -EBUSY;
  1094. }
  1095. set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1096. port->ic_pause_timer = 0;
  1097. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  1098. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  1099. else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
  1100. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  1101. if (atomic == GFP_KERNEL) {
  1102. if (fis->command != ATA_CMD_STANDBYNOW1) {
  1103. /* wait for io to complete if non atomic */
  1104. if (mtip_quiesce_io(port, 5000) < 0) {
  1105. dev_warn(&port->dd->pdev->dev,
  1106. "Failed to quiesce IO\n");
  1107. release_slot(port, MTIP_TAG_INTERNAL);
  1108. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1109. wake_up_interruptible(&port->svc_wait);
  1110. return -EBUSY;
  1111. }
  1112. }
  1113. /* Set the completion function and data for the command. */
  1114. int_cmd->comp_data = &wait;
  1115. int_cmd->comp_func = mtip_completion;
  1116. } else {
  1117. /* Clear completion - we're going to poll */
  1118. int_cmd->comp_data = NULL;
  1119. int_cmd->comp_func = mtip_null_completion;
  1120. }
  1121. /* Copy the command to the command table */
  1122. memcpy(int_cmd->command, fis, fis_len*4);
  1123. /* Populate the SG list */
  1124. int_cmd->command_header->opts =
  1125. __force_bit2int cpu_to_le32(opts | fis_len);
  1126. if (buf_len) {
  1127. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  1128. command_sg->info =
  1129. __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
  1130. command_sg->dba =
  1131. __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
  1132. command_sg->dba_upper =
  1133. __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
  1134. int_cmd->command_header->opts |=
  1135. __force_bit2int cpu_to_le32((1 << 16));
  1136. }
  1137. /* Populate the command header */
  1138. int_cmd->command_header->byte_count = 0;
  1139. /* Issue the command to the hardware */
  1140. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  1141. /* Poll if atomic, wait_for_completion otherwise */
  1142. if (atomic == GFP_KERNEL) {
  1143. /* Wait for the command to complete or timeout. */
  1144. if (wait_for_completion_timeout(
  1145. &wait,
  1146. msecs_to_jiffies(timeout)) == 0) {
  1147. dev_err(&port->dd->pdev->dev,
  1148. "Internal command did not complete [%d] "
  1149. "within timeout of %lu ms\n",
  1150. atomic, timeout);
  1151. if (mtip_check_surprise_removal(port->dd->pdev) ||
  1152. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1153. &port->dd->dd_flag)) {
  1154. rv = -ENXIO;
  1155. goto exec_ic_exit;
  1156. }
  1157. rv = -EAGAIN;
  1158. }
  1159. } else {
  1160. /* Spin for <timeout> checking if command still outstanding */
  1161. timeout = jiffies + msecs_to_jiffies(timeout);
  1162. while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1163. & (1 << MTIP_TAG_INTERNAL))
  1164. && time_before(jiffies, timeout)) {
  1165. if (mtip_check_surprise_removal(port->dd->pdev)) {
  1166. rv = -ENXIO;
  1167. goto exec_ic_exit;
  1168. }
  1169. if ((fis->command != ATA_CMD_STANDBYNOW1) &&
  1170. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1171. &port->dd->dd_flag)) {
  1172. rv = -ENXIO;
  1173. goto exec_ic_exit;
  1174. }
  1175. if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
  1176. atomic_inc(&int_cmd->active); /* error */
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. if (atomic_read(&int_cmd->active) > 1) {
  1182. dev_err(&port->dd->pdev->dev,
  1183. "Internal command [%02X] failed\n", fis->command);
  1184. rv = -EIO;
  1185. }
  1186. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1187. & (1 << MTIP_TAG_INTERNAL)) {
  1188. rv = -ENXIO;
  1189. if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1190. &port->dd->dd_flag)) {
  1191. mtip_restart_port(port);
  1192. rv = -EAGAIN;
  1193. }
  1194. }
  1195. exec_ic_exit:
  1196. /* Clear the allocated and active bits for the internal command. */
  1197. atomic_set(&int_cmd->active, 0);
  1198. release_slot(port, MTIP_TAG_INTERNAL);
  1199. if (rv >= 0 && mtip_pause_ncq(port, fis)) {
  1200. /* NCQ paused */
  1201. return rv;
  1202. }
  1203. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1204. wake_up_interruptible(&port->svc_wait);
  1205. return rv;
  1206. }
  1207. /*
  1208. * Byte-swap ATA ID strings.
  1209. *
  1210. * ATA identify data contains strings in byte-swapped 16-bit words.
  1211. * They must be swapped (on all architectures) to be usable as C strings.
  1212. * This function swaps bytes in-place.
  1213. *
  1214. * @buf The buffer location of the string
  1215. * @len The number of bytes to swap
  1216. *
  1217. * return value
  1218. * None
  1219. */
  1220. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1221. {
  1222. int i;
  1223. for (i = 0; i < (len/2); i++)
  1224. be16_to_cpus(&buf[i]);
  1225. }
  1226. /*
  1227. * Request the device identity information.
  1228. *
  1229. * If a user space buffer is not specified, i.e. is NULL, the
  1230. * identify information is still read from the drive and placed
  1231. * into the identify data buffer (@e port->identify) in the
  1232. * port data structure.
  1233. * When the identify buffer contains valid identify information @e
  1234. * port->identify_valid is non-zero.
  1235. *
  1236. * @port Pointer to the port structure.
  1237. * @user_buffer A user space buffer where the identify data should be
  1238. * copied.
  1239. *
  1240. * return value
  1241. * 0 Command completed successfully.
  1242. * -EFAULT An error occurred while coping data to the user buffer.
  1243. * -1 Command failed.
  1244. */
  1245. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1246. {
  1247. int rv = 0;
  1248. struct host_to_dev_fis fis;
  1249. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1250. return -EFAULT;
  1251. /* Build the FIS. */
  1252. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1253. fis.type = 0x27;
  1254. fis.opts = 1 << 7;
  1255. fis.command = ATA_CMD_ID_ATA;
  1256. /* Set the identify information as invalid. */
  1257. port->identify_valid = 0;
  1258. /* Clear the identify information. */
  1259. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1260. /* Execute the command. */
  1261. if (mtip_exec_internal_command(port,
  1262. &fis,
  1263. 5,
  1264. port->identify_dma,
  1265. sizeof(u16) * ATA_ID_WORDS,
  1266. 0,
  1267. GFP_KERNEL,
  1268. MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
  1269. < 0) {
  1270. rv = -1;
  1271. goto out;
  1272. }
  1273. /*
  1274. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1275. * perform field-sensitive swapping on the string fields.
  1276. * See the kernel use of ata_id_string() for proof of this.
  1277. */
  1278. #ifdef __LITTLE_ENDIAN
  1279. ata_swap_string(port->identify + 27, 40); /* model string*/
  1280. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1281. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1282. #else
  1283. {
  1284. int i;
  1285. for (i = 0; i < ATA_ID_WORDS; i++)
  1286. port->identify[i] = le16_to_cpu(port->identify[i]);
  1287. }
  1288. #endif
  1289. /* Set the identify buffer as valid. */
  1290. port->identify_valid = 1;
  1291. if (user_buffer) {
  1292. if (copy_to_user(
  1293. user_buffer,
  1294. port->identify,
  1295. ATA_ID_WORDS * sizeof(u16))) {
  1296. rv = -EFAULT;
  1297. goto out;
  1298. }
  1299. }
  1300. out:
  1301. return rv;
  1302. }
  1303. /*
  1304. * Issue a standby immediate command to the device.
  1305. *
  1306. * @port Pointer to the port structure.
  1307. *
  1308. * return value
  1309. * 0 Command was executed successfully.
  1310. * -1 An error occurred while executing the command.
  1311. */
  1312. static int mtip_standby_immediate(struct mtip_port *port)
  1313. {
  1314. int rv;
  1315. struct host_to_dev_fis fis;
  1316. unsigned long start;
  1317. /* Build the FIS. */
  1318. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1319. fis.type = 0x27;
  1320. fis.opts = 1 << 7;
  1321. fis.command = ATA_CMD_STANDBYNOW1;
  1322. start = jiffies;
  1323. rv = mtip_exec_internal_command(port,
  1324. &fis,
  1325. 5,
  1326. 0,
  1327. 0,
  1328. 0,
  1329. GFP_ATOMIC,
  1330. 15000);
  1331. dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
  1332. jiffies_to_msecs(jiffies - start));
  1333. if (rv)
  1334. dev_warn(&port->dd->pdev->dev,
  1335. "STANDBY IMMEDIATE command failed.\n");
  1336. return rv;
  1337. }
  1338. /*
  1339. * Issue a READ LOG EXT command to the device.
  1340. *
  1341. * @port pointer to the port structure.
  1342. * @page page number to fetch
  1343. * @buffer pointer to buffer
  1344. * @buffer_dma dma address corresponding to @buffer
  1345. * @sectors page length to fetch, in sectors
  1346. *
  1347. * return value
  1348. * @rv return value from mtip_exec_internal_command()
  1349. */
  1350. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  1351. dma_addr_t buffer_dma, unsigned int sectors)
  1352. {
  1353. struct host_to_dev_fis fis;
  1354. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1355. fis.type = 0x27;
  1356. fis.opts = 1 << 7;
  1357. fis.command = ATA_CMD_READ_LOG_EXT;
  1358. fis.sect_count = sectors & 0xFF;
  1359. fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
  1360. fis.lba_low = page;
  1361. fis.lba_mid = 0;
  1362. fis.device = ATA_DEVICE_OBS;
  1363. memset(buffer, 0, sectors * ATA_SECT_SIZE);
  1364. return mtip_exec_internal_command(port,
  1365. &fis,
  1366. 5,
  1367. buffer_dma,
  1368. sectors * ATA_SECT_SIZE,
  1369. 0,
  1370. GFP_ATOMIC,
  1371. MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
  1372. }
  1373. /*
  1374. * Issue a SMART READ DATA command to the device.
  1375. *
  1376. * @port pointer to the port structure.
  1377. * @buffer pointer to buffer
  1378. * @buffer_dma dma address corresponding to @buffer
  1379. *
  1380. * return value
  1381. * @rv return value from mtip_exec_internal_command()
  1382. */
  1383. static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
  1384. dma_addr_t buffer_dma)
  1385. {
  1386. struct host_to_dev_fis fis;
  1387. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1388. fis.type = 0x27;
  1389. fis.opts = 1 << 7;
  1390. fis.command = ATA_CMD_SMART;
  1391. fis.features = 0xD0;
  1392. fis.sect_count = 1;
  1393. fis.lba_mid = 0x4F;
  1394. fis.lba_hi = 0xC2;
  1395. fis.device = ATA_DEVICE_OBS;
  1396. return mtip_exec_internal_command(port,
  1397. &fis,
  1398. 5,
  1399. buffer_dma,
  1400. ATA_SECT_SIZE,
  1401. 0,
  1402. GFP_ATOMIC,
  1403. 15000);
  1404. }
  1405. /*
  1406. * Get the value of a smart attribute
  1407. *
  1408. * @port pointer to the port structure
  1409. * @id attribute number
  1410. * @attrib pointer to return attrib information corresponding to @id
  1411. *
  1412. * return value
  1413. * -EINVAL NULL buffer passed or unsupported attribute @id.
  1414. * -EPERM Identify data not valid, SMART not supported or not enabled
  1415. */
  1416. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  1417. struct smart_attr *attrib)
  1418. {
  1419. int rv, i;
  1420. struct smart_attr *pattr;
  1421. if (!attrib)
  1422. return -EINVAL;
  1423. if (!port->identify_valid) {
  1424. dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
  1425. return -EPERM;
  1426. }
  1427. if (!(port->identify[82] & 0x1)) {
  1428. dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
  1429. return -EPERM;
  1430. }
  1431. if (!(port->identify[85] & 0x1)) {
  1432. dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
  1433. return -EPERM;
  1434. }
  1435. memset(port->smart_buf, 0, ATA_SECT_SIZE);
  1436. rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
  1437. if (rv) {
  1438. dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
  1439. return rv;
  1440. }
  1441. pattr = (struct smart_attr *)(port->smart_buf + 2);
  1442. for (i = 0; i < 29; i++, pattr++)
  1443. if (pattr->attr_id == id) {
  1444. memcpy(attrib, pattr, sizeof(struct smart_attr));
  1445. break;
  1446. }
  1447. if (i == 29) {
  1448. dev_warn(&port->dd->pdev->dev,
  1449. "Query for invalid SMART attribute ID\n");
  1450. rv = -EINVAL;
  1451. }
  1452. return rv;
  1453. }
  1454. /*
  1455. * Get the drive capacity.
  1456. *
  1457. * @dd Pointer to the device data structure.
  1458. * @sectors Pointer to the variable that will receive the sector count.
  1459. *
  1460. * return value
  1461. * 1 Capacity was returned successfully.
  1462. * 0 The identify information is invalid.
  1463. */
  1464. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1465. {
  1466. struct mtip_port *port = dd->port;
  1467. u64 total, raw0, raw1, raw2, raw3;
  1468. raw0 = port->identify[100];
  1469. raw1 = port->identify[101];
  1470. raw2 = port->identify[102];
  1471. raw3 = port->identify[103];
  1472. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1473. *sectors = total;
  1474. return (bool) !!port->identify_valid;
  1475. }
  1476. /*
  1477. * Reset the HBA.
  1478. *
  1479. * Resets the HBA by setting the HBA Reset bit in the Global
  1480. * HBA Control register. After setting the HBA Reset bit the
  1481. * function waits for 1 second before reading the HBA Reset
  1482. * bit to make sure it has cleared. If HBA Reset is not clear
  1483. * an error is returned. Cannot be used in non-blockable
  1484. * context.
  1485. *
  1486. * @dd Pointer to the driver data structure.
  1487. *
  1488. * return value
  1489. * 0 The reset was successful.
  1490. * -1 The HBA Reset bit did not clear.
  1491. */
  1492. static int mtip_hba_reset(struct driver_data *dd)
  1493. {
  1494. mtip_deinit_port(dd->port);
  1495. /* Set the reset bit */
  1496. writel(HOST_RESET, dd->mmio + HOST_CTL);
  1497. /* Flush */
  1498. readl(dd->mmio + HOST_CTL);
  1499. /* Wait for reset to clear */
  1500. ssleep(1);
  1501. /* Check the bit has cleared */
  1502. if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
  1503. dev_err(&dd->pdev->dev,
  1504. "Reset bit did not clear.\n");
  1505. return -1;
  1506. }
  1507. return 0;
  1508. }
  1509. /*
  1510. * Display the identify command data.
  1511. *
  1512. * @port Pointer to the port data structure.
  1513. *
  1514. * return value
  1515. * None
  1516. */
  1517. static void mtip_dump_identify(struct mtip_port *port)
  1518. {
  1519. sector_t sectors;
  1520. unsigned short revid;
  1521. char cbuf[42];
  1522. if (!port->identify_valid)
  1523. return;
  1524. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1525. dev_info(&port->dd->pdev->dev,
  1526. "Serial No.: %s\n", cbuf);
  1527. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1528. dev_info(&port->dd->pdev->dev,
  1529. "Firmware Ver.: %s\n", cbuf);
  1530. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1531. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1532. if (mtip_hw_get_capacity(port->dd, &sectors))
  1533. dev_info(&port->dd->pdev->dev,
  1534. "Capacity: %llu sectors (%llu MB)\n",
  1535. (u64)sectors,
  1536. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1537. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1538. switch (revid & 0xFF) {
  1539. case 0x1:
  1540. strlcpy(cbuf, "A0", 3);
  1541. break;
  1542. case 0x3:
  1543. strlcpy(cbuf, "A2", 3);
  1544. break;
  1545. default:
  1546. strlcpy(cbuf, "?", 2);
  1547. break;
  1548. }
  1549. dev_info(&port->dd->pdev->dev,
  1550. "Card Type: %s\n", cbuf);
  1551. }
  1552. /*
  1553. * Map the commands scatter list into the command table.
  1554. *
  1555. * @command Pointer to the command.
  1556. * @nents Number of scatter list entries.
  1557. *
  1558. * return value
  1559. * None
  1560. */
  1561. static inline void fill_command_sg(struct driver_data *dd,
  1562. struct mtip_cmd *command,
  1563. int nents)
  1564. {
  1565. int n;
  1566. unsigned int dma_len;
  1567. struct mtip_cmd_sg *command_sg;
  1568. struct scatterlist *sg = command->sg;
  1569. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1570. for (n = 0; n < nents; n++) {
  1571. dma_len = sg_dma_len(sg);
  1572. if (dma_len > 0x400000)
  1573. dev_err(&dd->pdev->dev,
  1574. "DMA segment length truncated\n");
  1575. command_sg->info = __force_bit2int
  1576. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1577. command_sg->dba = __force_bit2int
  1578. cpu_to_le32(sg_dma_address(sg));
  1579. command_sg->dba_upper = __force_bit2int
  1580. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1581. command_sg++;
  1582. sg++;
  1583. }
  1584. }
  1585. /*
  1586. * @brief Execute a drive command.
  1587. *
  1588. * return value 0 The command completed successfully.
  1589. * return value -1 An error occurred while executing the command.
  1590. */
  1591. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1592. {
  1593. struct host_to_dev_fis fis;
  1594. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1595. /* Build the FIS. */
  1596. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1597. fis.type = 0x27;
  1598. fis.opts = 1 << 7;
  1599. fis.command = command[0];
  1600. fis.features = command[1];
  1601. fis.sect_count = command[2];
  1602. fis.sector = command[3];
  1603. fis.cyl_low = command[4];
  1604. fis.cyl_hi = command[5];
  1605. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1606. dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1607. __func__,
  1608. command[0],
  1609. command[1],
  1610. command[2],
  1611. command[3],
  1612. command[4],
  1613. command[5],
  1614. command[6]);
  1615. /* Execute the command. */
  1616. if (mtip_exec_internal_command(port,
  1617. &fis,
  1618. 5,
  1619. 0,
  1620. 0,
  1621. 0,
  1622. GFP_KERNEL,
  1623. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1624. return -1;
  1625. }
  1626. command[0] = reply->command; /* Status*/
  1627. command[1] = reply->features; /* Error*/
  1628. command[4] = reply->cyl_low;
  1629. command[5] = reply->cyl_hi;
  1630. dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1631. __func__,
  1632. command[0],
  1633. command[1],
  1634. command[4],
  1635. command[5]);
  1636. return 0;
  1637. }
  1638. /*
  1639. * @brief Execute a drive command.
  1640. *
  1641. * @param port Pointer to the port data structure.
  1642. * @param command Pointer to the user specified command parameters.
  1643. * @param user_buffer Pointer to the user space buffer where read sector
  1644. * data should be copied.
  1645. *
  1646. * return value 0 The command completed successfully.
  1647. * return value -EFAULT An error occurred while copying the completion
  1648. * data to the user space buffer.
  1649. * return value -1 An error occurred while executing the command.
  1650. */
  1651. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1652. void __user *user_buffer)
  1653. {
  1654. struct host_to_dev_fis fis;
  1655. struct host_to_dev_fis *reply;
  1656. u8 *buf = NULL;
  1657. dma_addr_t dma_addr = 0;
  1658. int rv = 0, xfer_sz = command[3];
  1659. if (xfer_sz) {
  1660. if (!user_buffer)
  1661. return -EFAULT;
  1662. buf = dmam_alloc_coherent(&port->dd->pdev->dev,
  1663. ATA_SECT_SIZE * xfer_sz,
  1664. &dma_addr,
  1665. GFP_KERNEL);
  1666. if (!buf) {
  1667. dev_err(&port->dd->pdev->dev,
  1668. "Memory allocation failed (%d bytes)\n",
  1669. ATA_SECT_SIZE * xfer_sz);
  1670. return -ENOMEM;
  1671. }
  1672. memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
  1673. }
  1674. /* Build the FIS. */
  1675. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1676. fis.type = 0x27;
  1677. fis.opts = 1 << 7;
  1678. fis.command = command[0];
  1679. fis.features = command[2];
  1680. fis.sect_count = command[3];
  1681. if (fis.command == ATA_CMD_SMART) {
  1682. fis.sector = command[1];
  1683. fis.cyl_low = 0x4F;
  1684. fis.cyl_hi = 0xC2;
  1685. }
  1686. if (xfer_sz)
  1687. reply = (port->rxfis + RX_FIS_PIO_SETUP);
  1688. else
  1689. reply = (port->rxfis + RX_FIS_D2H_REG);
  1690. dbg_printk(MTIP_DRV_NAME
  1691. " %s: User Command: cmd %x, sect %x, "
  1692. "feat %x, sectcnt %x\n",
  1693. __func__,
  1694. command[0],
  1695. command[1],
  1696. command[2],
  1697. command[3]);
  1698. /* Execute the command. */
  1699. if (mtip_exec_internal_command(port,
  1700. &fis,
  1701. 5,
  1702. (xfer_sz ? dma_addr : 0),
  1703. (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
  1704. 0,
  1705. GFP_KERNEL,
  1706. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1707. < 0) {
  1708. rv = -EFAULT;
  1709. goto exit_drive_command;
  1710. }
  1711. /* Collect the completion status. */
  1712. command[0] = reply->command; /* Status*/
  1713. command[1] = reply->features; /* Error*/
  1714. command[2] = reply->sect_count;
  1715. dbg_printk(MTIP_DRV_NAME
  1716. " %s: Completion Status: stat %x, "
  1717. "err %x, nsect %x\n",
  1718. __func__,
  1719. command[0],
  1720. command[1],
  1721. command[2]);
  1722. if (xfer_sz) {
  1723. if (copy_to_user(user_buffer,
  1724. buf,
  1725. ATA_SECT_SIZE * command[3])) {
  1726. rv = -EFAULT;
  1727. goto exit_drive_command;
  1728. }
  1729. }
  1730. exit_drive_command:
  1731. if (buf)
  1732. dmam_free_coherent(&port->dd->pdev->dev,
  1733. ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
  1734. return rv;
  1735. }
  1736. /*
  1737. * Indicates whether a command has a single sector payload.
  1738. *
  1739. * @command passed to the device to perform the certain event.
  1740. * @features passed to the device to perform the certain event.
  1741. *
  1742. * return value
  1743. * 1 command is one that always has a single sector payload,
  1744. * regardless of the value in the Sector Count field.
  1745. * 0 otherwise
  1746. *
  1747. */
  1748. static unsigned int implicit_sector(unsigned char command,
  1749. unsigned char features)
  1750. {
  1751. unsigned int rv = 0;
  1752. /* list of commands that have an implicit sector count of 1 */
  1753. switch (command) {
  1754. case ATA_CMD_SEC_SET_PASS:
  1755. case ATA_CMD_SEC_UNLOCK:
  1756. case ATA_CMD_SEC_ERASE_PREP:
  1757. case ATA_CMD_SEC_ERASE_UNIT:
  1758. case ATA_CMD_SEC_FREEZE_LOCK:
  1759. case ATA_CMD_SEC_DISABLE_PASS:
  1760. case ATA_CMD_PMP_READ:
  1761. case ATA_CMD_PMP_WRITE:
  1762. rv = 1;
  1763. break;
  1764. case ATA_CMD_SET_MAX:
  1765. if (features == ATA_SET_MAX_UNLOCK)
  1766. rv = 1;
  1767. break;
  1768. case ATA_CMD_SMART:
  1769. if ((features == ATA_SMART_READ_VALUES) ||
  1770. (features == ATA_SMART_READ_THRESHOLDS))
  1771. rv = 1;
  1772. break;
  1773. case ATA_CMD_CONF_OVERLAY:
  1774. if ((features == ATA_DCO_IDENTIFY) ||
  1775. (features == ATA_DCO_SET))
  1776. rv = 1;
  1777. break;
  1778. }
  1779. return rv;
  1780. }
  1781. static void mtip_set_timeout(struct driver_data *dd,
  1782. struct host_to_dev_fis *fis,
  1783. unsigned int *timeout, u8 erasemode)
  1784. {
  1785. switch (fis->command) {
  1786. case ATA_CMD_DOWNLOAD_MICRO:
  1787. *timeout = 120000; /* 2 minutes */
  1788. break;
  1789. case ATA_CMD_SEC_ERASE_UNIT:
  1790. case 0xFC:
  1791. if (erasemode)
  1792. *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
  1793. else
  1794. *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
  1795. break;
  1796. case ATA_CMD_STANDBYNOW1:
  1797. *timeout = 120000; /* 2 minutes */
  1798. break;
  1799. case 0xF7:
  1800. case 0xFA:
  1801. *timeout = 60000; /* 60 seconds */
  1802. break;
  1803. case ATA_CMD_SMART:
  1804. *timeout = 15000; /* 15 seconds */
  1805. break;
  1806. default:
  1807. *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1808. break;
  1809. }
  1810. }
  1811. /*
  1812. * Executes a taskfile
  1813. * See ide_taskfile_ioctl() for derivation
  1814. */
  1815. static int exec_drive_taskfile(struct driver_data *dd,
  1816. void __user *buf,
  1817. ide_task_request_t *req_task,
  1818. int outtotal)
  1819. {
  1820. struct host_to_dev_fis fis;
  1821. struct host_to_dev_fis *reply;
  1822. u8 *outbuf = NULL;
  1823. u8 *inbuf = NULL;
  1824. dma_addr_t outbuf_dma = 0;
  1825. dma_addr_t inbuf_dma = 0;
  1826. dma_addr_t dma_buffer = 0;
  1827. int err = 0;
  1828. unsigned int taskin = 0;
  1829. unsigned int taskout = 0;
  1830. u8 nsect = 0;
  1831. unsigned int timeout;
  1832. unsigned int force_single_sector;
  1833. unsigned int transfer_size;
  1834. unsigned long task_file_data;
  1835. int intotal = outtotal + req_task->out_size;
  1836. int erasemode = 0;
  1837. taskout = req_task->out_size;
  1838. taskin = req_task->in_size;
  1839. /* 130560 = 512 * 0xFF*/
  1840. if (taskin > 130560 || taskout > 130560) {
  1841. err = -EINVAL;
  1842. goto abort;
  1843. }
  1844. if (taskout) {
  1845. outbuf = kzalloc(taskout, GFP_KERNEL);
  1846. if (outbuf == NULL) {
  1847. err = -ENOMEM;
  1848. goto abort;
  1849. }
  1850. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1851. err = -EFAULT;
  1852. goto abort;
  1853. }
  1854. outbuf_dma = pci_map_single(dd->pdev,
  1855. outbuf,
  1856. taskout,
  1857. DMA_TO_DEVICE);
  1858. if (outbuf_dma == 0) {
  1859. err = -ENOMEM;
  1860. goto abort;
  1861. }
  1862. dma_buffer = outbuf_dma;
  1863. }
  1864. if (taskin) {
  1865. inbuf = kzalloc(taskin, GFP_KERNEL);
  1866. if (inbuf == NULL) {
  1867. err = -ENOMEM;
  1868. goto abort;
  1869. }
  1870. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1871. err = -EFAULT;
  1872. goto abort;
  1873. }
  1874. inbuf_dma = pci_map_single(dd->pdev,
  1875. inbuf,
  1876. taskin, DMA_FROM_DEVICE);
  1877. if (inbuf_dma == 0) {
  1878. err = -ENOMEM;
  1879. goto abort;
  1880. }
  1881. dma_buffer = inbuf_dma;
  1882. }
  1883. /* only supports PIO and non-data commands from this ioctl. */
  1884. switch (req_task->data_phase) {
  1885. case TASKFILE_OUT:
  1886. nsect = taskout / ATA_SECT_SIZE;
  1887. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1888. break;
  1889. case TASKFILE_IN:
  1890. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1891. break;
  1892. case TASKFILE_NO_DATA:
  1893. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1894. break;
  1895. default:
  1896. err = -EINVAL;
  1897. goto abort;
  1898. }
  1899. /* Build the FIS. */
  1900. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1901. fis.type = 0x27;
  1902. fis.opts = 1 << 7;
  1903. fis.command = req_task->io_ports[7];
  1904. fis.features = req_task->io_ports[1];
  1905. fis.sect_count = req_task->io_ports[2];
  1906. fis.lba_low = req_task->io_ports[3];
  1907. fis.lba_mid = req_task->io_ports[4];
  1908. fis.lba_hi = req_task->io_ports[5];
  1909. /* Clear the dev bit*/
  1910. fis.device = req_task->io_ports[6] & ~0x10;
  1911. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1912. req_task->in_flags.all =
  1913. IDE_TASKFILE_STD_IN_FLAGS |
  1914. (IDE_HOB_STD_IN_FLAGS << 8);
  1915. fis.lba_low_ex = req_task->hob_ports[3];
  1916. fis.lba_mid_ex = req_task->hob_ports[4];
  1917. fis.lba_hi_ex = req_task->hob_ports[5];
  1918. fis.features_ex = req_task->hob_ports[1];
  1919. fis.sect_cnt_ex = req_task->hob_ports[2];
  1920. } else {
  1921. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1922. }
  1923. force_single_sector = implicit_sector(fis.command, fis.features);
  1924. if ((taskin || taskout) && (!fis.sect_count)) {
  1925. if (nsect)
  1926. fis.sect_count = nsect;
  1927. else {
  1928. if (!force_single_sector) {
  1929. dev_warn(&dd->pdev->dev,
  1930. "data movement but "
  1931. "sect_count is 0\n");
  1932. err = -EINVAL;
  1933. goto abort;
  1934. }
  1935. }
  1936. }
  1937. dbg_printk(MTIP_DRV_NAME
  1938. " %s: cmd %x, feat %x, nsect %x,"
  1939. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1940. " head/dev %x\n",
  1941. __func__,
  1942. fis.command,
  1943. fis.features,
  1944. fis.sect_count,
  1945. fis.lba_low,
  1946. fis.lba_mid,
  1947. fis.lba_hi,
  1948. fis.device);
  1949. /* check for erase mode support during secure erase.*/
  1950. if ((fis.command == ATA_CMD_SEC_ERASE_UNIT)
  1951. && (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
  1952. erasemode = 1;
  1953. }
  1954. mtip_set_timeout(dd, &fis, &timeout, erasemode);
  1955. /* Determine the correct transfer size.*/
  1956. if (force_single_sector)
  1957. transfer_size = ATA_SECT_SIZE;
  1958. else
  1959. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1960. /* Execute the command.*/
  1961. if (mtip_exec_internal_command(dd->port,
  1962. &fis,
  1963. 5,
  1964. dma_buffer,
  1965. transfer_size,
  1966. 0,
  1967. GFP_KERNEL,
  1968. timeout) < 0) {
  1969. err = -EIO;
  1970. goto abort;
  1971. }
  1972. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  1973. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  1974. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  1975. req_task->io_ports[7] = reply->control;
  1976. } else {
  1977. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  1978. req_task->io_ports[7] = reply->command;
  1979. }
  1980. /* reclaim the DMA buffers.*/
  1981. if (inbuf_dma)
  1982. pci_unmap_single(dd->pdev, inbuf_dma,
  1983. taskin, DMA_FROM_DEVICE);
  1984. if (outbuf_dma)
  1985. pci_unmap_single(dd->pdev, outbuf_dma,
  1986. taskout, DMA_TO_DEVICE);
  1987. inbuf_dma = 0;
  1988. outbuf_dma = 0;
  1989. /* return the ATA registers to the caller.*/
  1990. req_task->io_ports[1] = reply->features;
  1991. req_task->io_ports[2] = reply->sect_count;
  1992. req_task->io_ports[3] = reply->lba_low;
  1993. req_task->io_ports[4] = reply->lba_mid;
  1994. req_task->io_ports[5] = reply->lba_hi;
  1995. req_task->io_ports[6] = reply->device;
  1996. if (req_task->out_flags.all & 1) {
  1997. req_task->hob_ports[3] = reply->lba_low_ex;
  1998. req_task->hob_ports[4] = reply->lba_mid_ex;
  1999. req_task->hob_ports[5] = reply->lba_hi_ex;
  2000. req_task->hob_ports[1] = reply->features_ex;
  2001. req_task->hob_ports[2] = reply->sect_cnt_ex;
  2002. }
  2003. dbg_printk(MTIP_DRV_NAME
  2004. " %s: Completion: stat %x,"
  2005. "err %x, sect_cnt %x, lbalo %x,"
  2006. "lbamid %x, lbahi %x, dev %x\n",
  2007. __func__,
  2008. req_task->io_ports[7],
  2009. req_task->io_ports[1],
  2010. req_task->io_ports[2],
  2011. req_task->io_ports[3],
  2012. req_task->io_ports[4],
  2013. req_task->io_ports[5],
  2014. req_task->io_ports[6]);
  2015. if (taskout) {
  2016. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  2017. err = -EFAULT;
  2018. goto abort;
  2019. }
  2020. }
  2021. if (taskin) {
  2022. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  2023. err = -EFAULT;
  2024. goto abort;
  2025. }
  2026. }
  2027. abort:
  2028. if (inbuf_dma)
  2029. pci_unmap_single(dd->pdev, inbuf_dma,
  2030. taskin, DMA_FROM_DEVICE);
  2031. if (outbuf_dma)
  2032. pci_unmap_single(dd->pdev, outbuf_dma,
  2033. taskout, DMA_TO_DEVICE);
  2034. kfree(outbuf);
  2035. kfree(inbuf);
  2036. return err;
  2037. }
  2038. /*
  2039. * Handle IOCTL calls from the Block Layer.
  2040. *
  2041. * This function is called by the Block Layer when it receives an IOCTL
  2042. * command that it does not understand. If the IOCTL command is not supported
  2043. * this function returns -ENOTTY.
  2044. *
  2045. * @dd Pointer to the driver data structure.
  2046. * @cmd IOCTL command passed from the Block Layer.
  2047. * @arg IOCTL argument passed from the Block Layer.
  2048. *
  2049. * return value
  2050. * 0 The IOCTL completed successfully.
  2051. * -ENOTTY The specified command is not supported.
  2052. * -EFAULT An error occurred copying data to a user space buffer.
  2053. * -EIO An error occurred while executing the command.
  2054. */
  2055. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  2056. unsigned long arg)
  2057. {
  2058. switch (cmd) {
  2059. case HDIO_GET_IDENTITY:
  2060. {
  2061. if (copy_to_user((void __user *)arg, dd->port->identify,
  2062. sizeof(u16) * ATA_ID_WORDS))
  2063. return -EFAULT;
  2064. break;
  2065. }
  2066. case HDIO_DRIVE_CMD:
  2067. {
  2068. u8 drive_command[4];
  2069. /* Copy the user command info to our buffer. */
  2070. if (copy_from_user(drive_command,
  2071. (void __user *) arg,
  2072. sizeof(drive_command)))
  2073. return -EFAULT;
  2074. /* Execute the drive command. */
  2075. if (exec_drive_command(dd->port,
  2076. drive_command,
  2077. (void __user *) (arg+4)))
  2078. return -EIO;
  2079. /* Copy the status back to the users buffer. */
  2080. if (copy_to_user((void __user *) arg,
  2081. drive_command,
  2082. sizeof(drive_command)))
  2083. return -EFAULT;
  2084. break;
  2085. }
  2086. case HDIO_DRIVE_TASK:
  2087. {
  2088. u8 drive_command[7];
  2089. /* Copy the user command info to our buffer. */
  2090. if (copy_from_user(drive_command,
  2091. (void __user *) arg,
  2092. sizeof(drive_command)))
  2093. return -EFAULT;
  2094. /* Execute the drive command. */
  2095. if (exec_drive_task(dd->port, drive_command))
  2096. return -EIO;
  2097. /* Copy the status back to the users buffer. */
  2098. if (copy_to_user((void __user *) arg,
  2099. drive_command,
  2100. sizeof(drive_command)))
  2101. return -EFAULT;
  2102. break;
  2103. }
  2104. case HDIO_DRIVE_TASKFILE: {
  2105. ide_task_request_t req_task;
  2106. int ret, outtotal;
  2107. if (copy_from_user(&req_task, (void __user *) arg,
  2108. sizeof(req_task)))
  2109. return -EFAULT;
  2110. outtotal = sizeof(req_task);
  2111. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2112. &req_task, outtotal);
  2113. if (copy_to_user((void __user *) arg, &req_task,
  2114. sizeof(req_task)))
  2115. return -EFAULT;
  2116. return ret;
  2117. }
  2118. default:
  2119. return -EINVAL;
  2120. }
  2121. return 0;
  2122. }
  2123. /*
  2124. * Submit an IO to the hw
  2125. *
  2126. * This function is called by the block layer to issue an io
  2127. * to the device. Upon completion, the callback function will
  2128. * be called with the data parameter passed as the callback data.
  2129. *
  2130. * @dd Pointer to the driver data structure.
  2131. * @start First sector to read.
  2132. * @nsect Number of sectors to read.
  2133. * @nents Number of entries in scatter list for the read command.
  2134. * @tag The tag of this read command.
  2135. * @callback Pointer to the function that should be called
  2136. * when the read completes.
  2137. * @data Callback data passed to the callback function
  2138. * when the read completes.
  2139. * @dir Direction (read or write)
  2140. *
  2141. * return value
  2142. * None
  2143. */
  2144. static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
  2145. int nsect, int nents, int tag, void *callback,
  2146. void *data, int dir)
  2147. {
  2148. struct host_to_dev_fis *fis;
  2149. struct mtip_port *port = dd->port;
  2150. struct mtip_cmd *command = &port->commands[tag];
  2151. int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2152. /* Map the scatter list for DMA access */
  2153. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  2154. command->scatter_ents = nents;
  2155. /*
  2156. * The number of retries for this command before it is
  2157. * reported as a failure to the upper layers.
  2158. */
  2159. command->retries = MTIP_MAX_RETRIES;
  2160. /* Fill out fis */
  2161. fis = command->command;
  2162. fis->type = 0x27;
  2163. fis->opts = 1 << 7;
  2164. fis->command =
  2165. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  2166. *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
  2167. *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
  2168. fis->device = 1 << 6;
  2169. fis->features = nsect & 0xFF;
  2170. fis->features_ex = (nsect >> 8) & 0xFF;
  2171. fis->sect_count = ((tag << 3) | (tag >> 5));
  2172. fis->sect_cnt_ex = 0;
  2173. fis->control = 0;
  2174. fis->res2 = 0;
  2175. fis->res3 = 0;
  2176. fill_command_sg(dd, command, nents);
  2177. /* Populate the command header */
  2178. command->command_header->opts =
  2179. __force_bit2int cpu_to_le32(
  2180. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  2181. command->command_header->byte_count = 0;
  2182. /*
  2183. * Set the completion function and data for the command
  2184. * within this layer.
  2185. */
  2186. command->comp_data = dd;
  2187. command->comp_func = mtip_async_complete;
  2188. command->direction = dma_dir;
  2189. /*
  2190. * Set the completion function and data for the command passed
  2191. * from the upper layer.
  2192. */
  2193. command->async_data = data;
  2194. command->async_callback = callback;
  2195. /*
  2196. * To prevent this command from being issued
  2197. * if an internal command is in progress or error handling is active.
  2198. */
  2199. if (port->flags & MTIP_PF_PAUSE_IO) {
  2200. set_bit(tag, port->cmds_to_issue);
  2201. set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2202. return;
  2203. }
  2204. /* Issue the command to the hardware */
  2205. mtip_issue_ncq_command(port, tag);
  2206. return;
  2207. }
  2208. /*
  2209. * Release a command slot.
  2210. *
  2211. * @dd Pointer to the driver data structure.
  2212. * @tag Slot tag
  2213. *
  2214. * return value
  2215. * None
  2216. */
  2217. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  2218. {
  2219. release_slot(dd->port, tag);
  2220. }
  2221. /*
  2222. * Obtain a command slot and return its associated scatter list.
  2223. *
  2224. * @dd Pointer to the driver data structure.
  2225. * @tag Pointer to an int that will receive the allocated command
  2226. * slot tag.
  2227. *
  2228. * return value
  2229. * Pointer to the scatter list for the allocated command slot
  2230. * or NULL if no command slots are available.
  2231. */
  2232. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  2233. int *tag)
  2234. {
  2235. /*
  2236. * It is possible that, even with this semaphore, a thread
  2237. * may think that no command slots are available. Therefore, we
  2238. * need to make an attempt to get_slot().
  2239. */
  2240. down(&dd->port->cmd_slot);
  2241. *tag = get_slot(dd->port);
  2242. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2243. up(&dd->port->cmd_slot);
  2244. return NULL;
  2245. }
  2246. if (unlikely(*tag < 0)) {
  2247. up(&dd->port->cmd_slot);
  2248. return NULL;
  2249. }
  2250. return dd->port->commands[*tag].sg;
  2251. }
  2252. /*
  2253. * Sysfs status dump.
  2254. *
  2255. * @dev Pointer to the device structure, passed by the kernrel.
  2256. * @attr Pointer to the device_attribute structure passed by the kernel.
  2257. * @buf Pointer to the char buffer that will receive the stats info.
  2258. *
  2259. * return value
  2260. * The size, in bytes, of the data copied into buf.
  2261. */
  2262. static ssize_t mtip_hw_show_status(struct device *dev,
  2263. struct device_attribute *attr,
  2264. char *buf)
  2265. {
  2266. struct driver_data *dd = dev_to_disk(dev)->private_data;
  2267. int size = 0;
  2268. if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
  2269. size += sprintf(buf, "%s", "thermal_shutdown\n");
  2270. else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
  2271. size += sprintf(buf, "%s", "write_protect\n");
  2272. else
  2273. size += sprintf(buf, "%s", "online\n");
  2274. return size;
  2275. }
  2276. static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
  2277. static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
  2278. size_t len, loff_t *offset)
  2279. {
  2280. struct driver_data *dd = (struct driver_data *)f->private_data;
  2281. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2282. u32 group_allocated;
  2283. int size = *offset;
  2284. int n;
  2285. if (!len || size)
  2286. return 0;
  2287. size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
  2288. for (n = dd->slot_groups-1; n >= 0; n--)
  2289. size += sprintf(&buf[size], "%08X ",
  2290. readl(dd->port->s_active[n]));
  2291. size += sprintf(&buf[size], "]\n");
  2292. size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
  2293. for (n = dd->slot_groups-1; n >= 0; n--)
  2294. size += sprintf(&buf[size], "%08X ",
  2295. readl(dd->port->cmd_issue[n]));
  2296. size += sprintf(&buf[size], "]\n");
  2297. size += sprintf(&buf[size], "H/ Completed : [ 0x");
  2298. for (n = dd->slot_groups-1; n >= 0; n--)
  2299. size += sprintf(&buf[size], "%08X ",
  2300. readl(dd->port->completed[n]));
  2301. size += sprintf(&buf[size], "]\n");
  2302. size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
  2303. readl(dd->port->mmio + PORT_IRQ_STAT));
  2304. size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
  2305. readl(dd->mmio + HOST_IRQ_STAT));
  2306. size += sprintf(&buf[size], "\n");
  2307. size += sprintf(&buf[size], "L/ Allocated : [ 0x");
  2308. for (n = dd->slot_groups-1; n >= 0; n--) {
  2309. if (sizeof(long) > sizeof(u32))
  2310. group_allocated =
  2311. dd->port->allocated[n/2] >> (32*(n&1));
  2312. else
  2313. group_allocated = dd->port->allocated[n];
  2314. size += sprintf(&buf[size], "%08X ", group_allocated);
  2315. }
  2316. size += sprintf(&buf[size], "]\n");
  2317. size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
  2318. for (n = dd->slot_groups-1; n >= 0; n--) {
  2319. if (sizeof(long) > sizeof(u32))
  2320. group_allocated =
  2321. dd->port->cmds_to_issue[n/2] >> (32*(n&1));
  2322. else
  2323. group_allocated = dd->port->cmds_to_issue[n];
  2324. size += sprintf(&buf[size], "%08X ", group_allocated);
  2325. }
  2326. size += sprintf(&buf[size], "]\n");
  2327. *offset = size <= len ? size : len;
  2328. size = copy_to_user(ubuf, buf, *offset);
  2329. if (size)
  2330. return -EFAULT;
  2331. return *offset;
  2332. }
  2333. static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
  2334. size_t len, loff_t *offset)
  2335. {
  2336. struct driver_data *dd = (struct driver_data *)f->private_data;
  2337. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2338. int size = *offset;
  2339. if (!len || size)
  2340. return 0;
  2341. size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
  2342. dd->port->flags);
  2343. size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
  2344. dd->dd_flag);
  2345. *offset = size <= len ? size : len;
  2346. size = copy_to_user(ubuf, buf, *offset);
  2347. if (size)
  2348. return -EFAULT;
  2349. return *offset;
  2350. }
  2351. static const struct file_operations mtip_regs_fops = {
  2352. .owner = THIS_MODULE,
  2353. .open = simple_open,
  2354. .read = mtip_hw_read_registers,
  2355. .llseek = no_llseek,
  2356. };
  2357. static const struct file_operations mtip_flags_fops = {
  2358. .owner = THIS_MODULE,
  2359. .open = simple_open,
  2360. .read = mtip_hw_read_flags,
  2361. .llseek = no_llseek,
  2362. };
  2363. /*
  2364. * Create the sysfs related attributes.
  2365. *
  2366. * @dd Pointer to the driver data structure.
  2367. * @kobj Pointer to the kobj for the block device.
  2368. *
  2369. * return value
  2370. * 0 Operation completed successfully.
  2371. * -EINVAL Invalid parameter.
  2372. */
  2373. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2374. {
  2375. if (!kobj || !dd)
  2376. return -EINVAL;
  2377. if (sysfs_create_file(kobj, &dev_attr_status.attr))
  2378. dev_warn(&dd->pdev->dev,
  2379. "Error creating 'status' sysfs entry\n");
  2380. return 0;
  2381. }
  2382. /*
  2383. * Remove the sysfs related attributes.
  2384. *
  2385. * @dd Pointer to the driver data structure.
  2386. * @kobj Pointer to the kobj for the block device.
  2387. *
  2388. * return value
  2389. * 0 Operation completed successfully.
  2390. * -EINVAL Invalid parameter.
  2391. */
  2392. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2393. {
  2394. if (!kobj || !dd)
  2395. return -EINVAL;
  2396. sysfs_remove_file(kobj, &dev_attr_status.attr);
  2397. return 0;
  2398. }
  2399. static int mtip_hw_debugfs_init(struct driver_data *dd)
  2400. {
  2401. if (!dfs_parent)
  2402. return -1;
  2403. dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
  2404. if (IS_ERR_OR_NULL(dd->dfs_node)) {
  2405. dev_warn(&dd->pdev->dev,
  2406. "Error creating node %s under debugfs\n",
  2407. dd->disk->disk_name);
  2408. dd->dfs_node = NULL;
  2409. return -1;
  2410. }
  2411. debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
  2412. &mtip_flags_fops);
  2413. debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
  2414. &mtip_regs_fops);
  2415. return 0;
  2416. }
  2417. static void mtip_hw_debugfs_exit(struct driver_data *dd)
  2418. {
  2419. debugfs_remove_recursive(dd->dfs_node);
  2420. }
  2421. /*
  2422. * Perform any init/resume time hardware setup
  2423. *
  2424. * @dd Pointer to the driver data structure.
  2425. *
  2426. * return value
  2427. * None
  2428. */
  2429. static inline void hba_setup(struct driver_data *dd)
  2430. {
  2431. u32 hwdata;
  2432. hwdata = readl(dd->mmio + HOST_HSORG);
  2433. /* interrupt bug workaround: use only 1 IS bit.*/
  2434. writel(hwdata |
  2435. HSORG_DISABLE_SLOTGRP_INTR |
  2436. HSORG_DISABLE_SLOTGRP_PXIS,
  2437. dd->mmio + HOST_HSORG);
  2438. }
  2439. /*
  2440. * Detect the details of the product, and store anything needed
  2441. * into the driver data structure. This includes product type and
  2442. * version and number of slot groups.
  2443. *
  2444. * @dd Pointer to the driver data structure.
  2445. *
  2446. * return value
  2447. * None
  2448. */
  2449. static void mtip_detect_product(struct driver_data *dd)
  2450. {
  2451. u32 hwdata;
  2452. unsigned int rev, slotgroups;
  2453. /*
  2454. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2455. * info register:
  2456. * [15:8] hardware/software interface rev#
  2457. * [ 3] asic-style interface
  2458. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2459. */
  2460. hwdata = readl(dd->mmio + HOST_HSORG);
  2461. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2462. dd->slot_groups = 1;
  2463. if (hwdata & 0x8) {
  2464. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2465. rev = (hwdata & HSORG_HWREV) >> 8;
  2466. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2467. dev_info(&dd->pdev->dev,
  2468. "ASIC-FPGA design, HS rev 0x%x, "
  2469. "%i slot groups [%i slots]\n",
  2470. rev,
  2471. slotgroups,
  2472. slotgroups * 32);
  2473. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2474. dev_warn(&dd->pdev->dev,
  2475. "Warning: driver only supports "
  2476. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2477. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2478. }
  2479. dd->slot_groups = slotgroups;
  2480. return;
  2481. }
  2482. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2483. }
  2484. /*
  2485. * Blocking wait for FTL rebuild to complete
  2486. *
  2487. * @dd Pointer to the DRIVER_DATA structure.
  2488. *
  2489. * return value
  2490. * 0 FTL rebuild completed successfully
  2491. * -EFAULT FTL rebuild error/timeout/interruption
  2492. */
  2493. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2494. {
  2495. unsigned long timeout, cnt = 0, start;
  2496. dev_warn(&dd->pdev->dev,
  2497. "FTL rebuild in progress. Polling for completion.\n");
  2498. start = jiffies;
  2499. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2500. do {
  2501. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2502. &dd->dd_flag)))
  2503. return -EFAULT;
  2504. if (mtip_check_surprise_removal(dd->pdev))
  2505. return -EFAULT;
  2506. if (mtip_get_identify(dd->port, NULL) < 0)
  2507. return -EFAULT;
  2508. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2509. MTIP_FTL_REBUILD_MAGIC) {
  2510. ssleep(1);
  2511. /* Print message every 3 minutes */
  2512. if (cnt++ >= 180) {
  2513. dev_warn(&dd->pdev->dev,
  2514. "FTL rebuild in progress (%d secs).\n",
  2515. jiffies_to_msecs(jiffies - start) / 1000);
  2516. cnt = 0;
  2517. }
  2518. } else {
  2519. dev_warn(&dd->pdev->dev,
  2520. "FTL rebuild complete (%d secs).\n",
  2521. jiffies_to_msecs(jiffies - start) / 1000);
  2522. mtip_block_initialize(dd);
  2523. return 0;
  2524. }
  2525. ssleep(10);
  2526. } while (time_before(jiffies, timeout));
  2527. /* Check for timeout */
  2528. dev_err(&dd->pdev->dev,
  2529. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2530. jiffies_to_msecs(jiffies - start) / 1000);
  2531. return -EFAULT;
  2532. }
  2533. /*
  2534. * service thread to issue queued commands
  2535. *
  2536. * @data Pointer to the driver data structure.
  2537. *
  2538. * return value
  2539. * 0
  2540. */
  2541. static int mtip_service_thread(void *data)
  2542. {
  2543. struct driver_data *dd = (struct driver_data *)data;
  2544. unsigned long slot, slot_start, slot_wrap;
  2545. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2546. struct mtip_port *port = dd->port;
  2547. while (1) {
  2548. /*
  2549. * the condition is to check neither an internal command is
  2550. * is in progress nor error handling is active
  2551. */
  2552. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2553. !(port->flags & MTIP_PF_PAUSE_IO));
  2554. if (kthread_should_stop())
  2555. break;
  2556. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2557. &dd->dd_flag)))
  2558. break;
  2559. set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2560. if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  2561. slot = 1;
  2562. /* used to restrict the loop to one iteration */
  2563. slot_start = num_cmd_slots;
  2564. slot_wrap = 0;
  2565. while (1) {
  2566. slot = find_next_bit(port->cmds_to_issue,
  2567. num_cmd_slots, slot);
  2568. if (slot_wrap == 1) {
  2569. if ((slot_start >= slot) ||
  2570. (slot >= num_cmd_slots))
  2571. break;
  2572. }
  2573. if (unlikely(slot_start == num_cmd_slots))
  2574. slot_start = slot;
  2575. if (unlikely(slot == num_cmd_slots)) {
  2576. slot = 1;
  2577. slot_wrap = 1;
  2578. continue;
  2579. }
  2580. /* Issue the command to the hardware */
  2581. mtip_issue_ncq_command(port, slot);
  2582. clear_bit(slot, port->cmds_to_issue);
  2583. }
  2584. clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2585. } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
  2586. if (!mtip_ftl_rebuild_poll(dd))
  2587. set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
  2588. &dd->dd_flag);
  2589. clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
  2590. }
  2591. clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2592. if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2593. break;
  2594. }
  2595. return 0;
  2596. }
  2597. /*
  2598. * Called once for each card.
  2599. *
  2600. * @dd Pointer to the driver data structure.
  2601. *
  2602. * return value
  2603. * 0 on success, else an error code.
  2604. */
  2605. static int mtip_hw_init(struct driver_data *dd)
  2606. {
  2607. int i;
  2608. int rv;
  2609. unsigned int num_command_slots;
  2610. unsigned long timeout, timetaken;
  2611. unsigned char *buf;
  2612. struct smart_attr attr242;
  2613. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2614. mtip_detect_product(dd);
  2615. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2616. rv = -EIO;
  2617. goto out1;
  2618. }
  2619. num_command_slots = dd->slot_groups * 32;
  2620. hba_setup(dd);
  2621. tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
  2622. dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
  2623. if (!dd->port) {
  2624. dev_err(&dd->pdev->dev,
  2625. "Memory allocation: port structure\n");
  2626. return -ENOMEM;
  2627. }
  2628. /* Counting semaphore to track command slot usage */
  2629. sema_init(&dd->port->cmd_slot, num_command_slots - 1);
  2630. /* Spinlock to prevent concurrent issue */
  2631. spin_lock_init(&dd->port->cmd_issue_lock);
  2632. /* Set the port mmio base address. */
  2633. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2634. dd->port->dd = dd;
  2635. /* Allocate memory for the command list. */
  2636. dd->port->command_list =
  2637. dmam_alloc_coherent(&dd->pdev->dev,
  2638. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2639. &dd->port->command_list_dma,
  2640. GFP_KERNEL);
  2641. if (!dd->port->command_list) {
  2642. dev_err(&dd->pdev->dev,
  2643. "Memory allocation: command list\n");
  2644. rv = -ENOMEM;
  2645. goto out1;
  2646. }
  2647. /* Clear the memory we have allocated. */
  2648. memset(dd->port->command_list,
  2649. 0,
  2650. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
  2651. /* Setup the addresse of the RX FIS. */
  2652. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2653. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2654. /* Setup the address of the command tables. */
  2655. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2656. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2657. /* Setup the address of the identify data. */
  2658. dd->port->identify = dd->port->command_table +
  2659. HW_CMD_TBL_AR_SZ;
  2660. dd->port->identify_dma = dd->port->command_tbl_dma +
  2661. HW_CMD_TBL_AR_SZ;
  2662. /* Setup the address of the sector buffer - for some non-ncq cmds */
  2663. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2664. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2665. /* Setup the address of the log buf - for read log command */
  2666. dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
  2667. dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
  2668. /* Setup the address of the smart buf - for smart read data command */
  2669. dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
  2670. dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
  2671. /* Point the command headers at the command tables. */
  2672. for (i = 0; i < num_command_slots; i++) {
  2673. dd->port->commands[i].command_header =
  2674. dd->port->command_list +
  2675. (sizeof(struct mtip_cmd_hdr) * i);
  2676. dd->port->commands[i].command_header_dma =
  2677. dd->port->command_list_dma +
  2678. (sizeof(struct mtip_cmd_hdr) * i);
  2679. dd->port->commands[i].command =
  2680. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2681. dd->port->commands[i].command_dma =
  2682. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2683. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2684. dd->port->commands[i].command_header->ctbau =
  2685. __force_bit2int cpu_to_le32(
  2686. (dd->port->commands[i].command_dma >> 16) >> 16);
  2687. dd->port->commands[i].command_header->ctba =
  2688. __force_bit2int cpu_to_le32(
  2689. dd->port->commands[i].command_dma & 0xFFFFFFFF);
  2690. /*
  2691. * If this is not done, a bug is reported by the stock
  2692. * FC11 i386. Due to the fact that it has lots of kernel
  2693. * debugging enabled.
  2694. */
  2695. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  2696. /* Mark all commands as currently inactive.*/
  2697. atomic_set(&dd->port->commands[i].active, 0);
  2698. }
  2699. /* Setup the pointers to the extended s_active and CI registers. */
  2700. for (i = 0; i < dd->slot_groups; i++) {
  2701. dd->port->s_active[i] =
  2702. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2703. dd->port->cmd_issue[i] =
  2704. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2705. dd->port->completed[i] =
  2706. dd->port->mmio + i*0x80 + PORT_SDBV;
  2707. }
  2708. timetaken = jiffies;
  2709. timeout = jiffies + msecs_to_jiffies(30000);
  2710. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  2711. time_before(jiffies, timeout)) {
  2712. mdelay(100);
  2713. }
  2714. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  2715. timetaken = jiffies - timetaken;
  2716. dev_warn(&dd->pdev->dev,
  2717. "Surprise removal detected at %u ms\n",
  2718. jiffies_to_msecs(timetaken));
  2719. rv = -ENODEV;
  2720. goto out2 ;
  2721. }
  2722. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2723. timetaken = jiffies - timetaken;
  2724. dev_warn(&dd->pdev->dev,
  2725. "Removal detected at %u ms\n",
  2726. jiffies_to_msecs(timetaken));
  2727. rv = -EFAULT;
  2728. goto out2;
  2729. }
  2730. /* Conditionally reset the HBA. */
  2731. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  2732. if (mtip_hba_reset(dd) < 0) {
  2733. dev_err(&dd->pdev->dev,
  2734. "Card did not reset within timeout\n");
  2735. rv = -EIO;
  2736. goto out2;
  2737. }
  2738. } else {
  2739. /* Clear any pending interrupts on the HBA */
  2740. writel(readl(dd->mmio + HOST_IRQ_STAT),
  2741. dd->mmio + HOST_IRQ_STAT);
  2742. }
  2743. mtip_init_port(dd->port);
  2744. mtip_start_port(dd->port);
  2745. /* Setup the ISR and enable interrupts. */
  2746. rv = devm_request_irq(&dd->pdev->dev,
  2747. dd->pdev->irq,
  2748. mtip_irq_handler,
  2749. IRQF_SHARED,
  2750. dev_driver_string(&dd->pdev->dev),
  2751. dd);
  2752. if (rv) {
  2753. dev_err(&dd->pdev->dev,
  2754. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2755. goto out2;
  2756. }
  2757. /* Enable interrupts on the HBA. */
  2758. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2759. dd->mmio + HOST_CTL);
  2760. init_timer(&dd->port->cmd_timer);
  2761. init_waitqueue_head(&dd->port->svc_wait);
  2762. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  2763. dd->port->cmd_timer.function = mtip_timeout_function;
  2764. mod_timer(&dd->port->cmd_timer,
  2765. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  2766. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2767. rv = -EFAULT;
  2768. goto out3;
  2769. }
  2770. if (mtip_get_identify(dd->port, NULL) < 0) {
  2771. rv = -EFAULT;
  2772. goto out3;
  2773. }
  2774. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2775. MTIP_FTL_REBUILD_MAGIC) {
  2776. set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
  2777. return MTIP_FTL_REBUILD_MAGIC;
  2778. }
  2779. mtip_dump_identify(dd->port);
  2780. /* check write protect, over temp and rebuild statuses */
  2781. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  2782. dd->port->log_buf,
  2783. dd->port->log_buf_dma, 1);
  2784. if (rv) {
  2785. dev_warn(&dd->pdev->dev,
  2786. "Error in READ LOG EXT (10h) command\n");
  2787. /* non-critical error, don't fail the load */
  2788. } else {
  2789. buf = (unsigned char *)dd->port->log_buf;
  2790. if (buf[259] & 0x1) {
  2791. dev_info(&dd->pdev->dev,
  2792. "Write protect bit is set.\n");
  2793. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  2794. }
  2795. if (buf[288] == 0xF7) {
  2796. dev_info(&dd->pdev->dev,
  2797. "Exceeded Tmax, drive in thermal shutdown.\n");
  2798. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  2799. }
  2800. if (buf[288] == 0xBF) {
  2801. dev_info(&dd->pdev->dev,
  2802. "Drive indicates rebuild has failed.\n");
  2803. /* TODO */
  2804. }
  2805. }
  2806. /* get write protect progess */
  2807. memset(&attr242, 0, sizeof(struct smart_attr));
  2808. if (mtip_get_smart_attr(dd->port, 242, &attr242))
  2809. dev_warn(&dd->pdev->dev,
  2810. "Unable to check write protect progress\n");
  2811. else
  2812. dev_info(&dd->pdev->dev,
  2813. "Write protect progress: %u%% (%u blocks)\n",
  2814. attr242.cur, le32_to_cpu(attr242.data));
  2815. return rv;
  2816. out3:
  2817. del_timer_sync(&dd->port->cmd_timer);
  2818. /* Disable interrupts on the HBA. */
  2819. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2820. dd->mmio + HOST_CTL);
  2821. /*Release the IRQ. */
  2822. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2823. out2:
  2824. mtip_deinit_port(dd->port);
  2825. /* Free the command/command header memory. */
  2826. dmam_free_coherent(&dd->pdev->dev,
  2827. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2828. dd->port->command_list,
  2829. dd->port->command_list_dma);
  2830. out1:
  2831. /* Free the memory allocated for the for structure. */
  2832. kfree(dd->port);
  2833. return rv;
  2834. }
  2835. /*
  2836. * Called to deinitialize an interface.
  2837. *
  2838. * @dd Pointer to the driver data structure.
  2839. *
  2840. * return value
  2841. * 0
  2842. */
  2843. static int mtip_hw_exit(struct driver_data *dd)
  2844. {
  2845. /*
  2846. * Send standby immediate (E0h) to the drive so that it
  2847. * saves its state.
  2848. */
  2849. if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  2850. if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
  2851. if (mtip_standby_immediate(dd->port))
  2852. dev_warn(&dd->pdev->dev,
  2853. "STANDBY IMMEDIATE failed\n");
  2854. /* de-initialize the port. */
  2855. mtip_deinit_port(dd->port);
  2856. /* Disable interrupts on the HBA. */
  2857. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2858. dd->mmio + HOST_CTL);
  2859. }
  2860. del_timer_sync(&dd->port->cmd_timer);
  2861. /* Release the IRQ. */
  2862. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2863. /* Stop the bottom half tasklet. */
  2864. tasklet_kill(&dd->tasklet);
  2865. /* Free the command/command header memory. */
  2866. dmam_free_coherent(&dd->pdev->dev,
  2867. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2868. dd->port->command_list,
  2869. dd->port->command_list_dma);
  2870. /* Free the memory allocated for the for structure. */
  2871. kfree(dd->port);
  2872. return 0;
  2873. }
  2874. /*
  2875. * Issue a Standby Immediate command to the device.
  2876. *
  2877. * This function is called by the Block Layer just before the
  2878. * system powers off during a shutdown.
  2879. *
  2880. * @dd Pointer to the driver data structure.
  2881. *
  2882. * return value
  2883. * 0
  2884. */
  2885. static int mtip_hw_shutdown(struct driver_data *dd)
  2886. {
  2887. /*
  2888. * Send standby immediate (E0h) to the drive so that it
  2889. * saves its state.
  2890. */
  2891. mtip_standby_immediate(dd->port);
  2892. return 0;
  2893. }
  2894. /*
  2895. * Suspend function
  2896. *
  2897. * This function is called by the Block Layer just before the
  2898. * system hibernates.
  2899. *
  2900. * @dd Pointer to the driver data structure.
  2901. *
  2902. * return value
  2903. * 0 Suspend was successful
  2904. * -EFAULT Suspend was not successful
  2905. */
  2906. static int mtip_hw_suspend(struct driver_data *dd)
  2907. {
  2908. /*
  2909. * Send standby immediate (E0h) to the drive
  2910. * so that it saves its state.
  2911. */
  2912. if (mtip_standby_immediate(dd->port) != 0) {
  2913. dev_err(&dd->pdev->dev,
  2914. "Failed standby-immediate command\n");
  2915. return -EFAULT;
  2916. }
  2917. /* Disable interrupts on the HBA.*/
  2918. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2919. dd->mmio + HOST_CTL);
  2920. mtip_deinit_port(dd->port);
  2921. return 0;
  2922. }
  2923. /*
  2924. * Resume function
  2925. *
  2926. * This function is called by the Block Layer as the
  2927. * system resumes.
  2928. *
  2929. * @dd Pointer to the driver data structure.
  2930. *
  2931. * return value
  2932. * 0 Resume was successful
  2933. * -EFAULT Resume was not successful
  2934. */
  2935. static int mtip_hw_resume(struct driver_data *dd)
  2936. {
  2937. /* Perform any needed hardware setup steps */
  2938. hba_setup(dd);
  2939. /* Reset the HBA */
  2940. if (mtip_hba_reset(dd) != 0) {
  2941. dev_err(&dd->pdev->dev,
  2942. "Unable to reset the HBA\n");
  2943. return -EFAULT;
  2944. }
  2945. /*
  2946. * Enable the port, DMA engine, and FIS reception specific
  2947. * h/w in controller.
  2948. */
  2949. mtip_init_port(dd->port);
  2950. mtip_start_port(dd->port);
  2951. /* Enable interrupts on the HBA.*/
  2952. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2953. dd->mmio + HOST_CTL);
  2954. return 0;
  2955. }
  2956. /*
  2957. * Helper function for reusing disk name
  2958. * upon hot insertion.
  2959. */
  2960. static int rssd_disk_name_format(char *prefix,
  2961. int index,
  2962. char *buf,
  2963. int buflen)
  2964. {
  2965. const int base = 'z' - 'a' + 1;
  2966. char *begin = buf + strlen(prefix);
  2967. char *end = buf + buflen;
  2968. char *p;
  2969. int unit;
  2970. p = end - 1;
  2971. *p = '\0';
  2972. unit = base;
  2973. do {
  2974. if (p == begin)
  2975. return -EINVAL;
  2976. *--p = 'a' + (index % unit);
  2977. index = (index / unit) - 1;
  2978. } while (index >= 0);
  2979. memmove(begin, p, end - p);
  2980. memcpy(buf, prefix, strlen(prefix));
  2981. return 0;
  2982. }
  2983. /*
  2984. * Block layer IOCTL handler.
  2985. *
  2986. * @dev Pointer to the block_device structure.
  2987. * @mode ignored
  2988. * @cmd IOCTL command passed from the user application.
  2989. * @arg Argument passed from the user application.
  2990. *
  2991. * return value
  2992. * 0 IOCTL completed successfully.
  2993. * -ENOTTY IOCTL not supported or invalid driver data
  2994. * structure pointer.
  2995. */
  2996. static int mtip_block_ioctl(struct block_device *dev,
  2997. fmode_t mode,
  2998. unsigned cmd,
  2999. unsigned long arg)
  3000. {
  3001. struct driver_data *dd = dev->bd_disk->private_data;
  3002. if (!capable(CAP_SYS_ADMIN))
  3003. return -EACCES;
  3004. if (!dd)
  3005. return -ENOTTY;
  3006. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3007. return -ENOTTY;
  3008. switch (cmd) {
  3009. case BLKFLSBUF:
  3010. return -ENOTTY;
  3011. default:
  3012. return mtip_hw_ioctl(dd, cmd, arg);
  3013. }
  3014. }
  3015. #ifdef CONFIG_COMPAT
  3016. /*
  3017. * Block layer compat IOCTL handler.
  3018. *
  3019. * @dev Pointer to the block_device structure.
  3020. * @mode ignored
  3021. * @cmd IOCTL command passed from the user application.
  3022. * @arg Argument passed from the user application.
  3023. *
  3024. * return value
  3025. * 0 IOCTL completed successfully.
  3026. * -ENOTTY IOCTL not supported or invalid driver data
  3027. * structure pointer.
  3028. */
  3029. static int mtip_block_compat_ioctl(struct block_device *dev,
  3030. fmode_t mode,
  3031. unsigned cmd,
  3032. unsigned long arg)
  3033. {
  3034. struct driver_data *dd = dev->bd_disk->private_data;
  3035. if (!capable(CAP_SYS_ADMIN))
  3036. return -EACCES;
  3037. if (!dd)
  3038. return -ENOTTY;
  3039. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3040. return -ENOTTY;
  3041. switch (cmd) {
  3042. case BLKFLSBUF:
  3043. return -ENOTTY;
  3044. case HDIO_DRIVE_TASKFILE: {
  3045. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  3046. ide_task_request_t req_task;
  3047. int compat_tasksize, outtotal, ret;
  3048. compat_tasksize =
  3049. sizeof(struct mtip_compat_ide_task_request_s);
  3050. compat_req_task =
  3051. (struct mtip_compat_ide_task_request_s __user *) arg;
  3052. if (copy_from_user(&req_task, (void __user *) arg,
  3053. compat_tasksize - (2 * sizeof(compat_long_t))))
  3054. return -EFAULT;
  3055. if (get_user(req_task.out_size, &compat_req_task->out_size))
  3056. return -EFAULT;
  3057. if (get_user(req_task.in_size, &compat_req_task->in_size))
  3058. return -EFAULT;
  3059. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  3060. ret = exec_drive_taskfile(dd, (void __user *) arg,
  3061. &req_task, outtotal);
  3062. if (copy_to_user((void __user *) arg, &req_task,
  3063. compat_tasksize -
  3064. (2 * sizeof(compat_long_t))))
  3065. return -EFAULT;
  3066. if (put_user(req_task.out_size, &compat_req_task->out_size))
  3067. return -EFAULT;
  3068. if (put_user(req_task.in_size, &compat_req_task->in_size))
  3069. return -EFAULT;
  3070. return ret;
  3071. }
  3072. default:
  3073. return mtip_hw_ioctl(dd, cmd, arg);
  3074. }
  3075. }
  3076. #endif
  3077. /*
  3078. * Obtain the geometry of the device.
  3079. *
  3080. * You may think that this function is obsolete, but some applications,
  3081. * fdisk for example still used CHS values. This function describes the
  3082. * device as having 224 heads and 56 sectors per cylinder. These values are
  3083. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  3084. * partition is described in terms of a start and end cylinder this means
  3085. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  3086. * affects performance.
  3087. *
  3088. * @dev Pointer to the block_device strucutre.
  3089. * @geo Pointer to a hd_geometry structure.
  3090. *
  3091. * return value
  3092. * 0 Operation completed successfully.
  3093. * -ENOTTY An error occurred while reading the drive capacity.
  3094. */
  3095. static int mtip_block_getgeo(struct block_device *dev,
  3096. struct hd_geometry *geo)
  3097. {
  3098. struct driver_data *dd = dev->bd_disk->private_data;
  3099. sector_t capacity;
  3100. if (!dd)
  3101. return -ENOTTY;
  3102. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3103. dev_warn(&dd->pdev->dev,
  3104. "Could not get drive capacity.\n");
  3105. return -ENOTTY;
  3106. }
  3107. geo->heads = 224;
  3108. geo->sectors = 56;
  3109. sector_div(capacity, (geo->heads * geo->sectors));
  3110. geo->cylinders = capacity;
  3111. return 0;
  3112. }
  3113. /*
  3114. * Block device operation function.
  3115. *
  3116. * This structure contains pointers to the functions required by the block
  3117. * layer.
  3118. */
  3119. static const struct block_device_operations mtip_block_ops = {
  3120. .ioctl = mtip_block_ioctl,
  3121. #ifdef CONFIG_COMPAT
  3122. .compat_ioctl = mtip_block_compat_ioctl,
  3123. #endif
  3124. .getgeo = mtip_block_getgeo,
  3125. .owner = THIS_MODULE
  3126. };
  3127. /*
  3128. * Block layer make request function.
  3129. *
  3130. * This function is called by the kernel to process a BIO for
  3131. * the P320 device.
  3132. *
  3133. * @queue Pointer to the request queue. Unused other than to obtain
  3134. * the driver data structure.
  3135. * @bio Pointer to the BIO.
  3136. *
  3137. */
  3138. static void mtip_make_request(struct request_queue *queue, struct bio *bio)
  3139. {
  3140. struct driver_data *dd = queue->queuedata;
  3141. struct scatterlist *sg;
  3142. struct bio_vec *bvec;
  3143. int nents = 0;
  3144. int tag = 0;
  3145. if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
  3146. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  3147. &dd->dd_flag))) {
  3148. bio_endio(bio, -ENXIO);
  3149. return;
  3150. }
  3151. if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
  3152. bio_endio(bio, -ENODATA);
  3153. return;
  3154. }
  3155. if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
  3156. &dd->dd_flag) &&
  3157. bio_data_dir(bio))) {
  3158. bio_endio(bio, -ENODATA);
  3159. return;
  3160. }
  3161. if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
  3162. bio_endio(bio, -ENODATA);
  3163. return;
  3164. }
  3165. }
  3166. if (unlikely(!bio_has_data(bio))) {
  3167. blk_queue_flush(queue, 0);
  3168. bio_endio(bio, 0);
  3169. return;
  3170. }
  3171. sg = mtip_hw_get_scatterlist(dd, &tag);
  3172. if (likely(sg != NULL)) {
  3173. blk_queue_bounce(queue, &bio);
  3174. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  3175. dev_warn(&dd->pdev->dev,
  3176. "Maximum number of SGL entries exceeded\n");
  3177. bio_io_error(bio);
  3178. mtip_hw_release_scatterlist(dd, tag);
  3179. return;
  3180. }
  3181. /* Create the scatter list for this bio. */
  3182. bio_for_each_segment(bvec, bio, nents) {
  3183. sg_set_page(&sg[nents],
  3184. bvec->bv_page,
  3185. bvec->bv_len,
  3186. bvec->bv_offset);
  3187. }
  3188. /* Issue the read/write. */
  3189. mtip_hw_submit_io(dd,
  3190. bio->bi_sector,
  3191. bio_sectors(bio),
  3192. nents,
  3193. tag,
  3194. bio_endio,
  3195. bio,
  3196. bio_data_dir(bio));
  3197. } else
  3198. bio_io_error(bio);
  3199. }
  3200. /*
  3201. * Block layer initialization function.
  3202. *
  3203. * This function is called once by the PCI layer for each P320
  3204. * device that is connected to the system.
  3205. *
  3206. * @dd Pointer to the driver data structure.
  3207. *
  3208. * return value
  3209. * 0 on success else an error code.
  3210. */
  3211. static int mtip_block_initialize(struct driver_data *dd)
  3212. {
  3213. int rv = 0, wait_for_rebuild = 0;
  3214. sector_t capacity;
  3215. unsigned int index = 0;
  3216. struct kobject *kobj;
  3217. unsigned char thd_name[16];
  3218. if (dd->disk)
  3219. goto skip_create_disk; /* hw init done, before rebuild */
  3220. /* Initialize the protocol layer. */
  3221. wait_for_rebuild = mtip_hw_init(dd);
  3222. if (wait_for_rebuild < 0) {
  3223. dev_err(&dd->pdev->dev,
  3224. "Protocol layer initialization failed\n");
  3225. rv = -EINVAL;
  3226. goto protocol_init_error;
  3227. }
  3228. dd->disk = alloc_disk(MTIP_MAX_MINORS);
  3229. if (dd->disk == NULL) {
  3230. dev_err(&dd->pdev->dev,
  3231. "Unable to allocate gendisk structure\n");
  3232. rv = -EINVAL;
  3233. goto alloc_disk_error;
  3234. }
  3235. /* Generate the disk name, implemented same as in sd.c */
  3236. do {
  3237. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  3238. goto ida_get_error;
  3239. spin_lock(&rssd_index_lock);
  3240. rv = ida_get_new(&rssd_index_ida, &index);
  3241. spin_unlock(&rssd_index_lock);
  3242. } while (rv == -EAGAIN);
  3243. if (rv)
  3244. goto ida_get_error;
  3245. rv = rssd_disk_name_format("rssd",
  3246. index,
  3247. dd->disk->disk_name,
  3248. DISK_NAME_LEN);
  3249. if (rv)
  3250. goto disk_index_error;
  3251. dd->disk->driverfs_dev = &dd->pdev->dev;
  3252. dd->disk->major = dd->major;
  3253. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  3254. dd->disk->fops = &mtip_block_ops;
  3255. dd->disk->private_data = dd;
  3256. dd->index = index;
  3257. /*
  3258. * if rebuild pending, start the service thread, and delay the block
  3259. * queue creation and add_disk()
  3260. */
  3261. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3262. goto start_service_thread;
  3263. skip_create_disk:
  3264. /* Allocate the request queue. */
  3265. dd->queue = blk_alloc_queue(GFP_KERNEL);
  3266. if (dd->queue == NULL) {
  3267. dev_err(&dd->pdev->dev,
  3268. "Unable to allocate request queue\n");
  3269. rv = -ENOMEM;
  3270. goto block_queue_alloc_init_error;
  3271. }
  3272. /* Attach our request function to the request queue. */
  3273. blk_queue_make_request(dd->queue, mtip_make_request);
  3274. dd->disk->queue = dd->queue;
  3275. dd->queue->queuedata = dd;
  3276. /* Set device limits. */
  3277. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  3278. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  3279. blk_queue_physical_block_size(dd->queue, 4096);
  3280. blk_queue_max_hw_sectors(dd->queue, 0xffff);
  3281. blk_queue_max_segment_size(dd->queue, 0x400000);
  3282. blk_queue_io_min(dd->queue, 4096);
  3283. /*
  3284. * write back cache is not supported in the device. FUA depends on
  3285. * write back cache support, hence setting flush support to zero.
  3286. */
  3287. blk_queue_flush(dd->queue, 0);
  3288. /* Set the capacity of the device in 512 byte sectors. */
  3289. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3290. dev_warn(&dd->pdev->dev,
  3291. "Could not read drive capacity\n");
  3292. rv = -EIO;
  3293. goto read_capacity_error;
  3294. }
  3295. set_capacity(dd->disk, capacity);
  3296. /* Enable the block device and add it to /dev */
  3297. add_disk(dd->disk);
  3298. /*
  3299. * Now that the disk is active, initialize any sysfs attributes
  3300. * managed by the protocol layer.
  3301. */
  3302. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3303. if (kobj) {
  3304. mtip_hw_sysfs_init(dd, kobj);
  3305. kobject_put(kobj);
  3306. }
  3307. mtip_hw_debugfs_init(dd);
  3308. if (dd->mtip_svc_handler) {
  3309. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3310. return rv; /* service thread created for handling rebuild */
  3311. }
  3312. start_service_thread:
  3313. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  3314. dd->mtip_svc_handler = kthread_run(mtip_service_thread,
  3315. dd, thd_name);
  3316. if (IS_ERR(dd->mtip_svc_handler)) {
  3317. dev_err(&dd->pdev->dev, "service thread failed to start\n");
  3318. dd->mtip_svc_handler = NULL;
  3319. rv = -EFAULT;
  3320. goto kthread_run_error;
  3321. }
  3322. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3323. rv = wait_for_rebuild;
  3324. return rv;
  3325. kthread_run_error:
  3326. mtip_hw_debugfs_exit(dd);
  3327. /* Delete our gendisk. This also removes the device from /dev */
  3328. del_gendisk(dd->disk);
  3329. read_capacity_error:
  3330. blk_cleanup_queue(dd->queue);
  3331. block_queue_alloc_init_error:
  3332. disk_index_error:
  3333. spin_lock(&rssd_index_lock);
  3334. ida_remove(&rssd_index_ida, index);
  3335. spin_unlock(&rssd_index_lock);
  3336. ida_get_error:
  3337. put_disk(dd->disk);
  3338. alloc_disk_error:
  3339. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  3340. protocol_init_error:
  3341. return rv;
  3342. }
  3343. /*
  3344. * Block layer deinitialization function.
  3345. *
  3346. * Called by the PCI layer as each P320 device is removed.
  3347. *
  3348. * @dd Pointer to the driver data structure.
  3349. *
  3350. * return value
  3351. * 0
  3352. */
  3353. static int mtip_block_remove(struct driver_data *dd)
  3354. {
  3355. struct kobject *kobj;
  3356. if (dd->mtip_svc_handler) {
  3357. set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
  3358. wake_up_interruptible(&dd->port->svc_wait);
  3359. kthread_stop(dd->mtip_svc_handler);
  3360. }
  3361. /* Clean up the sysfs attributes, if created */
  3362. if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
  3363. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3364. if (kobj) {
  3365. mtip_hw_sysfs_exit(dd, kobj);
  3366. kobject_put(kobj);
  3367. }
  3368. }
  3369. mtip_hw_debugfs_exit(dd);
  3370. /*
  3371. * Delete our gendisk structure. This also removes the device
  3372. * from /dev
  3373. */
  3374. del_gendisk(dd->disk);
  3375. spin_lock(&rssd_index_lock);
  3376. ida_remove(&rssd_index_ida, dd->index);
  3377. spin_unlock(&rssd_index_lock);
  3378. blk_cleanup_queue(dd->queue);
  3379. dd->disk = NULL;
  3380. dd->queue = NULL;
  3381. /* De-initialize the protocol layer. */
  3382. mtip_hw_exit(dd);
  3383. return 0;
  3384. }
  3385. /*
  3386. * Function called by the PCI layer when just before the
  3387. * machine shuts down.
  3388. *
  3389. * If a protocol layer shutdown function is present it will be called
  3390. * by this function.
  3391. *
  3392. * @dd Pointer to the driver data structure.
  3393. *
  3394. * return value
  3395. * 0
  3396. */
  3397. static int mtip_block_shutdown(struct driver_data *dd)
  3398. {
  3399. dev_info(&dd->pdev->dev,
  3400. "Shutting down %s ...\n", dd->disk->disk_name);
  3401. /* Delete our gendisk structure, and cleanup the blk queue. */
  3402. del_gendisk(dd->disk);
  3403. spin_lock(&rssd_index_lock);
  3404. ida_remove(&rssd_index_ida, dd->index);
  3405. spin_unlock(&rssd_index_lock);
  3406. blk_cleanup_queue(dd->queue);
  3407. dd->disk = NULL;
  3408. dd->queue = NULL;
  3409. mtip_hw_shutdown(dd);
  3410. return 0;
  3411. }
  3412. static int mtip_block_suspend(struct driver_data *dd)
  3413. {
  3414. dev_info(&dd->pdev->dev,
  3415. "Suspending %s ...\n", dd->disk->disk_name);
  3416. mtip_hw_suspend(dd);
  3417. return 0;
  3418. }
  3419. static int mtip_block_resume(struct driver_data *dd)
  3420. {
  3421. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  3422. dd->disk->disk_name);
  3423. mtip_hw_resume(dd);
  3424. return 0;
  3425. }
  3426. /*
  3427. * Called for each supported PCI device detected.
  3428. *
  3429. * This function allocates the private data structure, enables the
  3430. * PCI device and then calls the block layer initialization function.
  3431. *
  3432. * return value
  3433. * 0 on success else an error code.
  3434. */
  3435. static int mtip_pci_probe(struct pci_dev *pdev,
  3436. const struct pci_device_id *ent)
  3437. {
  3438. int rv = 0;
  3439. struct driver_data *dd = NULL;
  3440. /* Allocate memory for this devices private data. */
  3441. dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
  3442. if (dd == NULL) {
  3443. dev_err(&pdev->dev,
  3444. "Unable to allocate memory for driver data\n");
  3445. return -ENOMEM;
  3446. }
  3447. /* Attach the private data to this PCI device. */
  3448. pci_set_drvdata(pdev, dd);
  3449. rv = pcim_enable_device(pdev);
  3450. if (rv < 0) {
  3451. dev_err(&pdev->dev, "Unable to enable device\n");
  3452. goto iomap_err;
  3453. }
  3454. /* Map BAR5 to memory. */
  3455. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3456. if (rv < 0) {
  3457. dev_err(&pdev->dev, "Unable to map regions\n");
  3458. goto iomap_err;
  3459. }
  3460. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3461. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3462. if (rv) {
  3463. rv = pci_set_consistent_dma_mask(pdev,
  3464. DMA_BIT_MASK(32));
  3465. if (rv) {
  3466. dev_warn(&pdev->dev,
  3467. "64-bit DMA enable failed\n");
  3468. goto setmask_err;
  3469. }
  3470. }
  3471. }
  3472. pci_set_master(pdev);
  3473. if (pci_enable_msi(pdev)) {
  3474. dev_warn(&pdev->dev,
  3475. "Unable to enable MSI interrupt.\n");
  3476. goto block_initialize_err;
  3477. }
  3478. /* Copy the info we may need later into the private data structure. */
  3479. dd->major = mtip_major;
  3480. dd->instance = instance;
  3481. dd->pdev = pdev;
  3482. /* Initialize the block layer. */
  3483. rv = mtip_block_initialize(dd);
  3484. if (rv < 0) {
  3485. dev_err(&pdev->dev,
  3486. "Unable to initialize block layer\n");
  3487. goto block_initialize_err;
  3488. }
  3489. /*
  3490. * Increment the instance count so that each device has a unique
  3491. * instance number.
  3492. */
  3493. instance++;
  3494. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3495. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3496. goto done;
  3497. block_initialize_err:
  3498. pci_disable_msi(pdev);
  3499. setmask_err:
  3500. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3501. iomap_err:
  3502. kfree(dd);
  3503. pci_set_drvdata(pdev, NULL);
  3504. return rv;
  3505. done:
  3506. return rv;
  3507. }
  3508. /*
  3509. * Called for each probed device when the device is removed or the
  3510. * driver is unloaded.
  3511. *
  3512. * return value
  3513. * None
  3514. */
  3515. static void mtip_pci_remove(struct pci_dev *pdev)
  3516. {
  3517. struct driver_data *dd = pci_get_drvdata(pdev);
  3518. int counter = 0;
  3519. set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
  3520. if (mtip_check_surprise_removal(pdev)) {
  3521. while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  3522. counter++;
  3523. msleep(20);
  3524. if (counter == 10) {
  3525. /* Cleanup the outstanding commands */
  3526. mtip_command_cleanup(dd);
  3527. break;
  3528. }
  3529. }
  3530. }
  3531. /* Clean up the block layer. */
  3532. mtip_block_remove(dd);
  3533. pci_disable_msi(pdev);
  3534. kfree(dd);
  3535. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3536. }
  3537. /*
  3538. * Called for each probed device when the device is suspended.
  3539. *
  3540. * return value
  3541. * 0 Success
  3542. * <0 Error
  3543. */
  3544. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  3545. {
  3546. int rv = 0;
  3547. struct driver_data *dd = pci_get_drvdata(pdev);
  3548. if (!dd) {
  3549. dev_err(&pdev->dev,
  3550. "Driver private datastructure is NULL\n");
  3551. return -EFAULT;
  3552. }
  3553. set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3554. /* Disable ports & interrupts then send standby immediate */
  3555. rv = mtip_block_suspend(dd);
  3556. if (rv < 0) {
  3557. dev_err(&pdev->dev,
  3558. "Failed to suspend controller\n");
  3559. return rv;
  3560. }
  3561. /*
  3562. * Save the pci config space to pdev structure &
  3563. * disable the device
  3564. */
  3565. pci_save_state(pdev);
  3566. pci_disable_device(pdev);
  3567. /* Move to Low power state*/
  3568. pci_set_power_state(pdev, PCI_D3hot);
  3569. return rv;
  3570. }
  3571. /*
  3572. * Called for each probed device when the device is resumed.
  3573. *
  3574. * return value
  3575. * 0 Success
  3576. * <0 Error
  3577. */
  3578. static int mtip_pci_resume(struct pci_dev *pdev)
  3579. {
  3580. int rv = 0;
  3581. struct driver_data *dd;
  3582. dd = pci_get_drvdata(pdev);
  3583. if (!dd) {
  3584. dev_err(&pdev->dev,
  3585. "Driver private datastructure is NULL\n");
  3586. return -EFAULT;
  3587. }
  3588. /* Move the device to active State */
  3589. pci_set_power_state(pdev, PCI_D0);
  3590. /* Restore PCI configuration space */
  3591. pci_restore_state(pdev);
  3592. /* Enable the PCI device*/
  3593. rv = pcim_enable_device(pdev);
  3594. if (rv < 0) {
  3595. dev_err(&pdev->dev,
  3596. "Failed to enable card during resume\n");
  3597. goto err;
  3598. }
  3599. pci_set_master(pdev);
  3600. /*
  3601. * Calls hbaReset, initPort, & startPort function
  3602. * then enables interrupts
  3603. */
  3604. rv = mtip_block_resume(dd);
  3605. if (rv < 0)
  3606. dev_err(&pdev->dev, "Unable to resume\n");
  3607. err:
  3608. clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3609. return rv;
  3610. }
  3611. /*
  3612. * Shutdown routine
  3613. *
  3614. * return value
  3615. * None
  3616. */
  3617. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3618. {
  3619. struct driver_data *dd = pci_get_drvdata(pdev);
  3620. if (dd)
  3621. mtip_block_shutdown(dd);
  3622. }
  3623. /* Table of device ids supported by this driver. */
  3624. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  3625. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
  3626. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
  3627. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
  3628. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
  3629. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
  3630. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
  3631. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
  3632. { 0 }
  3633. };
  3634. /* Structure that describes the PCI driver functions. */
  3635. static struct pci_driver mtip_pci_driver = {
  3636. .name = MTIP_DRV_NAME,
  3637. .id_table = mtip_pci_tbl,
  3638. .probe = mtip_pci_probe,
  3639. .remove = mtip_pci_remove,
  3640. .suspend = mtip_pci_suspend,
  3641. .resume = mtip_pci_resume,
  3642. .shutdown = mtip_pci_shutdown,
  3643. };
  3644. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3645. /*
  3646. * Module initialization function.
  3647. *
  3648. * Called once when the module is loaded. This function allocates a major
  3649. * block device number to the Cyclone devices and registers the PCI layer
  3650. * of the driver.
  3651. *
  3652. * Return value
  3653. * 0 on success else error code.
  3654. */
  3655. static int __init mtip_init(void)
  3656. {
  3657. int error;
  3658. pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3659. /* Allocate a major block device number to use with this driver. */
  3660. error = register_blkdev(0, MTIP_DRV_NAME);
  3661. if (error <= 0) {
  3662. pr_err("Unable to register block device (%d)\n",
  3663. error);
  3664. return -EBUSY;
  3665. }
  3666. mtip_major = error;
  3667. if (!dfs_parent) {
  3668. dfs_parent = debugfs_create_dir("rssd", NULL);
  3669. if (IS_ERR_OR_NULL(dfs_parent)) {
  3670. pr_warn("Error creating debugfs parent\n");
  3671. dfs_parent = NULL;
  3672. }
  3673. }
  3674. /* Register our PCI operations. */
  3675. error = pci_register_driver(&mtip_pci_driver);
  3676. if (error) {
  3677. debugfs_remove(dfs_parent);
  3678. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3679. }
  3680. return error;
  3681. }
  3682. /*
  3683. * Module de-initialization function.
  3684. *
  3685. * Called once when the module is unloaded. This function deallocates
  3686. * the major block device number allocated by mtip_init() and
  3687. * unregisters the PCI layer of the driver.
  3688. *
  3689. * Return value
  3690. * none
  3691. */
  3692. static void __exit mtip_exit(void)
  3693. {
  3694. debugfs_remove_recursive(dfs_parent);
  3695. /* Release the allocated major block device number. */
  3696. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3697. /* Unregister the PCI driver. */
  3698. pci_unregister_driver(&mtip_pci_driver);
  3699. }
  3700. MODULE_AUTHOR("Micron Technology, Inc");
  3701. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  3702. MODULE_LICENSE("GPL");
  3703. MODULE_VERSION(MTIP_DRV_VERSION);
  3704. module_init(mtip_init);
  3705. module_exit(mtip_exit);