hpsa.c 96 KB

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