mtip32xx.c 98 KB

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