megaraid.c 105 KB

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