hpsa.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/fs.h>
  29. #include <linux/timer.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/compat.h>
  35. #include <linux/blktrace_api.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/completion.h>
  40. #include <linux/moduleparam.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_host.h>
  45. #include <linux/cciss_ioctl.h>
  46. #include <linux/string.h>
  47. #include <linux/bitmap.h>
  48. #include <asm/atomic.h>
  49. #include <linux/kthread.h>
  50. #include "hpsa_cmd.h"
  51. #include "hpsa.h"
  52. /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
  53. #define HPSA_DRIVER_VERSION "1.0.0"
  54. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  55. /* How long to wait (in milliseconds) for board to go into simple mode */
  56. #define MAX_CONFIG_WAIT 30000
  57. #define MAX_IOCTL_CONFIG_WAIT 1000
  58. /*define how many times we will try a command because of bus resets */
  59. #define MAX_CMD_RETRIES 3
  60. /* Embedded module documentation macros - see modules.h */
  61. MODULE_AUTHOR("Hewlett-Packard Company");
  62. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  63. HPSA_DRIVER_VERSION);
  64. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  65. MODULE_VERSION(HPSA_DRIVER_VERSION);
  66. MODULE_LICENSE("GPL");
  67. static int hpsa_allow_any;
  68. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  69. MODULE_PARM_DESC(hpsa_allow_any,
  70. "Allow hpsa driver to access unknown HP Smart Array hardware");
  71. /* define the PCI info for the cards we can control */
  72. static const struct pci_device_id hpsa_pci_device_id[] = {
  73. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  74. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  75. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  76. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  77. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  78. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
  79. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  81. #define PCI_DEVICE_ID_HP_CISSF 0x333f
  82. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
  83. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  84. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  85. {0,}
  86. };
  87. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  88. /* board_id = Subsystem Device ID & Vendor ID
  89. * product = Marketing Name for the board
  90. * access = Address of the struct of function pointers
  91. */
  92. static struct board_type products[] = {
  93. {0x3241103C, "Smart Array P212", &SA5_access},
  94. {0x3243103C, "Smart Array P410", &SA5_access},
  95. {0x3245103C, "Smart Array P410i", &SA5_access},
  96. {0x3247103C, "Smart Array P411", &SA5_access},
  97. {0x3249103C, "Smart Array P812", &SA5_access},
  98. {0x324a103C, "Smart Array P712m", &SA5_access},
  99. {0x324b103C, "Smart Array P711m", &SA5_access},
  100. {0x3233103C, "StorageWorks P1210m", &SA5_access},
  101. {0x333F103C, "StorageWorks P1210m", &SA5_access},
  102. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  103. };
  104. static int number_of_controllers;
  105. static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
  106. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
  107. static void start_io(struct ctlr_info *h);
  108. #ifdef CONFIG_COMPAT
  109. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
  110. #endif
  111. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  112. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
  113. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  114. static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
  115. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  116. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  117. int cmd_type);
  118. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  119. void (*done)(struct scsi_cmnd *));
  120. static void hpsa_scan_start(struct Scsi_Host *);
  121. static int hpsa_scan_finished(struct Scsi_Host *sh,
  122. unsigned long elapsed_time);
  123. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  124. static int hpsa_slave_alloc(struct scsi_device *sdev);
  125. static void hpsa_slave_destroy(struct scsi_device *sdev);
  126. static ssize_t raid_level_show(struct device *dev,
  127. struct device_attribute *attr, char *buf);
  128. static ssize_t lunid_show(struct device *dev,
  129. struct device_attribute *attr, char *buf);
  130. static ssize_t unique_id_show(struct device *dev,
  131. struct device_attribute *attr, char *buf);
  132. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
  133. static ssize_t host_store_rescan(struct device *dev,
  134. struct device_attribute *attr, const char *buf, size_t count);
  135. static int check_for_unit_attention(struct ctlr_info *h,
  136. struct CommandList *c);
  137. static void check_ioctl_unit_attention(struct ctlr_info *h,
  138. struct CommandList *c);
  139. /* performant mode helper functions */
  140. static void calc_bucket_map(int *bucket, int num_buckets,
  141. int nsgs, int *bucket_map);
  142. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  143. static inline u32 next_command(struct ctlr_info *h);
  144. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  145. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  146. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  147. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  148. static struct device_attribute *hpsa_sdev_attrs[] = {
  149. &dev_attr_raid_level,
  150. &dev_attr_lunid,
  151. &dev_attr_unique_id,
  152. NULL,
  153. };
  154. static struct device_attribute *hpsa_shost_attrs[] = {
  155. &dev_attr_rescan,
  156. NULL,
  157. };
  158. static struct scsi_host_template hpsa_driver_template = {
  159. .module = THIS_MODULE,
  160. .name = "hpsa",
  161. .proc_name = "hpsa",
  162. .queuecommand = hpsa_scsi_queue_command,
  163. .scan_start = hpsa_scan_start,
  164. .scan_finished = hpsa_scan_finished,
  165. .this_id = -1,
  166. .sg_tablesize = MAXSGENTRIES,
  167. .use_clustering = ENABLE_CLUSTERING,
  168. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  169. .ioctl = hpsa_ioctl,
  170. .slave_alloc = hpsa_slave_alloc,
  171. .slave_destroy = hpsa_slave_destroy,
  172. #ifdef CONFIG_COMPAT
  173. .compat_ioctl = hpsa_compat_ioctl,
  174. #endif
  175. .sdev_attrs = hpsa_sdev_attrs,
  176. .shost_attrs = hpsa_shost_attrs,
  177. };
  178. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  179. {
  180. unsigned long *priv = shost_priv(sdev->host);
  181. return (struct ctlr_info *) *priv;
  182. }
  183. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  184. {
  185. unsigned long *priv = shost_priv(sh);
  186. return (struct ctlr_info *) *priv;
  187. }
  188. static struct task_struct *hpsa_scan_thread;
  189. static DEFINE_MUTEX(hpsa_scan_mutex);
  190. static LIST_HEAD(hpsa_scan_q);
  191. static int hpsa_scan_func(void *data);
  192. /**
  193. * add_to_scan_list() - add controller to rescan queue
  194. * @h: Pointer to the controller.
  195. *
  196. * Adds the controller to the rescan queue if not already on the queue.
  197. *
  198. * returns 1 if added to the queue, 0 if skipped (could be on the
  199. * queue already, or the controller could be initializing or shutting
  200. * down).
  201. **/
  202. static int add_to_scan_list(struct ctlr_info *h)
  203. {
  204. struct ctlr_info *test_h;
  205. int found = 0;
  206. int ret = 0;
  207. if (h->busy_initializing)
  208. return 0;
  209. /*
  210. * If we don't get the lock, it means the driver is unloading
  211. * and there's no point in scheduling a new scan.
  212. */
  213. if (!mutex_trylock(&h->busy_shutting_down))
  214. return 0;
  215. mutex_lock(&hpsa_scan_mutex);
  216. list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
  217. if (test_h == h) {
  218. found = 1;
  219. break;
  220. }
  221. }
  222. if (!found && !h->busy_scanning) {
  223. INIT_COMPLETION(h->scan_wait);
  224. list_add_tail(&h->scan_list, &hpsa_scan_q);
  225. ret = 1;
  226. }
  227. mutex_unlock(&hpsa_scan_mutex);
  228. mutex_unlock(&h->busy_shutting_down);
  229. return ret;
  230. }
  231. /**
  232. * remove_from_scan_list() - remove controller from rescan queue
  233. * @h: Pointer to the controller.
  234. *
  235. * Removes the controller from the rescan queue if present. Blocks if
  236. * the controller is currently conducting a rescan. The controller
  237. * can be in one of three states:
  238. * 1. Doesn't need a scan
  239. * 2. On the scan list, but not scanning yet (we remove it)
  240. * 3. Busy scanning (and not on the list). In this case we want to wait for
  241. * the scan to complete to make sure the scanning thread for this
  242. * controller is completely idle.
  243. **/
  244. static void remove_from_scan_list(struct ctlr_info *h)
  245. {
  246. struct ctlr_info *test_h, *tmp_h;
  247. mutex_lock(&hpsa_scan_mutex);
  248. list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
  249. if (test_h == h) { /* state 2. */
  250. list_del(&h->scan_list);
  251. complete_all(&h->scan_wait);
  252. mutex_unlock(&hpsa_scan_mutex);
  253. return;
  254. }
  255. }
  256. if (h->busy_scanning) { /* state 3. */
  257. mutex_unlock(&hpsa_scan_mutex);
  258. wait_for_completion(&h->scan_wait);
  259. } else { /* state 1, nothing to do. */
  260. mutex_unlock(&hpsa_scan_mutex);
  261. }
  262. }
  263. /* hpsa_scan_func() - kernel thread used to rescan controllers
  264. * @data: Ignored.
  265. *
  266. * A kernel thread used scan for drive topology changes on
  267. * controllers. The thread processes only one controller at a time
  268. * using a queue. Controllers are added to the queue using
  269. * add_to_scan_list() and removed from the queue either after done
  270. * processing or using remove_from_scan_list().
  271. *
  272. * returns 0.
  273. **/
  274. static int hpsa_scan_func(__attribute__((unused)) void *data)
  275. {
  276. struct ctlr_info *h;
  277. int host_no;
  278. while (1) {
  279. set_current_state(TASK_INTERRUPTIBLE);
  280. schedule();
  281. if (kthread_should_stop())
  282. break;
  283. while (1) {
  284. mutex_lock(&hpsa_scan_mutex);
  285. if (list_empty(&hpsa_scan_q)) {
  286. mutex_unlock(&hpsa_scan_mutex);
  287. break;
  288. }
  289. h = list_entry(hpsa_scan_q.next, struct ctlr_info,
  290. scan_list);
  291. list_del(&h->scan_list);
  292. h->busy_scanning = 1;
  293. mutex_unlock(&hpsa_scan_mutex);
  294. host_no = h->scsi_host ? h->scsi_host->host_no : -1;
  295. hpsa_scan_start(h->scsi_host);
  296. complete_all(&h->scan_wait);
  297. mutex_lock(&hpsa_scan_mutex);
  298. h->busy_scanning = 0;
  299. mutex_unlock(&hpsa_scan_mutex);
  300. }
  301. }
  302. return 0;
  303. }
  304. static int check_for_unit_attention(struct ctlr_info *h,
  305. struct CommandList *c)
  306. {
  307. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  308. return 0;
  309. switch (c->err_info->SenseInfo[12]) {
  310. case STATE_CHANGED:
  311. dev_warn(&h->pdev->dev, "hpsa%d: a state change "
  312. "detected, command retried\n", h->ctlr);
  313. break;
  314. case LUN_FAILED:
  315. dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
  316. "detected, action required\n", h->ctlr);
  317. break;
  318. case REPORT_LUNS_CHANGED:
  319. dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
  320. "changed\n", h->ctlr);
  321. /*
  322. * Here, we could call add_to_scan_list and wake up the scan thread,
  323. * except that it's quite likely that we will get more than one
  324. * REPORT_LUNS_CHANGED condition in quick succession, which means
  325. * that those which occur after the first one will likely happen
  326. * *during* the hpsa_scan_thread's rescan. And the rescan code is not
  327. * robust enough to restart in the middle, undoing what it has already
  328. * done, and it's not clear that it's even possible to do this, since
  329. * part of what it does is notify the SCSI mid layer, which starts
  330. * doing it's own i/o to read partition tables and so on, and the
  331. * driver doesn't have visibility to know what might need undoing.
  332. * In any event, if possible, it is horribly complicated to get right
  333. * so we just don't do it for now.
  334. *
  335. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  336. */
  337. break;
  338. case POWER_OR_RESET:
  339. dev_warn(&h->pdev->dev, "hpsa%d: a power on "
  340. "or device reset detected\n", h->ctlr);
  341. break;
  342. case UNIT_ATTENTION_CLEARED:
  343. dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
  344. "cleared by another initiator\n", h->ctlr);
  345. break;
  346. default:
  347. dev_warn(&h->pdev->dev, "hpsa%d: unknown "
  348. "unit attention detected\n", h->ctlr);
  349. break;
  350. }
  351. return 1;
  352. }
  353. static ssize_t host_store_rescan(struct device *dev,
  354. struct device_attribute *attr,
  355. const char *buf, size_t count)
  356. {
  357. struct ctlr_info *h;
  358. struct Scsi_Host *shost = class_to_shost(dev);
  359. h = shost_to_hba(shost);
  360. if (add_to_scan_list(h)) {
  361. wake_up_process(hpsa_scan_thread);
  362. wait_for_completion_interruptible(&h->scan_wait);
  363. }
  364. return count;
  365. }
  366. /* Enqueuing and dequeuing functions for cmdlists. */
  367. static inline void addQ(struct hlist_head *list, struct CommandList *c)
  368. {
  369. hlist_add_head(&c->list, list);
  370. }
  371. static inline u32 next_command(struct ctlr_info *h)
  372. {
  373. u32 a;
  374. if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
  375. return h->access.command_completed(h);
  376. if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
  377. a = *(h->reply_pool_head); /* Next cmd in ring buffer */
  378. (h->reply_pool_head)++;
  379. h->commands_outstanding--;
  380. } else {
  381. a = FIFO_EMPTY;
  382. }
  383. /* Check for wraparound */
  384. if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
  385. h->reply_pool_head = h->reply_pool;
  386. h->reply_pool_wraparound ^= 1;
  387. }
  388. return a;
  389. }
  390. /* set_performant_mode: Modify the tag for cciss performant
  391. * set bit 0 for pull model, bits 3-1 for block fetch
  392. * register number
  393. */
  394. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
  395. {
  396. if (likely(h->transMethod == CFGTBL_Trans_Performant))
  397. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  398. }
  399. static void enqueue_cmd_and_start_io(struct ctlr_info *h,
  400. struct CommandList *c)
  401. {
  402. unsigned long flags;
  403. set_performant_mode(h, c);
  404. spin_lock_irqsave(&h->lock, flags);
  405. addQ(&h->reqQ, c);
  406. h->Qdepth++;
  407. start_io(h);
  408. spin_unlock_irqrestore(&h->lock, flags);
  409. }
  410. static inline void removeQ(struct CommandList *c)
  411. {
  412. if (WARN_ON(hlist_unhashed(&c->list)))
  413. return;
  414. hlist_del_init(&c->list);
  415. }
  416. static inline int is_hba_lunid(unsigned char scsi3addr[])
  417. {
  418. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  419. }
  420. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  421. {
  422. return (scsi3addr[3] & 0xC0) == 0x40;
  423. }
  424. static inline int is_scsi_rev_5(struct ctlr_info *h)
  425. {
  426. if (!h->hba_inquiry_data)
  427. return 0;
  428. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  429. return 1;
  430. return 0;
  431. }
  432. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  433. "UNKNOWN"
  434. };
  435. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
  436. static ssize_t raid_level_show(struct device *dev,
  437. struct device_attribute *attr, char *buf)
  438. {
  439. ssize_t l = 0;
  440. unsigned char rlevel;
  441. struct ctlr_info *h;
  442. struct scsi_device *sdev;
  443. struct hpsa_scsi_dev_t *hdev;
  444. unsigned long flags;
  445. sdev = to_scsi_device(dev);
  446. h = sdev_to_hba(sdev);
  447. spin_lock_irqsave(&h->lock, flags);
  448. hdev = sdev->hostdata;
  449. if (!hdev) {
  450. spin_unlock_irqrestore(&h->lock, flags);
  451. return -ENODEV;
  452. }
  453. /* Is this even a logical drive? */
  454. if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
  455. spin_unlock_irqrestore(&h->lock, flags);
  456. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  457. return l;
  458. }
  459. rlevel = hdev->raid_level;
  460. spin_unlock_irqrestore(&h->lock, flags);
  461. if (rlevel > RAID_UNKNOWN)
  462. rlevel = RAID_UNKNOWN;
  463. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  464. return l;
  465. }
  466. static ssize_t lunid_show(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct ctlr_info *h;
  470. struct scsi_device *sdev;
  471. struct hpsa_scsi_dev_t *hdev;
  472. unsigned long flags;
  473. unsigned char lunid[8];
  474. sdev = to_scsi_device(dev);
  475. h = sdev_to_hba(sdev);
  476. spin_lock_irqsave(&h->lock, flags);
  477. hdev = sdev->hostdata;
  478. if (!hdev) {
  479. spin_unlock_irqrestore(&h->lock, flags);
  480. return -ENODEV;
  481. }
  482. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  483. spin_unlock_irqrestore(&h->lock, flags);
  484. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  485. lunid[0], lunid[1], lunid[2], lunid[3],
  486. lunid[4], lunid[5], lunid[6], lunid[7]);
  487. }
  488. static ssize_t unique_id_show(struct device *dev,
  489. struct device_attribute *attr, char *buf)
  490. {
  491. struct ctlr_info *h;
  492. struct scsi_device *sdev;
  493. struct hpsa_scsi_dev_t *hdev;
  494. unsigned long flags;
  495. unsigned char sn[16];
  496. sdev = to_scsi_device(dev);
  497. h = sdev_to_hba(sdev);
  498. spin_lock_irqsave(&h->lock, flags);
  499. hdev = sdev->hostdata;
  500. if (!hdev) {
  501. spin_unlock_irqrestore(&h->lock, flags);
  502. return -ENODEV;
  503. }
  504. memcpy(sn, hdev->device_id, sizeof(sn));
  505. spin_unlock_irqrestore(&h->lock, flags);
  506. return snprintf(buf, 16 * 2 + 2,
  507. "%02X%02X%02X%02X%02X%02X%02X%02X"
  508. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  509. sn[0], sn[1], sn[2], sn[3],
  510. sn[4], sn[5], sn[6], sn[7],
  511. sn[8], sn[9], sn[10], sn[11],
  512. sn[12], sn[13], sn[14], sn[15]);
  513. }
  514. static int hpsa_find_target_lun(struct ctlr_info *h,
  515. unsigned char scsi3addr[], int bus, int *target, int *lun)
  516. {
  517. /* finds an unused bus, target, lun for a new physical device
  518. * assumes h->devlock is held
  519. */
  520. int i, found = 0;
  521. DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
  522. memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
  523. for (i = 0; i < h->ndevices; i++) {
  524. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  525. set_bit(h->dev[i]->target, lun_taken);
  526. }
  527. for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
  528. if (!test_bit(i, lun_taken)) {
  529. /* *bus = 1; */
  530. *target = i;
  531. *lun = 0;
  532. found = 1;
  533. break;
  534. }
  535. }
  536. return !found;
  537. }
  538. /* Add an entry into h->dev[] array. */
  539. static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
  540. struct hpsa_scsi_dev_t *device,
  541. struct hpsa_scsi_dev_t *added[], int *nadded)
  542. {
  543. /* assumes h->devlock is held */
  544. int n = h->ndevices;
  545. int i;
  546. unsigned char addr1[8], addr2[8];
  547. struct hpsa_scsi_dev_t *sd;
  548. if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
  549. dev_err(&h->pdev->dev, "too many devices, some will be "
  550. "inaccessible.\n");
  551. return -1;
  552. }
  553. /* physical devices do not have lun or target assigned until now. */
  554. if (device->lun != -1)
  555. /* Logical device, lun is already assigned. */
  556. goto lun_assigned;
  557. /* If this device a non-zero lun of a multi-lun device
  558. * byte 4 of the 8-byte LUN addr will contain the logical
  559. * unit no, zero otherise.
  560. */
  561. if (device->scsi3addr[4] == 0) {
  562. /* This is not a non-zero lun of a multi-lun device */
  563. if (hpsa_find_target_lun(h, device->scsi3addr,
  564. device->bus, &device->target, &device->lun) != 0)
  565. return -1;
  566. goto lun_assigned;
  567. }
  568. /* This is a non-zero lun of a multi-lun device.
  569. * Search through our list and find the device which
  570. * has the same 8 byte LUN address, excepting byte 4.
  571. * Assign the same bus and target for this new LUN.
  572. * Use the logical unit number from the firmware.
  573. */
  574. memcpy(addr1, device->scsi3addr, 8);
  575. addr1[4] = 0;
  576. for (i = 0; i < n; i++) {
  577. sd = h->dev[i];
  578. memcpy(addr2, sd->scsi3addr, 8);
  579. addr2[4] = 0;
  580. /* differ only in byte 4? */
  581. if (memcmp(addr1, addr2, 8) == 0) {
  582. device->bus = sd->bus;
  583. device->target = sd->target;
  584. device->lun = device->scsi3addr[4];
  585. break;
  586. }
  587. }
  588. if (device->lun == -1) {
  589. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  590. " suspect firmware bug or unsupported hardware "
  591. "configuration.\n");
  592. return -1;
  593. }
  594. lun_assigned:
  595. h->dev[n] = device;
  596. h->ndevices++;
  597. added[*nadded] = device;
  598. (*nadded)++;
  599. /* initially, (before registering with scsi layer) we don't
  600. * know our hostno and we don't want to print anything first
  601. * time anyway (the scsi layer's inquiries will show that info)
  602. */
  603. /* if (hostno != -1) */
  604. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
  605. scsi_device_type(device->devtype), hostno,
  606. device->bus, device->target, device->lun);
  607. return 0;
  608. }
  609. /* Replace an entry from h->dev[] array. */
  610. static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
  611. int entry, struct hpsa_scsi_dev_t *new_entry,
  612. struct hpsa_scsi_dev_t *added[], int *nadded,
  613. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  614. {
  615. /* assumes h->devlock is held */
  616. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  617. removed[*nremoved] = h->dev[entry];
  618. (*nremoved)++;
  619. h->dev[entry] = new_entry;
  620. added[*nadded] = new_entry;
  621. (*nadded)++;
  622. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
  623. scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
  624. new_entry->target, new_entry->lun);
  625. }
  626. /* Remove an entry from h->dev[] array. */
  627. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
  628. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  629. {
  630. /* assumes h->devlock is held */
  631. int i;
  632. struct hpsa_scsi_dev_t *sd;
  633. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  634. sd = h->dev[entry];
  635. removed[*nremoved] = h->dev[entry];
  636. (*nremoved)++;
  637. for (i = entry; i < h->ndevices-1; i++)
  638. h->dev[i] = h->dev[i+1];
  639. h->ndevices--;
  640. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
  641. scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
  642. sd->lun);
  643. }
  644. #define SCSI3ADDR_EQ(a, b) ( \
  645. (a)[7] == (b)[7] && \
  646. (a)[6] == (b)[6] && \
  647. (a)[5] == (b)[5] && \
  648. (a)[4] == (b)[4] && \
  649. (a)[3] == (b)[3] && \
  650. (a)[2] == (b)[2] && \
  651. (a)[1] == (b)[1] && \
  652. (a)[0] == (b)[0])
  653. static void fixup_botched_add(struct ctlr_info *h,
  654. struct hpsa_scsi_dev_t *added)
  655. {
  656. /* called when scsi_add_device fails in order to re-adjust
  657. * h->dev[] to match the mid layer's view.
  658. */
  659. unsigned long flags;
  660. int i, j;
  661. spin_lock_irqsave(&h->lock, flags);
  662. for (i = 0; i < h->ndevices; i++) {
  663. if (h->dev[i] == added) {
  664. for (j = i; j < h->ndevices-1; j++)
  665. h->dev[j] = h->dev[j+1];
  666. h->ndevices--;
  667. break;
  668. }
  669. }
  670. spin_unlock_irqrestore(&h->lock, flags);
  671. kfree(added);
  672. }
  673. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  674. struct hpsa_scsi_dev_t *dev2)
  675. {
  676. if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
  677. (dev1->lun != -1 && dev2->lun != -1)) &&
  678. dev1->devtype != 0x0C)
  679. return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
  680. /* we compare everything except lun and target as these
  681. * are not yet assigned. Compare parts likely
  682. * to differ first
  683. */
  684. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  685. sizeof(dev1->scsi3addr)) != 0)
  686. return 0;
  687. if (memcmp(dev1->device_id, dev2->device_id,
  688. sizeof(dev1->device_id)) != 0)
  689. return 0;
  690. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  691. return 0;
  692. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  693. return 0;
  694. if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
  695. return 0;
  696. if (dev1->devtype != dev2->devtype)
  697. return 0;
  698. if (dev1->raid_level != dev2->raid_level)
  699. return 0;
  700. if (dev1->bus != dev2->bus)
  701. return 0;
  702. return 1;
  703. }
  704. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  705. * and return needle location in *index. If scsi3addr matches, but not
  706. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  707. * location in *index. If needle not found, return DEVICE_NOT_FOUND.
  708. */
  709. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  710. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  711. int *index)
  712. {
  713. int i;
  714. #define DEVICE_NOT_FOUND 0
  715. #define DEVICE_CHANGED 1
  716. #define DEVICE_SAME 2
  717. for (i = 0; i < haystack_size; i++) {
  718. if (haystack[i] == NULL) /* previously removed. */
  719. continue;
  720. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  721. *index = i;
  722. if (device_is_the_same(needle, haystack[i]))
  723. return DEVICE_SAME;
  724. else
  725. return DEVICE_CHANGED;
  726. }
  727. }
  728. *index = -1;
  729. return DEVICE_NOT_FOUND;
  730. }
  731. static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
  732. struct hpsa_scsi_dev_t *sd[], int nsds)
  733. {
  734. /* sd contains scsi3 addresses and devtypes, and inquiry
  735. * data. This function takes what's in sd to be the current
  736. * reality and updates h->dev[] to reflect that reality.
  737. */
  738. int i, entry, device_change, changes = 0;
  739. struct hpsa_scsi_dev_t *csd;
  740. unsigned long flags;
  741. struct hpsa_scsi_dev_t **added, **removed;
  742. int nadded, nremoved;
  743. struct Scsi_Host *sh = NULL;
  744. added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  745. GFP_KERNEL);
  746. removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  747. GFP_KERNEL);
  748. if (!added || !removed) {
  749. dev_warn(&h->pdev->dev, "out of memory in "
  750. "adjust_hpsa_scsi_table\n");
  751. goto free_and_out;
  752. }
  753. spin_lock_irqsave(&h->devlock, flags);
  754. /* find any devices in h->dev[] that are not in
  755. * sd[] and remove them from h->dev[], and for any
  756. * devices which have changed, remove the old device
  757. * info and add the new device info.
  758. */
  759. i = 0;
  760. nremoved = 0;
  761. nadded = 0;
  762. while (i < h->ndevices) {
  763. csd = h->dev[i];
  764. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  765. if (device_change == DEVICE_NOT_FOUND) {
  766. changes++;
  767. hpsa_scsi_remove_entry(h, hostno, i,
  768. removed, &nremoved);
  769. continue; /* remove ^^^, hence i not incremented */
  770. } else if (device_change == DEVICE_CHANGED) {
  771. changes++;
  772. hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
  773. added, &nadded, removed, &nremoved);
  774. /* Set it to NULL to prevent it from being freed
  775. * at the bottom of hpsa_update_scsi_devices()
  776. */
  777. sd[entry] = NULL;
  778. }
  779. i++;
  780. }
  781. /* Now, make sure every device listed in sd[] is also
  782. * listed in h->dev[], adding them if they aren't found
  783. */
  784. for (i = 0; i < nsds; i++) {
  785. if (!sd[i]) /* if already added above. */
  786. continue;
  787. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  788. h->ndevices, &entry);
  789. if (device_change == DEVICE_NOT_FOUND) {
  790. changes++;
  791. if (hpsa_scsi_add_entry(h, hostno, sd[i],
  792. added, &nadded) != 0)
  793. break;
  794. sd[i] = NULL; /* prevent from being freed later. */
  795. } else if (device_change == DEVICE_CHANGED) {
  796. /* should never happen... */
  797. changes++;
  798. dev_warn(&h->pdev->dev,
  799. "device unexpectedly changed.\n");
  800. /* but if it does happen, we just ignore that device */
  801. }
  802. }
  803. spin_unlock_irqrestore(&h->devlock, flags);
  804. /* Don't notify scsi mid layer of any changes the first time through
  805. * (or if there are no changes) scsi_scan_host will do it later the
  806. * first time through.
  807. */
  808. if (hostno == -1 || !changes)
  809. goto free_and_out;
  810. sh = h->scsi_host;
  811. /* Notify scsi mid layer of any removed devices */
  812. for (i = 0; i < nremoved; i++) {
  813. struct scsi_device *sdev =
  814. scsi_device_lookup(sh, removed[i]->bus,
  815. removed[i]->target, removed[i]->lun);
  816. if (sdev != NULL) {
  817. scsi_remove_device(sdev);
  818. scsi_device_put(sdev);
  819. } else {
  820. /* We don't expect to get here.
  821. * future cmds to this device will get selection
  822. * timeout as if the device was gone.
  823. */
  824. dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
  825. " for removal.", hostno, removed[i]->bus,
  826. removed[i]->target, removed[i]->lun);
  827. }
  828. kfree(removed[i]);
  829. removed[i] = NULL;
  830. }
  831. /* Notify scsi mid layer of any added devices */
  832. for (i = 0; i < nadded; i++) {
  833. if (scsi_add_device(sh, added[i]->bus,
  834. added[i]->target, added[i]->lun) == 0)
  835. continue;
  836. dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
  837. "device not added.\n", hostno, added[i]->bus,
  838. added[i]->target, added[i]->lun);
  839. /* now we have to remove it from h->dev,
  840. * since it didn't get added to scsi mid layer
  841. */
  842. fixup_botched_add(h, added[i]);
  843. }
  844. free_and_out:
  845. kfree(added);
  846. kfree(removed);
  847. }
  848. /*
  849. * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
  850. * Assume's h->devlock is held.
  851. */
  852. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  853. int bus, int target, int lun)
  854. {
  855. int i;
  856. struct hpsa_scsi_dev_t *sd;
  857. for (i = 0; i < h->ndevices; i++) {
  858. sd = h->dev[i];
  859. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  860. return sd;
  861. }
  862. return NULL;
  863. }
  864. /* link sdev->hostdata to our per-device structure. */
  865. static int hpsa_slave_alloc(struct scsi_device *sdev)
  866. {
  867. struct hpsa_scsi_dev_t *sd;
  868. unsigned long flags;
  869. struct ctlr_info *h;
  870. h = sdev_to_hba(sdev);
  871. spin_lock_irqsave(&h->devlock, flags);
  872. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  873. sdev_id(sdev), sdev->lun);
  874. if (sd != NULL)
  875. sdev->hostdata = sd;
  876. spin_unlock_irqrestore(&h->devlock, flags);
  877. return 0;
  878. }
  879. static void hpsa_slave_destroy(struct scsi_device *sdev)
  880. {
  881. /* nothing to do. */
  882. }
  883. static void hpsa_scsi_setup(struct ctlr_info *h)
  884. {
  885. h->ndevices = 0;
  886. h->scsi_host = NULL;
  887. spin_lock_init(&h->devlock);
  888. }
  889. static void complete_scsi_command(struct CommandList *cp,
  890. int timeout, u32 tag)
  891. {
  892. struct scsi_cmnd *cmd;
  893. struct ctlr_info *h;
  894. struct ErrorInfo *ei;
  895. unsigned char sense_key;
  896. unsigned char asc; /* additional sense code */
  897. unsigned char ascq; /* additional sense code qualifier */
  898. ei = cp->err_info;
  899. cmd = (struct scsi_cmnd *) cp->scsi_cmd;
  900. h = cp->h;
  901. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  902. cmd->result = (DID_OK << 16); /* host byte */
  903. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  904. cmd->result |= (ei->ScsiStatus << 1);
  905. /* copy the sense data whether we need to or not. */
  906. memcpy(cmd->sense_buffer, ei->SenseInfo,
  907. ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  908. SCSI_SENSE_BUFFERSIZE :
  909. ei->SenseLen);
  910. scsi_set_resid(cmd, ei->ResidualCnt);
  911. if (ei->CommandStatus == 0) {
  912. cmd->scsi_done(cmd);
  913. cmd_free(h, cp);
  914. return;
  915. }
  916. /* an error has occurred */
  917. switch (ei->CommandStatus) {
  918. case CMD_TARGET_STATUS:
  919. if (ei->ScsiStatus) {
  920. /* Get sense key */
  921. sense_key = 0xf & ei->SenseInfo[2];
  922. /* Get additional sense code */
  923. asc = ei->SenseInfo[12];
  924. /* Get addition sense code qualifier */
  925. ascq = ei->SenseInfo[13];
  926. }
  927. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  928. if (check_for_unit_attention(h, cp)) {
  929. cmd->result = DID_SOFT_ERROR << 16;
  930. break;
  931. }
  932. if (sense_key == ILLEGAL_REQUEST) {
  933. /*
  934. * SCSI REPORT_LUNS is commonly unsupported on
  935. * Smart Array. Suppress noisy complaint.
  936. */
  937. if (cp->Request.CDB[0] == REPORT_LUNS)
  938. break;
  939. /* If ASC/ASCQ indicate Logical Unit
  940. * Not Supported condition,
  941. */
  942. if ((asc == 0x25) && (ascq == 0x0)) {
  943. dev_warn(&h->pdev->dev, "cp %p "
  944. "has check condition\n", cp);
  945. break;
  946. }
  947. }
  948. if (sense_key == NOT_READY) {
  949. /* If Sense is Not Ready, Logical Unit
  950. * Not ready, Manual Intervention
  951. * required
  952. */
  953. if ((asc == 0x04) && (ascq == 0x03)) {
  954. dev_warn(&h->pdev->dev, "cp %p "
  955. "has check condition: unit "
  956. "not ready, manual "
  957. "intervention required\n", cp);
  958. break;
  959. }
  960. }
  961. if (sense_key == ABORTED_COMMAND) {
  962. /* Aborted command is retryable */
  963. dev_warn(&h->pdev->dev, "cp %p "
  964. "has check condition: aborted command: "
  965. "ASC: 0x%x, ASCQ: 0x%x\n",
  966. cp, asc, ascq);
  967. cmd->result = DID_SOFT_ERROR << 16;
  968. break;
  969. }
  970. /* Must be some other type of check condition */
  971. dev_warn(&h->pdev->dev, "cp %p has check condition: "
  972. "unknown type: "
  973. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  974. "Returning result: 0x%x, "
  975. "cmd=[%02x %02x %02x %02x %02x "
  976. "%02x %02x %02x %02x %02x %02x "
  977. "%02x %02x %02x %02x %02x]\n",
  978. cp, sense_key, asc, ascq,
  979. cmd->result,
  980. cmd->cmnd[0], cmd->cmnd[1],
  981. cmd->cmnd[2], cmd->cmnd[3],
  982. cmd->cmnd[4], cmd->cmnd[5],
  983. cmd->cmnd[6], cmd->cmnd[7],
  984. cmd->cmnd[8], cmd->cmnd[9],
  985. cmd->cmnd[10], cmd->cmnd[11],
  986. cmd->cmnd[12], cmd->cmnd[13],
  987. cmd->cmnd[14], cmd->cmnd[15]);
  988. break;
  989. }
  990. /* Problem was not a check condition
  991. * Pass it up to the upper layers...
  992. */
  993. if (ei->ScsiStatus) {
  994. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  995. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  996. "Returning result: 0x%x\n",
  997. cp, ei->ScsiStatus,
  998. sense_key, asc, ascq,
  999. cmd->result);
  1000. } else { /* scsi status is zero??? How??? */
  1001. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  1002. "Returning no connection.\n", cp),
  1003. /* Ordinarily, this case should never happen,
  1004. * but there is a bug in some released firmware
  1005. * revisions that allows it to happen if, for
  1006. * example, a 4100 backplane loses power and
  1007. * the tape drive is in it. We assume that
  1008. * it's a fatal error of some kind because we
  1009. * can't show that it wasn't. We will make it
  1010. * look like selection timeout since that is
  1011. * the most common reason for this to occur,
  1012. * and it's severe enough.
  1013. */
  1014. cmd->result = DID_NO_CONNECT << 16;
  1015. }
  1016. break;
  1017. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1018. break;
  1019. case CMD_DATA_OVERRUN:
  1020. dev_warn(&h->pdev->dev, "cp %p has"
  1021. " completed with data overrun "
  1022. "reported\n", cp);
  1023. break;
  1024. case CMD_INVALID: {
  1025. /* print_bytes(cp, sizeof(*cp), 1, 0);
  1026. print_cmd(cp); */
  1027. /* We get CMD_INVALID if you address a non-existent device
  1028. * instead of a selection timeout (no response). You will
  1029. * see this if you yank out a drive, then try to access it.
  1030. * This is kind of a shame because it means that any other
  1031. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  1032. * missing target. */
  1033. cmd->result = DID_NO_CONNECT << 16;
  1034. }
  1035. break;
  1036. case CMD_PROTOCOL_ERR:
  1037. dev_warn(&h->pdev->dev, "cp %p has "
  1038. "protocol error \n", cp);
  1039. break;
  1040. case CMD_HARDWARE_ERR:
  1041. cmd->result = DID_ERROR << 16;
  1042. dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
  1043. break;
  1044. case CMD_CONNECTION_LOST:
  1045. cmd->result = DID_ERROR << 16;
  1046. dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
  1047. break;
  1048. case CMD_ABORTED:
  1049. cmd->result = DID_ABORT << 16;
  1050. dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
  1051. cp, ei->ScsiStatus);
  1052. break;
  1053. case CMD_ABORT_FAILED:
  1054. cmd->result = DID_ERROR << 16;
  1055. dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
  1056. break;
  1057. case CMD_UNSOLICITED_ABORT:
  1058. cmd->result = DID_RESET << 16;
  1059. dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
  1060. "abort\n", cp);
  1061. break;
  1062. case CMD_TIMEOUT:
  1063. cmd->result = DID_TIME_OUT << 16;
  1064. dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
  1065. break;
  1066. default:
  1067. cmd->result = DID_ERROR << 16;
  1068. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  1069. cp, ei->CommandStatus);
  1070. }
  1071. cmd->scsi_done(cmd);
  1072. cmd_free(h, cp);
  1073. }
  1074. static int hpsa_scsi_detect(struct ctlr_info *h)
  1075. {
  1076. struct Scsi_Host *sh;
  1077. int error;
  1078. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  1079. if (sh == NULL)
  1080. goto fail;
  1081. sh->io_port = 0;
  1082. sh->n_io_port = 0;
  1083. sh->this_id = -1;
  1084. sh->max_channel = 3;
  1085. sh->max_cmd_len = MAX_COMMAND_SIZE;
  1086. sh->max_lun = HPSA_MAX_LUN;
  1087. sh->max_id = HPSA_MAX_LUN;
  1088. sh->can_queue = h->nr_cmds;
  1089. sh->cmd_per_lun = h->nr_cmds;
  1090. h->scsi_host = sh;
  1091. sh->hostdata[0] = (unsigned long) h;
  1092. sh->irq = h->intr[PERF_MODE_INT];
  1093. sh->unique_id = sh->irq;
  1094. error = scsi_add_host(sh, &h->pdev->dev);
  1095. if (error)
  1096. goto fail_host_put;
  1097. scsi_scan_host(sh);
  1098. return 0;
  1099. fail_host_put:
  1100. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
  1101. " failed for controller %d\n", h->ctlr);
  1102. scsi_host_put(sh);
  1103. return error;
  1104. fail:
  1105. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
  1106. " failed for controller %d\n", h->ctlr);
  1107. return -ENOMEM;
  1108. }
  1109. static void hpsa_pci_unmap(struct pci_dev *pdev,
  1110. struct CommandList *c, int sg_used, int data_direction)
  1111. {
  1112. int i;
  1113. union u64bit addr64;
  1114. for (i = 0; i < sg_used; i++) {
  1115. addr64.val32.lower = c->SG[i].Addr.lower;
  1116. addr64.val32.upper = c->SG[i].Addr.upper;
  1117. pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
  1118. data_direction);
  1119. }
  1120. }
  1121. static void hpsa_map_one(struct pci_dev *pdev,
  1122. struct CommandList *cp,
  1123. unsigned char *buf,
  1124. size_t buflen,
  1125. int data_direction)
  1126. {
  1127. u64 addr64;
  1128. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  1129. cp->Header.SGList = 0;
  1130. cp->Header.SGTotal = 0;
  1131. return;
  1132. }
  1133. addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
  1134. cp->SG[0].Addr.lower =
  1135. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1136. cp->SG[0].Addr.upper =
  1137. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1138. cp->SG[0].Len = buflen;
  1139. cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
  1140. cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
  1141. }
  1142. static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  1143. struct CommandList *c)
  1144. {
  1145. DECLARE_COMPLETION_ONSTACK(wait);
  1146. c->waiting = &wait;
  1147. enqueue_cmd_and_start_io(h, c);
  1148. wait_for_completion(&wait);
  1149. }
  1150. static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  1151. struct CommandList *c, int data_direction)
  1152. {
  1153. int retry_count = 0;
  1154. do {
  1155. memset(c->err_info, 0, sizeof(c->err_info));
  1156. hpsa_scsi_do_simple_cmd_core(h, c);
  1157. retry_count++;
  1158. } while (check_for_unit_attention(h, c) && retry_count <= 3);
  1159. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  1160. }
  1161. static void hpsa_scsi_interpret_error(struct CommandList *cp)
  1162. {
  1163. struct ErrorInfo *ei;
  1164. struct device *d = &cp->h->pdev->dev;
  1165. ei = cp->err_info;
  1166. switch (ei->CommandStatus) {
  1167. case CMD_TARGET_STATUS:
  1168. dev_warn(d, "cmd %p has completed with errors\n", cp);
  1169. dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
  1170. ei->ScsiStatus);
  1171. if (ei->ScsiStatus == 0)
  1172. dev_warn(d, "SCSI status is abnormally zero. "
  1173. "(probably indicates selection timeout "
  1174. "reported incorrectly due to a known "
  1175. "firmware bug, circa July, 2001.)\n");
  1176. break;
  1177. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1178. dev_info(d, "UNDERRUN\n");
  1179. break;
  1180. case CMD_DATA_OVERRUN:
  1181. dev_warn(d, "cp %p has completed with data overrun\n", cp);
  1182. break;
  1183. case CMD_INVALID: {
  1184. /* controller unfortunately reports SCSI passthru's
  1185. * to non-existent targets as invalid commands.
  1186. */
  1187. dev_warn(d, "cp %p is reported invalid (probably means "
  1188. "target device no longer present)\n", cp);
  1189. /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  1190. print_cmd(cp); */
  1191. }
  1192. break;
  1193. case CMD_PROTOCOL_ERR:
  1194. dev_warn(d, "cp %p has protocol error \n", cp);
  1195. break;
  1196. case CMD_HARDWARE_ERR:
  1197. /* cmd->result = DID_ERROR << 16; */
  1198. dev_warn(d, "cp %p had hardware error\n", cp);
  1199. break;
  1200. case CMD_CONNECTION_LOST:
  1201. dev_warn(d, "cp %p had connection lost\n", cp);
  1202. break;
  1203. case CMD_ABORTED:
  1204. dev_warn(d, "cp %p was aborted\n", cp);
  1205. break;
  1206. case CMD_ABORT_FAILED:
  1207. dev_warn(d, "cp %p reports abort failed\n", cp);
  1208. break;
  1209. case CMD_UNSOLICITED_ABORT:
  1210. dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
  1211. break;
  1212. case CMD_TIMEOUT:
  1213. dev_warn(d, "cp %p timed out\n", cp);
  1214. break;
  1215. default:
  1216. dev_warn(d, "cp %p returned unknown status %x\n", cp,
  1217. ei->CommandStatus);
  1218. }
  1219. }
  1220. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  1221. unsigned char page, unsigned char *buf,
  1222. unsigned char bufsize)
  1223. {
  1224. int rc = IO_OK;
  1225. struct CommandList *c;
  1226. struct ErrorInfo *ei;
  1227. c = cmd_special_alloc(h);
  1228. if (c == NULL) { /* trouble... */
  1229. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1230. return -ENOMEM;
  1231. }
  1232. fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
  1233. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1234. ei = c->err_info;
  1235. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1236. hpsa_scsi_interpret_error(c);
  1237. rc = -1;
  1238. }
  1239. cmd_special_free(h, c);
  1240. return rc;
  1241. }
  1242. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
  1243. {
  1244. int rc = IO_OK;
  1245. struct CommandList *c;
  1246. struct ErrorInfo *ei;
  1247. c = cmd_special_alloc(h);
  1248. if (c == NULL) { /* trouble... */
  1249. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1250. return -1;
  1251. }
  1252. fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
  1253. hpsa_scsi_do_simple_cmd_core(h, c);
  1254. /* no unmap needed here because no data xfer. */
  1255. ei = c->err_info;
  1256. if (ei->CommandStatus != 0) {
  1257. hpsa_scsi_interpret_error(c);
  1258. rc = -1;
  1259. }
  1260. cmd_special_free(h, c);
  1261. return rc;
  1262. }
  1263. static void hpsa_get_raid_level(struct ctlr_info *h,
  1264. unsigned char *scsi3addr, unsigned char *raid_level)
  1265. {
  1266. int rc;
  1267. unsigned char *buf;
  1268. *raid_level = RAID_UNKNOWN;
  1269. buf = kzalloc(64, GFP_KERNEL);
  1270. if (!buf)
  1271. return;
  1272. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
  1273. if (rc == 0)
  1274. *raid_level = buf[8];
  1275. if (*raid_level > RAID_UNKNOWN)
  1276. *raid_level = RAID_UNKNOWN;
  1277. kfree(buf);
  1278. return;
  1279. }
  1280. /* Get the device id from inquiry page 0x83 */
  1281. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  1282. unsigned char *device_id, int buflen)
  1283. {
  1284. int rc;
  1285. unsigned char *buf;
  1286. if (buflen > 16)
  1287. buflen = 16;
  1288. buf = kzalloc(64, GFP_KERNEL);
  1289. if (!buf)
  1290. return -1;
  1291. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
  1292. if (rc == 0)
  1293. memcpy(device_id, &buf[8], buflen);
  1294. kfree(buf);
  1295. return rc != 0;
  1296. }
  1297. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  1298. struct ReportLUNdata *buf, int bufsize,
  1299. int extended_response)
  1300. {
  1301. int rc = IO_OK;
  1302. struct CommandList *c;
  1303. unsigned char scsi3addr[8];
  1304. struct ErrorInfo *ei;
  1305. c = cmd_special_alloc(h);
  1306. if (c == NULL) { /* trouble... */
  1307. dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1308. return -1;
  1309. }
  1310. /* address the controller */
  1311. memset(scsi3addr, 0, sizeof(scsi3addr));
  1312. fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  1313. buf, bufsize, 0, scsi3addr, TYPE_CMD);
  1314. if (extended_response)
  1315. c->Request.CDB[1] = extended_response;
  1316. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1317. ei = c->err_info;
  1318. if (ei->CommandStatus != 0 &&
  1319. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1320. hpsa_scsi_interpret_error(c);
  1321. rc = -1;
  1322. }
  1323. cmd_special_free(h, c);
  1324. return rc;
  1325. }
  1326. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  1327. struct ReportLUNdata *buf,
  1328. int bufsize, int extended_response)
  1329. {
  1330. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
  1331. }
  1332. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  1333. struct ReportLUNdata *buf, int bufsize)
  1334. {
  1335. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  1336. }
  1337. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  1338. int bus, int target, int lun)
  1339. {
  1340. device->bus = bus;
  1341. device->target = target;
  1342. device->lun = lun;
  1343. }
  1344. static int hpsa_update_device_info(struct ctlr_info *h,
  1345. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
  1346. {
  1347. #define OBDR_TAPE_INQ_SIZE 49
  1348. unsigned char *inq_buff;
  1349. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1350. if (!inq_buff)
  1351. goto bail_out;
  1352. /* Do an inquiry to the device to see what it is. */
  1353. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  1354. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  1355. /* Inquiry failed (msg printed already) */
  1356. dev_err(&h->pdev->dev,
  1357. "hpsa_update_device_info: inquiry failed\n");
  1358. goto bail_out;
  1359. }
  1360. /* As a side effect, record the firmware version number
  1361. * if we happen to be talking to the RAID controller.
  1362. */
  1363. if (is_hba_lunid(scsi3addr))
  1364. memcpy(h->firm_ver, &inq_buff[32], 4);
  1365. this_device->devtype = (inq_buff[0] & 0x1f);
  1366. memcpy(this_device->scsi3addr, scsi3addr, 8);
  1367. memcpy(this_device->vendor, &inq_buff[8],
  1368. sizeof(this_device->vendor));
  1369. memcpy(this_device->model, &inq_buff[16],
  1370. sizeof(this_device->model));
  1371. memcpy(this_device->revision, &inq_buff[32],
  1372. sizeof(this_device->revision));
  1373. memset(this_device->device_id, 0,
  1374. sizeof(this_device->device_id));
  1375. hpsa_get_device_id(h, scsi3addr, this_device->device_id,
  1376. sizeof(this_device->device_id));
  1377. if (this_device->devtype == TYPE_DISK &&
  1378. is_logical_dev_addr_mode(scsi3addr))
  1379. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  1380. else
  1381. this_device->raid_level = RAID_UNKNOWN;
  1382. kfree(inq_buff);
  1383. return 0;
  1384. bail_out:
  1385. kfree(inq_buff);
  1386. return 1;
  1387. }
  1388. static unsigned char *msa2xxx_model[] = {
  1389. "MSA2012",
  1390. "MSA2024",
  1391. "MSA2312",
  1392. "MSA2324",
  1393. NULL,
  1394. };
  1395. static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1396. {
  1397. int i;
  1398. for (i = 0; msa2xxx_model[i]; i++)
  1399. if (strncmp(device->model, msa2xxx_model[i],
  1400. strlen(msa2xxx_model[i])) == 0)
  1401. return 1;
  1402. return 0;
  1403. }
  1404. /* Helper function to assign bus, target, lun mapping of devices.
  1405. * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
  1406. * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
  1407. * Logical drive target and lun are assigned at this time, but
  1408. * physical device lun and target assignment are deferred (assigned
  1409. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  1410. */
  1411. static void figure_bus_target_lun(struct ctlr_info *h,
  1412. u8 *lunaddrbytes, int *bus, int *target, int *lun,
  1413. struct hpsa_scsi_dev_t *device)
  1414. {
  1415. u32 lunid;
  1416. if (is_logical_dev_addr_mode(lunaddrbytes)) {
  1417. /* logical device */
  1418. if (unlikely(is_scsi_rev_5(h))) {
  1419. /* p1210m, logical drives lun assignments
  1420. * match SCSI REPORT LUNS data.
  1421. */
  1422. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1423. *bus = 0;
  1424. *target = 0;
  1425. *lun = (lunid & 0x3fff) + 1;
  1426. } else {
  1427. /* not p1210m... */
  1428. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1429. if (is_msa2xxx(h, device)) {
  1430. /* msa2xxx way, put logicals on bus 1
  1431. * and match target/lun numbers box
  1432. * reports.
  1433. */
  1434. *bus = 1;
  1435. *target = (lunid >> 16) & 0x3fff;
  1436. *lun = lunid & 0x00ff;
  1437. } else {
  1438. /* Traditional smart array way. */
  1439. *bus = 0;
  1440. *lun = 0;
  1441. *target = lunid & 0x3fff;
  1442. }
  1443. }
  1444. } else {
  1445. /* physical device */
  1446. if (is_hba_lunid(lunaddrbytes))
  1447. if (unlikely(is_scsi_rev_5(h))) {
  1448. *bus = 0; /* put p1210m ctlr at 0,0,0 */
  1449. *target = 0;
  1450. *lun = 0;
  1451. return;
  1452. } else
  1453. *bus = 3; /* traditional smartarray */
  1454. else
  1455. *bus = 2; /* physical disk */
  1456. *target = -1;
  1457. *lun = -1; /* we will fill these in later. */
  1458. }
  1459. }
  1460. /*
  1461. * If there is no lun 0 on a target, linux won't find any devices.
  1462. * For the MSA2xxx boxes, we have to manually detect the enclosure
  1463. * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
  1464. * it for some reason. *tmpdevice is the target we're adding,
  1465. * this_device is a pointer into the current element of currentsd[]
  1466. * that we're building up in update_scsi_devices(), below.
  1467. * lunzerobits is a bitmap that tracks which targets already have a
  1468. * lun 0 assigned.
  1469. * Returns 1 if an enclosure was added, 0 if not.
  1470. */
  1471. static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
  1472. struct hpsa_scsi_dev_t *tmpdevice,
  1473. struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
  1474. int bus, int target, int lun, unsigned long lunzerobits[],
  1475. int *nmsa2xxx_enclosures)
  1476. {
  1477. unsigned char scsi3addr[8];
  1478. if (test_bit(target, lunzerobits))
  1479. return 0; /* There is already a lun 0 on this target. */
  1480. if (!is_logical_dev_addr_mode(lunaddrbytes))
  1481. return 0; /* It's the logical targets that may lack lun 0. */
  1482. if (!is_msa2xxx(h, tmpdevice))
  1483. return 0; /* It's only the MSA2xxx that have this problem. */
  1484. if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
  1485. return 0;
  1486. if (is_hba_lunid(scsi3addr))
  1487. return 0; /* Don't add the RAID controller here. */
  1488. if (is_scsi_rev_5(h))
  1489. return 0; /* p1210m doesn't need to do this. */
  1490. #define MAX_MSA2XXX_ENCLOSURES 32
  1491. if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
  1492. dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
  1493. "enclosures exceeded. Check your hardware "
  1494. "configuration.");
  1495. return 0;
  1496. }
  1497. memset(scsi3addr, 0, 8);
  1498. scsi3addr[3] = target;
  1499. if (hpsa_update_device_info(h, scsi3addr, this_device))
  1500. return 0;
  1501. (*nmsa2xxx_enclosures)++;
  1502. hpsa_set_bus_target_lun(this_device, bus, target, 0);
  1503. set_bit(target, lunzerobits);
  1504. return 1;
  1505. }
  1506. /*
  1507. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  1508. * logdev. The number of luns in physdev and logdev are returned in
  1509. * *nphysicals and *nlogicals, respectively.
  1510. * Returns 0 on success, -1 otherwise.
  1511. */
  1512. static int hpsa_gather_lun_info(struct ctlr_info *h,
  1513. int reportlunsize,
  1514. struct ReportLUNdata *physdev, u32 *nphysicals,
  1515. struct ReportLUNdata *logdev, u32 *nlogicals)
  1516. {
  1517. if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
  1518. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  1519. return -1;
  1520. }
  1521. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
  1522. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  1523. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
  1524. " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1525. *nphysicals - HPSA_MAX_PHYS_LUN);
  1526. *nphysicals = HPSA_MAX_PHYS_LUN;
  1527. }
  1528. if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
  1529. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  1530. return -1;
  1531. }
  1532. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  1533. /* Reject Logicals in excess of our max capability. */
  1534. if (*nlogicals > HPSA_MAX_LUN) {
  1535. dev_warn(&h->pdev->dev,
  1536. "maximum logical LUNs (%d) exceeded. "
  1537. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  1538. *nlogicals - HPSA_MAX_LUN);
  1539. *nlogicals = HPSA_MAX_LUN;
  1540. }
  1541. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  1542. dev_warn(&h->pdev->dev,
  1543. "maximum logical + physical LUNs (%d) exceeded. "
  1544. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1545. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  1546. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  1547. }
  1548. return 0;
  1549. }
  1550. u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
  1551. int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
  1552. struct ReportLUNdata *logdev_list)
  1553. {
  1554. /* Helper function, figure out where the LUN ID info is coming from
  1555. * given index i, lists of physical and logical devices, where in
  1556. * the list the raid controller is supposed to appear (first or last)
  1557. */
  1558. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  1559. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  1560. if (i == raid_ctlr_position)
  1561. return RAID_CTLR_LUNID;
  1562. if (i < logicals_start)
  1563. return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
  1564. if (i < last_device)
  1565. return &logdev_list->LUN[i - nphysicals -
  1566. (raid_ctlr_position == 0)][0];
  1567. BUG();
  1568. return NULL;
  1569. }
  1570. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
  1571. {
  1572. /* the idea here is we could get notified
  1573. * that some devices have changed, so we do a report
  1574. * physical luns and report logical luns cmd, and adjust
  1575. * our list of devices accordingly.
  1576. *
  1577. * The scsi3addr's of devices won't change so long as the
  1578. * adapter is not reset. That means we can rescan and
  1579. * tell which devices we already know about, vs. new
  1580. * devices, vs. disappearing devices.
  1581. */
  1582. struct ReportLUNdata *physdev_list = NULL;
  1583. struct ReportLUNdata *logdev_list = NULL;
  1584. unsigned char *inq_buff = NULL;
  1585. u32 nphysicals = 0;
  1586. u32 nlogicals = 0;
  1587. u32 ndev_allocated = 0;
  1588. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  1589. int ncurrent = 0;
  1590. int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
  1591. int i, nmsa2xxx_enclosures, ndevs_to_allocate;
  1592. int bus, target, lun;
  1593. int raid_ctlr_position;
  1594. DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
  1595. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  1596. GFP_KERNEL);
  1597. physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1598. logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1599. inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1600. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  1601. if (!currentsd || !physdev_list || !logdev_list ||
  1602. !inq_buff || !tmpdevice) {
  1603. dev_err(&h->pdev->dev, "out of memory\n");
  1604. goto out;
  1605. }
  1606. memset(lunzerobits, 0, sizeof(lunzerobits));
  1607. if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
  1608. logdev_list, &nlogicals))
  1609. goto out;
  1610. /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
  1611. * but each of them 4 times through different paths. The plus 1
  1612. * is for the RAID controller.
  1613. */
  1614. ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
  1615. /* Allocate the per device structures */
  1616. for (i = 0; i < ndevs_to_allocate; i++) {
  1617. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  1618. if (!currentsd[i]) {
  1619. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  1620. __FILE__, __LINE__);
  1621. goto out;
  1622. }
  1623. ndev_allocated++;
  1624. }
  1625. if (unlikely(is_scsi_rev_5(h)))
  1626. raid_ctlr_position = 0;
  1627. else
  1628. raid_ctlr_position = nphysicals + nlogicals;
  1629. /* adjust our table of devices */
  1630. nmsa2xxx_enclosures = 0;
  1631. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  1632. u8 *lunaddrbytes;
  1633. /* Figure out where the LUN ID info is coming from */
  1634. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  1635. i, nphysicals, nlogicals, physdev_list, logdev_list);
  1636. /* skip masked physical devices. */
  1637. if (lunaddrbytes[3] & 0xC0 &&
  1638. i < nphysicals + (raid_ctlr_position == 0))
  1639. continue;
  1640. /* Get device type, vendor, model, device id */
  1641. if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
  1642. continue; /* skip it if we can't talk to it. */
  1643. figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
  1644. tmpdevice);
  1645. this_device = currentsd[ncurrent];
  1646. /*
  1647. * For the msa2xxx boxes, we have to insert a LUN 0 which
  1648. * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
  1649. * is nonetheless an enclosure device there. We have to
  1650. * present that otherwise linux won't find anything if
  1651. * there is no lun 0.
  1652. */
  1653. if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
  1654. lunaddrbytes, bus, target, lun, lunzerobits,
  1655. &nmsa2xxx_enclosures)) {
  1656. ncurrent++;
  1657. this_device = currentsd[ncurrent];
  1658. }
  1659. *this_device = *tmpdevice;
  1660. hpsa_set_bus_target_lun(this_device, bus, target, lun);
  1661. switch (this_device->devtype) {
  1662. case TYPE_ROM: {
  1663. /* We don't *really* support actual CD-ROM devices,
  1664. * just "One Button Disaster Recovery" tape drive
  1665. * which temporarily pretends to be a CD-ROM drive.
  1666. * So we check that the device is really an OBDR tape
  1667. * device by checking for "$DR-10" in bytes 43-48 of
  1668. * the inquiry data.
  1669. */
  1670. char obdr_sig[7];
  1671. #define OBDR_TAPE_SIG "$DR-10"
  1672. strncpy(obdr_sig, &inq_buff[43], 6);
  1673. obdr_sig[6] = '\0';
  1674. if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
  1675. /* Not OBDR device, ignore it. */
  1676. break;
  1677. }
  1678. ncurrent++;
  1679. break;
  1680. case TYPE_DISK:
  1681. if (i < nphysicals)
  1682. break;
  1683. ncurrent++;
  1684. break;
  1685. case TYPE_TAPE:
  1686. case TYPE_MEDIUM_CHANGER:
  1687. ncurrent++;
  1688. break;
  1689. case TYPE_RAID:
  1690. /* Only present the Smartarray HBA as a RAID controller.
  1691. * If it's a RAID controller other than the HBA itself
  1692. * (an external RAID controller, MSA500 or similar)
  1693. * don't present it.
  1694. */
  1695. if (!is_hba_lunid(lunaddrbytes))
  1696. break;
  1697. ncurrent++;
  1698. break;
  1699. default:
  1700. break;
  1701. }
  1702. if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
  1703. break;
  1704. }
  1705. adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
  1706. out:
  1707. kfree(tmpdevice);
  1708. for (i = 0; i < ndev_allocated; i++)
  1709. kfree(currentsd[i]);
  1710. kfree(currentsd);
  1711. kfree(inq_buff);
  1712. kfree(physdev_list);
  1713. kfree(logdev_list);
  1714. }
  1715. /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  1716. * dma mapping and fills in the scatter gather entries of the
  1717. * hpsa command, cp.
  1718. */
  1719. static int hpsa_scatter_gather(struct pci_dev *pdev,
  1720. struct CommandList *cp,
  1721. struct scsi_cmnd *cmd)
  1722. {
  1723. unsigned int len;
  1724. struct scatterlist *sg;
  1725. u64 addr64;
  1726. int use_sg, i;
  1727. BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
  1728. use_sg = scsi_dma_map(cmd);
  1729. if (use_sg < 0)
  1730. return use_sg;
  1731. if (!use_sg)
  1732. goto sglist_finished;
  1733. scsi_for_each_sg(cmd, sg, use_sg, i) {
  1734. addr64 = (u64) sg_dma_address(sg);
  1735. len = sg_dma_len(sg);
  1736. cp->SG[i].Addr.lower =
  1737. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1738. cp->SG[i].Addr.upper =
  1739. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1740. cp->SG[i].Len = len;
  1741. cp->SG[i].Ext = 0; /* we are not chaining */
  1742. }
  1743. sglist_finished:
  1744. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  1745. cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
  1746. return 0;
  1747. }
  1748. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  1749. void (*done)(struct scsi_cmnd *))
  1750. {
  1751. struct ctlr_info *h;
  1752. struct hpsa_scsi_dev_t *dev;
  1753. unsigned char scsi3addr[8];
  1754. struct CommandList *c;
  1755. unsigned long flags;
  1756. /* Get the ptr to our adapter structure out of cmd->host. */
  1757. h = sdev_to_hba(cmd->device);
  1758. dev = cmd->device->hostdata;
  1759. if (!dev) {
  1760. cmd->result = DID_NO_CONNECT << 16;
  1761. done(cmd);
  1762. return 0;
  1763. }
  1764. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  1765. /* Need a lock as this is being allocated from the pool */
  1766. spin_lock_irqsave(&h->lock, flags);
  1767. c = cmd_alloc(h);
  1768. spin_unlock_irqrestore(&h->lock, flags);
  1769. if (c == NULL) { /* trouble... */
  1770. dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
  1771. return SCSI_MLQUEUE_HOST_BUSY;
  1772. }
  1773. /* Fill in the command list header */
  1774. cmd->scsi_done = done; /* save this for use by completion code */
  1775. /* save c in case we have to abort it */
  1776. cmd->host_scribble = (unsigned char *) c;
  1777. c->cmd_type = CMD_SCSI;
  1778. c->scsi_cmd = cmd;
  1779. c->Header.ReplyQueue = 0; /* unused in simple mode */
  1780. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  1781. c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
  1782. c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
  1783. /* Fill in the request block... */
  1784. c->Request.Timeout = 0;
  1785. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  1786. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  1787. c->Request.CDBLen = cmd->cmd_len;
  1788. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  1789. c->Request.Type.Type = TYPE_CMD;
  1790. c->Request.Type.Attribute = ATTR_SIMPLE;
  1791. switch (cmd->sc_data_direction) {
  1792. case DMA_TO_DEVICE:
  1793. c->Request.Type.Direction = XFER_WRITE;
  1794. break;
  1795. case DMA_FROM_DEVICE:
  1796. c->Request.Type.Direction = XFER_READ;
  1797. break;
  1798. case DMA_NONE:
  1799. c->Request.Type.Direction = XFER_NONE;
  1800. break;
  1801. case DMA_BIDIRECTIONAL:
  1802. /* This can happen if a buggy application does a scsi passthru
  1803. * and sets both inlen and outlen to non-zero. ( see
  1804. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  1805. */
  1806. c->Request.Type.Direction = XFER_RSVD;
  1807. /* This is technically wrong, and hpsa controllers should
  1808. * reject it with CMD_INVALID, which is the most correct
  1809. * response, but non-fibre backends appear to let it
  1810. * slide by, and give the same results as if this field
  1811. * were set correctly. Either way is acceptable for
  1812. * our purposes here.
  1813. */
  1814. break;
  1815. default:
  1816. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  1817. cmd->sc_data_direction);
  1818. BUG();
  1819. break;
  1820. }
  1821. if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
  1822. cmd_free(h, c);
  1823. return SCSI_MLQUEUE_HOST_BUSY;
  1824. }
  1825. enqueue_cmd_and_start_io(h, c);
  1826. /* the cmd'll come back via intr handler in complete_scsi_command() */
  1827. return 0;
  1828. }
  1829. static void hpsa_scan_start(struct Scsi_Host *sh)
  1830. {
  1831. struct ctlr_info *h = shost_to_hba(sh);
  1832. unsigned long flags;
  1833. /* wait until any scan already in progress is finished. */
  1834. while (1) {
  1835. spin_lock_irqsave(&h->scan_lock, flags);
  1836. if (h->scan_finished)
  1837. break;
  1838. spin_unlock_irqrestore(&h->scan_lock, flags);
  1839. wait_event(h->scan_wait_queue, h->scan_finished);
  1840. /* Note: We don't need to worry about a race between this
  1841. * thread and driver unload because the midlayer will
  1842. * have incremented the reference count, so unload won't
  1843. * happen if we're in here.
  1844. */
  1845. }
  1846. h->scan_finished = 0; /* mark scan as in progress */
  1847. spin_unlock_irqrestore(&h->scan_lock, flags);
  1848. hpsa_update_scsi_devices(h, h->scsi_host->host_no);
  1849. spin_lock_irqsave(&h->scan_lock, flags);
  1850. h->scan_finished = 1; /* mark scan as finished. */
  1851. wake_up_all(&h->scan_wait_queue);
  1852. spin_unlock_irqrestore(&h->scan_lock, flags);
  1853. }
  1854. static int hpsa_scan_finished(struct Scsi_Host *sh,
  1855. unsigned long elapsed_time)
  1856. {
  1857. struct ctlr_info *h = shost_to_hba(sh);
  1858. unsigned long flags;
  1859. int finished;
  1860. spin_lock_irqsave(&h->scan_lock, flags);
  1861. finished = h->scan_finished;
  1862. spin_unlock_irqrestore(&h->scan_lock, flags);
  1863. return finished;
  1864. }
  1865. static void hpsa_unregister_scsi(struct ctlr_info *h)
  1866. {
  1867. /* we are being forcibly unloaded, and may not refuse. */
  1868. scsi_remove_host(h->scsi_host);
  1869. scsi_host_put(h->scsi_host);
  1870. h->scsi_host = NULL;
  1871. }
  1872. static int hpsa_register_scsi(struct ctlr_info *h)
  1873. {
  1874. int rc;
  1875. rc = hpsa_scsi_detect(h);
  1876. if (rc != 0)
  1877. dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
  1878. " hpsa_scsi_detect(), rc is %d\n", rc);
  1879. return rc;
  1880. }
  1881. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  1882. unsigned char lunaddr[])
  1883. {
  1884. int rc = 0;
  1885. int count = 0;
  1886. int waittime = 1; /* seconds */
  1887. struct CommandList *c;
  1888. c = cmd_special_alloc(h);
  1889. if (!c) {
  1890. dev_warn(&h->pdev->dev, "out of memory in "
  1891. "wait_for_device_to_become_ready.\n");
  1892. return IO_ERROR;
  1893. }
  1894. /* Send test unit ready until device ready, or give up. */
  1895. while (count < HPSA_TUR_RETRY_LIMIT) {
  1896. /* Wait for a bit. do this first, because if we send
  1897. * the TUR right away, the reset will just abort it.
  1898. */
  1899. msleep(1000 * waittime);
  1900. count++;
  1901. /* Increase wait time with each try, up to a point. */
  1902. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  1903. waittime = waittime * 2;
  1904. /* Send the Test Unit Ready */
  1905. fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
  1906. hpsa_scsi_do_simple_cmd_core(h, c);
  1907. /* no unmap needed here because no data xfer. */
  1908. if (c->err_info->CommandStatus == CMD_SUCCESS)
  1909. break;
  1910. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  1911. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  1912. (c->err_info->SenseInfo[2] == NO_SENSE ||
  1913. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  1914. break;
  1915. dev_warn(&h->pdev->dev, "waiting %d secs "
  1916. "for device to become ready.\n", waittime);
  1917. rc = 1; /* device not ready. */
  1918. }
  1919. if (rc)
  1920. dev_warn(&h->pdev->dev, "giving up on device.\n");
  1921. else
  1922. dev_warn(&h->pdev->dev, "device is ready.\n");
  1923. cmd_special_free(h, c);
  1924. return rc;
  1925. }
  1926. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  1927. * complaining. Doing a host- or bus-reset can't do anything good here.
  1928. */
  1929. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  1930. {
  1931. int rc;
  1932. struct ctlr_info *h;
  1933. struct hpsa_scsi_dev_t *dev;
  1934. /* find the controller to which the command to be aborted was sent */
  1935. h = sdev_to_hba(scsicmd->device);
  1936. if (h == NULL) /* paranoia */
  1937. return FAILED;
  1938. dev = scsicmd->device->hostdata;
  1939. if (!dev) {
  1940. dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
  1941. "device lookup failed.\n");
  1942. return FAILED;
  1943. }
  1944. dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
  1945. h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
  1946. /* send a reset to the SCSI LUN which the command was sent to */
  1947. rc = hpsa_send_reset(h, dev->scsi3addr);
  1948. if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
  1949. return SUCCESS;
  1950. dev_warn(&h->pdev->dev, "resetting device failed.\n");
  1951. return FAILED;
  1952. }
  1953. /*
  1954. * For operations that cannot sleep, a command block is allocated at init,
  1955. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  1956. * which ones are free or in use. Lock must be held when calling this.
  1957. * cmd_free() is the complement.
  1958. */
  1959. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  1960. {
  1961. struct CommandList *c;
  1962. int i;
  1963. union u64bit temp64;
  1964. dma_addr_t cmd_dma_handle, err_dma_handle;
  1965. do {
  1966. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  1967. if (i == h->nr_cmds)
  1968. return NULL;
  1969. } while (test_and_set_bit
  1970. (i & (BITS_PER_LONG - 1),
  1971. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  1972. c = h->cmd_pool + i;
  1973. memset(c, 0, sizeof(*c));
  1974. cmd_dma_handle = h->cmd_pool_dhandle
  1975. + i * sizeof(*c);
  1976. c->err_info = h->errinfo_pool + i;
  1977. memset(c->err_info, 0, sizeof(*c->err_info));
  1978. err_dma_handle = h->errinfo_pool_dhandle
  1979. + i * sizeof(*c->err_info);
  1980. h->nr_allocs++;
  1981. c->cmdindex = i;
  1982. INIT_HLIST_NODE(&c->list);
  1983. c->busaddr = (u32) cmd_dma_handle;
  1984. temp64.val = (u64) err_dma_handle;
  1985. c->ErrDesc.Addr.lower = temp64.val32.lower;
  1986. c->ErrDesc.Addr.upper = temp64.val32.upper;
  1987. c->ErrDesc.Len = sizeof(*c->err_info);
  1988. c->h = h;
  1989. return c;
  1990. }
  1991. /* For operations that can wait for kmalloc to possibly sleep,
  1992. * this routine can be called. Lock need not be held to call
  1993. * cmd_special_alloc. cmd_special_free() is the complement.
  1994. */
  1995. static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
  1996. {
  1997. struct CommandList *c;
  1998. union u64bit temp64;
  1999. dma_addr_t cmd_dma_handle, err_dma_handle;
  2000. c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
  2001. if (c == NULL)
  2002. return NULL;
  2003. memset(c, 0, sizeof(*c));
  2004. c->cmdindex = -1;
  2005. c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
  2006. &err_dma_handle);
  2007. if (c->err_info == NULL) {
  2008. pci_free_consistent(h->pdev,
  2009. sizeof(*c), c, cmd_dma_handle);
  2010. return NULL;
  2011. }
  2012. memset(c->err_info, 0, sizeof(*c->err_info));
  2013. INIT_HLIST_NODE(&c->list);
  2014. c->busaddr = (u32) cmd_dma_handle;
  2015. temp64.val = (u64) err_dma_handle;
  2016. c->ErrDesc.Addr.lower = temp64.val32.lower;
  2017. c->ErrDesc.Addr.upper = temp64.val32.upper;
  2018. c->ErrDesc.Len = sizeof(*c->err_info);
  2019. c->h = h;
  2020. return c;
  2021. }
  2022. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  2023. {
  2024. int i;
  2025. i = c - h->cmd_pool;
  2026. clear_bit(i & (BITS_PER_LONG - 1),
  2027. h->cmd_pool_bits + (i / BITS_PER_LONG));
  2028. h->nr_frees++;
  2029. }
  2030. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
  2031. {
  2032. union u64bit temp64;
  2033. temp64.val32.lower = c->ErrDesc.Addr.lower;
  2034. temp64.val32.upper = c->ErrDesc.Addr.upper;
  2035. pci_free_consistent(h->pdev, sizeof(*c->err_info),
  2036. c->err_info, (dma_addr_t) temp64.val);
  2037. pci_free_consistent(h->pdev, sizeof(*c),
  2038. c, (dma_addr_t) c->busaddr);
  2039. }
  2040. #ifdef CONFIG_COMPAT
  2041. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
  2042. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  2043. int cmd, void *arg);
  2044. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2045. {
  2046. switch (cmd) {
  2047. case CCISS_GETPCIINFO:
  2048. case CCISS_GETINTINFO:
  2049. case CCISS_SETINTINFO:
  2050. case CCISS_GETNODENAME:
  2051. case CCISS_SETNODENAME:
  2052. case CCISS_GETHEARTBEAT:
  2053. case CCISS_GETBUSTYPES:
  2054. case CCISS_GETFIRMVER:
  2055. case CCISS_GETDRIVVER:
  2056. case CCISS_REVALIDVOLS:
  2057. case CCISS_DEREGDISK:
  2058. case CCISS_REGNEWDISK:
  2059. case CCISS_REGNEWD:
  2060. case CCISS_RESCANDISK:
  2061. case CCISS_GETLUNINFO:
  2062. return hpsa_ioctl(dev, cmd, arg);
  2063. case CCISS_PASSTHRU32:
  2064. return hpsa_ioctl32_passthru(dev, cmd, arg);
  2065. case CCISS_BIG_PASSTHRU32:
  2066. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  2067. default:
  2068. return -ENOIOCTLCMD;
  2069. }
  2070. }
  2071. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
  2072. {
  2073. IOCTL32_Command_struct __user *arg32 =
  2074. (IOCTL32_Command_struct __user *) arg;
  2075. IOCTL_Command_struct arg64;
  2076. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  2077. int err;
  2078. u32 cp;
  2079. err = 0;
  2080. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2081. sizeof(arg64.LUN_info));
  2082. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2083. sizeof(arg64.Request));
  2084. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2085. sizeof(arg64.error_info));
  2086. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2087. err |= get_user(cp, &arg32->buf);
  2088. arg64.buf = compat_ptr(cp);
  2089. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2090. if (err)
  2091. return -EFAULT;
  2092. err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
  2093. if (err)
  2094. return err;
  2095. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2096. sizeof(arg32->error_info));
  2097. if (err)
  2098. return -EFAULT;
  2099. return err;
  2100. }
  2101. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  2102. int cmd, void *arg)
  2103. {
  2104. BIG_IOCTL32_Command_struct __user *arg32 =
  2105. (BIG_IOCTL32_Command_struct __user *) arg;
  2106. BIG_IOCTL_Command_struct arg64;
  2107. BIG_IOCTL_Command_struct __user *p =
  2108. compat_alloc_user_space(sizeof(arg64));
  2109. int err;
  2110. u32 cp;
  2111. err = 0;
  2112. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2113. sizeof(arg64.LUN_info));
  2114. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2115. sizeof(arg64.Request));
  2116. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2117. sizeof(arg64.error_info));
  2118. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2119. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  2120. err |= get_user(cp, &arg32->buf);
  2121. arg64.buf = compat_ptr(cp);
  2122. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2123. if (err)
  2124. return -EFAULT;
  2125. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
  2126. if (err)
  2127. return err;
  2128. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2129. sizeof(arg32->error_info));
  2130. if (err)
  2131. return -EFAULT;
  2132. return err;
  2133. }
  2134. #endif
  2135. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  2136. {
  2137. struct hpsa_pci_info pciinfo;
  2138. if (!argp)
  2139. return -EINVAL;
  2140. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  2141. pciinfo.bus = h->pdev->bus->number;
  2142. pciinfo.dev_fn = h->pdev->devfn;
  2143. pciinfo.board_id = h->board_id;
  2144. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  2145. return -EFAULT;
  2146. return 0;
  2147. }
  2148. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  2149. {
  2150. DriverVer_type DriverVer;
  2151. unsigned char vmaj, vmin, vsubmin;
  2152. int rc;
  2153. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  2154. &vmaj, &vmin, &vsubmin);
  2155. if (rc != 3) {
  2156. dev_info(&h->pdev->dev, "driver version string '%s' "
  2157. "unrecognized.", HPSA_DRIVER_VERSION);
  2158. vmaj = 0;
  2159. vmin = 0;
  2160. vsubmin = 0;
  2161. }
  2162. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  2163. if (!argp)
  2164. return -EINVAL;
  2165. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  2166. return -EFAULT;
  2167. return 0;
  2168. }
  2169. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2170. {
  2171. IOCTL_Command_struct iocommand;
  2172. struct CommandList *c;
  2173. char *buff = NULL;
  2174. union u64bit temp64;
  2175. if (!argp)
  2176. return -EINVAL;
  2177. if (!capable(CAP_SYS_RAWIO))
  2178. return -EPERM;
  2179. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  2180. return -EFAULT;
  2181. if ((iocommand.buf_size < 1) &&
  2182. (iocommand.Request.Type.Direction != XFER_NONE)) {
  2183. return -EINVAL;
  2184. }
  2185. if (iocommand.buf_size > 0) {
  2186. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  2187. if (buff == NULL)
  2188. return -EFAULT;
  2189. }
  2190. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  2191. /* Copy the data into the buffer we created */
  2192. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
  2193. kfree(buff);
  2194. return -EFAULT;
  2195. }
  2196. } else
  2197. memset(buff, 0, iocommand.buf_size);
  2198. c = cmd_special_alloc(h);
  2199. if (c == NULL) {
  2200. kfree(buff);
  2201. return -ENOMEM;
  2202. }
  2203. /* Fill in the command type */
  2204. c->cmd_type = CMD_IOCTL_PEND;
  2205. /* Fill in Command Header */
  2206. c->Header.ReplyQueue = 0; /* unused in simple mode */
  2207. if (iocommand.buf_size > 0) { /* buffer to fill */
  2208. c->Header.SGList = 1;
  2209. c->Header.SGTotal = 1;
  2210. } else { /* no buffers to fill */
  2211. c->Header.SGList = 0;
  2212. c->Header.SGTotal = 0;
  2213. }
  2214. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  2215. /* use the kernel address the cmd block for tag */
  2216. c->Header.Tag.lower = c->busaddr;
  2217. /* Fill in Request block */
  2218. memcpy(&c->Request, &iocommand.Request,
  2219. sizeof(c->Request));
  2220. /* Fill in the scatter gather information */
  2221. if (iocommand.buf_size > 0) {
  2222. temp64.val = pci_map_single(h->pdev, buff,
  2223. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  2224. c->SG[0].Addr.lower = temp64.val32.lower;
  2225. c->SG[0].Addr.upper = temp64.val32.upper;
  2226. c->SG[0].Len = iocommand.buf_size;
  2227. c->SG[0].Ext = 0; /* we are not chaining*/
  2228. }
  2229. hpsa_scsi_do_simple_cmd_core(h, c);
  2230. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  2231. check_ioctl_unit_attention(h, c);
  2232. /* Copy the error information out */
  2233. memcpy(&iocommand.error_info, c->err_info,
  2234. sizeof(iocommand.error_info));
  2235. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  2236. kfree(buff);
  2237. cmd_special_free(h, c);
  2238. return -EFAULT;
  2239. }
  2240. if (iocommand.Request.Type.Direction == XFER_READ) {
  2241. /* Copy the data out of the buffer we created */
  2242. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  2243. kfree(buff);
  2244. cmd_special_free(h, c);
  2245. return -EFAULT;
  2246. }
  2247. }
  2248. kfree(buff);
  2249. cmd_special_free(h, c);
  2250. return 0;
  2251. }
  2252. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2253. {
  2254. BIG_IOCTL_Command_struct *ioc;
  2255. struct CommandList *c;
  2256. unsigned char **buff = NULL;
  2257. int *buff_size = NULL;
  2258. union u64bit temp64;
  2259. BYTE sg_used = 0;
  2260. int status = 0;
  2261. int i;
  2262. u32 left;
  2263. u32 sz;
  2264. BYTE __user *data_ptr;
  2265. if (!argp)
  2266. return -EINVAL;
  2267. if (!capable(CAP_SYS_RAWIO))
  2268. return -EPERM;
  2269. ioc = (BIG_IOCTL_Command_struct *)
  2270. kmalloc(sizeof(*ioc), GFP_KERNEL);
  2271. if (!ioc) {
  2272. status = -ENOMEM;
  2273. goto cleanup1;
  2274. }
  2275. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  2276. status = -EFAULT;
  2277. goto cleanup1;
  2278. }
  2279. if ((ioc->buf_size < 1) &&
  2280. (ioc->Request.Type.Direction != XFER_NONE)) {
  2281. status = -EINVAL;
  2282. goto cleanup1;
  2283. }
  2284. /* Check kmalloc limits using all SGs */
  2285. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  2286. status = -EINVAL;
  2287. goto cleanup1;
  2288. }
  2289. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  2290. status = -EINVAL;
  2291. goto cleanup1;
  2292. }
  2293. buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  2294. if (!buff) {
  2295. status = -ENOMEM;
  2296. goto cleanup1;
  2297. }
  2298. buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
  2299. if (!buff_size) {
  2300. status = -ENOMEM;
  2301. goto cleanup1;
  2302. }
  2303. left = ioc->buf_size;
  2304. data_ptr = ioc->buf;
  2305. while (left) {
  2306. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  2307. buff_size[sg_used] = sz;
  2308. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  2309. if (buff[sg_used] == NULL) {
  2310. status = -ENOMEM;
  2311. goto cleanup1;
  2312. }
  2313. if (ioc->Request.Type.Direction == XFER_WRITE) {
  2314. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  2315. status = -ENOMEM;
  2316. goto cleanup1;
  2317. }
  2318. } else
  2319. memset(buff[sg_used], 0, sz);
  2320. left -= sz;
  2321. data_ptr += sz;
  2322. sg_used++;
  2323. }
  2324. c = cmd_special_alloc(h);
  2325. if (c == NULL) {
  2326. status = -ENOMEM;
  2327. goto cleanup1;
  2328. }
  2329. c->cmd_type = CMD_IOCTL_PEND;
  2330. c->Header.ReplyQueue = 0;
  2331. if (ioc->buf_size > 0) {
  2332. c->Header.SGList = sg_used;
  2333. c->Header.SGTotal = sg_used;
  2334. } else {
  2335. c->Header.SGList = 0;
  2336. c->Header.SGTotal = 0;
  2337. }
  2338. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  2339. c->Header.Tag.lower = c->busaddr;
  2340. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  2341. if (ioc->buf_size > 0) {
  2342. int i;
  2343. for (i = 0; i < sg_used; i++) {
  2344. temp64.val = pci_map_single(h->pdev, buff[i],
  2345. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  2346. c->SG[i].Addr.lower = temp64.val32.lower;
  2347. c->SG[i].Addr.upper = temp64.val32.upper;
  2348. c->SG[i].Len = buff_size[i];
  2349. /* we are not chaining */
  2350. c->SG[i].Ext = 0;
  2351. }
  2352. }
  2353. hpsa_scsi_do_simple_cmd_core(h, c);
  2354. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  2355. check_ioctl_unit_attention(h, c);
  2356. /* Copy the error information out */
  2357. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  2358. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  2359. cmd_special_free(h, c);
  2360. status = -EFAULT;
  2361. goto cleanup1;
  2362. }
  2363. if (ioc->Request.Type.Direction == XFER_READ) {
  2364. /* Copy the data out of the buffer we created */
  2365. BYTE __user *ptr = ioc->buf;
  2366. for (i = 0; i < sg_used; i++) {
  2367. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  2368. cmd_special_free(h, c);
  2369. status = -EFAULT;
  2370. goto cleanup1;
  2371. }
  2372. ptr += buff_size[i];
  2373. }
  2374. }
  2375. cmd_special_free(h, c);
  2376. status = 0;
  2377. cleanup1:
  2378. if (buff) {
  2379. for (i = 0; i < sg_used; i++)
  2380. kfree(buff[i]);
  2381. kfree(buff);
  2382. }
  2383. kfree(buff_size);
  2384. kfree(ioc);
  2385. return status;
  2386. }
  2387. static void check_ioctl_unit_attention(struct ctlr_info *h,
  2388. struct CommandList *c)
  2389. {
  2390. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  2391. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  2392. (void) check_for_unit_attention(h, c);
  2393. }
  2394. /*
  2395. * ioctl
  2396. */
  2397. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2398. {
  2399. struct ctlr_info *h;
  2400. void __user *argp = (void __user *)arg;
  2401. h = sdev_to_hba(dev);
  2402. switch (cmd) {
  2403. case CCISS_DEREGDISK:
  2404. case CCISS_REGNEWDISK:
  2405. case CCISS_REGNEWD:
  2406. hpsa_scan_start(h->scsi_host);
  2407. return 0;
  2408. case CCISS_GETPCIINFO:
  2409. return hpsa_getpciinfo_ioctl(h, argp);
  2410. case CCISS_GETDRIVVER:
  2411. return hpsa_getdrivver_ioctl(h, argp);
  2412. case CCISS_PASSTHRU:
  2413. return hpsa_passthru_ioctl(h, argp);
  2414. case CCISS_BIG_PASSTHRU:
  2415. return hpsa_big_passthru_ioctl(h, argp);
  2416. default:
  2417. return -ENOTTY;
  2418. }
  2419. }
  2420. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  2421. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  2422. int cmd_type)
  2423. {
  2424. int pci_dir = XFER_NONE;
  2425. c->cmd_type = CMD_IOCTL_PEND;
  2426. c->Header.ReplyQueue = 0;
  2427. if (buff != NULL && size > 0) {
  2428. c->Header.SGList = 1;
  2429. c->Header.SGTotal = 1;
  2430. } else {
  2431. c->Header.SGList = 0;
  2432. c->Header.SGTotal = 0;
  2433. }
  2434. c->Header.Tag.lower = c->busaddr;
  2435. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2436. c->Request.Type.Type = cmd_type;
  2437. if (cmd_type == TYPE_CMD) {
  2438. switch (cmd) {
  2439. case HPSA_INQUIRY:
  2440. /* are we trying to read a vital product page */
  2441. if (page_code != 0) {
  2442. c->Request.CDB[1] = 0x01;
  2443. c->Request.CDB[2] = page_code;
  2444. }
  2445. c->Request.CDBLen = 6;
  2446. c->Request.Type.Attribute = ATTR_SIMPLE;
  2447. c->Request.Type.Direction = XFER_READ;
  2448. c->Request.Timeout = 0;
  2449. c->Request.CDB[0] = HPSA_INQUIRY;
  2450. c->Request.CDB[4] = size & 0xFF;
  2451. break;
  2452. case HPSA_REPORT_LOG:
  2453. case HPSA_REPORT_PHYS:
  2454. /* Talking to controller so It's a physical command
  2455. mode = 00 target = 0. Nothing to write.
  2456. */
  2457. c->Request.CDBLen = 12;
  2458. c->Request.Type.Attribute = ATTR_SIMPLE;
  2459. c->Request.Type.Direction = XFER_READ;
  2460. c->Request.Timeout = 0;
  2461. c->Request.CDB[0] = cmd;
  2462. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  2463. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2464. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2465. c->Request.CDB[9] = size & 0xFF;
  2466. break;
  2467. case HPSA_READ_CAPACITY:
  2468. c->Request.CDBLen = 10;
  2469. c->Request.Type.Attribute = ATTR_SIMPLE;
  2470. c->Request.Type.Direction = XFER_READ;
  2471. c->Request.Timeout = 0;
  2472. c->Request.CDB[0] = cmd;
  2473. break;
  2474. case HPSA_CACHE_FLUSH:
  2475. c->Request.CDBLen = 12;
  2476. c->Request.Type.Attribute = ATTR_SIMPLE;
  2477. c->Request.Type.Direction = XFER_WRITE;
  2478. c->Request.Timeout = 0;
  2479. c->Request.CDB[0] = BMIC_WRITE;
  2480. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2481. break;
  2482. case TEST_UNIT_READY:
  2483. c->Request.CDBLen = 6;
  2484. c->Request.Type.Attribute = ATTR_SIMPLE;
  2485. c->Request.Type.Direction = XFER_NONE;
  2486. c->Request.Timeout = 0;
  2487. break;
  2488. default:
  2489. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  2490. BUG();
  2491. return;
  2492. }
  2493. } else if (cmd_type == TYPE_MSG) {
  2494. switch (cmd) {
  2495. case HPSA_DEVICE_RESET_MSG:
  2496. c->Request.CDBLen = 16;
  2497. c->Request.Type.Type = 1; /* It is a MSG not a CMD */
  2498. c->Request.Type.Attribute = ATTR_SIMPLE;
  2499. c->Request.Type.Direction = XFER_NONE;
  2500. c->Request.Timeout = 0; /* Don't time out */
  2501. c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
  2502. c->Request.CDB[1] = 0x03; /* Reset target above */
  2503. /* If bytes 4-7 are zero, it means reset the */
  2504. /* LunID device */
  2505. c->Request.CDB[4] = 0x00;
  2506. c->Request.CDB[5] = 0x00;
  2507. c->Request.CDB[6] = 0x00;
  2508. c->Request.CDB[7] = 0x00;
  2509. break;
  2510. default:
  2511. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  2512. cmd);
  2513. BUG();
  2514. }
  2515. } else {
  2516. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  2517. BUG();
  2518. }
  2519. switch (c->Request.Type.Direction) {
  2520. case XFER_READ:
  2521. pci_dir = PCI_DMA_FROMDEVICE;
  2522. break;
  2523. case XFER_WRITE:
  2524. pci_dir = PCI_DMA_TODEVICE;
  2525. break;
  2526. case XFER_NONE:
  2527. pci_dir = PCI_DMA_NONE;
  2528. break;
  2529. default:
  2530. pci_dir = PCI_DMA_BIDIRECTIONAL;
  2531. }
  2532. hpsa_map_one(h->pdev, c, buff, size, pci_dir);
  2533. return;
  2534. }
  2535. /*
  2536. * Map (physical) PCI mem into (virtual) kernel space
  2537. */
  2538. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2539. {
  2540. ulong page_base = ((ulong) base) & PAGE_MASK;
  2541. ulong page_offs = ((ulong) base) - page_base;
  2542. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2543. return page_remapped ? (page_remapped + page_offs) : NULL;
  2544. }
  2545. /* Takes cmds off the submission queue and sends them to the hardware,
  2546. * then puts them on the queue of cmds waiting for completion.
  2547. */
  2548. static void start_io(struct ctlr_info *h)
  2549. {
  2550. struct CommandList *c;
  2551. while (!hlist_empty(&h->reqQ)) {
  2552. c = hlist_entry(h->reqQ.first, struct CommandList, list);
  2553. /* can't do anything if fifo is full */
  2554. if ((h->access.fifo_full(h))) {
  2555. dev_warn(&h->pdev->dev, "fifo full\n");
  2556. break;
  2557. }
  2558. /* Get the first entry from the Request Q */
  2559. removeQ(c);
  2560. h->Qdepth--;
  2561. /* Tell the controller execute command */
  2562. h->access.submit_command(h, c);
  2563. /* Put job onto the completed Q */
  2564. addQ(&h->cmpQ, c);
  2565. }
  2566. }
  2567. static inline unsigned long get_next_completion(struct ctlr_info *h)
  2568. {
  2569. return h->access.command_completed(h);
  2570. }
  2571. static inline bool interrupt_pending(struct ctlr_info *h)
  2572. {
  2573. return h->access.intr_pending(h);
  2574. }
  2575. static inline long interrupt_not_for_us(struct ctlr_info *h)
  2576. {
  2577. return !(h->msi_vector || h->msix_vector) &&
  2578. ((h->access.intr_pending(h) == 0) ||
  2579. (h->interrupts_enabled == 0));
  2580. }
  2581. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  2582. u32 raw_tag)
  2583. {
  2584. if (unlikely(tag_index >= h->nr_cmds)) {
  2585. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  2586. return 1;
  2587. }
  2588. return 0;
  2589. }
  2590. static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
  2591. {
  2592. removeQ(c);
  2593. if (likely(c->cmd_type == CMD_SCSI))
  2594. complete_scsi_command(c, 0, raw_tag);
  2595. else if (c->cmd_type == CMD_IOCTL_PEND)
  2596. complete(c->waiting);
  2597. }
  2598. static inline u32 hpsa_tag_contains_index(u32 tag)
  2599. {
  2600. #define DIRECT_LOOKUP_BIT 0x10
  2601. return tag & DIRECT_LOOKUP_BIT;
  2602. }
  2603. static inline u32 hpsa_tag_to_index(u32 tag)
  2604. {
  2605. #define DIRECT_LOOKUP_SHIFT 5
  2606. return tag >> DIRECT_LOOKUP_SHIFT;
  2607. }
  2608. static inline u32 hpsa_tag_discard_error_bits(u32 tag)
  2609. {
  2610. #define HPSA_ERROR_BITS 0x03
  2611. return tag & ~HPSA_ERROR_BITS;
  2612. }
  2613. /* process completion of an indexed ("direct lookup") command */
  2614. static inline u32 process_indexed_cmd(struct ctlr_info *h,
  2615. u32 raw_tag)
  2616. {
  2617. u32 tag_index;
  2618. struct CommandList *c;
  2619. tag_index = hpsa_tag_to_index(raw_tag);
  2620. if (bad_tag(h, tag_index, raw_tag))
  2621. return next_command(h);
  2622. c = h->cmd_pool + tag_index;
  2623. finish_cmd(c, raw_tag);
  2624. return next_command(h);
  2625. }
  2626. /* process completion of a non-indexed command */
  2627. static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
  2628. u32 raw_tag)
  2629. {
  2630. u32 tag;
  2631. struct CommandList *c = NULL;
  2632. struct hlist_node *tmp;
  2633. tag = hpsa_tag_discard_error_bits(raw_tag);
  2634. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  2635. if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
  2636. finish_cmd(c, raw_tag);
  2637. return next_command(h);
  2638. }
  2639. }
  2640. bad_tag(h, h->nr_cmds + 1, raw_tag);
  2641. return next_command(h);
  2642. }
  2643. static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
  2644. {
  2645. struct ctlr_info *h = dev_id;
  2646. unsigned long flags;
  2647. u32 raw_tag;
  2648. if (interrupt_not_for_us(h))
  2649. return IRQ_NONE;
  2650. spin_lock_irqsave(&h->lock, flags);
  2651. raw_tag = get_next_completion(h);
  2652. while (raw_tag != FIFO_EMPTY) {
  2653. if (hpsa_tag_contains_index(raw_tag))
  2654. raw_tag = process_indexed_cmd(h, raw_tag);
  2655. else
  2656. raw_tag = process_nonindexed_cmd(h, raw_tag);
  2657. }
  2658. spin_unlock_irqrestore(&h->lock, flags);
  2659. return IRQ_HANDLED;
  2660. }
  2661. /* Send a message CDB to the firmwart. */
  2662. static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  2663. unsigned char type)
  2664. {
  2665. struct Command {
  2666. struct CommandListHeader CommandHeader;
  2667. struct RequestBlock Request;
  2668. struct ErrDescriptor ErrorDescriptor;
  2669. };
  2670. struct Command *cmd;
  2671. static const size_t cmd_sz = sizeof(*cmd) +
  2672. sizeof(cmd->ErrorDescriptor);
  2673. dma_addr_t paddr64;
  2674. uint32_t paddr32, tag;
  2675. void __iomem *vaddr;
  2676. int i, err;
  2677. vaddr = pci_ioremap_bar(pdev, 0);
  2678. if (vaddr == NULL)
  2679. return -ENOMEM;
  2680. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  2681. * CCISS commands, so they must be allocated from the lower 4GiB of
  2682. * memory.
  2683. */
  2684. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2685. if (err) {
  2686. iounmap(vaddr);
  2687. return -ENOMEM;
  2688. }
  2689. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  2690. if (cmd == NULL) {
  2691. iounmap(vaddr);
  2692. return -ENOMEM;
  2693. }
  2694. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  2695. * although there's no guarantee, we assume that the address is at
  2696. * least 4-byte aligned (most likely, it's page-aligned).
  2697. */
  2698. paddr32 = paddr64;
  2699. cmd->CommandHeader.ReplyQueue = 0;
  2700. cmd->CommandHeader.SGList = 0;
  2701. cmd->CommandHeader.SGTotal = 0;
  2702. cmd->CommandHeader.Tag.lower = paddr32;
  2703. cmd->CommandHeader.Tag.upper = 0;
  2704. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  2705. cmd->Request.CDBLen = 16;
  2706. cmd->Request.Type.Type = TYPE_MSG;
  2707. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  2708. cmd->Request.Type.Direction = XFER_NONE;
  2709. cmd->Request.Timeout = 0; /* Don't time out */
  2710. cmd->Request.CDB[0] = opcode;
  2711. cmd->Request.CDB[1] = type;
  2712. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  2713. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
  2714. cmd->ErrorDescriptor.Addr.upper = 0;
  2715. cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
  2716. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  2717. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  2718. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  2719. if (hpsa_tag_discard_error_bits(tag) == paddr32)
  2720. break;
  2721. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  2722. }
  2723. iounmap(vaddr);
  2724. /* we leak the DMA buffer here ... no choice since the controller could
  2725. * still complete the command.
  2726. */
  2727. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  2728. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  2729. opcode, type);
  2730. return -ETIMEDOUT;
  2731. }
  2732. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  2733. if (tag & HPSA_ERROR_BIT) {
  2734. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  2735. opcode, type);
  2736. return -EIO;
  2737. }
  2738. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  2739. opcode, type);
  2740. return 0;
  2741. }
  2742. #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
  2743. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  2744. static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
  2745. {
  2746. /* the #defines are stolen from drivers/pci/msi.h. */
  2747. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  2748. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  2749. int pos;
  2750. u16 control = 0;
  2751. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  2752. if (pos) {
  2753. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2754. if (control & PCI_MSI_FLAGS_ENABLE) {
  2755. dev_info(&pdev->dev, "resetting MSI\n");
  2756. pci_write_config_word(pdev, msi_control_reg(pos),
  2757. control & ~PCI_MSI_FLAGS_ENABLE);
  2758. }
  2759. }
  2760. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  2761. if (pos) {
  2762. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2763. if (control & PCI_MSIX_FLAGS_ENABLE) {
  2764. dev_info(&pdev->dev, "resetting MSI-X\n");
  2765. pci_write_config_word(pdev, msi_control_reg(pos),
  2766. control & ~PCI_MSIX_FLAGS_ENABLE);
  2767. }
  2768. }
  2769. return 0;
  2770. }
  2771. /* This does a hard reset of the controller using PCI power management
  2772. * states.
  2773. */
  2774. static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
  2775. {
  2776. u16 pmcsr, saved_config_space[32];
  2777. int i, pos;
  2778. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  2779. /* This is very nearly the same thing as
  2780. *
  2781. * pci_save_state(pci_dev);
  2782. * pci_set_power_state(pci_dev, PCI_D3hot);
  2783. * pci_set_power_state(pci_dev, PCI_D0);
  2784. * pci_restore_state(pci_dev);
  2785. *
  2786. * but we can't use these nice canned kernel routines on
  2787. * kexec, because they also check the MSI/MSI-X state in PCI
  2788. * configuration space and do the wrong thing when it is
  2789. * set/cleared. Also, the pci_save/restore_state functions
  2790. * violate the ordering requirements for restoring the
  2791. * configuration space from the CCISS document (see the
  2792. * comment below). So we roll our own ....
  2793. */
  2794. for (i = 0; i < 32; i++)
  2795. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  2796. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  2797. if (pos == 0) {
  2798. dev_err(&pdev->dev,
  2799. "hpsa_reset_controller: PCI PM not supported\n");
  2800. return -ENODEV;
  2801. }
  2802. /* Quoting from the Open CISS Specification: "The Power
  2803. * Management Control/Status Register (CSR) controls the power
  2804. * state of the device. The normal operating state is D0,
  2805. * CSR=00h. The software off state is D3, CSR=03h. To reset
  2806. * the controller, place the interface device in D3 then to
  2807. * D0, this causes a secondary PCI reset which will reset the
  2808. * controller."
  2809. */
  2810. /* enter the D3hot power management state */
  2811. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  2812. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2813. pmcsr |= PCI_D3hot;
  2814. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2815. msleep(500);
  2816. /* enter the D0 power management state */
  2817. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2818. pmcsr |= PCI_D0;
  2819. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2820. msleep(500);
  2821. /* Restore the PCI configuration space. The Open CISS
  2822. * Specification says, "Restore the PCI Configuration
  2823. * Registers, offsets 00h through 60h. It is important to
  2824. * restore the command register, 16-bits at offset 04h,
  2825. * last. Do not restore the configuration status register,
  2826. * 16-bits at offset 06h." Note that the offset is 2*i.
  2827. */
  2828. for (i = 0; i < 32; i++) {
  2829. if (i == 2 || i == 3)
  2830. continue;
  2831. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  2832. }
  2833. wmb();
  2834. pci_write_config_word(pdev, 4, saved_config_space[2]);
  2835. return 0;
  2836. }
  2837. /*
  2838. * We cannot read the structure directly, for portability we must use
  2839. * the io functions.
  2840. * This is for debug only.
  2841. */
  2842. #ifdef HPSA_DEBUG
  2843. static void print_cfg_table(struct device *dev, struct CfgTable *tb)
  2844. {
  2845. int i;
  2846. char temp_name[17];
  2847. dev_info(dev, "Controller Configuration information\n");
  2848. dev_info(dev, "------------------------------------\n");
  2849. for (i = 0; i < 4; i++)
  2850. temp_name[i] = readb(&(tb->Signature[i]));
  2851. temp_name[4] = '\0';
  2852. dev_info(dev, " Signature = %s\n", temp_name);
  2853. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  2854. dev_info(dev, " Transport methods supported = 0x%x\n",
  2855. readl(&(tb->TransportSupport)));
  2856. dev_info(dev, " Transport methods active = 0x%x\n",
  2857. readl(&(tb->TransportActive)));
  2858. dev_info(dev, " Requested transport Method = 0x%x\n",
  2859. readl(&(tb->HostWrite.TransportRequest)));
  2860. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  2861. readl(&(tb->HostWrite.CoalIntDelay)));
  2862. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  2863. readl(&(tb->HostWrite.CoalIntCount)));
  2864. dev_info(dev, " Max outstanding commands = 0x%d\n",
  2865. readl(&(tb->CmdsOutMax)));
  2866. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  2867. for (i = 0; i < 16; i++)
  2868. temp_name[i] = readb(&(tb->ServerName[i]));
  2869. temp_name[16] = '\0';
  2870. dev_info(dev, " Server Name = %s\n", temp_name);
  2871. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  2872. readl(&(tb->HeartBeat)));
  2873. }
  2874. #endif /* HPSA_DEBUG */
  2875. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  2876. {
  2877. int i, offset, mem_type, bar_type;
  2878. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2879. return 0;
  2880. offset = 0;
  2881. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2882. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  2883. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2884. offset += 4;
  2885. else {
  2886. mem_type = pci_resource_flags(pdev, i) &
  2887. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2888. switch (mem_type) {
  2889. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2890. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2891. offset += 4; /* 32 bit */
  2892. break;
  2893. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2894. offset += 8;
  2895. break;
  2896. default: /* reserved in PCI 2.2 */
  2897. dev_warn(&pdev->dev,
  2898. "base address is invalid\n");
  2899. return -1;
  2900. break;
  2901. }
  2902. }
  2903. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2904. return i + 1;
  2905. }
  2906. return -1;
  2907. }
  2908. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  2909. * controllers that are capable. If not, we use IO-APIC mode.
  2910. */
  2911. static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
  2912. struct pci_dev *pdev, u32 board_id)
  2913. {
  2914. #ifdef CONFIG_PCI_MSI
  2915. int err;
  2916. struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
  2917. {0, 2}, {0, 3}
  2918. };
  2919. /* Some boards advertise MSI but don't really support it */
  2920. if ((board_id == 0x40700E11) ||
  2921. (board_id == 0x40800E11) ||
  2922. (board_id == 0x40820E11) || (board_id == 0x40830E11))
  2923. goto default_int_mode;
  2924. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  2925. dev_info(&pdev->dev, "MSIX\n");
  2926. err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
  2927. if (!err) {
  2928. h->intr[0] = hpsa_msix_entries[0].vector;
  2929. h->intr[1] = hpsa_msix_entries[1].vector;
  2930. h->intr[2] = hpsa_msix_entries[2].vector;
  2931. h->intr[3] = hpsa_msix_entries[3].vector;
  2932. h->msix_vector = 1;
  2933. return;
  2934. }
  2935. if (err > 0) {
  2936. dev_warn(&pdev->dev, "only %d MSI-X vectors "
  2937. "available\n", err);
  2938. goto default_int_mode;
  2939. } else {
  2940. dev_warn(&pdev->dev, "MSI-X init failed %d\n",
  2941. err);
  2942. goto default_int_mode;
  2943. }
  2944. }
  2945. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  2946. dev_info(&pdev->dev, "MSI\n");
  2947. if (!pci_enable_msi(pdev))
  2948. h->msi_vector = 1;
  2949. else
  2950. dev_warn(&pdev->dev, "MSI init failed\n");
  2951. }
  2952. default_int_mode:
  2953. #endif /* CONFIG_PCI_MSI */
  2954. /* if we get here we're going to use the default interrupt mode */
  2955. h->intr[PERF_MODE_INT] = pdev->irq;
  2956. }
  2957. static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
  2958. {
  2959. ushort subsystem_vendor_id, subsystem_device_id, command;
  2960. u32 board_id, scratchpad = 0;
  2961. u64 cfg_offset;
  2962. u32 cfg_base_addr;
  2963. u64 cfg_base_addr_index;
  2964. u32 trans_offset;
  2965. int i, prod_index, err;
  2966. subsystem_vendor_id = pdev->subsystem_vendor;
  2967. subsystem_device_id = pdev->subsystem_device;
  2968. board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
  2969. subsystem_vendor_id);
  2970. for (i = 0; i < ARRAY_SIZE(products); i++)
  2971. if (board_id == products[i].board_id)
  2972. break;
  2973. prod_index = i;
  2974. if (prod_index == ARRAY_SIZE(products)) {
  2975. prod_index--;
  2976. if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
  2977. !hpsa_allow_any) {
  2978. dev_warn(&pdev->dev, "unrecognized board ID:"
  2979. " 0x%08lx, ignoring.\n",
  2980. (unsigned long) board_id);
  2981. return -ENODEV;
  2982. }
  2983. }
  2984. /* check to see if controller has been disabled
  2985. * BEFORE trying to enable it
  2986. */
  2987. (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
  2988. if (!(command & 0x02)) {
  2989. dev_warn(&pdev->dev, "controller appears to be disabled\n");
  2990. return -ENODEV;
  2991. }
  2992. err = pci_enable_device(pdev);
  2993. if (err) {
  2994. dev_warn(&pdev->dev, "unable to enable PCI device\n");
  2995. return err;
  2996. }
  2997. err = pci_request_regions(pdev, "hpsa");
  2998. if (err) {
  2999. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  3000. return err;
  3001. }
  3002. /* If the kernel supports MSI/MSI-X we will try to enable that,
  3003. * else we use the IO-APIC interrupt assigned to us by system ROM.
  3004. */
  3005. hpsa_interrupt_mode(h, pdev, board_id);
  3006. /* find the memory BAR */
  3007. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3008. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  3009. break;
  3010. }
  3011. if (i == DEVICE_COUNT_RESOURCE) {
  3012. dev_warn(&pdev->dev, "no memory BAR found\n");
  3013. err = -ENODEV;
  3014. goto err_out_free_res;
  3015. }
  3016. h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
  3017. * already removed
  3018. */
  3019. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  3020. /* Wait for the board to become ready. */
  3021. for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
  3022. scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  3023. if (scratchpad == HPSA_FIRMWARE_READY)
  3024. break;
  3025. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  3026. }
  3027. if (scratchpad != HPSA_FIRMWARE_READY) {
  3028. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  3029. err = -ENODEV;
  3030. goto err_out_free_res;
  3031. }
  3032. /* get the address index number */
  3033. cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
  3034. cfg_base_addr &= (u32) 0x0000ffff;
  3035. cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
  3036. if (cfg_base_addr_index == -1) {
  3037. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  3038. err = -ENODEV;
  3039. goto err_out_free_res;
  3040. }
  3041. cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
  3042. h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  3043. cfg_base_addr_index) + cfg_offset,
  3044. sizeof(h->cfgtable));
  3045. /* Find performant mode table. */
  3046. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3047. h->transtable = remap_pci_mem(pci_resource_start(pdev,
  3048. cfg_base_addr_index)+cfg_offset+trans_offset,
  3049. sizeof(*h->transtable));
  3050. h->board_id = board_id;
  3051. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3052. h->product_name = products[prod_index].product_name;
  3053. h->access = *(products[prod_index].access);
  3054. /* Allow room for some ioctls */
  3055. h->nr_cmds = h->max_commands - 4;
  3056. if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
  3057. (readb(&h->cfgtable->Signature[1]) != 'I') ||
  3058. (readb(&h->cfgtable->Signature[2]) != 'S') ||
  3059. (readb(&h->cfgtable->Signature[3]) != 'S')) {
  3060. dev_warn(&pdev->dev, "not a valid CISS config table\n");
  3061. err = -ENODEV;
  3062. goto err_out_free_res;
  3063. }
  3064. #ifdef CONFIG_X86
  3065. {
  3066. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3067. u32 prefetch;
  3068. prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
  3069. prefetch |= 0x100;
  3070. writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
  3071. }
  3072. #endif
  3073. /* Disabling DMA prefetch for the P600
  3074. * An ASIC bug may result in a prefetch beyond
  3075. * physical memory.
  3076. */
  3077. if (board_id == 0x3225103C) {
  3078. u32 dma_prefetch;
  3079. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  3080. dma_prefetch |= 0x8000;
  3081. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  3082. }
  3083. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  3084. /* Update the field, and then ring the doorbell */
  3085. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  3086. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3087. /* under certain very rare conditions, this can take awhile.
  3088. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3089. * as we enter this code.)
  3090. */
  3091. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3092. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3093. break;
  3094. /* delay and try again */
  3095. msleep(10);
  3096. }
  3097. #ifdef HPSA_DEBUG
  3098. print_cfg_table(&pdev->dev, h->cfgtable);
  3099. #endif /* HPSA_DEBUG */
  3100. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  3101. dev_warn(&pdev->dev, "unable to get board into simple mode\n");
  3102. err = -ENODEV;
  3103. goto err_out_free_res;
  3104. }
  3105. return 0;
  3106. err_out_free_res:
  3107. /*
  3108. * Deliberately omit pci_disable_device(): it does something nasty to
  3109. * Smart Array controllers that pci_enable_device does not undo
  3110. */
  3111. pci_release_regions(pdev);
  3112. return err;
  3113. }
  3114. static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
  3115. {
  3116. int rc;
  3117. #define HBA_INQUIRY_BYTE_COUNT 64
  3118. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  3119. if (!h->hba_inquiry_data)
  3120. return;
  3121. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  3122. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  3123. if (rc != 0) {
  3124. kfree(h->hba_inquiry_data);
  3125. h->hba_inquiry_data = NULL;
  3126. }
  3127. }
  3128. static int __devinit hpsa_init_one(struct pci_dev *pdev,
  3129. const struct pci_device_id *ent)
  3130. {
  3131. int i, rc;
  3132. int dac;
  3133. struct ctlr_info *h;
  3134. if (number_of_controllers == 0)
  3135. printk(KERN_INFO DRIVER_NAME "\n");
  3136. if (reset_devices) {
  3137. /* Reset the controller with a PCI power-cycle */
  3138. if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
  3139. return -ENODEV;
  3140. /* Some devices (notably the HP Smart Array 5i Controller)
  3141. need a little pause here */
  3142. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  3143. /* Now try to get the controller to respond to a no-op */
  3144. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  3145. if (hpsa_noop(pdev) == 0)
  3146. break;
  3147. else
  3148. dev_warn(&pdev->dev, "no-op failed%s\n",
  3149. (i < 11 ? "; re-trying" : ""));
  3150. }
  3151. }
  3152. /* Command structures must be aligned on a 32-byte boundary because
  3153. * the 5 lower bits of the address are used by the hardware. and by
  3154. * the driver. See comments in hpsa.h for more info.
  3155. */
  3156. #define COMMANDLIST_ALIGNMENT 32
  3157. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  3158. h = kzalloc(sizeof(*h), GFP_KERNEL);
  3159. if (!h)
  3160. return -ENOMEM;
  3161. h->busy_initializing = 1;
  3162. INIT_HLIST_HEAD(&h->cmpQ);
  3163. INIT_HLIST_HEAD(&h->reqQ);
  3164. mutex_init(&h->busy_shutting_down);
  3165. init_completion(&h->scan_wait);
  3166. rc = hpsa_pci_init(h, pdev);
  3167. if (rc != 0)
  3168. goto clean1;
  3169. sprintf(h->devname, "hpsa%d", number_of_controllers);
  3170. h->ctlr = number_of_controllers;
  3171. number_of_controllers++;
  3172. h->pdev = pdev;
  3173. /* configure PCI DMA stuff */
  3174. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3175. if (rc == 0) {
  3176. dac = 1;
  3177. } else {
  3178. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3179. if (rc == 0) {
  3180. dac = 0;
  3181. } else {
  3182. dev_err(&pdev->dev, "no suitable DMA available\n");
  3183. goto clean1;
  3184. }
  3185. }
  3186. /* make sure the board interrupts are off */
  3187. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3188. rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
  3189. IRQF_DISABLED, h->devname, h);
  3190. if (rc) {
  3191. dev_err(&pdev->dev, "unable to get irq %d for %s\n",
  3192. h->intr[PERF_MODE_INT], h->devname);
  3193. goto clean2;
  3194. }
  3195. dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
  3196. h->devname, pdev->device,
  3197. h->intr[PERF_MODE_INT], dac ? "" : " not");
  3198. h->cmd_pool_bits =
  3199. kmalloc(((h->nr_cmds + BITS_PER_LONG -
  3200. 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
  3201. h->cmd_pool = pci_alloc_consistent(h->pdev,
  3202. h->nr_cmds * sizeof(*h->cmd_pool),
  3203. &(h->cmd_pool_dhandle));
  3204. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  3205. h->nr_cmds * sizeof(*h->errinfo_pool),
  3206. &(h->errinfo_pool_dhandle));
  3207. if ((h->cmd_pool_bits == NULL)
  3208. || (h->cmd_pool == NULL)
  3209. || (h->errinfo_pool == NULL)) {
  3210. dev_err(&pdev->dev, "out of memory");
  3211. rc = -ENOMEM;
  3212. goto clean4;
  3213. }
  3214. spin_lock_init(&h->lock);
  3215. spin_lock_init(&h->scan_lock);
  3216. init_waitqueue_head(&h->scan_wait_queue);
  3217. h->scan_finished = 1; /* no scan currently in progress */
  3218. pci_set_drvdata(pdev, h);
  3219. memset(h->cmd_pool_bits, 0,
  3220. ((h->nr_cmds + BITS_PER_LONG -
  3221. 1) / BITS_PER_LONG) * sizeof(unsigned long));
  3222. hpsa_scsi_setup(h);
  3223. /* Turn the interrupts on so we can service requests */
  3224. h->access.set_intr_mask(h, HPSA_INTR_ON);
  3225. hpsa_put_ctlr_into_performant_mode(h);
  3226. hpsa_hba_inquiry(h);
  3227. hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
  3228. h->busy_initializing = 0;
  3229. return 1;
  3230. clean4:
  3231. kfree(h->cmd_pool_bits);
  3232. if (h->cmd_pool)
  3233. pci_free_consistent(h->pdev,
  3234. h->nr_cmds * sizeof(struct CommandList),
  3235. h->cmd_pool, h->cmd_pool_dhandle);
  3236. if (h->errinfo_pool)
  3237. pci_free_consistent(h->pdev,
  3238. h->nr_cmds * sizeof(struct ErrorInfo),
  3239. h->errinfo_pool,
  3240. h->errinfo_pool_dhandle);
  3241. free_irq(h->intr[PERF_MODE_INT], h);
  3242. clean2:
  3243. clean1:
  3244. h->busy_initializing = 0;
  3245. kfree(h);
  3246. return rc;
  3247. }
  3248. static void hpsa_flush_cache(struct ctlr_info *h)
  3249. {
  3250. char *flush_buf;
  3251. struct CommandList *c;
  3252. flush_buf = kzalloc(4, GFP_KERNEL);
  3253. if (!flush_buf)
  3254. return;
  3255. c = cmd_special_alloc(h);
  3256. if (!c) {
  3257. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  3258. goto out_of_memory;
  3259. }
  3260. fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  3261. RAID_CTLR_LUNID, TYPE_CMD);
  3262. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
  3263. if (c->err_info->CommandStatus != 0)
  3264. dev_warn(&h->pdev->dev,
  3265. "error flushing cache on controller\n");
  3266. cmd_special_free(h, c);
  3267. out_of_memory:
  3268. kfree(flush_buf);
  3269. }
  3270. static void hpsa_shutdown(struct pci_dev *pdev)
  3271. {
  3272. struct ctlr_info *h;
  3273. h = pci_get_drvdata(pdev);
  3274. /* Turn board interrupts off and send the flush cache command
  3275. * sendcmd will turn off interrupt, and send the flush...
  3276. * To write all data in the battery backed cache to disks
  3277. */
  3278. hpsa_flush_cache(h);
  3279. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3280. free_irq(h->intr[PERF_MODE_INT], h);
  3281. #ifdef CONFIG_PCI_MSI
  3282. if (h->msix_vector)
  3283. pci_disable_msix(h->pdev);
  3284. else if (h->msi_vector)
  3285. pci_disable_msi(h->pdev);
  3286. #endif /* CONFIG_PCI_MSI */
  3287. }
  3288. static void __devexit hpsa_remove_one(struct pci_dev *pdev)
  3289. {
  3290. struct ctlr_info *h;
  3291. if (pci_get_drvdata(pdev) == NULL) {
  3292. dev_err(&pdev->dev, "unable to remove device \n");
  3293. return;
  3294. }
  3295. h = pci_get_drvdata(pdev);
  3296. mutex_lock(&h->busy_shutting_down);
  3297. remove_from_scan_list(h);
  3298. hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
  3299. hpsa_shutdown(pdev);
  3300. iounmap(h->vaddr);
  3301. pci_free_consistent(h->pdev,
  3302. h->nr_cmds * sizeof(struct CommandList),
  3303. h->cmd_pool, h->cmd_pool_dhandle);
  3304. pci_free_consistent(h->pdev,
  3305. h->nr_cmds * sizeof(struct ErrorInfo),
  3306. h->errinfo_pool, h->errinfo_pool_dhandle);
  3307. pci_free_consistent(h->pdev, h->reply_pool_size,
  3308. h->reply_pool, h->reply_pool_dhandle);
  3309. kfree(h->cmd_pool_bits);
  3310. kfree(h->blockFetchTable);
  3311. kfree(h->hba_inquiry_data);
  3312. /*
  3313. * Deliberately omit pci_disable_device(): it does something nasty to
  3314. * Smart Array controllers that pci_enable_device does not undo
  3315. */
  3316. pci_release_regions(pdev);
  3317. pci_set_drvdata(pdev, NULL);
  3318. mutex_unlock(&h->busy_shutting_down);
  3319. kfree(h);
  3320. }
  3321. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  3322. __attribute__((unused)) pm_message_t state)
  3323. {
  3324. return -ENOSYS;
  3325. }
  3326. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  3327. {
  3328. return -ENOSYS;
  3329. }
  3330. static struct pci_driver hpsa_pci_driver = {
  3331. .name = "hpsa",
  3332. .probe = hpsa_init_one,
  3333. .remove = __devexit_p(hpsa_remove_one),
  3334. .id_table = hpsa_pci_device_id, /* id_table */
  3335. .shutdown = hpsa_shutdown,
  3336. .suspend = hpsa_suspend,
  3337. .resume = hpsa_resume,
  3338. };
  3339. /* Fill in bucket_map[], given nsgs (the max number of
  3340. * scatter gather elements supported) and bucket[],
  3341. * which is an array of 8 integers. The bucket[] array
  3342. * contains 8 different DMA transfer sizes (in 16
  3343. * byte increments) which the controller uses to fetch
  3344. * commands. This function fills in bucket_map[], which
  3345. * maps a given number of scatter gather elements to one of
  3346. * the 8 DMA transfer sizes. The point of it is to allow the
  3347. * controller to only do as much DMA as needed to fetch the
  3348. * command, with the DMA transfer size encoded in the lower
  3349. * bits of the command address.
  3350. */
  3351. static void calc_bucket_map(int bucket[], int num_buckets,
  3352. int nsgs, int *bucket_map)
  3353. {
  3354. int i, j, b, size;
  3355. /* even a command with 0 SGs requires 4 blocks */
  3356. #define MINIMUM_TRANSFER_BLOCKS 4
  3357. #define NUM_BUCKETS 8
  3358. /* Note, bucket_map must have nsgs+1 entries. */
  3359. for (i = 0; i <= nsgs; i++) {
  3360. /* Compute size of a command with i SG entries */
  3361. size = i + MINIMUM_TRANSFER_BLOCKS;
  3362. b = num_buckets; /* Assume the biggest bucket */
  3363. /* Find the bucket that is just big enough */
  3364. for (j = 0; j < 8; j++) {
  3365. if (bucket[j] >= size) {
  3366. b = j;
  3367. break;
  3368. }
  3369. }
  3370. /* for a command with i SG entries, use bucket b. */
  3371. bucket_map[i] = b;
  3372. }
  3373. }
  3374. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  3375. {
  3376. u32 trans_support;
  3377. u64 trans_offset;
  3378. /* 5 = 1 s/g entry or 4k
  3379. * 6 = 2 s/g entry or 8k
  3380. * 8 = 4 s/g entry or 16k
  3381. * 10 = 6 s/g entry or 24k
  3382. */
  3383. int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
  3384. int i = 0;
  3385. int l = 0;
  3386. unsigned long register_value;
  3387. trans_support = readl(&(h->cfgtable->TransportSupport));
  3388. if (!(trans_support & PERFORMANT_MODE))
  3389. return;
  3390. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3391. h->max_sg_entries = 32;
  3392. /* Performant mode ring buffer and supporting data structures */
  3393. h->reply_pool_size = h->max_commands * sizeof(u64);
  3394. h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
  3395. &(h->reply_pool_dhandle));
  3396. /* Need a block fetch table for performant mode */
  3397. h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
  3398. sizeof(u32)), GFP_KERNEL);
  3399. if ((h->reply_pool == NULL)
  3400. || (h->blockFetchTable == NULL))
  3401. goto clean_up;
  3402. h->reply_pool_wraparound = 1; /* spec: init to 1 */
  3403. /* Controller spec: zero out this buffer. */
  3404. memset(h->reply_pool, 0, h->reply_pool_size);
  3405. h->reply_pool_head = h->reply_pool;
  3406. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3407. bft[7] = h->max_sg_entries + 4;
  3408. calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
  3409. for (i = 0; i < 8; i++)
  3410. writel(bft[i], &h->transtable->BlockFetch[i]);
  3411. /* size of controller ring buffer */
  3412. writel(h->max_commands, &h->transtable->RepQSize);
  3413. writel(1, &h->transtable->RepQCount);
  3414. writel(0, &h->transtable->RepQCtrAddrLow32);
  3415. writel(0, &h->transtable->RepQCtrAddrHigh32);
  3416. writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
  3417. writel(0, &h->transtable->RepQAddr0High32);
  3418. writel(CFGTBL_Trans_Performant,
  3419. &(h->cfgtable->HostWrite.TransportRequest));
  3420. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3421. /* under certain very rare conditions, this can take awhile.
  3422. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3423. * as we enter this code.) */
  3424. for (l = 0; l < MAX_CONFIG_WAIT; l++) {
  3425. register_value = readl(h->vaddr + SA5_DOORBELL);
  3426. if (!(register_value & CFGTBL_ChangeReq))
  3427. break;
  3428. /* delay and try again */
  3429. set_current_state(TASK_INTERRUPTIBLE);
  3430. schedule_timeout(10);
  3431. }
  3432. register_value = readl(&(h->cfgtable->TransportActive));
  3433. if (!(register_value & CFGTBL_Trans_Performant)) {
  3434. dev_warn(&h->pdev->dev, "unable to get board into"
  3435. " performant mode\n");
  3436. return;
  3437. }
  3438. /* Change the access methods to the performant access methods */
  3439. h->access = SA5_performant_access;
  3440. h->transMethod = CFGTBL_Trans_Performant;
  3441. return;
  3442. clean_up:
  3443. if (h->reply_pool)
  3444. pci_free_consistent(h->pdev, h->reply_pool_size,
  3445. h->reply_pool, h->reply_pool_dhandle);
  3446. kfree(h->blockFetchTable);
  3447. }
  3448. /*
  3449. * This is it. Register the PCI driver information for the cards we control
  3450. * the OS will call our registered routines when it finds one of our cards.
  3451. */
  3452. static int __init hpsa_init(void)
  3453. {
  3454. int err;
  3455. /* Start the scan thread */
  3456. hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
  3457. if (IS_ERR(hpsa_scan_thread)) {
  3458. err = PTR_ERR(hpsa_scan_thread);
  3459. return -ENODEV;
  3460. }
  3461. err = pci_register_driver(&hpsa_pci_driver);
  3462. if (err)
  3463. kthread_stop(hpsa_scan_thread);
  3464. return err;
  3465. }
  3466. static void __exit hpsa_cleanup(void)
  3467. {
  3468. pci_unregister_driver(&hpsa_pci_driver);
  3469. kthread_stop(hpsa_scan_thread);
  3470. }
  3471. module_init(hpsa_init);
  3472. module_exit(hpsa_cleanup);