mtip32xx.c 111 KB

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