mtip32xx.c 86 KB

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