mtip32xx.c 115 KB

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