hpsa.c 107 KB

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