megaraid.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright © 2002 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  13. * - fixes
  14. * - speed-ups (list handling fixes, issued_list, optimizations.)
  15. * - lots of cleanups.
  16. *
  17. * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
  18. * - new-style, hotplug-aware pci probing and scsi registration
  19. *
  20. * Version : v2.00.3 (Feb 19, 2003) - Atul Mukker <Atul.Mukker@lsil.com>
  21. *
  22. * Description: Linux device driver for LSI Logic MegaRAID controller
  23. *
  24. * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
  25. * 518, 520, 531, 532
  26. *
  27. * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
  28. * and others. Please send updates to the mailing list
  29. * linux-scsi@vger.kernel.org .
  30. *
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/fs.h>
  34. #include <linux/blkdev.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/io.h>
  37. #include <linux/completion.h>
  38. #include <linux/delay.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/reboot.h>
  41. #include <linux/module.h>
  42. #include <linux/list.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/pci.h>
  45. #include <linux/init.h>
  46. #include <scsi/scsicam.h>
  47. #include "scsi.h"
  48. #include <scsi/scsi_host.h>
  49. #include "megaraid.h"
  50. #define MEGARAID_MODULE_VERSION "2.00.3"
  51. MODULE_AUTHOR ("LSI Logic Corporation");
  52. MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
  53. MODULE_LICENSE ("GPL");
  54. MODULE_VERSION(MEGARAID_MODULE_VERSION);
  55. static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  56. module_param(max_cmd_per_lun, uint, 0);
  57. MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  58. static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  59. module_param(max_sectors_per_io, ushort, 0);
  60. MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  61. static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  62. module_param(max_mbox_busy_wait, ushort, 0);
  63. MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
  64. #define RDINDOOR(adapter) readl((adapter)->base + 0x20)
  65. #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
  66. #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
  67. #define WROUTDOOR(adapter,value) writel(value, (adapter)->base + 0x2C)
  68. /*
  69. * Global variables
  70. */
  71. static int hba_count;
  72. static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  73. static struct proc_dir_entry *mega_proc_dir_entry;
  74. /* For controller re-ordering */
  75. static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  76. /*
  77. * The File Operations structure for the serial/ioctl interface of the driver
  78. */
  79. static struct file_operations megadev_fops = {
  80. .owner = THIS_MODULE,
  81. .ioctl = megadev_ioctl,
  82. .open = megadev_open,
  83. };
  84. /*
  85. * Array to structures for storing the information about the controllers. This
  86. * information is sent to the user level applications, when they do an ioctl
  87. * for this information.
  88. */
  89. static struct mcontroller mcontroller[MAX_CONTROLLERS];
  90. /* The current driver version */
  91. static u32 driver_ver = 0x02000000;
  92. /* major number used by the device for character interface */
  93. static int major;
  94. #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
  95. /*
  96. * Debug variable to print some diagnostic messages
  97. */
  98. static int trace_level;
  99. /**
  100. * mega_setup_mailbox()
  101. * @adapter - pointer to our soft state
  102. *
  103. * Allocates a 8 byte aligned memory for the handshake mailbox.
  104. */
  105. static int
  106. mega_setup_mailbox(adapter_t *adapter)
  107. {
  108. unsigned long align;
  109. adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
  110. sizeof(mbox64_t), &adapter->una_mbox64_dma);
  111. if( !adapter->una_mbox64 ) return -1;
  112. adapter->mbox = &adapter->una_mbox64->mbox;
  113. adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
  114. (~0UL ^ 0xFUL));
  115. adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
  116. align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
  117. adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
  118. /*
  119. * Register the mailbox if the controller is an io-mapped controller
  120. */
  121. if( adapter->flag & BOARD_IOMAP ) {
  122. outb_p(adapter->mbox_dma & 0xFF,
  123. adapter->host->io_port + MBOX_PORT0);
  124. outb_p((adapter->mbox_dma >> 8) & 0xFF,
  125. adapter->host->io_port + MBOX_PORT1);
  126. outb_p((adapter->mbox_dma >> 16) & 0xFF,
  127. adapter->host->io_port + MBOX_PORT2);
  128. outb_p((adapter->mbox_dma >> 24) & 0xFF,
  129. adapter->host->io_port + MBOX_PORT3);
  130. outb_p(ENABLE_MBOX_BYTE,
  131. adapter->host->io_port + ENABLE_MBOX_REGION);
  132. irq_ack(adapter);
  133. irq_enable(adapter);
  134. }
  135. return 0;
  136. }
  137. /*
  138. * mega_query_adapter()
  139. * @adapter - pointer to our soft state
  140. *
  141. * Issue the adapter inquiry commands to the controller and find out
  142. * information and parameter about the devices attached
  143. */
  144. static int
  145. mega_query_adapter(adapter_t *adapter)
  146. {
  147. dma_addr_t prod_info_dma_handle;
  148. mega_inquiry3 *inquiry3;
  149. u8 raw_mbox[sizeof(struct mbox_out)];
  150. mbox_t *mbox;
  151. int retval;
  152. /* Initialize adapter inquiry mailbox */
  153. mbox = (mbox_t *)raw_mbox;
  154. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  155. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  156. /*
  157. * Try to issue Inquiry3 command
  158. * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
  159. * update enquiry3 structure
  160. */
  161. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  162. inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
  163. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  164. raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
  165. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
  166. /* Issue a blocking command to the card */
  167. if ((retval = issue_scb_block(adapter, raw_mbox))) {
  168. /* the adapter does not support 40ld */
  169. mraid_ext_inquiry *ext_inq;
  170. mraid_inquiry *inq;
  171. dma_addr_t dma_handle;
  172. ext_inq = pci_alloc_consistent(adapter->dev,
  173. sizeof(mraid_ext_inquiry), &dma_handle);
  174. if( ext_inq == NULL ) return -1;
  175. inq = &ext_inq->raid_inq;
  176. mbox->m_out.xferaddr = (u32)dma_handle;
  177. /*issue old 0x04 command to adapter */
  178. mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  179. issue_scb_block(adapter, raw_mbox);
  180. /*
  181. * update Enquiry3 and ProductInfo structures with
  182. * mraid_inquiry structure
  183. */
  184. mega_8_to_40ld(inq, inquiry3,
  185. (mega_product_info *)&adapter->product_info);
  186. pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
  187. ext_inq, dma_handle);
  188. } else { /*adapter supports 40ld */
  189. adapter->flag |= BOARD_40LD;
  190. /*
  191. * get product_info, which is static information and will be
  192. * unchanged
  193. */
  194. prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
  195. &adapter->product_info,
  196. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  197. mbox->m_out.xferaddr = prod_info_dma_handle;
  198. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  199. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
  200. if ((retval = issue_scb_block(adapter, raw_mbox)))
  201. printk(KERN_WARNING
  202. "megaraid: Product_info cmd failed with error: %d\n",
  203. retval);
  204. pci_unmap_single(adapter->dev, prod_info_dma_handle,
  205. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  206. }
  207. /*
  208. * kernel scans the channels from 0 to <= max_channel
  209. */
  210. adapter->host->max_channel =
  211. adapter->product_info.nchannels + NVIRT_CHAN -1;
  212. adapter->host->max_id = 16; /* max targets per channel */
  213. adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
  214. adapter->host->cmd_per_lun = max_cmd_per_lun;
  215. adapter->numldrv = inquiry3->num_ldrv;
  216. adapter->max_cmds = adapter->product_info.max_commands;
  217. if(adapter->max_cmds > MAX_COMMANDS)
  218. adapter->max_cmds = MAX_COMMANDS;
  219. adapter->host->can_queue = adapter->max_cmds - 1;
  220. /*
  221. * Get the maximum number of scatter-gather elements supported by this
  222. * firmware
  223. */
  224. mega_get_max_sgl(adapter);
  225. adapter->host->sg_tablesize = adapter->sglen;
  226. /* use HP firmware and bios version encoding */
  227. if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
  228. sprintf (adapter->fw_version, "%c%d%d.%d%d",
  229. adapter->product_info.fw_version[2],
  230. adapter->product_info.fw_version[1] >> 8,
  231. adapter->product_info.fw_version[1] & 0x0f,
  232. adapter->product_info.fw_version[0] >> 8,
  233. adapter->product_info.fw_version[0] & 0x0f);
  234. sprintf (adapter->bios_version, "%c%d%d.%d%d",
  235. adapter->product_info.bios_version[2],
  236. adapter->product_info.bios_version[1] >> 8,
  237. adapter->product_info.bios_version[1] & 0x0f,
  238. adapter->product_info.bios_version[0] >> 8,
  239. adapter->product_info.bios_version[0] & 0x0f);
  240. } else {
  241. memcpy(adapter->fw_version,
  242. (char *)adapter->product_info.fw_version, 4);
  243. adapter->fw_version[4] = 0;
  244. memcpy(adapter->bios_version,
  245. (char *)adapter->product_info.bios_version, 4);
  246. adapter->bios_version[4] = 0;
  247. }
  248. printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
  249. adapter->fw_version, adapter->bios_version, adapter->numldrv);
  250. /*
  251. * Do we support extended (>10 bytes) cdbs
  252. */
  253. adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
  254. if (adapter->support_ext_cdb)
  255. printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
  256. return 0;
  257. }
  258. /**
  259. * mega_runpendq()
  260. * @adapter - pointer to our soft state
  261. *
  262. * Runs through the list of pending requests.
  263. */
  264. static inline void
  265. mega_runpendq(adapter_t *adapter)
  266. {
  267. if(!list_empty(&adapter->pending_list))
  268. __mega_runpendq(adapter);
  269. }
  270. /*
  271. * megaraid_queue()
  272. * @scmd - Issue this scsi command
  273. * @done - the callback hook into the scsi mid-layer
  274. *
  275. * The command queuing entry point for the mid-layer.
  276. */
  277. static int
  278. megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
  279. {
  280. adapter_t *adapter;
  281. scb_t *scb;
  282. int busy=0;
  283. adapter = (adapter_t *)scmd->device->host->hostdata;
  284. scmd->scsi_done = done;
  285. /*
  286. * Allocate and build a SCB request
  287. * busy flag will be set if mega_build_cmd() command could not
  288. * allocate scb. We will return non-zero status in that case.
  289. * NOTE: scb can be null even though certain commands completed
  290. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
  291. * return 0 in that case.
  292. */
  293. scb = mega_build_cmd(adapter, scmd, &busy);
  294. if(scb) {
  295. scb->state |= SCB_PENDQ;
  296. list_add_tail(&scb->list, &adapter->pending_list);
  297. /*
  298. * Check if the HBA is in quiescent state, e.g., during a
  299. * delete logical drive opertion. If it is, don't run
  300. * the pending_list.
  301. */
  302. if(atomic_read(&adapter->quiescent) == 0) {
  303. mega_runpendq(adapter);
  304. }
  305. return 0;
  306. }
  307. return busy;
  308. }
  309. /**
  310. * mega_allocate_scb()
  311. * @adapter - pointer to our soft state
  312. * @cmd - scsi command from the mid-layer
  313. *
  314. * Allocate a SCB structure. This is the central structure for controller
  315. * commands.
  316. */
  317. static inline scb_t *
  318. mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
  319. {
  320. struct list_head *head = &adapter->free_list;
  321. scb_t *scb;
  322. /* Unlink command from Free List */
  323. if( !list_empty(head) ) {
  324. scb = list_entry(head->next, scb_t, list);
  325. list_del_init(head->next);
  326. scb->state = SCB_ACTIVE;
  327. scb->cmd = cmd;
  328. scb->dma_type = MEGA_DMA_TYPE_NONE;
  329. return scb;
  330. }
  331. return NULL;
  332. }
  333. /**
  334. * mega_get_ldrv_num()
  335. * @adapter - pointer to our soft state
  336. * @cmd - scsi mid layer command
  337. * @channel - channel on the controller
  338. *
  339. * Calculate the logical drive number based on the information in scsi command
  340. * and the channel number.
  341. */
  342. static inline int
  343. mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
  344. {
  345. int tgt;
  346. int ldrv_num;
  347. tgt = cmd->device->id;
  348. if ( tgt > adapter->this_id )
  349. tgt--; /* we do not get inquires for initiator id */
  350. ldrv_num = (channel * 15) + tgt;
  351. /*
  352. * If we have a logical drive with boot enabled, project it first
  353. */
  354. if( adapter->boot_ldrv_enabled ) {
  355. if( ldrv_num == 0 ) {
  356. ldrv_num = adapter->boot_ldrv;
  357. }
  358. else {
  359. if( ldrv_num <= adapter->boot_ldrv ) {
  360. ldrv_num--;
  361. }
  362. }
  363. }
  364. /*
  365. * If "delete logical drive" feature is enabled on this controller.
  366. * Do only if at least one delete logical drive operation was done.
  367. *
  368. * Also, after logical drive deletion, instead of logical drive number,
  369. * the value returned should be 0x80+logical drive id.
  370. *
  371. * These is valid only for IO commands.
  372. */
  373. if (adapter->support_random_del && adapter->read_ldidmap )
  374. switch (cmd->cmnd[0]) {
  375. case READ_6: /* fall through */
  376. case WRITE_6: /* fall through */
  377. case READ_10: /* fall through */
  378. case WRITE_10:
  379. ldrv_num += 0x80;
  380. }
  381. return ldrv_num;
  382. }
  383. /**
  384. * mega_build_cmd()
  385. * @adapter - pointer to our soft state
  386. * @cmd - Prepare using this scsi command
  387. * @busy - busy flag if no resources
  388. *
  389. * Prepares a command and scatter gather list for the controller. This routine
  390. * also finds out if the commands is intended for a logical drive or a
  391. * physical device and prepares the controller command accordingly.
  392. *
  393. * We also re-order the logical drives and physical devices based on their
  394. * boot settings.
  395. */
  396. static scb_t *
  397. mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
  398. {
  399. mega_ext_passthru *epthru;
  400. mega_passthru *pthru;
  401. scb_t *scb;
  402. mbox_t *mbox;
  403. long seg;
  404. char islogical;
  405. int max_ldrv_num;
  406. int channel = 0;
  407. int target = 0;
  408. int ldrv_num = 0; /* logical drive number */
  409. /*
  410. * filter the internal and ioctl commands
  411. */
  412. if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
  413. return cmd->buffer;
  414. }
  415. /*
  416. * We know what channels our logical drives are on - mega_find_card()
  417. */
  418. islogical = adapter->logdrv_chan[cmd->device->channel];
  419. /*
  420. * The theory: If physical drive is chosen for boot, all the physical
  421. * devices are exported before the logical drives, otherwise physical
  422. * devices are pushed after logical drives, in which case - Kernel sees
  423. * the physical devices on virtual channel which is obviously converted
  424. * to actual channel on the HBA.
  425. */
  426. if( adapter->boot_pdrv_enabled ) {
  427. if( islogical ) {
  428. /* logical channel */
  429. channel = cmd->device->channel -
  430. adapter->product_info.nchannels;
  431. }
  432. else {
  433. /* this is physical channel */
  434. channel = cmd->device->channel;
  435. target = cmd->device->id;
  436. /*
  437. * boot from a physical disk, that disk needs to be
  438. * exposed first IF both the channels are SCSI, then
  439. * booting from the second channel is not allowed.
  440. */
  441. if( target == 0 ) {
  442. target = adapter->boot_pdrv_tgt;
  443. }
  444. else if( target == adapter->boot_pdrv_tgt ) {
  445. target = 0;
  446. }
  447. }
  448. }
  449. else {
  450. if( islogical ) {
  451. /* this is the logical channel */
  452. channel = cmd->device->channel;
  453. }
  454. else {
  455. /* physical channel */
  456. channel = cmd->device->channel - NVIRT_CHAN;
  457. target = cmd->device->id;
  458. }
  459. }
  460. if(islogical) {
  461. /* have just LUN 0 for each target on virtual channels */
  462. if (cmd->device->lun) {
  463. cmd->result = (DID_BAD_TARGET << 16);
  464. cmd->scsi_done(cmd);
  465. return NULL;
  466. }
  467. ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
  468. max_ldrv_num = (adapter->flag & BOARD_40LD) ?
  469. MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
  470. /*
  471. * max_ldrv_num increases by 0x80 if some logical drive was
  472. * deleted.
  473. */
  474. if(adapter->read_ldidmap)
  475. max_ldrv_num += 0x80;
  476. if(ldrv_num > max_ldrv_num ) {
  477. cmd->result = (DID_BAD_TARGET << 16);
  478. cmd->scsi_done(cmd);
  479. return NULL;
  480. }
  481. }
  482. else {
  483. if( cmd->device->lun > 7) {
  484. /*
  485. * Do not support lun >7 for physically accessed
  486. * devices
  487. */
  488. cmd->result = (DID_BAD_TARGET << 16);
  489. cmd->scsi_done(cmd);
  490. return NULL;
  491. }
  492. }
  493. /*
  494. *
  495. * Logical drive commands
  496. *
  497. */
  498. if(islogical) {
  499. switch (cmd->cmnd[0]) {
  500. case TEST_UNIT_READY:
  501. #if MEGA_HAVE_CLUSTERING
  502. /*
  503. * Do we support clustering and is the support enabled
  504. * If no, return success always
  505. */
  506. if( !adapter->has_cluster ) {
  507. cmd->result = (DID_OK << 16);
  508. cmd->scsi_done(cmd);
  509. return NULL;
  510. }
  511. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  512. *busy = 1;
  513. return NULL;
  514. }
  515. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  516. scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
  517. scb->raw_mbox[3] = ldrv_num;
  518. scb->dma_direction = PCI_DMA_NONE;
  519. return scb;
  520. #else
  521. cmd->result = (DID_OK << 16);
  522. cmd->scsi_done(cmd);
  523. return NULL;
  524. #endif
  525. case MODE_SENSE: {
  526. char *buf;
  527. if (cmd->use_sg) {
  528. struct scatterlist *sg;
  529. sg = (struct scatterlist *)cmd->request_buffer;
  530. buf = kmap_atomic(sg->page, KM_IRQ0) +
  531. sg->offset;
  532. } else
  533. buf = cmd->request_buffer;
  534. memset(cmd->request_buffer, 0, cmd->cmnd[4]);
  535. if (cmd->use_sg) {
  536. struct scatterlist *sg;
  537. sg = (struct scatterlist *)cmd->request_buffer;
  538. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  539. }
  540. cmd->result = (DID_OK << 16);
  541. cmd->scsi_done(cmd);
  542. return NULL;
  543. }
  544. case READ_CAPACITY:
  545. case INQUIRY:
  546. if(!(adapter->flag & (1L << cmd->device->channel))) {
  547. printk(KERN_NOTICE
  548. "scsi%d: scanning scsi channel %d ",
  549. adapter->host->host_no,
  550. cmd->device->channel);
  551. printk("for logical drives.\n");
  552. adapter->flag |= (1L << cmd->device->channel);
  553. }
  554. /* Allocate a SCB and initialize passthru */
  555. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  556. *busy = 1;
  557. return NULL;
  558. }
  559. pthru = scb->pthru;
  560. mbox = (mbox_t *)scb->raw_mbox;
  561. memset(mbox, 0, sizeof(scb->raw_mbox));
  562. memset(pthru, 0, sizeof(mega_passthru));
  563. pthru->timeout = 0;
  564. pthru->ars = 1;
  565. pthru->reqsenselen = 14;
  566. pthru->islogical = 1;
  567. pthru->logdrv = ldrv_num;
  568. pthru->cdblen = cmd->cmd_len;
  569. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  570. if( adapter->has_64bit_addr ) {
  571. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  572. }
  573. else {
  574. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  575. }
  576. scb->dma_direction = PCI_DMA_FROMDEVICE;
  577. pthru->numsgelements = mega_build_sglist(adapter, scb,
  578. &pthru->dataxferaddr, &pthru->dataxferlen);
  579. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  580. return scb;
  581. case READ_6:
  582. case WRITE_6:
  583. case READ_10:
  584. case WRITE_10:
  585. case READ_12:
  586. case WRITE_12:
  587. /* Allocate a SCB and initialize mailbox */
  588. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  589. *busy = 1;
  590. return NULL;
  591. }
  592. mbox = (mbox_t *)scb->raw_mbox;
  593. memset(mbox, 0, sizeof(scb->raw_mbox));
  594. mbox->m_out.logdrv = ldrv_num;
  595. /*
  596. * A little hack: 2nd bit is zero for all scsi read
  597. * commands and is set for all scsi write commands
  598. */
  599. if( adapter->has_64bit_addr ) {
  600. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  601. MEGA_MBOXCMD_LWRITE64:
  602. MEGA_MBOXCMD_LREAD64 ;
  603. }
  604. else {
  605. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  606. MEGA_MBOXCMD_LWRITE:
  607. MEGA_MBOXCMD_LREAD ;
  608. }
  609. /*
  610. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  611. */
  612. if( cmd->cmd_len == 6 ) {
  613. mbox->m_out.numsectors = (u32) cmd->cmnd[4];
  614. mbox->m_out.lba =
  615. ((u32)cmd->cmnd[1] << 16) |
  616. ((u32)cmd->cmnd[2] << 8) |
  617. (u32)cmd->cmnd[3];
  618. mbox->m_out.lba &= 0x1FFFFF;
  619. #if MEGA_HAVE_STATS
  620. /*
  621. * Take modulo 0x80, since the logical drive
  622. * number increases by 0x80 when a logical
  623. * drive was deleted
  624. */
  625. if (*cmd->cmnd == READ_6) {
  626. adapter->nreads[ldrv_num%0x80]++;
  627. adapter->nreadblocks[ldrv_num%0x80] +=
  628. mbox->m_out.numsectors;
  629. } else {
  630. adapter->nwrites[ldrv_num%0x80]++;
  631. adapter->nwriteblocks[ldrv_num%0x80] +=
  632. mbox->m_out.numsectors;
  633. }
  634. #endif
  635. }
  636. /*
  637. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  638. */
  639. if( cmd->cmd_len == 10 ) {
  640. mbox->m_out.numsectors =
  641. (u32)cmd->cmnd[8] |
  642. ((u32)cmd->cmnd[7] << 8);
  643. mbox->m_out.lba =
  644. ((u32)cmd->cmnd[2] << 24) |
  645. ((u32)cmd->cmnd[3] << 16) |
  646. ((u32)cmd->cmnd[4] << 8) |
  647. (u32)cmd->cmnd[5];
  648. #if MEGA_HAVE_STATS
  649. if (*cmd->cmnd == READ_10) {
  650. adapter->nreads[ldrv_num%0x80]++;
  651. adapter->nreadblocks[ldrv_num%0x80] +=
  652. mbox->m_out.numsectors;
  653. } else {
  654. adapter->nwrites[ldrv_num%0x80]++;
  655. adapter->nwriteblocks[ldrv_num%0x80] +=
  656. mbox->m_out.numsectors;
  657. }
  658. #endif
  659. }
  660. /*
  661. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  662. */
  663. if( cmd->cmd_len == 12 ) {
  664. mbox->m_out.lba =
  665. ((u32)cmd->cmnd[2] << 24) |
  666. ((u32)cmd->cmnd[3] << 16) |
  667. ((u32)cmd->cmnd[4] << 8) |
  668. (u32)cmd->cmnd[5];
  669. mbox->m_out.numsectors =
  670. ((u32)cmd->cmnd[6] << 24) |
  671. ((u32)cmd->cmnd[7] << 16) |
  672. ((u32)cmd->cmnd[8] << 8) |
  673. (u32)cmd->cmnd[9];
  674. #if MEGA_HAVE_STATS
  675. if (*cmd->cmnd == READ_12) {
  676. adapter->nreads[ldrv_num%0x80]++;
  677. adapter->nreadblocks[ldrv_num%0x80] +=
  678. mbox->m_out.numsectors;
  679. } else {
  680. adapter->nwrites[ldrv_num%0x80]++;
  681. adapter->nwriteblocks[ldrv_num%0x80] +=
  682. mbox->m_out.numsectors;
  683. }
  684. #endif
  685. }
  686. /*
  687. * If it is a read command
  688. */
  689. if( (*cmd->cmnd & 0x0F) == 0x08 ) {
  690. scb->dma_direction = PCI_DMA_FROMDEVICE;
  691. }
  692. else {
  693. scb->dma_direction = PCI_DMA_TODEVICE;
  694. }
  695. /* Calculate Scatter-Gather info */
  696. mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
  697. (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
  698. return scb;
  699. #if MEGA_HAVE_CLUSTERING
  700. case RESERVE: /* Fall through */
  701. case RELEASE:
  702. /*
  703. * Do we support clustering and is the support enabled
  704. */
  705. if( ! adapter->has_cluster ) {
  706. cmd->result = (DID_BAD_TARGET << 16);
  707. cmd->scsi_done(cmd);
  708. return NULL;
  709. }
  710. /* Allocate a SCB and initialize mailbox */
  711. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  712. *busy = 1;
  713. return NULL;
  714. }
  715. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  716. scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
  717. MEGA_RESERVE_LD : MEGA_RELEASE_LD;
  718. scb->raw_mbox[3] = ldrv_num;
  719. scb->dma_direction = PCI_DMA_NONE;
  720. return scb;
  721. #endif
  722. default:
  723. cmd->result = (DID_BAD_TARGET << 16);
  724. cmd->scsi_done(cmd);
  725. return NULL;
  726. }
  727. }
  728. /*
  729. * Passthru drive commands
  730. */
  731. else {
  732. /* Allocate a SCB and initialize passthru */
  733. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  734. *busy = 1;
  735. return NULL;
  736. }
  737. mbox = (mbox_t *)scb->raw_mbox;
  738. memset(mbox, 0, sizeof(scb->raw_mbox));
  739. if( adapter->support_ext_cdb ) {
  740. epthru = mega_prepare_extpassthru(adapter, scb, cmd,
  741. channel, target);
  742. mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
  743. mbox->m_out.xferaddr = scb->epthru_dma_addr;
  744. }
  745. else {
  746. pthru = mega_prepare_passthru(adapter, scb, cmd,
  747. channel, target);
  748. /* Initialize mailbox */
  749. if( adapter->has_64bit_addr ) {
  750. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  751. }
  752. else {
  753. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  754. }
  755. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  756. }
  757. return scb;
  758. }
  759. return NULL;
  760. }
  761. /**
  762. * mega_prepare_passthru()
  763. * @adapter - pointer to our soft state
  764. * @scb - our scsi control block
  765. * @cmd - scsi command from the mid-layer
  766. * @channel - actual channel on the controller
  767. * @target - actual id on the controller.
  768. *
  769. * prepare a command for the scsi physical devices.
  770. */
  771. static mega_passthru *
  772. mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  773. int channel, int target)
  774. {
  775. mega_passthru *pthru;
  776. pthru = scb->pthru;
  777. memset(pthru, 0, sizeof (mega_passthru));
  778. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  779. pthru->timeout = 2;
  780. pthru->ars = 1;
  781. pthru->reqsenselen = 14;
  782. pthru->islogical = 0;
  783. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  784. pthru->target = (adapter->flag & BOARD_40LD) ?
  785. (channel << 4) | target : target;
  786. pthru->cdblen = cmd->cmd_len;
  787. pthru->logdrv = cmd->device->lun;
  788. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  789. /* Not sure about the direction */
  790. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  791. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  792. switch (cmd->cmnd[0]) {
  793. case INQUIRY:
  794. case READ_CAPACITY:
  795. if(!(adapter->flag & (1L << cmd->device->channel))) {
  796. printk(KERN_NOTICE
  797. "scsi%d: scanning scsi channel %d [P%d] ",
  798. adapter->host->host_no,
  799. cmd->device->channel, channel);
  800. printk("for physical devices.\n");
  801. adapter->flag |= (1L << cmd->device->channel);
  802. }
  803. /* Fall through */
  804. default:
  805. pthru->numsgelements = mega_build_sglist(adapter, scb,
  806. &pthru->dataxferaddr, &pthru->dataxferlen);
  807. break;
  808. }
  809. return pthru;
  810. }
  811. /**
  812. * mega_prepare_extpassthru()
  813. * @adapter - pointer to our soft state
  814. * @scb - our scsi control block
  815. * @cmd - scsi command from the mid-layer
  816. * @channel - actual channel on the controller
  817. * @target - actual id on the controller.
  818. *
  819. * prepare a command for the scsi physical devices. This rountine prepares
  820. * commands for devices which can take extended CDBs (>10 bytes)
  821. */
  822. static mega_ext_passthru *
  823. mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  824. int channel, int target)
  825. {
  826. mega_ext_passthru *epthru;
  827. epthru = scb->epthru;
  828. memset(epthru, 0, sizeof(mega_ext_passthru));
  829. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  830. epthru->timeout = 2;
  831. epthru->ars = 1;
  832. epthru->reqsenselen = 14;
  833. epthru->islogical = 0;
  834. epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  835. epthru->target = (adapter->flag & BOARD_40LD) ?
  836. (channel << 4) | target : target;
  837. epthru->cdblen = cmd->cmd_len;
  838. epthru->logdrv = cmd->device->lun;
  839. memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
  840. /* Not sure about the direction */
  841. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  842. switch(cmd->cmnd[0]) {
  843. case INQUIRY:
  844. case READ_CAPACITY:
  845. if(!(adapter->flag & (1L << cmd->device->channel))) {
  846. printk(KERN_NOTICE
  847. "scsi%d: scanning scsi channel %d [P%d] ",
  848. adapter->host->host_no,
  849. cmd->device->channel, channel);
  850. printk("for physical devices.\n");
  851. adapter->flag |= (1L << cmd->device->channel);
  852. }
  853. /* Fall through */
  854. default:
  855. epthru->numsgelements = mega_build_sglist(adapter, scb,
  856. &epthru->dataxferaddr, &epthru->dataxferlen);
  857. break;
  858. }
  859. return epthru;
  860. }
  861. static void
  862. __mega_runpendq(adapter_t *adapter)
  863. {
  864. scb_t *scb;
  865. struct list_head *pos, *next;
  866. /* Issue any pending commands to the card */
  867. list_for_each_safe(pos, next, &adapter->pending_list) {
  868. scb = list_entry(pos, scb_t, list);
  869. if( !(scb->state & SCB_ISSUED) ) {
  870. if( issue_scb(adapter, scb) != 0 )
  871. return;
  872. }
  873. }
  874. return;
  875. }
  876. /**
  877. * issue_scb()
  878. * @adapter - pointer to our soft state
  879. * @scb - scsi control block
  880. *
  881. * Post a command to the card if the mailbox is available, otherwise return
  882. * busy. We also take the scb from the pending list if the mailbox is
  883. * available.
  884. */
  885. static int
  886. issue_scb(adapter_t *adapter, scb_t *scb)
  887. {
  888. volatile mbox64_t *mbox64 = adapter->mbox64;
  889. volatile mbox_t *mbox = adapter->mbox;
  890. unsigned int i = 0;
  891. if(unlikely(mbox->m_in.busy)) {
  892. do {
  893. udelay(1);
  894. i++;
  895. } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
  896. if(mbox->m_in.busy) return -1;
  897. }
  898. /* Copy mailbox data into host structure */
  899. memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
  900. sizeof(struct mbox_out));
  901. mbox->m_out.cmdid = scb->idx; /* Set cmdid */
  902. mbox->m_in.busy = 1; /* Set busy */
  903. /*
  904. * Increment the pending queue counter
  905. */
  906. atomic_inc(&adapter->pend_cmds);
  907. switch (mbox->m_out.cmd) {
  908. case MEGA_MBOXCMD_LREAD64:
  909. case MEGA_MBOXCMD_LWRITE64:
  910. case MEGA_MBOXCMD_PASSTHRU64:
  911. case MEGA_MBOXCMD_EXTPTHRU:
  912. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  913. mbox64->xfer_segment_hi = 0;
  914. mbox->m_out.xferaddr = 0xFFFFFFFF;
  915. break;
  916. default:
  917. mbox64->xfer_segment_lo = 0;
  918. mbox64->xfer_segment_hi = 0;
  919. }
  920. /*
  921. * post the command
  922. */
  923. scb->state |= SCB_ISSUED;
  924. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  925. mbox->m_in.poll = 0;
  926. mbox->m_in.ack = 0;
  927. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  928. }
  929. else {
  930. irq_enable(adapter);
  931. issue_command(adapter);
  932. }
  933. return 0;
  934. }
  935. /*
  936. * Wait until the controller's mailbox is available
  937. */
  938. static inline int
  939. mega_busywait_mbox (adapter_t *adapter)
  940. {
  941. if (adapter->mbox->m_in.busy)
  942. return __mega_busywait_mbox(adapter);
  943. return 0;
  944. }
  945. /**
  946. * issue_scb_block()
  947. * @adapter - pointer to our soft state
  948. * @raw_mbox - the mailbox
  949. *
  950. * Issue a scb in synchronous and non-interrupt mode
  951. */
  952. static int
  953. issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
  954. {
  955. volatile mbox64_t *mbox64 = adapter->mbox64;
  956. volatile mbox_t *mbox = adapter->mbox;
  957. u8 byte;
  958. /* Wait until mailbox is free */
  959. if(mega_busywait_mbox (adapter))
  960. goto bug_blocked_mailbox;
  961. /* Copy mailbox data into host structure */
  962. memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
  963. mbox->m_out.cmdid = 0xFE;
  964. mbox->m_in.busy = 1;
  965. switch (raw_mbox[0]) {
  966. case MEGA_MBOXCMD_LREAD64:
  967. case MEGA_MBOXCMD_LWRITE64:
  968. case MEGA_MBOXCMD_PASSTHRU64:
  969. case MEGA_MBOXCMD_EXTPTHRU:
  970. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  971. mbox64->xfer_segment_hi = 0;
  972. mbox->m_out.xferaddr = 0xFFFFFFFF;
  973. break;
  974. default:
  975. mbox64->xfer_segment_lo = 0;
  976. mbox64->xfer_segment_hi = 0;
  977. }
  978. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  979. mbox->m_in.poll = 0;
  980. mbox->m_in.ack = 0;
  981. mbox->m_in.numstatus = 0xFF;
  982. mbox->m_in.status = 0xFF;
  983. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  984. while((volatile u8)mbox->m_in.numstatus == 0xFF)
  985. cpu_relax();
  986. mbox->m_in.numstatus = 0xFF;
  987. while( (volatile u8)mbox->m_in.poll != 0x77 )
  988. cpu_relax();
  989. mbox->m_in.poll = 0;
  990. mbox->m_in.ack = 0x77;
  991. WRINDOOR(adapter, adapter->mbox_dma | 0x2);
  992. while(RDINDOOR(adapter) & 0x2)
  993. cpu_relax();
  994. }
  995. else {
  996. irq_disable(adapter);
  997. issue_command(adapter);
  998. while (!((byte = irq_state(adapter)) & INTR_VALID))
  999. cpu_relax();
  1000. set_irq_state(adapter, byte);
  1001. irq_enable(adapter);
  1002. irq_ack(adapter);
  1003. }
  1004. return mbox->m_in.status;
  1005. bug_blocked_mailbox:
  1006. printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
  1007. udelay (1000);
  1008. return -1;
  1009. }
  1010. /**
  1011. * megaraid_isr_iomapped()
  1012. * @irq - irq
  1013. * @devp - pointer to our soft state
  1014. * @regs - unused
  1015. *
  1016. * Interrupt service routine for io-mapped controllers.
  1017. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1018. * and service the completed commands.
  1019. */
  1020. static irqreturn_t
  1021. megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
  1022. {
  1023. adapter_t *adapter = devp;
  1024. unsigned long flags;
  1025. u8 status;
  1026. u8 nstatus;
  1027. u8 completed[MAX_FIRMWARE_STATUS];
  1028. u8 byte;
  1029. int handled = 0;
  1030. /*
  1031. * loop till F/W has more commands for us to complete.
  1032. */
  1033. spin_lock_irqsave(&adapter->lock, flags);
  1034. do {
  1035. /* Check if a valid interrupt is pending */
  1036. byte = irq_state(adapter);
  1037. if( (byte & VALID_INTR_BYTE) == 0 ) {
  1038. /*
  1039. * No more pending commands
  1040. */
  1041. goto out_unlock;
  1042. }
  1043. set_irq_state(adapter, byte);
  1044. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1045. == 0xFF)
  1046. cpu_relax();
  1047. adapter->mbox->m_in.numstatus = 0xFF;
  1048. status = adapter->mbox->m_in.status;
  1049. /*
  1050. * decrement the pending queue counter
  1051. */
  1052. atomic_sub(nstatus, &adapter->pend_cmds);
  1053. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1054. nstatus);
  1055. /* Acknowledge interrupt */
  1056. irq_ack(adapter);
  1057. mega_cmd_done(adapter, completed, nstatus, status);
  1058. mega_rundoneq(adapter);
  1059. handled = 1;
  1060. /* Loop through any pending requests */
  1061. if(atomic_read(&adapter->quiescent) == 0) {
  1062. mega_runpendq(adapter);
  1063. }
  1064. } while(1);
  1065. out_unlock:
  1066. spin_unlock_irqrestore(&adapter->lock, flags);
  1067. return IRQ_RETVAL(handled);
  1068. }
  1069. /**
  1070. * megaraid_isr_memmapped()
  1071. * @irq - irq
  1072. * @devp - pointer to our soft state
  1073. * @regs - unused
  1074. *
  1075. * Interrupt service routine for memory-mapped controllers.
  1076. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1077. * and service the completed commands.
  1078. */
  1079. static irqreturn_t
  1080. megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
  1081. {
  1082. adapter_t *adapter = devp;
  1083. unsigned long flags;
  1084. u8 status;
  1085. u32 dword = 0;
  1086. u8 nstatus;
  1087. u8 completed[MAX_FIRMWARE_STATUS];
  1088. int handled = 0;
  1089. /*
  1090. * loop till F/W has more commands for us to complete.
  1091. */
  1092. spin_lock_irqsave(&adapter->lock, flags);
  1093. do {
  1094. /* Check if a valid interrupt is pending */
  1095. dword = RDOUTDOOR(adapter);
  1096. if(dword != 0x10001234) {
  1097. /*
  1098. * No more pending commands
  1099. */
  1100. goto out_unlock;
  1101. }
  1102. WROUTDOOR(adapter, 0x10001234);
  1103. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1104. == 0xFF) {
  1105. cpu_relax();
  1106. }
  1107. adapter->mbox->m_in.numstatus = 0xFF;
  1108. status = adapter->mbox->m_in.status;
  1109. /*
  1110. * decrement the pending queue counter
  1111. */
  1112. atomic_sub(nstatus, &adapter->pend_cmds);
  1113. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1114. nstatus);
  1115. /* Acknowledge interrupt */
  1116. WRINDOOR(adapter, 0x2);
  1117. handled = 1;
  1118. while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
  1119. mega_cmd_done(adapter, completed, nstatus, status);
  1120. mega_rundoneq(adapter);
  1121. /* Loop through any pending requests */
  1122. if(atomic_read(&adapter->quiescent) == 0) {
  1123. mega_runpendq(adapter);
  1124. }
  1125. } while(1);
  1126. out_unlock:
  1127. spin_unlock_irqrestore(&adapter->lock, flags);
  1128. return IRQ_RETVAL(handled);
  1129. }
  1130. /**
  1131. * mega_cmd_done()
  1132. * @adapter - pointer to our soft state
  1133. * @completed - array of ids of completed commands
  1134. * @nstatus - number of completed commands
  1135. * @status - status of the last command completed
  1136. *
  1137. * Complete the comamnds and call the scsi mid-layer callback hooks.
  1138. */
  1139. static void
  1140. mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
  1141. {
  1142. mega_ext_passthru *epthru = NULL;
  1143. struct scatterlist *sgl;
  1144. Scsi_Cmnd *cmd = NULL;
  1145. mega_passthru *pthru = NULL;
  1146. mbox_t *mbox = NULL;
  1147. u8 c;
  1148. scb_t *scb;
  1149. int islogical;
  1150. int cmdid;
  1151. int i;
  1152. /*
  1153. * for all the commands completed, call the mid-layer callback routine
  1154. * and free the scb.
  1155. */
  1156. for( i = 0; i < nstatus; i++ ) {
  1157. cmdid = completed[i];
  1158. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1159. scb = &adapter->int_scb;
  1160. cmd = scb->cmd;
  1161. mbox = (mbox_t *)scb->raw_mbox;
  1162. /*
  1163. * Internal command interface do not fire the extended
  1164. * passthru or 64-bit passthru
  1165. */
  1166. pthru = scb->pthru;
  1167. }
  1168. else {
  1169. scb = &adapter->scb_list[cmdid];
  1170. /*
  1171. * Make sure f/w has completed a valid command
  1172. */
  1173. if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
  1174. printk(KERN_CRIT
  1175. "megaraid: invalid command ");
  1176. printk("Id %d, scb->state:%x, scsi cmd:%p\n",
  1177. cmdid, scb->state, scb->cmd);
  1178. continue;
  1179. }
  1180. /*
  1181. * Was a abort issued for this command
  1182. */
  1183. if( scb->state & SCB_ABORT ) {
  1184. printk(KERN_WARNING
  1185. "megaraid: aborted cmd %lx[%x] complete.\n",
  1186. scb->cmd->serial_number, scb->idx);
  1187. scb->cmd->result = (DID_ABORT << 16);
  1188. list_add_tail(SCSI_LIST(scb->cmd),
  1189. &adapter->completed_list);
  1190. mega_free_scb(adapter, scb);
  1191. continue;
  1192. }
  1193. /*
  1194. * Was a reset issued for this command
  1195. */
  1196. if( scb->state & SCB_RESET ) {
  1197. printk(KERN_WARNING
  1198. "megaraid: reset cmd %lx[%x] complete.\n",
  1199. scb->cmd->serial_number, scb->idx);
  1200. scb->cmd->result = (DID_RESET << 16);
  1201. list_add_tail(SCSI_LIST(scb->cmd),
  1202. &adapter->completed_list);
  1203. mega_free_scb (adapter, scb);
  1204. continue;
  1205. }
  1206. cmd = scb->cmd;
  1207. pthru = scb->pthru;
  1208. epthru = scb->epthru;
  1209. mbox = (mbox_t *)scb->raw_mbox;
  1210. #if MEGA_HAVE_STATS
  1211. {
  1212. int logdrv = mbox->m_out.logdrv;
  1213. islogical = adapter->logdrv_chan[cmd->channel];
  1214. /*
  1215. * Maintain an error counter for the logical drive.
  1216. * Some application like SNMP agent need such
  1217. * statistics
  1218. */
  1219. if( status && islogical && (cmd->cmnd[0] == READ_6 ||
  1220. cmd->cmnd[0] == READ_10 ||
  1221. cmd->cmnd[0] == READ_12)) {
  1222. /*
  1223. * Logical drive number increases by 0x80 when
  1224. * a logical drive is deleted
  1225. */
  1226. adapter->rd_errors[logdrv%0x80]++;
  1227. }
  1228. if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
  1229. cmd->cmnd[0] == WRITE_10 ||
  1230. cmd->cmnd[0] == WRITE_12)) {
  1231. /*
  1232. * Logical drive number increases by 0x80 when
  1233. * a logical drive is deleted
  1234. */
  1235. adapter->wr_errors[logdrv%0x80]++;
  1236. }
  1237. }
  1238. #endif
  1239. }
  1240. /*
  1241. * Do not return the presence of hard disk on the channel so,
  1242. * inquiry sent, and returned data==hard disk or removable
  1243. * hard disk and not logical, request should return failure! -
  1244. * PJ
  1245. */
  1246. islogical = adapter->logdrv_chan[cmd->device->channel];
  1247. if( cmd->cmnd[0] == INQUIRY && !islogical ) {
  1248. if( cmd->use_sg ) {
  1249. sgl = (struct scatterlist *)
  1250. cmd->request_buffer;
  1251. if( sgl->page ) {
  1252. c = *(unsigned char *)
  1253. page_address((&sgl[0])->page) +
  1254. (&sgl[0])->offset;
  1255. }
  1256. else {
  1257. printk(KERN_WARNING
  1258. "megaraid: invalid sg.\n");
  1259. c = 0;
  1260. }
  1261. }
  1262. else {
  1263. c = *(u8 *)cmd->request_buffer;
  1264. }
  1265. if(IS_RAID_CH(adapter, cmd->device->channel) &&
  1266. ((c & 0x1F ) == TYPE_DISK)) {
  1267. status = 0xF0;
  1268. }
  1269. }
  1270. /* clear result; otherwise, success returns corrupt value */
  1271. cmd->result = 0;
  1272. /* Convert MegaRAID status to Linux error code */
  1273. switch (status) {
  1274. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1275. cmd->result |= (DID_OK << 16);
  1276. break;
  1277. case 0x02: /* ERROR_ABORTED, i.e.
  1278. SCSI_STATUS_CHECK_CONDITION */
  1279. /* set sense_buffer and result fields */
  1280. if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  1281. mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  1282. memcpy(cmd->sense_buffer, pthru->reqsensearea,
  1283. 14);
  1284. cmd->result = (DRIVER_SENSE << 24) |
  1285. (DID_OK << 16) |
  1286. (CHECK_CONDITION << 1);
  1287. }
  1288. else {
  1289. if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  1290. memcpy(cmd->sense_buffer,
  1291. epthru->reqsensearea, 14);
  1292. cmd->result = (DRIVER_SENSE << 24) |
  1293. (DID_OK << 16) |
  1294. (CHECK_CONDITION << 1);
  1295. } else {
  1296. cmd->sense_buffer[0] = 0x70;
  1297. cmd->sense_buffer[2] = ABORTED_COMMAND;
  1298. cmd->result |= (CHECK_CONDITION << 1);
  1299. }
  1300. }
  1301. break;
  1302. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
  1303. SCSI_STATUS_BUSY */
  1304. cmd->result |= (DID_BUS_BUSY << 16) | status;
  1305. break;
  1306. default:
  1307. #if MEGA_HAVE_CLUSTERING
  1308. /*
  1309. * If TEST_UNIT_READY fails, we know
  1310. * MEGA_RESERVATION_STATUS failed
  1311. */
  1312. if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  1313. cmd->result |= (DID_ERROR << 16) |
  1314. (RESERVATION_CONFLICT << 1);
  1315. }
  1316. else
  1317. /*
  1318. * Error code returned is 1 if Reserve or Release
  1319. * failed or the input parameter is invalid
  1320. */
  1321. if( status == 1 &&
  1322. (cmd->cmnd[0] == RESERVE ||
  1323. cmd->cmnd[0] == RELEASE) ) {
  1324. cmd->result |= (DID_ERROR << 16) |
  1325. (RESERVATION_CONFLICT << 1);
  1326. }
  1327. else
  1328. #endif
  1329. cmd->result |= (DID_BAD_TARGET << 16)|status;
  1330. }
  1331. /*
  1332. * Only free SCBs for the commands coming down from the
  1333. * mid-layer, not for which were issued internally
  1334. *
  1335. * For internal command, restore the status returned by the
  1336. * firmware so that user can interpret it.
  1337. */
  1338. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1339. cmd->result = status;
  1340. /*
  1341. * Remove the internal command from the pending list
  1342. */
  1343. list_del_init(&scb->list);
  1344. scb->state = SCB_FREE;
  1345. }
  1346. else {
  1347. mega_free_scb(adapter, scb);
  1348. }
  1349. /* Add Scsi_Command to end of completed queue */
  1350. list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  1351. }
  1352. }
  1353. /*
  1354. * mega_runpendq()
  1355. *
  1356. * Run through the list of completed requests and finish it
  1357. */
  1358. static void
  1359. mega_rundoneq (adapter_t *adapter)
  1360. {
  1361. Scsi_Cmnd *cmd;
  1362. struct list_head *pos;
  1363. list_for_each(pos, &adapter->completed_list) {
  1364. Scsi_Pointer* spos = (Scsi_Pointer *)pos;
  1365. cmd = list_entry(spos, Scsi_Cmnd, SCp);
  1366. cmd->scsi_done(cmd);
  1367. }
  1368. INIT_LIST_HEAD(&adapter->completed_list);
  1369. }
  1370. /*
  1371. * Free a SCB structure
  1372. * Note: We assume the scsi commands associated with this scb is not free yet.
  1373. */
  1374. static void
  1375. mega_free_scb(adapter_t *adapter, scb_t *scb)
  1376. {
  1377. unsigned long length;
  1378. switch( scb->dma_type ) {
  1379. case MEGA_DMA_TYPE_NONE:
  1380. break;
  1381. case MEGA_BULK_DATA:
  1382. if (scb->cmd->use_sg == 0)
  1383. length = scb->cmd->request_bufflen;
  1384. else {
  1385. struct scatterlist *sgl =
  1386. (struct scatterlist *)scb->cmd->request_buffer;
  1387. length = sgl->length;
  1388. }
  1389. pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
  1390. length, scb->dma_direction);
  1391. break;
  1392. case MEGA_SGLIST:
  1393. pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
  1394. scb->cmd->use_sg, scb->dma_direction);
  1395. break;
  1396. default:
  1397. break;
  1398. }
  1399. /*
  1400. * Remove from the pending list
  1401. */
  1402. list_del_init(&scb->list);
  1403. /* Link the scb back into free list */
  1404. scb->state = SCB_FREE;
  1405. scb->cmd = NULL;
  1406. list_add(&scb->list, &adapter->free_list);
  1407. }
  1408. static int
  1409. __mega_busywait_mbox (adapter_t *adapter)
  1410. {
  1411. volatile mbox_t *mbox = adapter->mbox;
  1412. long counter;
  1413. for (counter = 0; counter < 10000; counter++) {
  1414. if (!mbox->m_in.busy)
  1415. return 0;
  1416. udelay(100); yield();
  1417. }
  1418. return -1; /* give up after 1 second */
  1419. }
  1420. /*
  1421. * Copies data to SGLIST
  1422. * Note: For 64 bit cards, we need a minimum of one SG element for read/write
  1423. */
  1424. static int
  1425. mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  1426. {
  1427. struct scatterlist *sgl;
  1428. struct page *page;
  1429. unsigned long offset;
  1430. unsigned int length;
  1431. Scsi_Cmnd *cmd;
  1432. int sgcnt;
  1433. int idx;
  1434. cmd = scb->cmd;
  1435. /* Scatter-gather not used */
  1436. if( cmd->use_sg == 0 || (cmd->use_sg == 1 &&
  1437. !adapter->has_64bit_addr)) {
  1438. if (cmd->use_sg == 0) {
  1439. page = virt_to_page(cmd->request_buffer);
  1440. offset = offset_in_page(cmd->request_buffer);
  1441. length = cmd->request_bufflen;
  1442. } else {
  1443. sgl = (struct scatterlist *)cmd->request_buffer;
  1444. page = sgl->page;
  1445. offset = sgl->offset;
  1446. length = sgl->length;
  1447. }
  1448. scb->dma_h_bulkdata = pci_map_page(adapter->dev,
  1449. page, offset,
  1450. length,
  1451. scb->dma_direction);
  1452. scb->dma_type = MEGA_BULK_DATA;
  1453. /*
  1454. * We need to handle special 64-bit commands that need a
  1455. * minimum of 1 SG
  1456. */
  1457. if( adapter->has_64bit_addr ) {
  1458. scb->sgl64[0].address = scb->dma_h_bulkdata;
  1459. scb->sgl64[0].length = length;
  1460. *buf = (u32)scb->sgl_dma_addr;
  1461. *len = (u32)length;
  1462. return 1;
  1463. }
  1464. else {
  1465. *buf = (u32)scb->dma_h_bulkdata;
  1466. *len = (u32)length;
  1467. }
  1468. return 0;
  1469. }
  1470. sgl = (struct scatterlist *)cmd->request_buffer;
  1471. /*
  1472. * Copy Scatter-Gather list info into controller structure.
  1473. *
  1474. * The number of sg elements returned must not exceed our limit
  1475. */
  1476. sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
  1477. scb->dma_direction);
  1478. scb->dma_type = MEGA_SGLIST;
  1479. if( sgcnt > adapter->sglen ) BUG();
  1480. *len = 0;
  1481. for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
  1482. if( adapter->has_64bit_addr ) {
  1483. scb->sgl64[idx].address = sg_dma_address(sgl);
  1484. *len += scb->sgl64[idx].length = sg_dma_len(sgl);
  1485. }
  1486. else {
  1487. scb->sgl[idx].address = sg_dma_address(sgl);
  1488. *len += scb->sgl[idx].length = sg_dma_len(sgl);
  1489. }
  1490. }
  1491. /* Reset pointer and length fields */
  1492. *buf = scb->sgl_dma_addr;
  1493. /* Return count of SG requests */
  1494. return sgcnt;
  1495. }
  1496. /*
  1497. * mega_8_to_40ld()
  1498. *
  1499. * takes all info in AdapterInquiry structure and puts it into ProductInfo and
  1500. * Enquiry3 structures for later use
  1501. */
  1502. static void
  1503. mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  1504. mega_product_info *product_info)
  1505. {
  1506. int i;
  1507. product_info->max_commands = inquiry->adapter_info.max_commands;
  1508. enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  1509. product_info->nchannels = inquiry->adapter_info.nchannels;
  1510. for (i = 0; i < 4; i++) {
  1511. product_info->fw_version[i] =
  1512. inquiry->adapter_info.fw_version[i];
  1513. product_info->bios_version[i] =
  1514. inquiry->adapter_info.bios_version[i];
  1515. }
  1516. enquiry3->cache_flush_interval =
  1517. inquiry->adapter_info.cache_flush_interval;
  1518. product_info->dram_size = inquiry->adapter_info.dram_size;
  1519. enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  1520. for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  1521. enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  1522. enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  1523. enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  1524. }
  1525. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  1526. enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  1527. }
  1528. static inline void
  1529. mega_free_sgl(adapter_t *adapter)
  1530. {
  1531. scb_t *scb;
  1532. int i;
  1533. for(i = 0; i < adapter->max_cmds; i++) {
  1534. scb = &adapter->scb_list[i];
  1535. if( scb->sgl64 ) {
  1536. pci_free_consistent(adapter->dev,
  1537. sizeof(mega_sgl64) * adapter->sglen,
  1538. scb->sgl64,
  1539. scb->sgl_dma_addr);
  1540. scb->sgl64 = NULL;
  1541. }
  1542. if( scb->pthru ) {
  1543. pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  1544. scb->pthru, scb->pthru_dma_addr);
  1545. scb->pthru = NULL;
  1546. }
  1547. if( scb->epthru ) {
  1548. pci_free_consistent(adapter->dev,
  1549. sizeof(mega_ext_passthru),
  1550. scb->epthru, scb->epthru_dma_addr);
  1551. scb->epthru = NULL;
  1552. }
  1553. }
  1554. }
  1555. /*
  1556. * Get information about the card/driver
  1557. */
  1558. const char *
  1559. megaraid_info(struct Scsi_Host *host)
  1560. {
  1561. static char buffer[512];
  1562. adapter_t *adapter;
  1563. adapter = (adapter_t *)host->hostdata;
  1564. sprintf (buffer,
  1565. "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  1566. adapter->fw_version, adapter->product_info.max_commands,
  1567. adapter->host->max_id, adapter->host->max_channel,
  1568. adapter->host->max_lun);
  1569. return buffer;
  1570. }
  1571. /*
  1572. * Abort a previous SCSI request. Only commands on the pending list can be
  1573. * aborted. All the commands issued to the F/W must complete.
  1574. */
  1575. static int
  1576. megaraid_abort(Scsi_Cmnd *cmd)
  1577. {
  1578. adapter_t *adapter;
  1579. int rval;
  1580. adapter = (adapter_t *)cmd->device->host->hostdata;
  1581. rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  1582. /*
  1583. * This is required here to complete any completed requests
  1584. * to be communicated over to the mid layer.
  1585. */
  1586. mega_rundoneq(adapter);
  1587. return rval;
  1588. }
  1589. static int
  1590. megaraid_reset(struct scsi_cmnd *cmd)
  1591. {
  1592. adapter_t *adapter;
  1593. megacmd_t mc;
  1594. int rval;
  1595. adapter = (adapter_t *)cmd->device->host->hostdata;
  1596. #if MEGA_HAVE_CLUSTERING
  1597. mc.cmd = MEGA_CLUSTER_CMD;
  1598. mc.opcode = MEGA_RESET_RESERVATIONS;
  1599. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
  1600. printk(KERN_WARNING
  1601. "megaraid: reservation reset failed.\n");
  1602. }
  1603. else {
  1604. printk(KERN_INFO "megaraid: reservation reset.\n");
  1605. }
  1606. #endif
  1607. spin_lock_irq(&adapter->lock);
  1608. rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  1609. /*
  1610. * This is required here to complete any completed requests
  1611. * to be communicated over to the mid layer.
  1612. */
  1613. mega_rundoneq(adapter);
  1614. spin_unlock_irq(&adapter->lock);
  1615. return rval;
  1616. }
  1617. /**
  1618. * megaraid_abort_and_reset()
  1619. * @adapter - megaraid soft state
  1620. * @cmd - scsi command to be aborted or reset
  1621. * @aor - abort or reset flag
  1622. *
  1623. * Try to locate the scsi command in the pending queue. If found and is not
  1624. * issued to the controller, abort/reset it. Otherwise return failure
  1625. */
  1626. static int
  1627. megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  1628. {
  1629. struct list_head *pos, *next;
  1630. scb_t *scb;
  1631. printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
  1632. (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
  1633. cmd->cmnd[0], cmd->device->channel,
  1634. cmd->device->id, cmd->device->lun);
  1635. if(list_empty(&adapter->pending_list))
  1636. return FALSE;
  1637. list_for_each_safe(pos, next, &adapter->pending_list) {
  1638. scb = list_entry(pos, scb_t, list);
  1639. if (scb->cmd == cmd) { /* Found command */
  1640. scb->state |= aor;
  1641. /*
  1642. * Check if this command has firmare owenership. If
  1643. * yes, we cannot reset this command. Whenever, f/w
  1644. * completes this command, we will return appropriate
  1645. * status from ISR.
  1646. */
  1647. if( scb->state & SCB_ISSUED ) {
  1648. printk(KERN_WARNING
  1649. "megaraid: %s-%lx[%x], fw owner.\n",
  1650. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1651. cmd->serial_number, scb->idx);
  1652. return FALSE;
  1653. }
  1654. else {
  1655. /*
  1656. * Not yet issued! Remove from the pending
  1657. * list
  1658. */
  1659. printk(KERN_WARNING
  1660. "megaraid: %s-%lx[%x], driver owner.\n",
  1661. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1662. cmd->serial_number, scb->idx);
  1663. mega_free_scb(adapter, scb);
  1664. if( aor == SCB_ABORT ) {
  1665. cmd->result = (DID_ABORT << 16);
  1666. }
  1667. else {
  1668. cmd->result = (DID_RESET << 16);
  1669. }
  1670. list_add_tail(SCSI_LIST(cmd),
  1671. &adapter->completed_list);
  1672. return TRUE;
  1673. }
  1674. }
  1675. }
  1676. return FALSE;
  1677. }
  1678. static inline int
  1679. make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  1680. {
  1681. *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1682. if( *pdev == NULL ) return -1;
  1683. memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
  1684. if( pci_set_dma_mask(*pdev, 0xffffffff) != 0 ) {
  1685. kfree(*pdev);
  1686. return -1;
  1687. }
  1688. return 0;
  1689. }
  1690. static inline void
  1691. free_local_pdev(struct pci_dev *pdev)
  1692. {
  1693. kfree(pdev);
  1694. }
  1695. /**
  1696. * mega_allocate_inquiry()
  1697. * @dma_handle - handle returned for dma address
  1698. * @pdev - handle to pci device
  1699. *
  1700. * allocates memory for inquiry structure
  1701. */
  1702. static inline void *
  1703. mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  1704. {
  1705. return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  1706. }
  1707. static inline void
  1708. mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  1709. {
  1710. pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  1711. }
  1712. #ifdef CONFIG_PROC_FS
  1713. /* Following code handles /proc fs */
  1714. #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
  1715. S_IRUSR | S_IFREG, \
  1716. controller_proc_dir_entry, \
  1717. func, adapter)
  1718. /**
  1719. * mega_create_proc_entry()
  1720. * @index - index in soft state array
  1721. * @parent - parent node for this /proc entry
  1722. *
  1723. * Creates /proc entries for our controllers.
  1724. */
  1725. static void
  1726. mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  1727. {
  1728. struct proc_dir_entry *controller_proc_dir_entry = NULL;
  1729. u8 string[64] = { 0 };
  1730. adapter_t *adapter = hba_soft_state[index];
  1731. sprintf(string, "hba%d", adapter->host->host_no);
  1732. controller_proc_dir_entry =
  1733. adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
  1734. if(!controller_proc_dir_entry) {
  1735. printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
  1736. return;
  1737. }
  1738. adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
  1739. adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
  1740. adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
  1741. #if MEGA_HAVE_ENH_PROC
  1742. adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
  1743. adapter->proc_battery = CREATE_READ_PROC("battery-status",
  1744. proc_battery);
  1745. /*
  1746. * Display each physical drive on its channel
  1747. */
  1748. adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
  1749. proc_pdrv_ch0);
  1750. adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
  1751. proc_pdrv_ch1);
  1752. adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
  1753. proc_pdrv_ch2);
  1754. adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
  1755. proc_pdrv_ch3);
  1756. /*
  1757. * Display a set of up to 10 logical drive through each of following
  1758. * /proc entries
  1759. */
  1760. adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
  1761. proc_rdrv_10);
  1762. adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
  1763. proc_rdrv_20);
  1764. adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
  1765. proc_rdrv_30);
  1766. adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
  1767. proc_rdrv_40);
  1768. #endif
  1769. }
  1770. /**
  1771. * proc_read_config()
  1772. * @page - buffer to write the data in
  1773. * @start - where the actual data has been written in page
  1774. * @offset - same meaning as the read system call
  1775. * @count - same meaning as the read system call
  1776. * @eof - set if no more data needs to be returned
  1777. * @data - pointer to our soft state
  1778. *
  1779. * Display configuration information about the controller.
  1780. */
  1781. static int
  1782. proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
  1783. void *data)
  1784. {
  1785. adapter_t *adapter = (adapter_t *)data;
  1786. int len = 0;
  1787. len += sprintf(page+len, "%s", MEGARAID_VERSION);
  1788. if(adapter->product_info.product_name[0])
  1789. len += sprintf(page+len, "%s\n",
  1790. adapter->product_info.product_name);
  1791. len += sprintf(page+len, "Controller Type: ");
  1792. if( adapter->flag & BOARD_MEMMAP ) {
  1793. len += sprintf(page+len,
  1794. "438/466/467/471/493/518/520/531/532\n");
  1795. }
  1796. else {
  1797. len += sprintf(page+len,
  1798. "418/428/434\n");
  1799. }
  1800. if(adapter->flag & BOARD_40LD) {
  1801. len += sprintf(page+len,
  1802. "Controller Supports 40 Logical Drives\n");
  1803. }
  1804. if(adapter->flag & BOARD_64BIT) {
  1805. len += sprintf(page+len,
  1806. "Controller capable of 64-bit memory addressing\n");
  1807. }
  1808. if( adapter->has_64bit_addr ) {
  1809. len += sprintf(page+len,
  1810. "Controller using 64-bit memory addressing\n");
  1811. }
  1812. else {
  1813. len += sprintf(page+len,
  1814. "Controller is not using 64-bit memory addressing\n");
  1815. }
  1816. len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
  1817. adapter->host->irq);
  1818. len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
  1819. adapter->numldrv, adapter->product_info.nchannels);
  1820. len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
  1821. adapter->fw_version, adapter->bios_version,
  1822. adapter->product_info.dram_size);
  1823. len += sprintf(page+len,
  1824. "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
  1825. adapter->product_info.max_commands, adapter->max_cmds);
  1826. len += sprintf(page+len, "support_ext_cdb = %d\n",
  1827. adapter->support_ext_cdb);
  1828. len += sprintf(page+len, "support_random_del = %d\n",
  1829. adapter->support_random_del);
  1830. len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
  1831. adapter->boot_ldrv_enabled);
  1832. len += sprintf(page+len, "boot_ldrv = %d\n",
  1833. adapter->boot_ldrv);
  1834. len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
  1835. adapter->boot_pdrv_enabled);
  1836. len += sprintf(page+len, "boot_pdrv_ch = %d\n",
  1837. adapter->boot_pdrv_ch);
  1838. len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
  1839. adapter->boot_pdrv_tgt);
  1840. len += sprintf(page+len, "quiescent = %d\n",
  1841. atomic_read(&adapter->quiescent));
  1842. len += sprintf(page+len, "has_cluster = %d\n",
  1843. adapter->has_cluster);
  1844. len += sprintf(page+len, "\nModule Parameters:\n");
  1845. len += sprintf(page+len, "max_cmd_per_lun = %d\n",
  1846. max_cmd_per_lun);
  1847. len += sprintf(page+len, "max_sectors_per_io = %d\n",
  1848. max_sectors_per_io);
  1849. *eof = 1;
  1850. return len;
  1851. }
  1852. /**
  1853. * proc_read_stat()
  1854. * @page - buffer to write the data in
  1855. * @start - where the actual data has been written in page
  1856. * @offset - same meaning as the read system call
  1857. * @count - same meaning as the read system call
  1858. * @eof - set if no more data needs to be returned
  1859. * @data - pointer to our soft state
  1860. *
  1861. * Diaplay statistical information about the I/O activity.
  1862. */
  1863. static int
  1864. proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
  1865. void *data)
  1866. {
  1867. adapter_t *adapter;
  1868. int len;
  1869. int i;
  1870. i = 0; /* avoid compilation warnings */
  1871. len = 0;
  1872. adapter = (adapter_t *)data;
  1873. len = sprintf(page, "Statistical Information for this controller\n");
  1874. len += sprintf(page+len, "pend_cmds = %d\n",
  1875. atomic_read(&adapter->pend_cmds));
  1876. #if MEGA_HAVE_STATS
  1877. for(i = 0; i < adapter->numldrv; i++) {
  1878. len += sprintf(page+len, "Logical Drive %d:\n", i);
  1879. len += sprintf(page+len,
  1880. "\tReads Issued = %lu, Writes Issued = %lu\n",
  1881. adapter->nreads[i], adapter->nwrites[i]);
  1882. len += sprintf(page+len,
  1883. "\tSectors Read = %lu, Sectors Written = %lu\n",
  1884. adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  1885. len += sprintf(page+len,
  1886. "\tRead errors = %lu, Write errors = %lu\n\n",
  1887. adapter->rd_errors[i], adapter->wr_errors[i]);
  1888. }
  1889. #else
  1890. len += sprintf(page+len,
  1891. "IO and error counters not compiled in driver.\n");
  1892. #endif
  1893. *eof = 1;
  1894. return len;
  1895. }
  1896. /**
  1897. * proc_read_mbox()
  1898. * @page - buffer to write the data in
  1899. * @start - where the actual data has been written in page
  1900. * @offset - same meaning as the read system call
  1901. * @count - same meaning as the read system call
  1902. * @eof - set if no more data needs to be returned
  1903. * @data - pointer to our soft state
  1904. *
  1905. * Display mailbox information for the last command issued. This information
  1906. * is good for debugging.
  1907. */
  1908. static int
  1909. proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
  1910. void *data)
  1911. {
  1912. adapter_t *adapter = (adapter_t *)data;
  1913. volatile mbox_t *mbox = adapter->mbox;
  1914. int len = 0;
  1915. len = sprintf(page, "Contents of Mail Box Structure\n");
  1916. len += sprintf(page+len, " Fw Command = 0x%02x\n",
  1917. mbox->m_out.cmd);
  1918. len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
  1919. mbox->m_out.cmdid);
  1920. len += sprintf(page+len, " No of Sectors= %04d\n",
  1921. mbox->m_out.numsectors);
  1922. len += sprintf(page+len, " LBA = 0x%02x\n",
  1923. mbox->m_out.lba);
  1924. len += sprintf(page+len, " DTA = 0x%08x\n",
  1925. mbox->m_out.xferaddr);
  1926. len += sprintf(page+len, " Logical Drive= 0x%02x\n",
  1927. mbox->m_out.logdrv);
  1928. len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
  1929. mbox->m_out.numsgelements);
  1930. len += sprintf(page+len, " Busy = %01x\n",
  1931. mbox->m_in.busy);
  1932. len += sprintf(page+len, " Status = 0x%02x\n",
  1933. mbox->m_in.status);
  1934. *eof = 1;
  1935. return len;
  1936. }
  1937. /**
  1938. * proc_rebuild_rate()
  1939. * @page - buffer to write the data in
  1940. * @start - where the actual data has been written in page
  1941. * @offset - same meaning as the read system call
  1942. * @count - same meaning as the read system call
  1943. * @eof - set if no more data needs to be returned
  1944. * @data - pointer to our soft state
  1945. *
  1946. * Display current rebuild rate
  1947. */
  1948. static int
  1949. proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
  1950. void *data)
  1951. {
  1952. adapter_t *adapter = (adapter_t *)data;
  1953. dma_addr_t dma_handle;
  1954. caddr_t inquiry;
  1955. struct pci_dev *pdev;
  1956. int len = 0;
  1957. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1958. *eof = 1;
  1959. return len;
  1960. }
  1961. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1962. free_local_pdev(pdev);
  1963. *eof = 1;
  1964. return len;
  1965. }
  1966. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1967. len = sprintf(page, "Adapter inquiry failed.\n");
  1968. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1969. mega_free_inquiry(inquiry, dma_handle, pdev);
  1970. free_local_pdev(pdev);
  1971. *eof = 1;
  1972. return len;
  1973. }
  1974. if( adapter->flag & BOARD_40LD ) {
  1975. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1976. ((mega_inquiry3 *)inquiry)->rebuild_rate);
  1977. }
  1978. else {
  1979. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1980. ((mraid_ext_inquiry *)
  1981. inquiry)->raid_inq.adapter_info.rebuild_rate);
  1982. }
  1983. mega_free_inquiry(inquiry, dma_handle, pdev);
  1984. free_local_pdev(pdev);
  1985. *eof = 1;
  1986. return len;
  1987. }
  1988. /**
  1989. * proc_battery()
  1990. * @page - buffer to write the data in
  1991. * @start - where the actual data has been written in page
  1992. * @offset - same meaning as the read system call
  1993. * @count - same meaning as the read system call
  1994. * @eof - set if no more data needs to be returned
  1995. * @data - pointer to our soft state
  1996. *
  1997. * Display information about the battery module on the controller.
  1998. */
  1999. static int
  2000. proc_battery(char *page, char **start, off_t offset, int count, int *eof,
  2001. void *data)
  2002. {
  2003. adapter_t *adapter = (adapter_t *)data;
  2004. dma_addr_t dma_handle;
  2005. caddr_t inquiry;
  2006. struct pci_dev *pdev;
  2007. u8 battery_status = 0;
  2008. char str[256];
  2009. int len = 0;
  2010. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2011. *eof = 1;
  2012. return len;
  2013. }
  2014. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2015. free_local_pdev(pdev);
  2016. *eof = 1;
  2017. return len;
  2018. }
  2019. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2020. len = sprintf(page, "Adapter inquiry failed.\n");
  2021. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2022. mega_free_inquiry(inquiry, dma_handle, pdev);
  2023. free_local_pdev(pdev);
  2024. *eof = 1;
  2025. return len;
  2026. }
  2027. if( adapter->flag & BOARD_40LD ) {
  2028. battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  2029. }
  2030. else {
  2031. battery_status = ((mraid_ext_inquiry *)inquiry)->
  2032. raid_inq.adapter_info.battery_status;
  2033. }
  2034. /*
  2035. * Decode the battery status
  2036. */
  2037. sprintf(str, "Battery Status:[%d]", battery_status);
  2038. if(battery_status == MEGA_BATT_CHARGE_DONE)
  2039. strcat(str, " Charge Done");
  2040. if(battery_status & MEGA_BATT_MODULE_MISSING)
  2041. strcat(str, " Module Missing");
  2042. if(battery_status & MEGA_BATT_LOW_VOLTAGE)
  2043. strcat(str, " Low Voltage");
  2044. if(battery_status & MEGA_BATT_TEMP_HIGH)
  2045. strcat(str, " Temperature High");
  2046. if(battery_status & MEGA_BATT_PACK_MISSING)
  2047. strcat(str, " Pack Missing");
  2048. if(battery_status & MEGA_BATT_CHARGE_INPROG)
  2049. strcat(str, " Charge In-progress");
  2050. if(battery_status & MEGA_BATT_CHARGE_FAIL)
  2051. strcat(str, " Charge Fail");
  2052. if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
  2053. strcat(str, " Cycles Exceeded");
  2054. len = sprintf(page, "%s\n", str);
  2055. mega_free_inquiry(inquiry, dma_handle, pdev);
  2056. free_local_pdev(pdev);
  2057. *eof = 1;
  2058. return len;
  2059. }
  2060. /**
  2061. * proc_pdrv_ch0()
  2062. * @page - buffer to write the data in
  2063. * @start - where the actual data has been written in page
  2064. * @offset - same meaning as the read system call
  2065. * @count - same meaning as the read system call
  2066. * @eof - set if no more data needs to be returned
  2067. * @data - pointer to our soft state
  2068. *
  2069. * Display information about the physical drives on physical channel 0.
  2070. */
  2071. static int
  2072. proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
  2073. void *data)
  2074. {
  2075. adapter_t *adapter = (adapter_t *)data;
  2076. *eof = 1;
  2077. return (proc_pdrv(adapter, page, 0));
  2078. }
  2079. /**
  2080. * proc_pdrv_ch1()
  2081. * @page - buffer to write the data in
  2082. * @start - where the actual data has been written in page
  2083. * @offset - same meaning as the read system call
  2084. * @count - same meaning as the read system call
  2085. * @eof - set if no more data needs to be returned
  2086. * @data - pointer to our soft state
  2087. *
  2088. * Display information about the physical drives on physical channel 1.
  2089. */
  2090. static int
  2091. proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
  2092. void *data)
  2093. {
  2094. adapter_t *adapter = (adapter_t *)data;
  2095. *eof = 1;
  2096. return (proc_pdrv(adapter, page, 1));
  2097. }
  2098. /**
  2099. * proc_pdrv_ch2()
  2100. * @page - buffer to write the data in
  2101. * @start - where the actual data has been written in page
  2102. * @offset - same meaning as the read system call
  2103. * @count - same meaning as the read system call
  2104. * @eof - set if no more data needs to be returned
  2105. * @data - pointer to our soft state
  2106. *
  2107. * Display information about the physical drives on physical channel 2.
  2108. */
  2109. static int
  2110. proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
  2111. void *data)
  2112. {
  2113. adapter_t *adapter = (adapter_t *)data;
  2114. *eof = 1;
  2115. return (proc_pdrv(adapter, page, 2));
  2116. }
  2117. /**
  2118. * proc_pdrv_ch3()
  2119. * @page - buffer to write the data in
  2120. * @start - where the actual data has been written in page
  2121. * @offset - same meaning as the read system call
  2122. * @count - same meaning as the read system call
  2123. * @eof - set if no more data needs to be returned
  2124. * @data - pointer to our soft state
  2125. *
  2126. * Display information about the physical drives on physical channel 3.
  2127. */
  2128. static int
  2129. proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
  2130. void *data)
  2131. {
  2132. adapter_t *adapter = (adapter_t *)data;
  2133. *eof = 1;
  2134. return (proc_pdrv(adapter, page, 3));
  2135. }
  2136. /**
  2137. * proc_pdrv()
  2138. * @page - buffer to write the data in
  2139. * @adapter - pointer to our soft state
  2140. *
  2141. * Display information about the physical drives.
  2142. */
  2143. static int
  2144. proc_pdrv(adapter_t *adapter, char *page, int channel)
  2145. {
  2146. dma_addr_t dma_handle;
  2147. char *scsi_inq;
  2148. dma_addr_t scsi_inq_dma_handle;
  2149. caddr_t inquiry;
  2150. struct pci_dev *pdev;
  2151. u8 *pdrv_state;
  2152. u8 state;
  2153. int tgt;
  2154. int max_channels;
  2155. int len = 0;
  2156. char str[80];
  2157. int i;
  2158. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2159. return len;
  2160. }
  2161. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2162. goto free_pdev;
  2163. }
  2164. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2165. len = sprintf(page, "Adapter inquiry failed.\n");
  2166. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2167. goto free_inquiry;
  2168. }
  2169. scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
  2170. if( scsi_inq == NULL ) {
  2171. len = sprintf(page, "memory not available for scsi inq.\n");
  2172. goto free_inquiry;
  2173. }
  2174. if( adapter->flag & BOARD_40LD ) {
  2175. pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  2176. }
  2177. else {
  2178. pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2179. raid_inq.pdrv_info.pdrv_state;
  2180. }
  2181. max_channels = adapter->product_info.nchannels;
  2182. if( channel >= max_channels ) {
  2183. goto free_pci;
  2184. }
  2185. for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  2186. i = channel*16 + tgt;
  2187. state = *(pdrv_state + i);
  2188. switch( state & 0x0F ) {
  2189. case PDRV_ONLINE:
  2190. sprintf(str,
  2191. "Channel:%2d Id:%2d State: Online",
  2192. channel, tgt);
  2193. break;
  2194. case PDRV_FAILED:
  2195. sprintf(str,
  2196. "Channel:%2d Id:%2d State: Failed",
  2197. channel, tgt);
  2198. break;
  2199. case PDRV_RBLD:
  2200. sprintf(str,
  2201. "Channel:%2d Id:%2d State: Rebuild",
  2202. channel, tgt);
  2203. break;
  2204. case PDRV_HOTSPARE:
  2205. sprintf(str,
  2206. "Channel:%2d Id:%2d State: Hot spare",
  2207. channel, tgt);
  2208. break;
  2209. default:
  2210. sprintf(str,
  2211. "Channel:%2d Id:%2d State: Un-configured",
  2212. channel, tgt);
  2213. break;
  2214. }
  2215. /*
  2216. * This interface displays inquiries for disk drives
  2217. * only. Inquries for logical drives and non-disk
  2218. * devices are available through /proc/scsi/scsi
  2219. */
  2220. memset(scsi_inq, 0, 256);
  2221. if( mega_internal_dev_inquiry(adapter, channel, tgt,
  2222. scsi_inq_dma_handle) ||
  2223. (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  2224. continue;
  2225. }
  2226. /*
  2227. * Check for overflow. We print less than 240
  2228. * characters for inquiry
  2229. */
  2230. if( (len + 240) >= PAGE_SIZE ) break;
  2231. len += sprintf(page+len, "%s.\n", str);
  2232. len += mega_print_inquiry(page+len, scsi_inq);
  2233. }
  2234. free_pci:
  2235. pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  2236. free_inquiry:
  2237. mega_free_inquiry(inquiry, dma_handle, pdev);
  2238. free_pdev:
  2239. free_local_pdev(pdev);
  2240. return len;
  2241. }
  2242. /*
  2243. * Display scsi inquiry
  2244. */
  2245. static int
  2246. mega_print_inquiry(char *page, char *scsi_inq)
  2247. {
  2248. int len = 0;
  2249. int i;
  2250. len = sprintf(page, " Vendor: ");
  2251. for( i = 8; i < 16; i++ ) {
  2252. len += sprintf(page+len, "%c", scsi_inq[i]);
  2253. }
  2254. len += sprintf(page+len, " Model: ");
  2255. for( i = 16; i < 32; i++ ) {
  2256. len += sprintf(page+len, "%c", scsi_inq[i]);
  2257. }
  2258. len += sprintf(page+len, " Rev: ");
  2259. for( i = 32; i < 36; i++ ) {
  2260. len += sprintf(page+len, "%c", scsi_inq[i]);
  2261. }
  2262. len += sprintf(page+len, "\n");
  2263. i = scsi_inq[0] & 0x1f;
  2264. len += sprintf(page+len, " Type: %s ",
  2265. i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
  2266. "Unknown ");
  2267. len += sprintf(page+len,
  2268. " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
  2269. if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  2270. len += sprintf(page+len, " CCS\n");
  2271. else
  2272. len += sprintf(page+len, "\n");
  2273. return len;
  2274. }
  2275. /**
  2276. * proc_rdrv_10()
  2277. * @page - buffer to write the data in
  2278. * @start - where the actual data has been written in page
  2279. * @offset - same meaning as the read system call
  2280. * @count - same meaning as the read system call
  2281. * @eof - set if no more data needs to be returned
  2282. * @data - pointer to our soft state
  2283. *
  2284. * Display real time information about the logical drives 0 through 9.
  2285. */
  2286. static int
  2287. proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
  2288. void *data)
  2289. {
  2290. adapter_t *adapter = (adapter_t *)data;
  2291. *eof = 1;
  2292. return (proc_rdrv(adapter, page, 0, 9));
  2293. }
  2294. /**
  2295. * proc_rdrv_20()
  2296. * @page - buffer to write the data in
  2297. * @start - where the actual data has been written in page
  2298. * @offset - same meaning as the read system call
  2299. * @count - same meaning as the read system call
  2300. * @eof - set if no more data needs to be returned
  2301. * @data - pointer to our soft state
  2302. *
  2303. * Display real time information about the logical drives 0 through 9.
  2304. */
  2305. static int
  2306. proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
  2307. void *data)
  2308. {
  2309. adapter_t *adapter = (adapter_t *)data;
  2310. *eof = 1;
  2311. return (proc_rdrv(adapter, page, 10, 19));
  2312. }
  2313. /**
  2314. * proc_rdrv_30()
  2315. * @page - buffer to write the data in
  2316. * @start - where the actual data has been written in page
  2317. * @offset - same meaning as the read system call
  2318. * @count - same meaning as the read system call
  2319. * @eof - set if no more data needs to be returned
  2320. * @data - pointer to our soft state
  2321. *
  2322. * Display real time information about the logical drives 0 through 9.
  2323. */
  2324. static int
  2325. proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
  2326. void *data)
  2327. {
  2328. adapter_t *adapter = (adapter_t *)data;
  2329. *eof = 1;
  2330. return (proc_rdrv(adapter, page, 20, 29));
  2331. }
  2332. /**
  2333. * proc_rdrv_40()
  2334. * @page - buffer to write the data in
  2335. * @start - where the actual data has been written in page
  2336. * @offset - same meaning as the read system call
  2337. * @count - same meaning as the read system call
  2338. * @eof - set if no more data needs to be returned
  2339. * @data - pointer to our soft state
  2340. *
  2341. * Display real time information about the logical drives 0 through 9.
  2342. */
  2343. static int
  2344. proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
  2345. void *data)
  2346. {
  2347. adapter_t *adapter = (adapter_t *)data;
  2348. *eof = 1;
  2349. return (proc_rdrv(adapter, page, 30, 39));
  2350. }
  2351. /**
  2352. * proc_rdrv()
  2353. * @page - buffer to write the data in
  2354. * @adapter - pointer to our soft state
  2355. * @start - starting logical drive to display
  2356. * @end - ending logical drive to display
  2357. *
  2358. * We do not print the inquiry information since its already available through
  2359. * /proc/scsi/scsi interface
  2360. */
  2361. static int
  2362. proc_rdrv(adapter_t *adapter, char *page, int start, int end )
  2363. {
  2364. dma_addr_t dma_handle;
  2365. logdrv_param *lparam;
  2366. megacmd_t mc;
  2367. char *disk_array;
  2368. dma_addr_t disk_array_dma_handle;
  2369. caddr_t inquiry;
  2370. struct pci_dev *pdev;
  2371. u8 *rdrv_state;
  2372. int num_ldrv;
  2373. u32 array_sz;
  2374. int len = 0;
  2375. int i;
  2376. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2377. return len;
  2378. }
  2379. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2380. free_local_pdev(pdev);
  2381. return len;
  2382. }
  2383. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2384. len = sprintf(page, "Adapter inquiry failed.\n");
  2385. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2386. mega_free_inquiry(inquiry, dma_handle, pdev);
  2387. free_local_pdev(pdev);
  2388. return len;
  2389. }
  2390. memset(&mc, 0, sizeof(megacmd_t));
  2391. if( adapter->flag & BOARD_40LD ) {
  2392. array_sz = sizeof(disk_array_40ld);
  2393. rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  2394. num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  2395. }
  2396. else {
  2397. array_sz = sizeof(disk_array_8ld);
  2398. rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2399. raid_inq.logdrv_info.ldrv_state;
  2400. num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  2401. raid_inq.logdrv_info.num_ldrv;
  2402. }
  2403. disk_array = pci_alloc_consistent(pdev, array_sz,
  2404. &disk_array_dma_handle);
  2405. if( disk_array == NULL ) {
  2406. len = sprintf(page, "memory not available.\n");
  2407. mega_free_inquiry(inquiry, dma_handle, pdev);
  2408. free_local_pdev(pdev);
  2409. return len;
  2410. }
  2411. mc.xferaddr = (u32)disk_array_dma_handle;
  2412. if( adapter->flag & BOARD_40LD ) {
  2413. mc.cmd = FC_NEW_CONFIG;
  2414. mc.opcode = OP_DCMD_READ_CONFIG;
  2415. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
  2416. len = sprintf(page, "40LD read config failed.\n");
  2417. mega_free_inquiry(inquiry, dma_handle, pdev);
  2418. pci_free_consistent(pdev, array_sz, disk_array,
  2419. disk_array_dma_handle);
  2420. free_local_pdev(pdev);
  2421. return len;
  2422. }
  2423. }
  2424. else {
  2425. mc.cmd = NEW_READ_CONFIG_8LD;
  2426. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
  2427. mc.cmd = READ_CONFIG_8LD;
  2428. if( mega_internal_command(adapter, LOCK_INT, &mc,
  2429. NULL) ){
  2430. len = sprintf(page,
  2431. "8LD read config failed.\n");
  2432. mega_free_inquiry(inquiry, dma_handle, pdev);
  2433. pci_free_consistent(pdev, array_sz,
  2434. disk_array,
  2435. disk_array_dma_handle);
  2436. free_local_pdev(pdev);
  2437. return len;
  2438. }
  2439. }
  2440. }
  2441. for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  2442. if( adapter->flag & BOARD_40LD ) {
  2443. lparam =
  2444. &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  2445. }
  2446. else {
  2447. lparam =
  2448. &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  2449. }
  2450. /*
  2451. * Check for overflow. We print less than 240 characters for
  2452. * information about each logical drive.
  2453. */
  2454. if( (len + 240) >= PAGE_SIZE ) break;
  2455. len += sprintf(page+len, "Logical drive:%2d:, ", i);
  2456. switch( rdrv_state[i] & 0x0F ) {
  2457. case RDRV_OFFLINE:
  2458. len += sprintf(page+len, "state: offline");
  2459. break;
  2460. case RDRV_DEGRADED:
  2461. len += sprintf(page+len, "state: degraded");
  2462. break;
  2463. case RDRV_OPTIMAL:
  2464. len += sprintf(page+len, "state: optimal");
  2465. break;
  2466. case RDRV_DELETED:
  2467. len += sprintf(page+len, "state: deleted");
  2468. break;
  2469. default:
  2470. len += sprintf(page+len, "state: unknown");
  2471. break;
  2472. }
  2473. /*
  2474. * Check if check consistency or initialization is going on
  2475. * for this logical drive.
  2476. */
  2477. if( (rdrv_state[i] & 0xF0) == 0x20 ) {
  2478. len += sprintf(page+len,
  2479. ", check-consistency in progress");
  2480. }
  2481. else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
  2482. len += sprintf(page+len,
  2483. ", initialization in progress");
  2484. }
  2485. len += sprintf(page+len, "\n");
  2486. len += sprintf(page+len, "Span depth:%3d, ",
  2487. lparam->span_depth);
  2488. len += sprintf(page+len, "RAID level:%3d, ",
  2489. lparam->level);
  2490. len += sprintf(page+len, "Stripe size:%3d, ",
  2491. lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  2492. len += sprintf(page+len, "Row size:%3d\n",
  2493. lparam->row_size);
  2494. len += sprintf(page+len, "Read Policy: ");
  2495. switch(lparam->read_ahead) {
  2496. case NO_READ_AHEAD:
  2497. len += sprintf(page+len, "No read ahead, ");
  2498. break;
  2499. case READ_AHEAD:
  2500. len += sprintf(page+len, "Read ahead, ");
  2501. break;
  2502. case ADAP_READ_AHEAD:
  2503. len += sprintf(page+len, "Adaptive, ");
  2504. break;
  2505. }
  2506. len += sprintf(page+len, "Write Policy: ");
  2507. switch(lparam->write_mode) {
  2508. case WRMODE_WRITE_THRU:
  2509. len += sprintf(page+len, "Write thru, ");
  2510. break;
  2511. case WRMODE_WRITE_BACK:
  2512. len += sprintf(page+len, "Write back, ");
  2513. break;
  2514. }
  2515. len += sprintf(page+len, "Cache Policy: ");
  2516. switch(lparam->direct_io) {
  2517. case CACHED_IO:
  2518. len += sprintf(page+len, "Cached IO\n\n");
  2519. break;
  2520. case DIRECT_IO:
  2521. len += sprintf(page+len, "Direct IO\n\n");
  2522. break;
  2523. }
  2524. }
  2525. mega_free_inquiry(inquiry, dma_handle, pdev);
  2526. pci_free_consistent(pdev, array_sz, disk_array,
  2527. disk_array_dma_handle);
  2528. free_local_pdev(pdev);
  2529. return len;
  2530. }
  2531. #endif
  2532. /**
  2533. * megaraid_biosparam()
  2534. *
  2535. * Return the disk geometry for a particular disk
  2536. */
  2537. static int
  2538. megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  2539. sector_t capacity, int geom[])
  2540. {
  2541. adapter_t *adapter;
  2542. unsigned char *bh;
  2543. int heads;
  2544. int sectors;
  2545. int cylinders;
  2546. int rval;
  2547. /* Get pointer to host config structure */
  2548. adapter = (adapter_t *)sdev->host->hostdata;
  2549. if (IS_RAID_CH(adapter, sdev->channel)) {
  2550. /* Default heads (64) & sectors (32) */
  2551. heads = 64;
  2552. sectors = 32;
  2553. cylinders = (ulong)capacity / (heads * sectors);
  2554. /*
  2555. * Handle extended translation size for logical drives
  2556. * > 1Gb
  2557. */
  2558. if ((ulong)capacity >= 0x200000) {
  2559. heads = 255;
  2560. sectors = 63;
  2561. cylinders = (ulong)capacity / (heads * sectors);
  2562. }
  2563. /* return result */
  2564. geom[0] = heads;
  2565. geom[1] = sectors;
  2566. geom[2] = cylinders;
  2567. }
  2568. else {
  2569. bh = scsi_bios_ptable(bdev);
  2570. if( bh ) {
  2571. rval = scsi_partsize(bh, capacity,
  2572. &geom[2], &geom[0], &geom[1]);
  2573. kfree(bh);
  2574. if( rval != -1 )
  2575. return rval;
  2576. }
  2577. printk(KERN_INFO
  2578. "megaraid: invalid partition on this disk on channel %d\n",
  2579. sdev->channel);
  2580. /* Default heads (64) & sectors (32) */
  2581. heads = 64;
  2582. sectors = 32;
  2583. cylinders = (ulong)capacity / (heads * sectors);
  2584. /* Handle extended translation size for logical drives > 1Gb */
  2585. if ((ulong)capacity >= 0x200000) {
  2586. heads = 255;
  2587. sectors = 63;
  2588. cylinders = (ulong)capacity / (heads * sectors);
  2589. }
  2590. /* return result */
  2591. geom[0] = heads;
  2592. geom[1] = sectors;
  2593. geom[2] = cylinders;
  2594. }
  2595. return 0;
  2596. }
  2597. /**
  2598. * mega_init_scb()
  2599. * @adapter - pointer to our soft state
  2600. *
  2601. * Allocate memory for the various pointers in the scb structures:
  2602. * scatter-gather list pointer, passthru and extended passthru structure
  2603. * pointers.
  2604. */
  2605. static int
  2606. mega_init_scb(adapter_t *adapter)
  2607. {
  2608. scb_t *scb;
  2609. int i;
  2610. for( i = 0; i < adapter->max_cmds; i++ ) {
  2611. scb = &adapter->scb_list[i];
  2612. scb->sgl64 = NULL;
  2613. scb->sgl = NULL;
  2614. scb->pthru = NULL;
  2615. scb->epthru = NULL;
  2616. }
  2617. for( i = 0; i < adapter->max_cmds; i++ ) {
  2618. scb = &adapter->scb_list[i];
  2619. scb->idx = i;
  2620. scb->sgl64 = pci_alloc_consistent(adapter->dev,
  2621. sizeof(mega_sgl64) * adapter->sglen,
  2622. &scb->sgl_dma_addr);
  2623. scb->sgl = (mega_sglist *)scb->sgl64;
  2624. if( !scb->sgl ) {
  2625. printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
  2626. mega_free_sgl(adapter);
  2627. return -1;
  2628. }
  2629. scb->pthru = pci_alloc_consistent(adapter->dev,
  2630. sizeof(mega_passthru),
  2631. &scb->pthru_dma_addr);
  2632. if( !scb->pthru ) {
  2633. printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
  2634. mega_free_sgl(adapter);
  2635. return -1;
  2636. }
  2637. scb->epthru = pci_alloc_consistent(adapter->dev,
  2638. sizeof(mega_ext_passthru),
  2639. &scb->epthru_dma_addr);
  2640. if( !scb->epthru ) {
  2641. printk(KERN_WARNING
  2642. "Can't allocate extended passthru.\n");
  2643. mega_free_sgl(adapter);
  2644. return -1;
  2645. }
  2646. scb->dma_type = MEGA_DMA_TYPE_NONE;
  2647. /*
  2648. * Link to free list
  2649. * lock not required since we are loading the driver, so no
  2650. * commands possible right now.
  2651. */
  2652. scb->state = SCB_FREE;
  2653. scb->cmd = NULL;
  2654. list_add(&scb->list, &adapter->free_list);
  2655. }
  2656. return 0;
  2657. }
  2658. /**
  2659. * megadev_open()
  2660. * @inode - unused
  2661. * @filep - unused
  2662. *
  2663. * Routines for the character/ioctl interface to the driver. Find out if this
  2664. * is a valid open. If yes, increment the module use count so that it cannot
  2665. * be unloaded.
  2666. */
  2667. static int
  2668. megadev_open (struct inode *inode, struct file *filep)
  2669. {
  2670. /*
  2671. * Only allow superuser to access private ioctl interface
  2672. */
  2673. if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  2674. return 0;
  2675. }
  2676. /**
  2677. * megadev_ioctl()
  2678. * @inode - Our device inode
  2679. * @filep - unused
  2680. * @cmd - ioctl command
  2681. * @arg - user buffer
  2682. *
  2683. * ioctl entry point for our private ioctl interface. We move the data in from
  2684. * the user space, prepare the command (if necessary, convert the old MIMD
  2685. * ioctl to new ioctl command), and issue a synchronous command to the
  2686. * controller.
  2687. */
  2688. static int
  2689. megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  2690. unsigned long arg)
  2691. {
  2692. adapter_t *adapter;
  2693. nitioctl_t uioc;
  2694. int adapno;
  2695. int rval;
  2696. mega_passthru __user *upthru; /* user address for passthru */
  2697. mega_passthru *pthru; /* copy user passthru here */
  2698. dma_addr_t pthru_dma_hndl;
  2699. void *data = NULL; /* data to be transferred */
  2700. dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
  2701. megacmd_t mc;
  2702. megastat_t __user *ustats;
  2703. int num_ldrv;
  2704. u32 uxferaddr = 0;
  2705. struct pci_dev *pdev;
  2706. ustats = NULL; /* avoid compilation warnings */
  2707. num_ldrv = 0;
  2708. /*
  2709. * Make sure only USCSICMD are issued through this interface.
  2710. * MIMD application would still fire different command.
  2711. */
  2712. if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  2713. return -EINVAL;
  2714. }
  2715. /*
  2716. * Check and convert a possible MIMD command to NIT command.
  2717. * mega_m_to_n() copies the data from the user space, so we do not
  2718. * have to do it here.
  2719. * NOTE: We will need some user address to copyout the data, therefore
  2720. * the inteface layer will also provide us with the required user
  2721. * addresses.
  2722. */
  2723. memset(&uioc, 0, sizeof(nitioctl_t));
  2724. if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  2725. return rval;
  2726. switch( uioc.opcode ) {
  2727. case GET_DRIVER_VER:
  2728. if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  2729. return (-EFAULT);
  2730. break;
  2731. case GET_N_ADAP:
  2732. if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  2733. return (-EFAULT);
  2734. /*
  2735. * Shucks. MIMD interface returns a positive value for number
  2736. * of adapters. TODO: Change it to return 0 when there is no
  2737. * applicatio using mimd interface.
  2738. */
  2739. return hba_count;
  2740. case GET_ADAP_INFO:
  2741. /*
  2742. * Which adapter
  2743. */
  2744. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2745. return (-ENODEV);
  2746. if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  2747. sizeof(struct mcontroller)) )
  2748. return (-EFAULT);
  2749. break;
  2750. #if MEGA_HAVE_STATS
  2751. case GET_STATS:
  2752. /*
  2753. * Which adapter
  2754. */
  2755. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2756. return (-ENODEV);
  2757. adapter = hba_soft_state[adapno];
  2758. ustats = uioc.uioc_uaddr;
  2759. if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  2760. return (-EFAULT);
  2761. /*
  2762. * Check for the validity of the logical drive number
  2763. */
  2764. if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  2765. if( copy_to_user(ustats->nreads, adapter->nreads,
  2766. num_ldrv*sizeof(u32)) )
  2767. return -EFAULT;
  2768. if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  2769. num_ldrv*sizeof(u32)) )
  2770. return -EFAULT;
  2771. if( copy_to_user(ustats->nwrites, adapter->nwrites,
  2772. num_ldrv*sizeof(u32)) )
  2773. return -EFAULT;
  2774. if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  2775. num_ldrv*sizeof(u32)) )
  2776. return -EFAULT;
  2777. if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  2778. num_ldrv*sizeof(u32)) )
  2779. return -EFAULT;
  2780. if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  2781. num_ldrv*sizeof(u32)) )
  2782. return -EFAULT;
  2783. return 0;
  2784. #endif
  2785. case MBOX_CMD:
  2786. /*
  2787. * Which adapter
  2788. */
  2789. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2790. return (-ENODEV);
  2791. adapter = hba_soft_state[adapno];
  2792. /*
  2793. * Deletion of logical drive is a special case. The adapter
  2794. * should be quiescent before this command is issued.
  2795. */
  2796. if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  2797. uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  2798. /*
  2799. * Do we support this feature
  2800. */
  2801. if( !adapter->support_random_del ) {
  2802. printk(KERN_WARNING "megaraid: logdrv ");
  2803. printk("delete on non-supporting F/W.\n");
  2804. return (-EINVAL);
  2805. }
  2806. rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  2807. if( rval == 0 ) {
  2808. memset(&mc, 0, sizeof(megacmd_t));
  2809. mc.status = rval;
  2810. rval = mega_n_to_m((void __user *)arg, &mc);
  2811. }
  2812. return rval;
  2813. }
  2814. /*
  2815. * This interface only support the regular passthru commands.
  2816. * Reject extended passthru and 64-bit passthru
  2817. */
  2818. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  2819. uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  2820. printk(KERN_WARNING "megaraid: rejected passthru.\n");
  2821. return (-EINVAL);
  2822. }
  2823. /*
  2824. * For all internal commands, the buffer must be allocated in
  2825. * <4GB address range
  2826. */
  2827. if( make_local_pdev(adapter, &pdev) != 0 )
  2828. return -EIO;
  2829. /* Is it a passthru command or a DCMD */
  2830. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  2831. /* Passthru commands */
  2832. pthru = pci_alloc_consistent(pdev,
  2833. sizeof(mega_passthru),
  2834. &pthru_dma_hndl);
  2835. if( pthru == NULL ) {
  2836. free_local_pdev(pdev);
  2837. return (-ENOMEM);
  2838. }
  2839. /*
  2840. * The user passthru structure
  2841. */
  2842. upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
  2843. /*
  2844. * Copy in the user passthru here.
  2845. */
  2846. if( copy_from_user(pthru, upthru,
  2847. sizeof(mega_passthru)) ) {
  2848. pci_free_consistent(pdev,
  2849. sizeof(mega_passthru), pthru,
  2850. pthru_dma_hndl);
  2851. free_local_pdev(pdev);
  2852. return (-EFAULT);
  2853. }
  2854. /*
  2855. * Is there a data transfer
  2856. */
  2857. if( pthru->dataxferlen ) {
  2858. data = pci_alloc_consistent(pdev,
  2859. pthru->dataxferlen,
  2860. &data_dma_hndl);
  2861. if( data == NULL ) {
  2862. pci_free_consistent(pdev,
  2863. sizeof(mega_passthru),
  2864. pthru,
  2865. pthru_dma_hndl);
  2866. free_local_pdev(pdev);
  2867. return (-ENOMEM);
  2868. }
  2869. /*
  2870. * Save the user address and point the kernel
  2871. * address at just allocated memory
  2872. */
  2873. uxferaddr = pthru->dataxferaddr;
  2874. pthru->dataxferaddr = data_dma_hndl;
  2875. }
  2876. /*
  2877. * Is data coming down-stream
  2878. */
  2879. if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  2880. /*
  2881. * Get the user data
  2882. */
  2883. if( copy_from_user(data, (char __user *)uxferaddr,
  2884. pthru->dataxferlen) ) {
  2885. rval = (-EFAULT);
  2886. goto freemem_and_return;
  2887. }
  2888. }
  2889. memset(&mc, 0, sizeof(megacmd_t));
  2890. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  2891. mc.xferaddr = (u32)pthru_dma_hndl;
  2892. /*
  2893. * Issue the command
  2894. */
  2895. mega_internal_command(adapter, LOCK_INT, &mc, pthru);
  2896. rval = mega_n_to_m((void __user *)arg, &mc);
  2897. if( rval ) goto freemem_and_return;
  2898. /*
  2899. * Is data going up-stream
  2900. */
  2901. if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
  2902. if( copy_to_user((char __user *)uxferaddr, data,
  2903. pthru->dataxferlen) ) {
  2904. rval = (-EFAULT);
  2905. }
  2906. }
  2907. /*
  2908. * Send the request sense data also, irrespective of
  2909. * whether the user has asked for it or not.
  2910. */
  2911. copy_to_user(upthru->reqsensearea,
  2912. pthru->reqsensearea, 14);
  2913. freemem_and_return:
  2914. if( pthru->dataxferlen ) {
  2915. pci_free_consistent(pdev,
  2916. pthru->dataxferlen, data,
  2917. data_dma_hndl);
  2918. }
  2919. pci_free_consistent(pdev, sizeof(mega_passthru),
  2920. pthru, pthru_dma_hndl);
  2921. free_local_pdev(pdev);
  2922. return rval;
  2923. }
  2924. else {
  2925. /* DCMD commands */
  2926. /*
  2927. * Is there a data transfer
  2928. */
  2929. if( uioc.xferlen ) {
  2930. data = pci_alloc_consistent(pdev,
  2931. uioc.xferlen, &data_dma_hndl);
  2932. if( data == NULL ) {
  2933. free_local_pdev(pdev);
  2934. return (-ENOMEM);
  2935. }
  2936. uxferaddr = MBOX(uioc)->xferaddr;
  2937. }
  2938. /*
  2939. * Is data coming down-stream
  2940. */
  2941. if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  2942. /*
  2943. * Get the user data
  2944. */
  2945. if( copy_from_user(data, (char __user *)uxferaddr,
  2946. uioc.xferlen) ) {
  2947. pci_free_consistent(pdev,
  2948. uioc.xferlen,
  2949. data, data_dma_hndl);
  2950. free_local_pdev(pdev);
  2951. return (-EFAULT);
  2952. }
  2953. }
  2954. memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  2955. mc.xferaddr = (u32)data_dma_hndl;
  2956. /*
  2957. * Issue the command
  2958. */
  2959. mega_internal_command(adapter, LOCK_INT, &mc, NULL);
  2960. rval = mega_n_to_m((void __user *)arg, &mc);
  2961. if( rval ) {
  2962. if( uioc.xferlen ) {
  2963. pci_free_consistent(pdev,
  2964. uioc.xferlen, data,
  2965. data_dma_hndl);
  2966. }
  2967. free_local_pdev(pdev);
  2968. return rval;
  2969. }
  2970. /*
  2971. * Is data going up-stream
  2972. */
  2973. if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
  2974. if( copy_to_user((char __user *)uxferaddr, data,
  2975. uioc.xferlen) ) {
  2976. rval = (-EFAULT);
  2977. }
  2978. }
  2979. if( uioc.xferlen ) {
  2980. pci_free_consistent(pdev,
  2981. uioc.xferlen, data,
  2982. data_dma_hndl);
  2983. }
  2984. free_local_pdev(pdev);
  2985. return rval;
  2986. }
  2987. default:
  2988. return (-EINVAL);
  2989. }
  2990. return 0;
  2991. }
  2992. /**
  2993. * mega_m_to_n()
  2994. * @arg - user address
  2995. * @uioc - new ioctl structure
  2996. *
  2997. * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
  2998. * structure
  2999. *
  3000. * Converts the older mimd ioctl structure to newer NIT structure
  3001. */
  3002. static int
  3003. mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  3004. {
  3005. struct uioctl_t uioc_mimd;
  3006. char signature[8] = {0};
  3007. u8 opcode;
  3008. u8 subopcode;
  3009. /*
  3010. * check is the application conforms to NIT. We do not have to do much
  3011. * in that case.
  3012. * We exploit the fact that the signature is stored in the very
  3013. * begining of the structure.
  3014. */
  3015. if( copy_from_user(signature, arg, 7) )
  3016. return (-EFAULT);
  3017. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3018. /*
  3019. * NOTE NOTE: The nit ioctl is still under flux because of
  3020. * change of mailbox definition, in HPE. No applications yet
  3021. * use this interface and let's not have applications use this
  3022. * interface till the new specifitions are in place.
  3023. */
  3024. return -EINVAL;
  3025. #if 0
  3026. if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  3027. return (-EFAULT);
  3028. return 0;
  3029. #endif
  3030. }
  3031. /*
  3032. * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  3033. *
  3034. * Get the user ioctl structure
  3035. */
  3036. if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  3037. return (-EFAULT);
  3038. /*
  3039. * Get the opcode and subopcode for the commands
  3040. */
  3041. opcode = uioc_mimd.ui.fcs.opcode;
  3042. subopcode = uioc_mimd.ui.fcs.subopcode;
  3043. switch (opcode) {
  3044. case 0x82:
  3045. switch (subopcode) {
  3046. case MEGAIOC_QDRVRVER: /* Query driver version */
  3047. uioc->opcode = GET_DRIVER_VER;
  3048. uioc->uioc_uaddr = uioc_mimd.data;
  3049. break;
  3050. case MEGAIOC_QNADAP: /* Get # of adapters */
  3051. uioc->opcode = GET_N_ADAP;
  3052. uioc->uioc_uaddr = uioc_mimd.data;
  3053. break;
  3054. case MEGAIOC_QADAPINFO: /* Get adapter information */
  3055. uioc->opcode = GET_ADAP_INFO;
  3056. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3057. uioc->uioc_uaddr = uioc_mimd.data;
  3058. break;
  3059. default:
  3060. return(-EINVAL);
  3061. }
  3062. break;
  3063. case 0x81:
  3064. uioc->opcode = MBOX_CMD;
  3065. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3066. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3067. uioc->xferlen = uioc_mimd.ui.fcs.length;
  3068. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3069. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3070. break;
  3071. case 0x80:
  3072. uioc->opcode = MBOX_CMD;
  3073. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3074. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3075. /*
  3076. * Choose the xferlen bigger of input and output data
  3077. */
  3078. uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  3079. uioc_mimd.outlen : uioc_mimd.inlen;
  3080. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3081. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3082. break;
  3083. default:
  3084. return (-EINVAL);
  3085. }
  3086. return 0;
  3087. }
  3088. /*
  3089. * mega_n_to_m()
  3090. * @arg - user address
  3091. * @mc - mailbox command
  3092. *
  3093. * Updates the status information to the application, depending on application
  3094. * conforms to older mimd ioctl interface or newer NIT ioctl interface
  3095. */
  3096. static int
  3097. mega_n_to_m(void __user *arg, megacmd_t *mc)
  3098. {
  3099. nitioctl_t __user *uiocp;
  3100. megacmd_t __user *umc;
  3101. mega_passthru __user *upthru;
  3102. struct uioctl_t __user *uioc_mimd;
  3103. char signature[8] = {0};
  3104. /*
  3105. * check is the application conforms to NIT.
  3106. */
  3107. if( copy_from_user(signature, arg, 7) )
  3108. return -EFAULT;
  3109. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3110. uiocp = arg;
  3111. if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  3112. return (-EFAULT);
  3113. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3114. umc = MBOX_P(uiocp);
  3115. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3116. return -EFAULT;
  3117. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  3118. return (-EFAULT);
  3119. }
  3120. }
  3121. else {
  3122. uioc_mimd = arg;
  3123. if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  3124. return (-EFAULT);
  3125. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3126. umc = (megacmd_t __user *)uioc_mimd->mbox;
  3127. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3128. return (-EFAULT);
  3129. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  3130. return (-EFAULT);
  3131. }
  3132. }
  3133. return 0;
  3134. }
  3135. /*
  3136. * MEGARAID 'FW' commands.
  3137. */
  3138. /**
  3139. * mega_is_bios_enabled()
  3140. * @adapter - pointer to our soft state
  3141. *
  3142. * issue command to find out if the BIOS is enabled for this controller
  3143. */
  3144. static int
  3145. mega_is_bios_enabled(adapter_t *adapter)
  3146. {
  3147. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3148. mbox_t *mbox;
  3149. int ret;
  3150. mbox = (mbox_t *)raw_mbox;
  3151. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3152. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3153. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3154. raw_mbox[0] = IS_BIOS_ENABLED;
  3155. raw_mbox[2] = GET_BIOS;
  3156. ret = issue_scb_block(adapter, raw_mbox);
  3157. return *(char *)adapter->mega_buffer;
  3158. }
  3159. /**
  3160. * mega_enum_raid_scsi()
  3161. * @adapter - pointer to our soft state
  3162. *
  3163. * Find out what channels are RAID/SCSI. This information is used to
  3164. * differentiate the virtual channels and physical channels and to support
  3165. * ROMB feature and non-disk devices.
  3166. */
  3167. static void
  3168. mega_enum_raid_scsi(adapter_t *adapter)
  3169. {
  3170. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3171. mbox_t *mbox;
  3172. int i;
  3173. mbox = (mbox_t *)raw_mbox;
  3174. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3175. /*
  3176. * issue command to find out what channels are raid/scsi
  3177. */
  3178. raw_mbox[0] = CHNL_CLASS;
  3179. raw_mbox[2] = GET_CHNL_CLASS;
  3180. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3181. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3182. /*
  3183. * Non-ROMB firmware fail this command, so all channels
  3184. * must be shown RAID
  3185. */
  3186. adapter->mega_ch_class = 0xFF;
  3187. if(!issue_scb_block(adapter, raw_mbox)) {
  3188. adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  3189. }
  3190. for( i = 0; i < adapter->product_info.nchannels; i++ ) {
  3191. if( (adapter->mega_ch_class >> i) & 0x01 ) {
  3192. printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
  3193. i);
  3194. }
  3195. else {
  3196. printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
  3197. i);
  3198. }
  3199. }
  3200. return;
  3201. }
  3202. /**
  3203. * mega_get_boot_drv()
  3204. * @adapter - pointer to our soft state
  3205. *
  3206. * Find out which device is the boot device. Note, any logical drive or any
  3207. * phyical device (e.g., a CDROM) can be designated as a boot device.
  3208. */
  3209. static void
  3210. mega_get_boot_drv(adapter_t *adapter)
  3211. {
  3212. struct private_bios_data *prv_bios_data;
  3213. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3214. mbox_t *mbox;
  3215. u16 cksum = 0;
  3216. u8 *cksum_p;
  3217. u8 boot_pdrv;
  3218. int i;
  3219. mbox = (mbox_t *)raw_mbox;
  3220. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3221. raw_mbox[0] = BIOS_PVT_DATA;
  3222. raw_mbox[2] = GET_BIOS_PVT_DATA;
  3223. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3224. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3225. adapter->boot_ldrv_enabled = 0;
  3226. adapter->boot_ldrv = 0;
  3227. adapter->boot_pdrv_enabled = 0;
  3228. adapter->boot_pdrv_ch = 0;
  3229. adapter->boot_pdrv_tgt = 0;
  3230. if(issue_scb_block(adapter, raw_mbox) == 0) {
  3231. prv_bios_data =
  3232. (struct private_bios_data *)adapter->mega_buffer;
  3233. cksum = 0;
  3234. cksum_p = (char *)prv_bios_data;
  3235. for (i = 0; i < 14; i++ ) {
  3236. cksum += (u16)(*cksum_p++);
  3237. }
  3238. if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  3239. /*
  3240. * If MSB is set, a physical drive is set as boot
  3241. * device
  3242. */
  3243. if( prv_bios_data->boot_drv & 0x80 ) {
  3244. adapter->boot_pdrv_enabled = 1;
  3245. boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  3246. adapter->boot_pdrv_ch = boot_pdrv / 16;
  3247. adapter->boot_pdrv_tgt = boot_pdrv % 16;
  3248. }
  3249. else {
  3250. adapter->boot_ldrv_enabled = 1;
  3251. adapter->boot_ldrv = prv_bios_data->boot_drv;
  3252. }
  3253. }
  3254. }
  3255. }
  3256. /**
  3257. * mega_support_random_del()
  3258. * @adapter - pointer to our soft state
  3259. *
  3260. * Find out if this controller supports random deletion and addition of
  3261. * logical drives
  3262. */
  3263. static int
  3264. mega_support_random_del(adapter_t *adapter)
  3265. {
  3266. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3267. mbox_t *mbox;
  3268. int rval;
  3269. mbox = (mbox_t *)raw_mbox;
  3270. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3271. /*
  3272. * issue command
  3273. */
  3274. raw_mbox[0] = FC_DEL_LOGDRV;
  3275. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  3276. rval = issue_scb_block(adapter, raw_mbox);
  3277. return !rval;
  3278. }
  3279. /**
  3280. * mega_support_ext_cdb()
  3281. * @adapter - pointer to our soft state
  3282. *
  3283. * Find out if this firmware support cdblen > 10
  3284. */
  3285. static int
  3286. mega_support_ext_cdb(adapter_t *adapter)
  3287. {
  3288. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3289. mbox_t *mbox;
  3290. int rval;
  3291. mbox = (mbox_t *)raw_mbox;
  3292. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3293. /*
  3294. * issue command to find out if controller supports extended CDBs.
  3295. */
  3296. raw_mbox[0] = 0xA4;
  3297. raw_mbox[2] = 0x16;
  3298. rval = issue_scb_block(adapter, raw_mbox);
  3299. return !rval;
  3300. }
  3301. /**
  3302. * mega_del_logdrv()
  3303. * @adapter - pointer to our soft state
  3304. * @logdrv - logical drive to be deleted
  3305. *
  3306. * Delete the specified logical drive. It is the responsibility of the user
  3307. * app to let the OS know about this operation.
  3308. */
  3309. static int
  3310. mega_del_logdrv(adapter_t *adapter, int logdrv)
  3311. {
  3312. unsigned long flags;
  3313. scb_t *scb;
  3314. int rval;
  3315. /*
  3316. * Stop sending commands to the controller, queue them internally.
  3317. * When deletion is complete, ISR will flush the queue.
  3318. */
  3319. atomic_set(&adapter->quiescent, 1);
  3320. /*
  3321. * Wait till all the issued commands are complete and there are no
  3322. * commands in the pending queue
  3323. */
  3324. while (atomic_read(&adapter->pend_cmds) > 0 ||
  3325. !list_empty(&adapter->pending_list))
  3326. msleep(1000); /* sleep for 1s */
  3327. rval = mega_do_del_logdrv(adapter, logdrv);
  3328. spin_lock_irqsave(&adapter->lock, flags);
  3329. /*
  3330. * If delete operation was successful, add 0x80 to the logical drive
  3331. * ids for commands in the pending queue.
  3332. */
  3333. if (adapter->read_ldidmap) {
  3334. struct list_head *pos;
  3335. list_for_each(pos, &adapter->pending_list) {
  3336. scb = list_entry(pos, scb_t, list);
  3337. if (scb->pthru->logdrv < 0x80 )
  3338. scb->pthru->logdrv += 0x80;
  3339. }
  3340. }
  3341. atomic_set(&adapter->quiescent, 0);
  3342. mega_runpendq(adapter);
  3343. spin_unlock_irqrestore(&adapter->lock, flags);
  3344. return rval;
  3345. }
  3346. static int
  3347. mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  3348. {
  3349. megacmd_t mc;
  3350. int rval;
  3351. memset( &mc, 0, sizeof(megacmd_t));
  3352. mc.cmd = FC_DEL_LOGDRV;
  3353. mc.opcode = OP_DEL_LOGDRV;
  3354. mc.subopcode = logdrv;
  3355. rval = mega_internal_command(adapter, LOCK_INT, &mc, NULL);
  3356. /* log this event */
  3357. if(rval) {
  3358. printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  3359. return rval;
  3360. }
  3361. /*
  3362. * After deleting first logical drive, the logical drives must be
  3363. * addressed by adding 0x80 to the logical drive id.
  3364. */
  3365. adapter->read_ldidmap = 1;
  3366. return rval;
  3367. }
  3368. /**
  3369. * mega_get_max_sgl()
  3370. * @adapter - pointer to our soft state
  3371. *
  3372. * Find out the maximum number of scatter-gather elements supported by this
  3373. * version of the firmware
  3374. */
  3375. static void
  3376. mega_get_max_sgl(adapter_t *adapter)
  3377. {
  3378. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3379. mbox_t *mbox;
  3380. mbox = (mbox_t *)raw_mbox;
  3381. memset(mbox, 0, sizeof(raw_mbox));
  3382. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3383. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3384. raw_mbox[0] = MAIN_MISC_OPCODE;
  3385. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  3386. if( issue_scb_block(adapter, raw_mbox) ) {
  3387. /*
  3388. * f/w does not support this command. Choose the default value
  3389. */
  3390. adapter->sglen = MIN_SGLIST;
  3391. }
  3392. else {
  3393. adapter->sglen = *((char *)adapter->mega_buffer);
  3394. /*
  3395. * Make sure this is not more than the resources we are
  3396. * planning to allocate
  3397. */
  3398. if ( adapter->sglen > MAX_SGLIST )
  3399. adapter->sglen = MAX_SGLIST;
  3400. }
  3401. return;
  3402. }
  3403. /**
  3404. * mega_support_cluster()
  3405. * @adapter - pointer to our soft state
  3406. *
  3407. * Find out if this firmware support cluster calls.
  3408. */
  3409. static int
  3410. mega_support_cluster(adapter_t *adapter)
  3411. {
  3412. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3413. mbox_t *mbox;
  3414. mbox = (mbox_t *)raw_mbox;
  3415. memset(mbox, 0, sizeof(raw_mbox));
  3416. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3417. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3418. /*
  3419. * Try to get the initiator id. This command will succeed iff the
  3420. * clustering is available on this HBA.
  3421. */
  3422. raw_mbox[0] = MEGA_GET_TARGET_ID;
  3423. if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  3424. /*
  3425. * Cluster support available. Get the initiator target id.
  3426. * Tell our id to mid-layer too.
  3427. */
  3428. adapter->this_id = *(u32 *)adapter->mega_buffer;
  3429. adapter->host->this_id = adapter->this_id;
  3430. return 1;
  3431. }
  3432. return 0;
  3433. }
  3434. /**
  3435. * mega_adapinq()
  3436. * @adapter - pointer to our soft state
  3437. * @dma_handle - DMA address of the buffer
  3438. *
  3439. * Issue internal comamnds while interrupts are available.
  3440. * We only issue direct mailbox commands from within the driver. ioctl()
  3441. * interface using these routines can issue passthru commands.
  3442. */
  3443. static int
  3444. mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  3445. {
  3446. megacmd_t mc;
  3447. memset(&mc, 0, sizeof(megacmd_t));
  3448. if( adapter->flag & BOARD_40LD ) {
  3449. mc.cmd = FC_NEW_CONFIG;
  3450. mc.opcode = NC_SUBOP_ENQUIRY3;
  3451. mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  3452. }
  3453. else {
  3454. mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  3455. }
  3456. mc.xferaddr = (u32)dma_handle;
  3457. if ( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
  3458. return -1;
  3459. }
  3460. return 0;
  3461. }
  3462. /** mega_internal_dev_inquiry()
  3463. * @adapter - pointer to our soft state
  3464. * @ch - channel for this device
  3465. * @tgt - ID of this device
  3466. * @buf_dma_handle - DMA address of the buffer
  3467. *
  3468. * Issue the scsi inquiry for the specified device.
  3469. */
  3470. static int
  3471. mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  3472. dma_addr_t buf_dma_handle)
  3473. {
  3474. mega_passthru *pthru;
  3475. dma_addr_t pthru_dma_handle;
  3476. megacmd_t mc;
  3477. int rval;
  3478. struct pci_dev *pdev;
  3479. /*
  3480. * For all internal commands, the buffer must be allocated in <4GB
  3481. * address range
  3482. */
  3483. if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  3484. pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  3485. &pthru_dma_handle);
  3486. if( pthru == NULL ) {
  3487. free_local_pdev(pdev);
  3488. return -1;
  3489. }
  3490. pthru->timeout = 2;
  3491. pthru->ars = 1;
  3492. pthru->reqsenselen = 14;
  3493. pthru->islogical = 0;
  3494. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  3495. pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  3496. pthru->cdblen = 6;
  3497. pthru->cdb[0] = INQUIRY;
  3498. pthru->cdb[1] = 0;
  3499. pthru->cdb[2] = 0;
  3500. pthru->cdb[3] = 0;
  3501. pthru->cdb[4] = 255;
  3502. pthru->cdb[5] = 0;
  3503. pthru->dataxferaddr = (u32)buf_dma_handle;
  3504. pthru->dataxferlen = 256;
  3505. memset(&mc, 0, sizeof(megacmd_t));
  3506. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  3507. mc.xferaddr = (u32)pthru_dma_handle;
  3508. rval = mega_internal_command(adapter, LOCK_INT, &mc, pthru);
  3509. pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  3510. pthru_dma_handle);
  3511. free_local_pdev(pdev);
  3512. return rval;
  3513. }
  3514. /**
  3515. * mega_internal_command()
  3516. * @adapter - pointer to our soft state
  3517. * @ls - the scope of the exclusion lock.
  3518. * @mc - the mailbox command
  3519. * @pthru - Passthru structure for DCDB commands
  3520. *
  3521. * Issue the internal commands in interrupt mode.
  3522. * The last argument is the address of the passthru structure if the command
  3523. * to be fired is a passthru command
  3524. *
  3525. * lockscope specifies whether the caller has already acquired the lock. Of
  3526. * course, the caller must know which lock we are talking about.
  3527. *
  3528. * Note: parameter 'pthru' is null for non-passthru commands.
  3529. */
  3530. static int
  3531. mega_internal_command(adapter_t *adapter, lockscope_t ls, megacmd_t *mc,
  3532. mega_passthru *pthru )
  3533. {
  3534. Scsi_Cmnd *scmd;
  3535. struct scsi_device *sdev;
  3536. unsigned long flags = 0;
  3537. scb_t *scb;
  3538. int rval;
  3539. /*
  3540. * The internal commands share one command id and hence are
  3541. * serialized. This is so because we want to reserve maximum number of
  3542. * available command ids for the I/O commands.
  3543. */
  3544. down(&adapter->int_mtx);
  3545. scb = &adapter->int_scb;
  3546. memset(scb, 0, sizeof(scb_t));
  3547. scmd = &adapter->int_scmd;
  3548. memset(scmd, 0, sizeof(Scsi_Cmnd));
  3549. sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
  3550. memset(sdev, 0, sizeof(struct scsi_device));
  3551. scmd->device = sdev;
  3552. scmd->device->host = adapter->host;
  3553. scmd->buffer = (void *)scb;
  3554. scmd->cmnd[0] = MEGA_INTERNAL_CMD;
  3555. scb->state |= SCB_ACTIVE;
  3556. scb->cmd = scmd;
  3557. memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  3558. /*
  3559. * Is it a passthru command
  3560. */
  3561. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3562. scb->pthru = pthru;
  3563. }
  3564. scb->idx = CMDID_INT_CMDS;
  3565. /*
  3566. * Get the lock only if the caller has not acquired it already
  3567. */
  3568. if( ls == LOCK_INT ) spin_lock_irqsave(&adapter->lock, flags);
  3569. megaraid_queue(scmd, mega_internal_done);
  3570. if( ls == LOCK_INT ) spin_unlock_irqrestore(&adapter->lock, flags);
  3571. wait_for_completion(&adapter->int_waitq);
  3572. rval = scmd->result;
  3573. mc->status = scmd->result;
  3574. kfree(sdev);
  3575. /*
  3576. * Print a debug message for all failed commands. Applications can use
  3577. * this information.
  3578. */
  3579. if( scmd->result && trace_level ) {
  3580. printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
  3581. mc->cmd, mc->opcode, mc->subopcode, scmd->result);
  3582. }
  3583. up(&adapter->int_mtx);
  3584. return rval;
  3585. }
  3586. /**
  3587. * mega_internal_done()
  3588. * @scmd - internal scsi command
  3589. *
  3590. * Callback routine for internal commands.
  3591. */
  3592. static void
  3593. mega_internal_done(Scsi_Cmnd *scmd)
  3594. {
  3595. adapter_t *adapter;
  3596. adapter = (adapter_t *)scmd->device->host->hostdata;
  3597. complete(&adapter->int_waitq);
  3598. }
  3599. static struct scsi_host_template megaraid_template = {
  3600. .module = THIS_MODULE,
  3601. .name = "MegaRAID",
  3602. .proc_name = "megaraid",
  3603. .info = megaraid_info,
  3604. .queuecommand = megaraid_queue,
  3605. .bios_param = megaraid_biosparam,
  3606. .max_sectors = MAX_SECTORS_PER_IO,
  3607. .can_queue = MAX_COMMANDS,
  3608. .this_id = DEFAULT_INITIATOR_ID,
  3609. .sg_tablesize = MAX_SGLIST,
  3610. .cmd_per_lun = DEF_CMD_PER_LUN,
  3611. .use_clustering = ENABLE_CLUSTERING,
  3612. .eh_abort_handler = megaraid_abort,
  3613. .eh_device_reset_handler = megaraid_reset,
  3614. .eh_bus_reset_handler = megaraid_reset,
  3615. .eh_host_reset_handler = megaraid_reset,
  3616. };
  3617. static int __devinit
  3618. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3619. {
  3620. struct Scsi_Host *host;
  3621. adapter_t *adapter;
  3622. unsigned long mega_baseport, tbase, flag = 0;
  3623. u16 subsysid, subsysvid;
  3624. u8 pci_bus, pci_dev_func;
  3625. int irq, i, j;
  3626. int error = -ENODEV;
  3627. if (pci_enable_device(pdev))
  3628. goto out;
  3629. pci_set_master(pdev);
  3630. pci_bus = pdev->bus->number;
  3631. pci_dev_func = pdev->devfn;
  3632. /*
  3633. * The megaraid3 stuff reports the ID of the Intel part which is not
  3634. * remotely specific to the megaraid
  3635. */
  3636. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  3637. u16 magic;
  3638. /*
  3639. * Don't fall over the Compaq management cards using the same
  3640. * PCI identifier
  3641. */
  3642. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  3643. pdev->subsystem_device == 0xC000)
  3644. return -ENODEV;
  3645. /* Now check the magic signature byte */
  3646. pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  3647. if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  3648. return -ENODEV;
  3649. /* Ok it is probably a megaraid */
  3650. }
  3651. /*
  3652. * For these vendor and device ids, signature offsets are not
  3653. * valid and 64 bit is implicit
  3654. */
  3655. if (id->driver_data & BOARD_64BIT)
  3656. flag |= BOARD_64BIT;
  3657. else {
  3658. u32 magic64;
  3659. pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  3660. if (magic64 == HBA_SIGNATURE_64BIT)
  3661. flag |= BOARD_64BIT;
  3662. }
  3663. subsysvid = pdev->subsystem_vendor;
  3664. subsysid = pdev->subsystem_device;
  3665. printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  3666. id->vendor, id->device, pci_bus);
  3667. printk("slot %d:func %d\n",
  3668. PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  3669. /* Read the base port and IRQ from PCI */
  3670. mega_baseport = pci_resource_start(pdev, 0);
  3671. irq = pdev->irq;
  3672. tbase = mega_baseport;
  3673. if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  3674. flag |= BOARD_MEMMAP;
  3675. if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  3676. printk(KERN_WARNING "megaraid: mem region busy!\n");
  3677. goto out_disable_device;
  3678. }
  3679. mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  3680. if (!mega_baseport) {
  3681. printk(KERN_WARNING
  3682. "megaraid: could not map hba memory\n");
  3683. goto out_release_region;
  3684. }
  3685. } else {
  3686. flag |= BOARD_IOMAP;
  3687. mega_baseport += 0x10;
  3688. if (!request_region(mega_baseport, 16, "megaraid"))
  3689. goto out_disable_device;
  3690. }
  3691. /* Initialize SCSI Host structure */
  3692. host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  3693. if (!host)
  3694. goto out_iounmap;
  3695. adapter = (adapter_t *)host->hostdata;
  3696. memset(adapter, 0, sizeof(adapter_t));
  3697. printk(KERN_NOTICE
  3698. "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
  3699. host->host_no, mega_baseport, irq);
  3700. adapter->base = mega_baseport;
  3701. INIT_LIST_HEAD(&adapter->free_list);
  3702. INIT_LIST_HEAD(&adapter->pending_list);
  3703. INIT_LIST_HEAD(&adapter->completed_list);
  3704. adapter->flag = flag;
  3705. spin_lock_init(&adapter->lock);
  3706. scsi_assign_lock(host, &adapter->lock);
  3707. host->cmd_per_lun = max_cmd_per_lun;
  3708. host->max_sectors = max_sectors_per_io;
  3709. adapter->dev = pdev;
  3710. adapter->host = host;
  3711. adapter->host->irq = irq;
  3712. if (flag & BOARD_MEMMAP)
  3713. adapter->host->base = tbase;
  3714. else {
  3715. adapter->host->io_port = tbase;
  3716. adapter->host->n_io_port = 16;
  3717. }
  3718. adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  3719. /*
  3720. * Allocate buffer to issue internal commands.
  3721. */
  3722. adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  3723. MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  3724. if (!adapter->mega_buffer) {
  3725. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3726. goto out_host_put;
  3727. }
  3728. adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  3729. if (!adapter->scb_list) {
  3730. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3731. goto out_free_cmd_buffer;
  3732. }
  3733. if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  3734. megaraid_isr_memmapped : megaraid_isr_iomapped,
  3735. SA_SHIRQ, "megaraid", adapter)) {
  3736. printk(KERN_WARNING
  3737. "megaraid: Couldn't register IRQ %d!\n", irq);
  3738. goto out_free_scb_list;
  3739. }
  3740. if (mega_setup_mailbox(adapter))
  3741. goto out_free_irq;
  3742. if (mega_query_adapter(adapter))
  3743. goto out_free_mbox;
  3744. /*
  3745. * Have checks for some buggy f/w
  3746. */
  3747. if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  3748. /*
  3749. * Which firmware
  3750. */
  3751. if (!strcmp(adapter->fw_version, "3.00") ||
  3752. !strcmp(adapter->fw_version, "3.01")) {
  3753. printk( KERN_WARNING
  3754. "megaraid: Your card is a Dell PERC "
  3755. "2/SC RAID controller with "
  3756. "firmware\nmegaraid: 3.00 or 3.01. "
  3757. "This driver is known to have "
  3758. "corruption issues\nmegaraid: with "
  3759. "those firmware versions on this "
  3760. "specific card. In order\nmegaraid: "
  3761. "to protect your data, please upgrade "
  3762. "your firmware to version\nmegaraid: "
  3763. "3.10 or later, available from the "
  3764. "Dell Technical Support web\n"
  3765. "megaraid: site at\nhttp://support."
  3766. "dell.com/us/en/filelib/download/"
  3767. "index.asp?fileid=2940\n"
  3768. );
  3769. }
  3770. }
  3771. /*
  3772. * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  3773. * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  3774. * support, since this firmware cannot handle 64 bit
  3775. * addressing
  3776. */
  3777. if ((subsysvid == HP_SUBSYS_VID) &&
  3778. ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  3779. /*
  3780. * which firmware
  3781. */
  3782. if (!strcmp(adapter->fw_version, "H01.07") ||
  3783. !strcmp(adapter->fw_version, "H01.08") ||
  3784. !strcmp(adapter->fw_version, "H01.09") ) {
  3785. printk(KERN_WARNING
  3786. "megaraid: Firmware H.01.07, "
  3787. "H.01.08, and H.01.09 on 1M/2M "
  3788. "controllers\n"
  3789. "megaraid: do not support 64 bit "
  3790. "addressing.\nmegaraid: DISABLING "
  3791. "64 bit support.\n");
  3792. adapter->flag &= ~BOARD_64BIT;
  3793. }
  3794. }
  3795. if (mega_is_bios_enabled(adapter))
  3796. mega_hbas[hba_count].is_bios_enabled = 1;
  3797. mega_hbas[hba_count].hostdata_addr = adapter;
  3798. /*
  3799. * Find out which channel is raid and which is scsi. This is
  3800. * for ROMB support.
  3801. */
  3802. mega_enum_raid_scsi(adapter);
  3803. /*
  3804. * Find out if a logical drive is set as the boot drive. If
  3805. * there is one, will make that as the first logical drive.
  3806. * ROMB: Do we have to boot from a physical drive. Then all
  3807. * the physical drives would appear before the logical disks.
  3808. * Else, all the physical drives would be exported to the mid
  3809. * layer after logical drives.
  3810. */
  3811. mega_get_boot_drv(adapter);
  3812. if (adapter->boot_pdrv_enabled) {
  3813. j = adapter->product_info.nchannels;
  3814. for( i = 0; i < j; i++ )
  3815. adapter->logdrv_chan[i] = 0;
  3816. for( i = j; i < NVIRT_CHAN + j; i++ )
  3817. adapter->logdrv_chan[i] = 1;
  3818. } else {
  3819. for (i = 0; i < NVIRT_CHAN; i++)
  3820. adapter->logdrv_chan[i] = 1;
  3821. for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  3822. adapter->logdrv_chan[i] = 0;
  3823. adapter->mega_ch_class <<= NVIRT_CHAN;
  3824. }
  3825. /*
  3826. * Do we support random deletion and addition of logical
  3827. * drives
  3828. */
  3829. adapter->read_ldidmap = 0; /* set it after first logdrv
  3830. delete cmd */
  3831. adapter->support_random_del = mega_support_random_del(adapter);
  3832. /* Initialize SCBs */
  3833. if (mega_init_scb(adapter))
  3834. goto out_free_mbox;
  3835. /*
  3836. * Reset the pending commands counter
  3837. */
  3838. atomic_set(&adapter->pend_cmds, 0);
  3839. /*
  3840. * Reset the adapter quiescent flag
  3841. */
  3842. atomic_set(&adapter->quiescent, 0);
  3843. hba_soft_state[hba_count] = adapter;
  3844. /*
  3845. * Fill in the structure which needs to be passed back to the
  3846. * application when it does an ioctl() for controller related
  3847. * information.
  3848. */
  3849. i = hba_count;
  3850. mcontroller[i].base = mega_baseport;
  3851. mcontroller[i].irq = irq;
  3852. mcontroller[i].numldrv = adapter->numldrv;
  3853. mcontroller[i].pcibus = pci_bus;
  3854. mcontroller[i].pcidev = id->device;
  3855. mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  3856. mcontroller[i].pciid = -1;
  3857. mcontroller[i].pcivendor = id->vendor;
  3858. mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  3859. mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  3860. /* Set the Mode of addressing to 64 bit if we can */
  3861. if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
  3862. pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
  3863. adapter->has_64bit_addr = 1;
  3864. } else {
  3865. pci_set_dma_mask(pdev, 0xffffffff);
  3866. adapter->has_64bit_addr = 0;
  3867. }
  3868. init_MUTEX(&adapter->int_mtx);
  3869. init_completion(&adapter->int_waitq);
  3870. adapter->this_id = DEFAULT_INITIATOR_ID;
  3871. adapter->host->this_id = DEFAULT_INITIATOR_ID;
  3872. #if MEGA_HAVE_CLUSTERING
  3873. /*
  3874. * Is cluster support enabled on this controller
  3875. * Note: In a cluster the HBAs ( the initiators ) will have
  3876. * different target IDs and we cannot assume it to be 7. Call
  3877. * to mega_support_cluster() will get the target ids also if
  3878. * the cluster support is available
  3879. */
  3880. adapter->has_cluster = mega_support_cluster(adapter);
  3881. if (adapter->has_cluster) {
  3882. printk(KERN_NOTICE
  3883. "megaraid: Cluster driver, initiator id:%d\n",
  3884. adapter->this_id);
  3885. }
  3886. #endif
  3887. pci_set_drvdata(pdev, host);
  3888. mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  3889. error = scsi_add_host(host, &pdev->dev);
  3890. if (error)
  3891. goto out_free_mbox;
  3892. scsi_scan_host(host);
  3893. hba_count++;
  3894. return 0;
  3895. out_free_mbox:
  3896. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3897. adapter->una_mbox64, adapter->una_mbox64_dma);
  3898. out_free_irq:
  3899. free_irq(adapter->host->irq, adapter);
  3900. out_free_scb_list:
  3901. kfree(adapter->scb_list);
  3902. out_free_cmd_buffer:
  3903. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3904. adapter->mega_buffer, adapter->buf_dma_handle);
  3905. out_host_put:
  3906. scsi_host_put(host);
  3907. out_iounmap:
  3908. if (flag & BOARD_MEMMAP)
  3909. iounmap((void *)mega_baseport);
  3910. out_release_region:
  3911. if (flag & BOARD_MEMMAP)
  3912. release_mem_region(tbase, 128);
  3913. else
  3914. release_region(mega_baseport, 16);
  3915. out_disable_device:
  3916. pci_disable_device(pdev);
  3917. out:
  3918. return error;
  3919. }
  3920. static void
  3921. __megaraid_shutdown(adapter_t *adapter)
  3922. {
  3923. u_char raw_mbox[sizeof(struct mbox_out)];
  3924. mbox_t *mbox = (mbox_t *)raw_mbox;
  3925. int i;
  3926. /* Flush adapter cache */
  3927. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3928. raw_mbox[0] = FLUSH_ADAPTER;
  3929. free_irq(adapter->host->irq, adapter);
  3930. /* Issue a blocking (interrupts disabled) command to the card */
  3931. issue_scb_block(adapter, raw_mbox);
  3932. /* Flush disks cache */
  3933. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3934. raw_mbox[0] = FLUSH_SYSTEM;
  3935. /* Issue a blocking (interrupts disabled) command to the card */
  3936. issue_scb_block(adapter, raw_mbox);
  3937. if (atomic_read(&adapter->pend_cmds) > 0)
  3938. printk(KERN_WARNING "megaraid: pending commands!!\n");
  3939. /*
  3940. * Have a delibrate delay to make sure all the caches are
  3941. * actually flushed.
  3942. */
  3943. for (i = 0; i <= 10; i++)
  3944. mdelay(1000);
  3945. }
  3946. static void
  3947. megaraid_remove_one(struct pci_dev *pdev)
  3948. {
  3949. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3950. adapter_t *adapter = (adapter_t *)host->hostdata;
  3951. char buf[12] = { 0 };
  3952. scsi_remove_host(host);
  3953. __megaraid_shutdown(adapter);
  3954. /* Free our resources */
  3955. if (adapter->flag & BOARD_MEMMAP) {
  3956. iounmap((void *)adapter->base);
  3957. release_mem_region(adapter->host->base, 128);
  3958. } else
  3959. release_region(adapter->base, 16);
  3960. mega_free_sgl(adapter);
  3961. #ifdef CONFIG_PROC_FS
  3962. if (adapter->controller_proc_dir_entry) {
  3963. remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  3964. remove_proc_entry("config",
  3965. adapter->controller_proc_dir_entry);
  3966. remove_proc_entry("mailbox",
  3967. adapter->controller_proc_dir_entry);
  3968. #if MEGA_HAVE_ENH_PROC
  3969. remove_proc_entry("rebuild-rate",
  3970. adapter->controller_proc_dir_entry);
  3971. remove_proc_entry("battery-status",
  3972. adapter->controller_proc_dir_entry);
  3973. remove_proc_entry("diskdrives-ch0",
  3974. adapter->controller_proc_dir_entry);
  3975. remove_proc_entry("diskdrives-ch1",
  3976. adapter->controller_proc_dir_entry);
  3977. remove_proc_entry("diskdrives-ch2",
  3978. adapter->controller_proc_dir_entry);
  3979. remove_proc_entry("diskdrives-ch3",
  3980. adapter->controller_proc_dir_entry);
  3981. remove_proc_entry("raiddrives-0-9",
  3982. adapter->controller_proc_dir_entry);
  3983. remove_proc_entry("raiddrives-10-19",
  3984. adapter->controller_proc_dir_entry);
  3985. remove_proc_entry("raiddrives-20-29",
  3986. adapter->controller_proc_dir_entry);
  3987. remove_proc_entry("raiddrives-30-39",
  3988. adapter->controller_proc_dir_entry);
  3989. #endif
  3990. sprintf(buf, "hba%d", adapter->host->host_no);
  3991. remove_proc_entry(buf, mega_proc_dir_entry);
  3992. }
  3993. #endif
  3994. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3995. adapter->mega_buffer, adapter->buf_dma_handle);
  3996. kfree(adapter->scb_list);
  3997. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3998. adapter->una_mbox64, adapter->una_mbox64_dma);
  3999. scsi_host_put(host);
  4000. pci_disable_device(pdev);
  4001. hba_count--;
  4002. }
  4003. static void
  4004. megaraid_shutdown(struct pci_dev *pdev)
  4005. {
  4006. struct Scsi_Host *host = pci_get_drvdata(pdev);
  4007. adapter_t *adapter = (adapter_t *)host->hostdata;
  4008. __megaraid_shutdown(adapter);
  4009. }
  4010. static struct pci_device_id megaraid_pci_tbl[] = {
  4011. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DISCOVERY,
  4012. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4013. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_PERC4_DI,
  4014. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4015. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_PERC4_QC_VERDE,
  4016. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4017. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  4018. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4019. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  4020. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4021. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID3,
  4022. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4023. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  4024. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4025. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_AMI_MEGARAID3,
  4026. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4027. {0,}
  4028. };
  4029. MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  4030. static struct pci_driver megaraid_pci_driver = {
  4031. .name = "megaraid",
  4032. .id_table = megaraid_pci_tbl,
  4033. .probe = megaraid_probe_one,
  4034. .remove = __devexit_p(megaraid_remove_one),
  4035. .shutdown = megaraid_shutdown,
  4036. };
  4037. static int __init megaraid_init(void)
  4038. {
  4039. int error;
  4040. if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  4041. max_cmd_per_lun = MAX_CMD_PER_LUN;
  4042. if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  4043. max_mbox_busy_wait = MBOX_BUSY_WAIT;
  4044. #ifdef CONFIG_PROC_FS
  4045. mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
  4046. if (!mega_proc_dir_entry) {
  4047. printk(KERN_WARNING
  4048. "megaraid: failed to create megaraid root\n");
  4049. }
  4050. #endif
  4051. error = pci_module_init(&megaraid_pci_driver);
  4052. if (error) {
  4053. #ifdef CONFIG_PROC_FS
  4054. remove_proc_entry("megaraid", &proc_root);
  4055. #endif
  4056. return error;
  4057. }
  4058. /*
  4059. * Register the driver as a character device, for applications
  4060. * to access it for ioctls.
  4061. * First argument (major) to register_chrdev implies a dynamic
  4062. * major number allocation.
  4063. */
  4064. major = register_chrdev(0, "megadev", &megadev_fops);
  4065. if (!major) {
  4066. printk(KERN_WARNING
  4067. "megaraid: failed to register char device\n");
  4068. }
  4069. return 0;
  4070. }
  4071. static void __exit megaraid_exit(void)
  4072. {
  4073. /*
  4074. * Unregister the character device interface to the driver.
  4075. */
  4076. unregister_chrdev(major, "megadev");
  4077. pci_unregister_driver(&megaraid_pci_driver);
  4078. #ifdef CONFIG_PROC_FS
  4079. remove_proc_entry("megaraid", &proc_root);
  4080. #endif
  4081. }
  4082. module_init(megaraid_init);
  4083. module_exit(megaraid_exit);
  4084. /* vi: set ts=8 sw=8 tw=78: */