sbp2.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  1. /*
  2. * sbp2.c - SBP-2 protocol driver for IEEE-1394
  3. *
  4. * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
  5. * jamesg@filanet.com (JSG)
  6. *
  7. * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. /*
  24. * Brief Description:
  25. *
  26. * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394
  27. * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level
  28. * driver. It also registers as a SCSI lower-level driver in order to accept
  29. * SCSI commands for transport using SBP-2.
  30. *
  31. * You may access any attached SBP-2 storage devices as if they were SCSI
  32. * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.).
  33. *
  34. * Current Issues:
  35. *
  36. * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
  37. * but the code needs additional debugging.
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/list.h>
  41. #include <linux/string.h>
  42. #include <linux/stringify.h>
  43. #include <linux/slab.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/fs.h>
  46. #include <linux/poll.h>
  47. #include <linux/module.h>
  48. #include <linux/moduleparam.h>
  49. #include <linux/types.h>
  50. #include <linux/delay.h>
  51. #include <linux/sched.h>
  52. #include <linux/blkdev.h>
  53. #include <linux/smp_lock.h>
  54. #include <linux/init.h>
  55. #include <linux/pci.h>
  56. #include <linux/wait.h>
  57. #include <asm/current.h>
  58. #include <asm/uaccess.h>
  59. #include <asm/io.h>
  60. #include <asm/byteorder.h>
  61. #include <asm/system.h>
  62. #include <asm/scatterlist.h>
  63. #include <scsi/scsi.h>
  64. #include <scsi/scsi_cmnd.h>
  65. #include <scsi/scsi_dbg.h>
  66. #include <scsi/scsi_device.h>
  67. #include <scsi/scsi_host.h>
  68. #include "csr1212.h"
  69. #include "ieee1394.h"
  70. #include "ieee1394_types.h"
  71. #include "ieee1394_core.h"
  72. #include "nodemgr.h"
  73. #include "hosts.h"
  74. #include "highlevel.h"
  75. #include "ieee1394_transactions.h"
  76. #include "sbp2.h"
  77. /*
  78. * Module load parameter definitions
  79. */
  80. /*
  81. * Change max_speed on module load if you have a bad IEEE-1394
  82. * controller that has trouble running 2KB packets at 400mb.
  83. *
  84. * NOTE: On certain OHCI parts I have seen short packets on async transmit
  85. * (probably due to PCI latency/throughput issues with the part). You can
  86. * bump down the speed if you are running into problems.
  87. */
  88. static int max_speed = IEEE1394_SPEED_MAX;
  89. module_param(max_speed, int, 0644);
  90. MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
  91. /*
  92. * Set serialize_io to 1 if you'd like only one scsi command sent
  93. * down to us at a time (debugging). This might be necessary for very
  94. * badly behaved sbp2 devices.
  95. *
  96. * TODO: Make this configurable per device.
  97. */
  98. static int serialize_io = 1;
  99. module_param(serialize_io, int, 0444);
  100. MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
  101. /*
  102. * Bump up max_sectors if you'd like to support very large sized
  103. * transfers. Please note that some older sbp2 bridge chips are broken for
  104. * transfers greater or equal to 128KB. Default is a value of 255
  105. * sectors, or just under 128KB (at 512 byte sector size). I can note that
  106. * the Oxsemi sbp2 chipsets have no problems supporting very large
  107. * transfer sizes.
  108. */
  109. static int max_sectors = SBP2_MAX_SECTORS;
  110. module_param(max_sectors, int, 0444);
  111. MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
  112. __stringify(SBP2_MAX_SECTORS) ")");
  113. /*
  114. * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
  115. * do an exclusive login, as it's generally unsafe to have two hosts
  116. * talking to a single sbp2 device at the same time (filesystem coherency,
  117. * etc.). If you're running an sbp2 device that supports multiple logins,
  118. * and you're either running read-only filesystems or some sort of special
  119. * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
  120. * File System, or Lustre, then set exclusive_login to zero.
  121. *
  122. * So far only bridges from Oxford Semiconductor are known to support
  123. * concurrent logins. Depending on firmware, four or two concurrent logins
  124. * are possible on OXFW911 and newer Oxsemi bridges.
  125. */
  126. static int exclusive_login = 1;
  127. module_param(exclusive_login, int, 0644);
  128. MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
  129. /*
  130. * If any of the following workarounds is required for your device to work,
  131. * please submit the kernel messages logged by sbp2 to the linux1394-devel
  132. * mailing list.
  133. *
  134. * - 128kB max transfer
  135. * Limit transfer size. Necessary for some old bridges.
  136. *
  137. * - 36 byte inquiry
  138. * When scsi_mod probes the device, let the inquiry command look like that
  139. * from MS Windows.
  140. *
  141. * - skip mode page 8
  142. * Suppress sending of mode_sense for mode page 8 if the device pretends to
  143. * support the SCSI Primary Block commands instead of Reduced Block Commands.
  144. *
  145. * - fix capacity
  146. * Tell sd_mod to correct the last sector number reported by read_capacity.
  147. * Avoids access beyond actual disk limits on devices with an off-by-one bug.
  148. * Don't use this with devices which don't have this bug.
  149. *
  150. * - override internal blacklist
  151. * Instead of adding to the built-in blacklist, use only the workarounds
  152. * specified in the module load parameter.
  153. * Useful if a blacklist entry interfered with a non-broken device.
  154. */
  155. static int sbp2_default_workarounds;
  156. module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
  157. MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
  158. ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
  159. ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
  160. ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
  161. ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
  162. ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
  163. ", or a combination)");
  164. /*
  165. * Export information about protocols/devices supported by this driver.
  166. */
  167. static struct ieee1394_device_id sbp2_id_table[] = {
  168. {
  169. .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  170. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
  171. .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
  172. {}
  173. };
  174. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  175. /*
  176. * Debug levels, configured via kernel config, or enable here.
  177. */
  178. #define CONFIG_IEEE1394_SBP2_DEBUG 0
  179. /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
  180. /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
  181. /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
  182. /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
  183. /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
  184. #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
  185. #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
  186. static u32 global_outstanding_command_orbs = 0;
  187. #define outstanding_orb_incr global_outstanding_command_orbs++
  188. #define outstanding_orb_decr global_outstanding_command_orbs--
  189. #else
  190. #define SBP2_ORB_DEBUG(fmt, args...) do {} while (0)
  191. #define outstanding_orb_incr do {} while (0)
  192. #define outstanding_orb_decr do {} while (0)
  193. #endif
  194. #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
  195. #define SBP2_DMA_ALLOC(fmt, args...) \
  196. HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
  197. ++global_outstanding_dmas, ## args)
  198. #define SBP2_DMA_FREE(fmt, args...) \
  199. HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
  200. --global_outstanding_dmas, ## args)
  201. static u32 global_outstanding_dmas = 0;
  202. #else
  203. #define SBP2_DMA_ALLOC(fmt, args...) do {} while (0)
  204. #define SBP2_DMA_FREE(fmt, args...) do {} while (0)
  205. #endif
  206. #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
  207. #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  208. #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  209. #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  210. #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  211. #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
  212. #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
  213. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  214. #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
  215. #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
  216. #else
  217. #define SBP2_DEBUG(fmt, args...) do {} while (0)
  218. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  219. #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
  220. #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
  221. #endif
  222. #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  223. #define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
  224. /*
  225. * Globals
  226. */
  227. static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
  228. u32 status);
  229. static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
  230. u32 scsi_status, struct scsi_cmnd *SCpnt,
  231. void (*done)(struct scsi_cmnd *));
  232. static struct scsi_host_template scsi_driver_template;
  233. static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
  234. static void sbp2_host_reset(struct hpsb_host *host);
  235. static int sbp2_probe(struct device *dev);
  236. static int sbp2_remove(struct device *dev);
  237. static int sbp2_update(struct unit_directory *ud);
  238. static struct hpsb_highlevel sbp2_highlevel = {
  239. .name = SBP2_DEVICE_NAME,
  240. .host_reset = sbp2_host_reset,
  241. };
  242. static struct hpsb_address_ops sbp2_ops = {
  243. .write = sbp2_handle_status_write
  244. };
  245. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  246. static struct hpsb_address_ops sbp2_physdma_ops = {
  247. .read = sbp2_handle_physdma_read,
  248. .write = sbp2_handle_physdma_write,
  249. };
  250. #endif
  251. static struct hpsb_protocol_driver sbp2_driver = {
  252. .name = "SBP2 Driver",
  253. .id_table = sbp2_id_table,
  254. .update = sbp2_update,
  255. .driver = {
  256. .name = SBP2_DEVICE_NAME,
  257. .bus = &ieee1394_bus_type,
  258. .probe = sbp2_probe,
  259. .remove = sbp2_remove,
  260. },
  261. };
  262. /*
  263. * List of devices with known bugs.
  264. *
  265. * The firmware_revision field, masked with 0xffff00, is the best indicator
  266. * for the type of bridge chip of a device. It yields a few false positives
  267. * but this did not break correctly behaving devices so far.
  268. */
  269. static const struct {
  270. u32 firmware_revision;
  271. u32 model_id;
  272. unsigned workarounds;
  273. } sbp2_workarounds_table[] = {
  274. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  275. .firmware_revision = 0x002800,
  276. .model_id = 0x001010,
  277. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  278. SBP2_WORKAROUND_MODE_SENSE_8,
  279. },
  280. /* Initio bridges, actually only needed for some older ones */ {
  281. .firmware_revision = 0x000200,
  282. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  283. },
  284. /* Symbios bridge */ {
  285. .firmware_revision = 0xa0b800,
  286. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  287. },
  288. /*
  289. * Note about the following Apple iPod blacklist entries:
  290. *
  291. * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
  292. * matching logic treats 0 as a wildcard, we cannot match this ID
  293. * without rewriting the matching routine. Fortunately these iPods
  294. * do not feature the read_capacity bug according to one report.
  295. * Read_capacity behaviour as well as model_id could change due to
  296. * Apple-supplied firmware updates though.
  297. */
  298. /* iPod 4th generation */ {
  299. .firmware_revision = 0x0a2700,
  300. .model_id = 0x000021,
  301. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  302. },
  303. /* iPod mini */ {
  304. .firmware_revision = 0x0a2700,
  305. .model_id = 0x000023,
  306. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  307. },
  308. /* iPod Photo */ {
  309. .firmware_revision = 0x0a2700,
  310. .model_id = 0x00007e,
  311. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  312. }
  313. };
  314. /**************************************
  315. * General utility functions
  316. **************************************/
  317. #ifndef __BIG_ENDIAN
  318. /*
  319. * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
  320. */
  321. static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
  322. {
  323. u32 *temp = buffer;
  324. for (length = (length >> 2); length--; )
  325. temp[length] = be32_to_cpu(temp[length]);
  326. return;
  327. }
  328. /*
  329. * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
  330. */
  331. static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
  332. {
  333. u32 *temp = buffer;
  334. for (length = (length >> 2); length--; )
  335. temp[length] = cpu_to_be32(temp[length]);
  336. return;
  337. }
  338. #else /* BIG_ENDIAN */
  339. /* Why waste the cpu cycles? */
  340. #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
  341. #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
  342. #endif
  343. #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
  344. /*
  345. * Debug packet dump routine. Length is in bytes.
  346. */
  347. static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
  348. u32 dump_phys_addr)
  349. {
  350. int i;
  351. unsigned char *dump = buffer;
  352. if (!dump || !length || !dump_name)
  353. return;
  354. if (dump_phys_addr)
  355. printk("[%s, 0x%x]", dump_name, dump_phys_addr);
  356. else
  357. printk("[%s]", dump_name);
  358. for (i = 0; i < length; i++) {
  359. if (i > 0x3f) {
  360. printk("\n ...");
  361. break;
  362. }
  363. if ((i & 0x3) == 0)
  364. printk(" ");
  365. if ((i & 0xf) == 0)
  366. printk("\n ");
  367. printk("%02x ", (int)dump[i]);
  368. }
  369. printk("\n");
  370. return;
  371. }
  372. #else
  373. #define sbp2util_packet_dump(w,x,y,z) do {} while (0)
  374. #endif
  375. static DECLARE_WAIT_QUEUE_HEAD(access_wq);
  376. /*
  377. * Waits for completion of an SBP-2 access request.
  378. * Returns nonzero if timed out or prematurely interrupted.
  379. */
  380. static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
  381. int timeout)
  382. {
  383. long leftover = wait_event_interruptible_timeout(
  384. access_wq, scsi_id->access_complete, timeout);
  385. scsi_id->access_complete = 0;
  386. return leftover <= 0;
  387. }
  388. /* Frees an allocated packet */
  389. static void sbp2_free_packet(struct hpsb_packet *packet)
  390. {
  391. hpsb_free_tlabel(packet);
  392. hpsb_free_packet(packet);
  393. }
  394. /* This is much like hpsb_node_write(), except it ignores the response
  395. * subaction and returns immediately. Can be used from interrupts.
  396. */
  397. static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
  398. quadlet_t *buffer, size_t length)
  399. {
  400. struct hpsb_packet *packet;
  401. packet = hpsb_make_writepacket(ne->host, ne->nodeid,
  402. addr, buffer, length);
  403. if (!packet)
  404. return -ENOMEM;
  405. hpsb_set_packet_complete_task(packet,
  406. (void (*)(void *))sbp2_free_packet,
  407. packet);
  408. hpsb_node_fill_packet(ne, packet);
  409. if (hpsb_send_packet(packet) < 0) {
  410. sbp2_free_packet(packet);
  411. return -EIO;
  412. }
  413. return 0;
  414. }
  415. static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
  416. u64 offset, quadlet_t *data, size_t len)
  417. {
  418. /*
  419. * There is a small window after a bus reset within which the node
  420. * entry's generation is current but the reconnect wasn't completed.
  421. */
  422. if (atomic_read(&scsi_id->unfinished_reset))
  423. return;
  424. if (hpsb_node_write(scsi_id->ne,
  425. scsi_id->sbp2_command_block_agent_addr + offset,
  426. data, len))
  427. SBP2_ERR("sbp2util_notify_fetch_agent failed.");
  428. /*
  429. * Now accept new SCSI commands, unless a bus reset happended during
  430. * hpsb_node_write.
  431. */
  432. if (!atomic_read(&scsi_id->unfinished_reset))
  433. scsi_unblock_requests(scsi_id->scsi_host);
  434. }
  435. static void sbp2util_write_orb_pointer(void *p)
  436. {
  437. quadlet_t data[2];
  438. data[0] = ORB_SET_NODE_ID(
  439. ((struct scsi_id_instance_data *)p)->hi->host->node_id);
  440. data[1] = ((struct scsi_id_instance_data *)p)->last_orb_dma;
  441. sbp2util_cpu_to_be32_buffer(data, 8);
  442. sbp2util_notify_fetch_agent(p, SBP2_ORB_POINTER_OFFSET, data, 8);
  443. }
  444. static void sbp2util_write_doorbell(void *p)
  445. {
  446. sbp2util_notify_fetch_agent(p, SBP2_DOORBELL_OFFSET, NULL, 4);
  447. }
  448. /*
  449. * This function is called to create a pool of command orbs used for
  450. * command processing. It is called when a new sbp2 device is detected.
  451. */
  452. static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
  453. {
  454. struct sbp2scsi_host_info *hi = scsi_id->hi;
  455. int i;
  456. unsigned long flags, orbs;
  457. struct sbp2_command_info *command;
  458. orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
  459. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  460. for (i = 0; i < orbs; i++) {
  461. command = kzalloc(sizeof(*command), GFP_ATOMIC);
  462. if (!command) {
  463. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
  464. flags);
  465. return -ENOMEM;
  466. }
  467. command->command_orb_dma =
  468. pci_map_single(hi->host->pdev, &command->command_orb,
  469. sizeof(struct sbp2_command_orb),
  470. PCI_DMA_TODEVICE);
  471. SBP2_DMA_ALLOC("single command orb DMA");
  472. command->sge_dma =
  473. pci_map_single(hi->host->pdev,
  474. &command->scatter_gather_element,
  475. sizeof(command->scatter_gather_element),
  476. PCI_DMA_BIDIRECTIONAL);
  477. SBP2_DMA_ALLOC("scatter_gather_element");
  478. INIT_LIST_HEAD(&command->list);
  479. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
  480. }
  481. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  482. return 0;
  483. }
  484. /*
  485. * This function is called to delete a pool of command orbs.
  486. */
  487. static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
  488. {
  489. struct hpsb_host *host = scsi_id->hi->host;
  490. struct list_head *lh, *next;
  491. struct sbp2_command_info *command;
  492. unsigned long flags;
  493. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  494. if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
  495. list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
  496. command = list_entry(lh, struct sbp2_command_info, list);
  497. /* Release our generic DMA's */
  498. pci_unmap_single(host->pdev, command->command_orb_dma,
  499. sizeof(struct sbp2_command_orb),
  500. PCI_DMA_TODEVICE);
  501. SBP2_DMA_FREE("single command orb DMA");
  502. pci_unmap_single(host->pdev, command->sge_dma,
  503. sizeof(command->scatter_gather_element),
  504. PCI_DMA_BIDIRECTIONAL);
  505. SBP2_DMA_FREE("scatter_gather_element");
  506. kfree(command);
  507. }
  508. }
  509. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  510. return;
  511. }
  512. /*
  513. * This function finds the sbp2_command for a given outstanding command
  514. * orb.Only looks at the inuse list.
  515. */
  516. static struct sbp2_command_info *sbp2util_find_command_for_orb(
  517. struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
  518. {
  519. struct sbp2_command_info *command;
  520. unsigned long flags;
  521. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  522. if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
  523. list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
  524. if (command->command_orb_dma == orb) {
  525. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  526. return command;
  527. }
  528. }
  529. }
  530. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  531. SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
  532. return NULL;
  533. }
  534. /*
  535. * This function finds the sbp2_command for a given outstanding SCpnt.
  536. * Only looks at the inuse list.
  537. * Must be called with scsi_id->sbp2_command_orb_lock held.
  538. */
  539. static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
  540. struct scsi_id_instance_data *scsi_id, void *SCpnt)
  541. {
  542. struct sbp2_command_info *command;
  543. if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
  544. list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
  545. if (command->Current_SCpnt == SCpnt)
  546. return command;
  547. return NULL;
  548. }
  549. /*
  550. * This function allocates a command orb used to send a scsi command.
  551. */
  552. static struct sbp2_command_info *sbp2util_allocate_command_orb(
  553. struct scsi_id_instance_data *scsi_id,
  554. struct scsi_cmnd *Current_SCpnt,
  555. void (*Current_done)(struct scsi_cmnd *))
  556. {
  557. struct list_head *lh;
  558. struct sbp2_command_info *command = NULL;
  559. unsigned long flags;
  560. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  561. if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
  562. lh = scsi_id->sbp2_command_orb_completed.next;
  563. list_del(lh);
  564. command = list_entry(lh, struct sbp2_command_info, list);
  565. command->Current_done = Current_done;
  566. command->Current_SCpnt = Current_SCpnt;
  567. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
  568. } else {
  569. SBP2_ERR("%s: no orbs available", __FUNCTION__);
  570. }
  571. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  572. return command;
  573. }
  574. /* Free our DMA's */
  575. static void sbp2util_free_command_dma(struct sbp2_command_info *command)
  576. {
  577. struct scsi_id_instance_data *scsi_id =
  578. (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
  579. struct hpsb_host *host;
  580. if (!scsi_id) {
  581. SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
  582. return;
  583. }
  584. host = scsi_id->ud->ne->host;
  585. if (command->cmd_dma) {
  586. if (command->dma_type == CMD_DMA_SINGLE) {
  587. pci_unmap_single(host->pdev, command->cmd_dma,
  588. command->dma_size, command->dma_dir);
  589. SBP2_DMA_FREE("single bulk");
  590. } else if (command->dma_type == CMD_DMA_PAGE) {
  591. pci_unmap_page(host->pdev, command->cmd_dma,
  592. command->dma_size, command->dma_dir);
  593. SBP2_DMA_FREE("single page");
  594. } /* XXX: Check for CMD_DMA_NONE bug */
  595. command->dma_type = CMD_DMA_NONE;
  596. command->cmd_dma = 0;
  597. }
  598. if (command->sge_buffer) {
  599. pci_unmap_sg(host->pdev, command->sge_buffer,
  600. command->dma_size, command->dma_dir);
  601. SBP2_DMA_FREE("scatter list");
  602. command->sge_buffer = NULL;
  603. }
  604. }
  605. /*
  606. * This function moves a command to the completed orb list.
  607. * Must be called with scsi_id->sbp2_command_orb_lock held.
  608. */
  609. static void sbp2util_mark_command_completed(
  610. struct scsi_id_instance_data *scsi_id,
  611. struct sbp2_command_info *command)
  612. {
  613. list_del(&command->list);
  614. sbp2util_free_command_dma(command);
  615. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
  616. }
  617. /*
  618. * Is scsi_id valid? Is the 1394 node still present?
  619. */
  620. static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
  621. {
  622. return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
  623. }
  624. /*********************************************
  625. * IEEE-1394 core driver stack related section
  626. *********************************************/
  627. static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
  628. static int sbp2_probe(struct device *dev)
  629. {
  630. struct unit_directory *ud;
  631. struct scsi_id_instance_data *scsi_id;
  632. SBP2_DEBUG_ENTER();
  633. ud = container_of(dev, struct unit_directory, device);
  634. /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
  635. * instead. */
  636. if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
  637. return -ENODEV;
  638. scsi_id = sbp2_alloc_device(ud);
  639. if (!scsi_id)
  640. return -ENOMEM;
  641. sbp2_parse_unit_directory(scsi_id, ud);
  642. return sbp2_start_device(scsi_id);
  643. }
  644. static int sbp2_remove(struct device *dev)
  645. {
  646. struct unit_directory *ud;
  647. struct scsi_id_instance_data *scsi_id;
  648. struct scsi_device *sdev;
  649. SBP2_DEBUG_ENTER();
  650. ud = container_of(dev, struct unit_directory, device);
  651. scsi_id = ud->device.driver_data;
  652. if (!scsi_id)
  653. return 0;
  654. if (scsi_id->scsi_host) {
  655. /* Get rid of enqueued commands if there is no chance to
  656. * send them. */
  657. if (!sbp2util_node_is_available(scsi_id))
  658. sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
  659. /* scsi_remove_device() will trigger shutdown functions of SCSI
  660. * highlevel drivers which would deadlock if blocked. */
  661. atomic_set(&scsi_id->unfinished_reset, 0);
  662. scsi_unblock_requests(scsi_id->scsi_host);
  663. }
  664. sdev = scsi_id->sdev;
  665. if (sdev) {
  666. scsi_id->sdev = NULL;
  667. scsi_remove_device(sdev);
  668. }
  669. sbp2_logout_device(scsi_id);
  670. sbp2_remove_device(scsi_id);
  671. return 0;
  672. }
  673. static int sbp2_update(struct unit_directory *ud)
  674. {
  675. struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
  676. SBP2_DEBUG_ENTER();
  677. if (sbp2_reconnect_device(scsi_id)) {
  678. /*
  679. * Ok, reconnect has failed. Perhaps we didn't
  680. * reconnect fast enough. Try doing a regular login, but
  681. * first do a logout just in case of any weirdness.
  682. */
  683. sbp2_logout_device(scsi_id);
  684. if (sbp2_login_device(scsi_id)) {
  685. /* Login failed too, just fail, and the backend
  686. * will call our sbp2_remove for us */
  687. SBP2_ERR("Failed to reconnect to sbp2 device!");
  688. return -EBUSY;
  689. }
  690. }
  691. /* Set max retries to something large on the device. */
  692. sbp2_set_busy_timeout(scsi_id);
  693. /* Do a SBP-2 fetch agent reset. */
  694. sbp2_agent_reset(scsi_id, 1);
  695. /* Get the max speed and packet size that we can use. */
  696. sbp2_max_speed_and_size(scsi_id);
  697. /* Complete any pending commands with busy (so they get
  698. * retried) and remove them from our queue
  699. */
  700. sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
  701. /* Make sure we unblock requests (since this is likely after a bus
  702. * reset). */
  703. atomic_set(&scsi_id->unfinished_reset, 0);
  704. scsi_unblock_requests(scsi_id->scsi_host);
  705. return 0;
  706. }
  707. /* This functions is called by the sbp2_probe, for each new device. We now
  708. * allocate one scsi host for each scsi_id (unit directory). */
  709. static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
  710. {
  711. struct sbp2scsi_host_info *hi;
  712. struct Scsi_Host *scsi_host = NULL;
  713. struct scsi_id_instance_data *scsi_id = NULL;
  714. SBP2_DEBUG_ENTER();
  715. scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
  716. if (!scsi_id) {
  717. SBP2_ERR("failed to create scsi_id");
  718. goto failed_alloc;
  719. }
  720. scsi_id->ne = ud->ne;
  721. scsi_id->ud = ud;
  722. scsi_id->speed_code = IEEE1394_SPEED_100;
  723. scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
  724. scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
  725. INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
  726. INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
  727. INIT_LIST_HEAD(&scsi_id->scsi_list);
  728. spin_lock_init(&scsi_id->sbp2_command_orb_lock);
  729. atomic_set(&scsi_id->unfinished_reset, 0);
  730. INIT_WORK(&scsi_id->protocol_work, NULL, NULL);
  731. ud->device.driver_data = scsi_id;
  732. hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
  733. if (!hi) {
  734. hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
  735. if (!hi) {
  736. SBP2_ERR("failed to allocate hostinfo");
  737. goto failed_alloc;
  738. }
  739. SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
  740. hi->host = ud->ne->host;
  741. INIT_LIST_HEAD(&hi->scsi_ids);
  742. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  743. /* Handle data movement if physical dma is not
  744. * enabled or not supported on host controller */
  745. if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
  746. &sbp2_physdma_ops,
  747. 0x0ULL, 0xfffffffcULL)) {
  748. SBP2_ERR("failed to register lower 4GB address range");
  749. goto failed_alloc;
  750. }
  751. #endif
  752. }
  753. /* Prevent unloading of the 1394 host */
  754. if (!try_module_get(hi->host->driver->owner)) {
  755. SBP2_ERR("failed to get a reference on 1394 host driver");
  756. goto failed_alloc;
  757. }
  758. scsi_id->hi = hi;
  759. list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
  760. /* Register the status FIFO address range. We could use the same FIFO
  761. * for targets at different nodes. However we need different FIFOs per
  762. * target in order to support multi-unit devices.
  763. * The FIFO is located out of the local host controller's physical range
  764. * but, if possible, within the posted write area. Status writes will
  765. * then be performed as unified transactions. This slightly reduces
  766. * bandwidth usage, and some Prolific based devices seem to require it.
  767. */
  768. scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  769. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  770. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  771. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  772. if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  773. SBP2_ERR("failed to allocate status FIFO address range");
  774. goto failed_alloc;
  775. }
  776. /* Register our host with the SCSI stack. */
  777. scsi_host = scsi_host_alloc(&scsi_driver_template,
  778. sizeof(unsigned long));
  779. if (!scsi_host) {
  780. SBP2_ERR("failed to register scsi host");
  781. goto failed_alloc;
  782. }
  783. scsi_host->hostdata[0] = (unsigned long)scsi_id;
  784. if (!scsi_add_host(scsi_host, &ud->device)) {
  785. scsi_id->scsi_host = scsi_host;
  786. return scsi_id;
  787. }
  788. SBP2_ERR("failed to add scsi host");
  789. scsi_host_put(scsi_host);
  790. failed_alloc:
  791. sbp2_remove_device(scsi_id);
  792. return NULL;
  793. }
  794. static void sbp2_host_reset(struct hpsb_host *host)
  795. {
  796. struct sbp2scsi_host_info *hi;
  797. struct scsi_id_instance_data *scsi_id;
  798. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  799. if (hi) {
  800. list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list) {
  801. atomic_set(&scsi_id->unfinished_reset, 1);
  802. scsi_block_requests(scsi_id->scsi_host);
  803. }
  804. }
  805. }
  806. /*
  807. * This function is where we first pull the node unique ids, and then
  808. * allocate memory and register a SBP-2 device.
  809. */
  810. static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
  811. {
  812. struct sbp2scsi_host_info *hi = scsi_id->hi;
  813. int error;
  814. SBP2_DEBUG_ENTER();
  815. /* Login FIFO DMA */
  816. scsi_id->login_response =
  817. pci_alloc_consistent(hi->host->pdev,
  818. sizeof(struct sbp2_login_response),
  819. &scsi_id->login_response_dma);
  820. if (!scsi_id->login_response)
  821. goto alloc_fail;
  822. SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
  823. /* Query logins ORB DMA */
  824. scsi_id->query_logins_orb =
  825. pci_alloc_consistent(hi->host->pdev,
  826. sizeof(struct sbp2_query_logins_orb),
  827. &scsi_id->query_logins_orb_dma);
  828. if (!scsi_id->query_logins_orb)
  829. goto alloc_fail;
  830. SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
  831. /* Query logins response DMA */
  832. scsi_id->query_logins_response =
  833. pci_alloc_consistent(hi->host->pdev,
  834. sizeof(struct sbp2_query_logins_response),
  835. &scsi_id->query_logins_response_dma);
  836. if (!scsi_id->query_logins_response)
  837. goto alloc_fail;
  838. SBP2_DMA_ALLOC("consistent DMA region for query logins response");
  839. /* Reconnect ORB DMA */
  840. scsi_id->reconnect_orb =
  841. pci_alloc_consistent(hi->host->pdev,
  842. sizeof(struct sbp2_reconnect_orb),
  843. &scsi_id->reconnect_orb_dma);
  844. if (!scsi_id->reconnect_orb)
  845. goto alloc_fail;
  846. SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
  847. /* Logout ORB DMA */
  848. scsi_id->logout_orb =
  849. pci_alloc_consistent(hi->host->pdev,
  850. sizeof(struct sbp2_logout_orb),
  851. &scsi_id->logout_orb_dma);
  852. if (!scsi_id->logout_orb)
  853. goto alloc_fail;
  854. SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
  855. /* Login ORB DMA */
  856. scsi_id->login_orb =
  857. pci_alloc_consistent(hi->host->pdev,
  858. sizeof(struct sbp2_login_orb),
  859. &scsi_id->login_orb_dma);
  860. if (!scsi_id->login_orb)
  861. goto alloc_fail;
  862. SBP2_DMA_ALLOC("consistent DMA region for login ORB");
  863. SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
  864. /*
  865. * Create our command orb pool
  866. */
  867. if (sbp2util_create_command_orb_pool(scsi_id)) {
  868. SBP2_ERR("sbp2util_create_command_orb_pool failed!");
  869. sbp2_remove_device(scsi_id);
  870. return -ENOMEM;
  871. }
  872. /* Schedule a timeout here. The reason is that we may be so close
  873. * to a bus reset, that the device is not available for logins.
  874. * This can happen when the bus reset is caused by the host
  875. * connected to the sbp2 device being removed. That host would
  876. * have a certain amount of time to relogin before the sbp2 device
  877. * allows someone else to login instead. One second makes sense. */
  878. msleep_interruptible(1000);
  879. if (signal_pending(current)) {
  880. sbp2_remove_device(scsi_id);
  881. return -EINTR;
  882. }
  883. /*
  884. * Login to the sbp-2 device
  885. */
  886. if (sbp2_login_device(scsi_id)) {
  887. /* Login failed, just remove the device. */
  888. sbp2_remove_device(scsi_id);
  889. return -EBUSY;
  890. }
  891. /*
  892. * Set max retries to something large on the device
  893. */
  894. sbp2_set_busy_timeout(scsi_id);
  895. /*
  896. * Do a SBP-2 fetch agent reset
  897. */
  898. sbp2_agent_reset(scsi_id, 1);
  899. /*
  900. * Get the max speed and packet size that we can use
  901. */
  902. sbp2_max_speed_and_size(scsi_id);
  903. /* Add this device to the scsi layer now */
  904. error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
  905. if (error) {
  906. SBP2_ERR("scsi_add_device failed");
  907. sbp2_logout_device(scsi_id);
  908. sbp2_remove_device(scsi_id);
  909. return error;
  910. }
  911. return 0;
  912. alloc_fail:
  913. SBP2_ERR("Could not allocate memory for scsi_id");
  914. sbp2_remove_device(scsi_id);
  915. return -ENOMEM;
  916. }
  917. /*
  918. * This function removes an sbp2 device from the sbp2scsi_host_info struct.
  919. */
  920. static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
  921. {
  922. struct sbp2scsi_host_info *hi;
  923. SBP2_DEBUG_ENTER();
  924. if (!scsi_id)
  925. return;
  926. hi = scsi_id->hi;
  927. /* This will remove our scsi device aswell */
  928. if (scsi_id->scsi_host) {
  929. scsi_remove_host(scsi_id->scsi_host);
  930. scsi_host_put(scsi_id->scsi_host);
  931. }
  932. flush_scheduled_work();
  933. sbp2util_remove_command_orb_pool(scsi_id);
  934. list_del(&scsi_id->scsi_list);
  935. if (scsi_id->login_response) {
  936. pci_free_consistent(hi->host->pdev,
  937. sizeof(struct sbp2_login_response),
  938. scsi_id->login_response,
  939. scsi_id->login_response_dma);
  940. SBP2_DMA_FREE("single login FIFO");
  941. }
  942. if (scsi_id->login_orb) {
  943. pci_free_consistent(hi->host->pdev,
  944. sizeof(struct sbp2_login_orb),
  945. scsi_id->login_orb,
  946. scsi_id->login_orb_dma);
  947. SBP2_DMA_FREE("single login ORB");
  948. }
  949. if (scsi_id->reconnect_orb) {
  950. pci_free_consistent(hi->host->pdev,
  951. sizeof(struct sbp2_reconnect_orb),
  952. scsi_id->reconnect_orb,
  953. scsi_id->reconnect_orb_dma);
  954. SBP2_DMA_FREE("single reconnect orb");
  955. }
  956. if (scsi_id->logout_orb) {
  957. pci_free_consistent(hi->host->pdev,
  958. sizeof(struct sbp2_logout_orb),
  959. scsi_id->logout_orb,
  960. scsi_id->logout_orb_dma);
  961. SBP2_DMA_FREE("single logout orb");
  962. }
  963. if (scsi_id->query_logins_orb) {
  964. pci_free_consistent(hi->host->pdev,
  965. sizeof(struct sbp2_query_logins_orb),
  966. scsi_id->query_logins_orb,
  967. scsi_id->query_logins_orb_dma);
  968. SBP2_DMA_FREE("single query logins orb");
  969. }
  970. if (scsi_id->query_logins_response) {
  971. pci_free_consistent(hi->host->pdev,
  972. sizeof(struct sbp2_query_logins_response),
  973. scsi_id->query_logins_response,
  974. scsi_id->query_logins_response_dma);
  975. SBP2_DMA_FREE("single query logins data");
  976. }
  977. if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  978. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  979. scsi_id->status_fifo_addr);
  980. scsi_id->ud->device.driver_data = NULL;
  981. if (hi)
  982. module_put(hi->host->driver->owner);
  983. SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
  984. kfree(scsi_id);
  985. }
  986. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  987. /*
  988. * This function deals with physical dma write requests (for adapters that do not support
  989. * physical dma in hardware). Mostly just here for debugging...
  990. */
  991. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  992. int destid, quadlet_t *data, u64 addr,
  993. size_t length, u16 flags)
  994. {
  995. /*
  996. * Manually put the data in the right place.
  997. */
  998. memcpy(bus_to_virt((u32) addr), data, length);
  999. sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
  1000. (u32) addr);
  1001. return RCODE_COMPLETE;
  1002. }
  1003. /*
  1004. * This function deals with physical dma read requests (for adapters that do not support
  1005. * physical dma in hardware). Mostly just here for debugging...
  1006. */
  1007. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  1008. quadlet_t *data, u64 addr, size_t length,
  1009. u16 flags)
  1010. {
  1011. /*
  1012. * Grab data from memory and send a read response.
  1013. */
  1014. memcpy(data, bus_to_virt((u32) addr), length);
  1015. sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
  1016. (u32) addr);
  1017. return RCODE_COMPLETE;
  1018. }
  1019. #endif
  1020. /**************************************
  1021. * SBP-2 protocol related section
  1022. **************************************/
  1023. /*
  1024. * This function queries the device for the maximum concurrent logins it
  1025. * supports.
  1026. */
  1027. static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
  1028. {
  1029. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1030. quadlet_t data[2];
  1031. int max_logins;
  1032. int active_logins;
  1033. SBP2_DEBUG_ENTER();
  1034. scsi_id->query_logins_orb->reserved1 = 0x0;
  1035. scsi_id->query_logins_orb->reserved2 = 0x0;
  1036. scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
  1037. scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1038. scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  1039. scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  1040. scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
  1041. scsi_id->query_logins_orb->reserved_resp_length =
  1042. ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
  1043. scsi_id->query_logins_orb->status_fifo_hi =
  1044. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1045. scsi_id->query_logins_orb->status_fifo_lo =
  1046. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1047. sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
  1048. sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
  1049. "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
  1050. memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
  1051. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1052. data[1] = scsi_id->query_logins_orb_dma;
  1053. sbp2util_cpu_to_be32_buffer(data, 8);
  1054. hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
  1055. if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
  1056. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  1057. return -EIO;
  1058. }
  1059. if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
  1060. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  1061. return -EIO;
  1062. }
  1063. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1064. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  1065. return -EIO;
  1066. }
  1067. sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
  1068. SBP2_DEBUG("length_max_logins = %x",
  1069. (unsigned int)scsi_id->query_logins_response->length_max_logins);
  1070. max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
  1071. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  1072. active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
  1073. SBP2_INFO("Number of active logins: %d", active_logins);
  1074. if (active_logins >= max_logins) {
  1075. return -EIO;
  1076. }
  1077. return 0;
  1078. }
  1079. /*
  1080. * This function is called in order to login to a particular SBP-2 device,
  1081. * after a bus reset.
  1082. */
  1083. static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
  1084. {
  1085. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1086. quadlet_t data[2];
  1087. SBP2_DEBUG_ENTER();
  1088. if (!scsi_id->login_orb) {
  1089. SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
  1090. return -EIO;
  1091. }
  1092. if (!exclusive_login) {
  1093. if (sbp2_query_logins(scsi_id)) {
  1094. SBP2_INFO("Device does not support any more concurrent logins");
  1095. return -EIO;
  1096. }
  1097. }
  1098. /* Set-up login ORB, assume no password */
  1099. scsi_id->login_orb->password_hi = 0;
  1100. scsi_id->login_orb->password_lo = 0;
  1101. scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
  1102. scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1103. scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  1104. scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
  1105. scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
  1106. scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
  1107. scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
  1108. scsi_id->login_orb->passwd_resp_lengths =
  1109. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  1110. scsi_id->login_orb->status_fifo_hi =
  1111. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1112. scsi_id->login_orb->status_fifo_lo =
  1113. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1114. sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
  1115. sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
  1116. "sbp2 login orb", scsi_id->login_orb_dma);
  1117. memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
  1118. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1119. data[1] = scsi_id->login_orb_dma;
  1120. sbp2util_cpu_to_be32_buffer(data, 8);
  1121. hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
  1122. /*
  1123. * Wait for login status (up to 20 seconds)...
  1124. */
  1125. if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
  1126. SBP2_ERR("Error logging into SBP-2 device - timed out");
  1127. return -EIO;
  1128. }
  1129. /*
  1130. * Sanity. Make sure status returned matches login orb.
  1131. */
  1132. if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
  1133. SBP2_ERR("Error logging into SBP-2 device - timed out");
  1134. return -EIO;
  1135. }
  1136. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1137. SBP2_ERR("Error logging into SBP-2 device - failed");
  1138. return -EIO;
  1139. }
  1140. /*
  1141. * Byte swap the login response, for use when reconnecting or
  1142. * logging out.
  1143. */
  1144. sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
  1145. /*
  1146. * Grab our command block agent address from the login response.
  1147. */
  1148. SBP2_DEBUG("command_block_agent_hi = %x",
  1149. (unsigned int)scsi_id->login_response->command_block_agent_hi);
  1150. SBP2_DEBUG("command_block_agent_lo = %x",
  1151. (unsigned int)scsi_id->login_response->command_block_agent_lo);
  1152. scsi_id->sbp2_command_block_agent_addr =
  1153. ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
  1154. scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
  1155. scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
  1156. SBP2_INFO("Logged into SBP-2 device");
  1157. return 0;
  1158. }
  1159. /*
  1160. * This function is called in order to logout from a particular SBP-2
  1161. * device, usually called during driver unload.
  1162. */
  1163. static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
  1164. {
  1165. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1166. quadlet_t data[2];
  1167. int error;
  1168. SBP2_DEBUG_ENTER();
  1169. /*
  1170. * Set-up logout ORB
  1171. */
  1172. scsi_id->logout_orb->reserved1 = 0x0;
  1173. scsi_id->logout_orb->reserved2 = 0x0;
  1174. scsi_id->logout_orb->reserved3 = 0x0;
  1175. scsi_id->logout_orb->reserved4 = 0x0;
  1176. scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1177. scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
  1178. /* Notify us when complete */
  1179. scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1180. scsi_id->logout_orb->reserved5 = 0x0;
  1181. scsi_id->logout_orb->status_fifo_hi =
  1182. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1183. scsi_id->logout_orb->status_fifo_lo =
  1184. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1185. /*
  1186. * Byte swap ORB if necessary
  1187. */
  1188. sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
  1189. sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
  1190. "sbp2 logout orb", scsi_id->logout_orb_dma);
  1191. /*
  1192. * Ok, let's write to the target's management agent register
  1193. */
  1194. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1195. data[1] = scsi_id->logout_orb_dma;
  1196. sbp2util_cpu_to_be32_buffer(data, 8);
  1197. error = hpsb_node_write(scsi_id->ne,
  1198. scsi_id->sbp2_management_agent_addr, data, 8);
  1199. if (error)
  1200. return error;
  1201. /* Wait for device to logout...1 second. */
  1202. if (sbp2util_access_timeout(scsi_id, HZ))
  1203. return -EIO;
  1204. SBP2_INFO("Logged out of SBP-2 device");
  1205. return 0;
  1206. }
  1207. /*
  1208. * This function is called in order to reconnect to a particular SBP-2
  1209. * device, after a bus reset.
  1210. */
  1211. static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
  1212. {
  1213. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1214. quadlet_t data[2];
  1215. int error;
  1216. SBP2_DEBUG_ENTER();
  1217. /*
  1218. * Set-up reconnect ORB
  1219. */
  1220. scsi_id->reconnect_orb->reserved1 = 0x0;
  1221. scsi_id->reconnect_orb->reserved2 = 0x0;
  1222. scsi_id->reconnect_orb->reserved3 = 0x0;
  1223. scsi_id->reconnect_orb->reserved4 = 0x0;
  1224. scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1225. scsi_id->reconnect_orb->login_ID_misc |=
  1226. ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
  1227. /* Notify us when complete */
  1228. scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1229. scsi_id->reconnect_orb->reserved5 = 0x0;
  1230. scsi_id->reconnect_orb->status_fifo_hi =
  1231. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1232. scsi_id->reconnect_orb->status_fifo_lo =
  1233. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1234. /*
  1235. * Byte swap ORB if necessary
  1236. */
  1237. sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
  1238. sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
  1239. "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
  1240. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1241. data[1] = scsi_id->reconnect_orb_dma;
  1242. sbp2util_cpu_to_be32_buffer(data, 8);
  1243. error = hpsb_node_write(scsi_id->ne,
  1244. scsi_id->sbp2_management_agent_addr, data, 8);
  1245. if (error)
  1246. return error;
  1247. /*
  1248. * Wait for reconnect status (up to 1 second)...
  1249. */
  1250. if (sbp2util_access_timeout(scsi_id, HZ)) {
  1251. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1252. return -EIO;
  1253. }
  1254. /*
  1255. * Sanity. Make sure status returned matches reconnect orb.
  1256. */
  1257. if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
  1258. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1259. return -EIO;
  1260. }
  1261. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1262. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1263. return -EIO;
  1264. }
  1265. HPSB_DEBUG("Reconnected to SBP-2 device");
  1266. return 0;
  1267. }
  1268. /*
  1269. * This function is called in order to set the busy timeout (number of
  1270. * retries to attempt) on the sbp2 device.
  1271. */
  1272. static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
  1273. {
  1274. quadlet_t data;
  1275. SBP2_DEBUG_ENTER();
  1276. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1277. if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1278. SBP2_ERR("%s error", __FUNCTION__);
  1279. return 0;
  1280. }
  1281. /*
  1282. * This function is called to parse sbp2 device's config rom unit
  1283. * directory. Used to determine things like sbp2 management agent offset,
  1284. * and command set used (SCSI or RBC).
  1285. */
  1286. static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
  1287. struct unit_directory *ud)
  1288. {
  1289. struct csr1212_keyval *kv;
  1290. struct csr1212_dentry *dentry;
  1291. u64 management_agent_addr;
  1292. u32 command_set_spec_id, command_set, unit_characteristics,
  1293. firmware_revision;
  1294. unsigned workarounds;
  1295. int i;
  1296. SBP2_DEBUG_ENTER();
  1297. management_agent_addr = 0x0;
  1298. command_set_spec_id = 0x0;
  1299. command_set = 0x0;
  1300. unit_characteristics = 0x0;
  1301. firmware_revision = 0x0;
  1302. /* Handle different fields in the unit directory, based on keys */
  1303. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1304. switch (kv->key.id) {
  1305. case CSR1212_KV_ID_DEPENDENT_INFO:
  1306. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
  1307. /* Save off the management agent address */
  1308. management_agent_addr =
  1309. CSR1212_REGISTER_SPACE_BASE +
  1310. (kv->value.csr_offset << 2);
  1311. SBP2_DEBUG("sbp2_management_agent_addr = %x",
  1312. (unsigned int)management_agent_addr);
  1313. } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  1314. scsi_id->sbp2_lun =
  1315. ORB_SET_LUN(kv->value.immediate);
  1316. }
  1317. break;
  1318. case SBP2_COMMAND_SET_SPEC_ID_KEY:
  1319. /* Command spec organization */
  1320. command_set_spec_id = kv->value.immediate;
  1321. SBP2_DEBUG("sbp2_command_set_spec_id = %x",
  1322. (unsigned int)command_set_spec_id);
  1323. break;
  1324. case SBP2_COMMAND_SET_KEY:
  1325. /* Command set used by sbp2 device */
  1326. command_set = kv->value.immediate;
  1327. SBP2_DEBUG("sbp2_command_set = %x",
  1328. (unsigned int)command_set);
  1329. break;
  1330. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1331. /*
  1332. * Unit characterisitcs (orb related stuff
  1333. * that I'm not yet paying attention to)
  1334. */
  1335. unit_characteristics = kv->value.immediate;
  1336. SBP2_DEBUG("sbp2_unit_characteristics = %x",
  1337. (unsigned int)unit_characteristics);
  1338. break;
  1339. case SBP2_FIRMWARE_REVISION_KEY:
  1340. /* Firmware revision */
  1341. firmware_revision = kv->value.immediate;
  1342. SBP2_DEBUG("sbp2_firmware_revision = %x",
  1343. (unsigned int)firmware_revision);
  1344. break;
  1345. default:
  1346. break;
  1347. }
  1348. }
  1349. workarounds = sbp2_default_workarounds;
  1350. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1351. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1352. if (sbp2_workarounds_table[i].firmware_revision &&
  1353. sbp2_workarounds_table[i].firmware_revision !=
  1354. (firmware_revision & 0xffff00))
  1355. continue;
  1356. if (sbp2_workarounds_table[i].model_id &&
  1357. sbp2_workarounds_table[i].model_id != ud->model_id)
  1358. continue;
  1359. workarounds |= sbp2_workarounds_table[i].workarounds;
  1360. break;
  1361. }
  1362. if (workarounds)
  1363. SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
  1364. "(firmware_revision 0x%06x, vendor_id 0x%06x,"
  1365. " model_id 0x%06x)",
  1366. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1367. workarounds, firmware_revision,
  1368. ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
  1369. ud->model_id);
  1370. /* We would need one SCSI host template for each target to adjust
  1371. * max_sectors on the fly, therefore warn only. */
  1372. if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1373. (max_sectors * 512) > (128 * 1024))
  1374. SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
  1375. "max transfer size. WARNING: Current max_sectors "
  1376. "setting is larger than 128KB (%d sectors)",
  1377. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1378. max_sectors);
  1379. /* If this is a logical unit directory entry, process the parent
  1380. * to get the values. */
  1381. if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
  1382. struct unit_directory *parent_ud =
  1383. container_of(ud->device.parent, struct unit_directory, device);
  1384. sbp2_parse_unit_directory(scsi_id, parent_ud);
  1385. } else {
  1386. scsi_id->sbp2_management_agent_addr = management_agent_addr;
  1387. scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
  1388. scsi_id->sbp2_command_set = command_set;
  1389. scsi_id->sbp2_unit_characteristics = unit_characteristics;
  1390. scsi_id->sbp2_firmware_revision = firmware_revision;
  1391. scsi_id->workarounds = workarounds;
  1392. if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
  1393. scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
  1394. }
  1395. }
  1396. #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
  1397. /*
  1398. * This function is called in order to determine the max speed and packet
  1399. * size we can use in our ORBs. Note, that we (the driver and host) only
  1400. * initiate the transaction. The SBP-2 device actually transfers the data
  1401. * (by reading from the DMA area we tell it). This means that the SBP-2
  1402. * device decides the actual maximum data it can transfer. We just tell it
  1403. * the speed that it needs to use, and the max_rec the host supports, and
  1404. * it takes care of the rest.
  1405. */
  1406. static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
  1407. {
  1408. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1409. u8 payload;
  1410. SBP2_DEBUG_ENTER();
  1411. scsi_id->speed_code =
  1412. hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
  1413. /* Bump down our speed if the user requested it */
  1414. if (scsi_id->speed_code > max_speed) {
  1415. scsi_id->speed_code = max_speed;
  1416. SBP2_ERR("Forcing SBP-2 max speed down to %s",
  1417. hpsb_speedto_str[scsi_id->speed_code]);
  1418. }
  1419. /* Payload size is the lesser of what our speed supports and what
  1420. * our host supports. */
  1421. payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
  1422. (u8) (hi->host->csr.max_rec - 1));
  1423. /* If physical DMA is off, work around limitation in ohci1394:
  1424. * packet size must not exceed PAGE_SIZE */
  1425. if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
  1426. while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
  1427. payload)
  1428. payload--;
  1429. HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
  1430. NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
  1431. hpsb_speedto_str[scsi_id->speed_code],
  1432. SBP2_PAYLOAD_TO_BYTES(payload));
  1433. scsi_id->max_payload_size = payload;
  1434. return 0;
  1435. }
  1436. /*
  1437. * This function is called in order to perform a SBP-2 agent reset.
  1438. */
  1439. static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
  1440. {
  1441. quadlet_t data;
  1442. u64 addr;
  1443. int retval;
  1444. unsigned long flags;
  1445. SBP2_DEBUG_ENTER();
  1446. cancel_delayed_work(&scsi_id->protocol_work);
  1447. if (wait)
  1448. flush_scheduled_work();
  1449. data = ntohl(SBP2_AGENT_RESET_DATA);
  1450. addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
  1451. if (wait)
  1452. retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
  1453. else
  1454. retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
  1455. if (retval < 0) {
  1456. SBP2_ERR("hpsb_node_write failed.\n");
  1457. return -EIO;
  1458. }
  1459. /*
  1460. * Need to make sure orb pointer is written on next command
  1461. */
  1462. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1463. scsi_id->last_orb = NULL;
  1464. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1465. return 0;
  1466. }
  1467. static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
  1468. struct sbp2scsi_host_info *hi,
  1469. struct sbp2_command_info *command,
  1470. unsigned int scsi_use_sg,
  1471. struct scatterlist *sgpnt,
  1472. u32 orb_direction,
  1473. enum dma_data_direction dma_dir)
  1474. {
  1475. command->dma_dir = dma_dir;
  1476. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1477. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1478. /* Special case if only one element (and less than 64KB in size) */
  1479. if ((scsi_use_sg == 1) &&
  1480. (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
  1481. SBP2_DEBUG("Only one s/g element");
  1482. command->dma_size = sgpnt[0].length;
  1483. command->dma_type = CMD_DMA_PAGE;
  1484. command->cmd_dma = pci_map_page(hi->host->pdev,
  1485. sgpnt[0].page,
  1486. sgpnt[0].offset,
  1487. command->dma_size,
  1488. command->dma_dir);
  1489. SBP2_DMA_ALLOC("single page scatter element");
  1490. orb->data_descriptor_lo = command->cmd_dma;
  1491. orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
  1492. } else {
  1493. struct sbp2_unrestricted_page_table *sg_element =
  1494. &command->scatter_gather_element[0];
  1495. u32 sg_count, sg_len;
  1496. dma_addr_t sg_addr;
  1497. int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
  1498. dma_dir);
  1499. SBP2_DMA_ALLOC("scatter list");
  1500. command->dma_size = scsi_use_sg;
  1501. command->sge_buffer = sgpnt;
  1502. /* use page tables (s/g) */
  1503. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1504. orb->data_descriptor_lo = command->sge_dma;
  1505. /*
  1506. * Loop through and fill out our sbp-2 page tables
  1507. * (and split up anything too large)
  1508. */
  1509. for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
  1510. sg_len = sg_dma_len(sgpnt);
  1511. sg_addr = sg_dma_address(sgpnt);
  1512. while (sg_len) {
  1513. sg_element[sg_count].segment_base_lo = sg_addr;
  1514. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1515. sg_element[sg_count].length_segment_base_hi =
  1516. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1517. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1518. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1519. } else {
  1520. sg_element[sg_count].length_segment_base_hi =
  1521. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1522. sg_len = 0;
  1523. }
  1524. sg_count++;
  1525. }
  1526. }
  1527. /* Number of page table (s/g) elements */
  1528. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1529. sbp2util_packet_dump(sg_element,
  1530. (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
  1531. "sbp2 s/g list", command->sge_dma);
  1532. /* Byte swap page tables if necessary */
  1533. sbp2util_cpu_to_be32_buffer(sg_element,
  1534. (sizeof(struct sbp2_unrestricted_page_table)) *
  1535. sg_count);
  1536. }
  1537. }
  1538. static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
  1539. struct sbp2scsi_host_info *hi,
  1540. struct sbp2_command_info *command,
  1541. struct scatterlist *sgpnt,
  1542. u32 orb_direction,
  1543. unsigned int scsi_request_bufflen,
  1544. void *scsi_request_buffer,
  1545. enum dma_data_direction dma_dir)
  1546. {
  1547. command->dma_dir = dma_dir;
  1548. command->dma_size = scsi_request_bufflen;
  1549. command->dma_type = CMD_DMA_SINGLE;
  1550. command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
  1551. command->dma_size, command->dma_dir);
  1552. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1553. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1554. SBP2_DMA_ALLOC("single bulk");
  1555. /*
  1556. * Handle case where we get a command w/o s/g enabled (but
  1557. * check for transfers larger than 64K)
  1558. */
  1559. if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
  1560. orb->data_descriptor_lo = command->cmd_dma;
  1561. orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
  1562. } else {
  1563. struct sbp2_unrestricted_page_table *sg_element =
  1564. &command->scatter_gather_element[0];
  1565. u32 sg_count, sg_len;
  1566. dma_addr_t sg_addr;
  1567. /*
  1568. * Need to turn this into page tables, since the
  1569. * buffer is too large.
  1570. */
  1571. orb->data_descriptor_lo = command->sge_dma;
  1572. /* Use page tables (s/g) */
  1573. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1574. /*
  1575. * fill out our sbp-2 page tables (and split up
  1576. * the large buffer)
  1577. */
  1578. sg_count = 0;
  1579. sg_len = scsi_request_bufflen;
  1580. sg_addr = command->cmd_dma;
  1581. while (sg_len) {
  1582. sg_element[sg_count].segment_base_lo = sg_addr;
  1583. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1584. sg_element[sg_count].length_segment_base_hi =
  1585. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1586. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1587. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1588. } else {
  1589. sg_element[sg_count].length_segment_base_hi =
  1590. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1591. sg_len = 0;
  1592. }
  1593. sg_count++;
  1594. }
  1595. /* Number of page table (s/g) elements */
  1596. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1597. sbp2util_packet_dump(sg_element,
  1598. (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
  1599. "sbp2 s/g list", command->sge_dma);
  1600. /* Byte swap page tables if necessary */
  1601. sbp2util_cpu_to_be32_buffer(sg_element,
  1602. (sizeof(struct sbp2_unrestricted_page_table)) *
  1603. sg_count);
  1604. }
  1605. }
  1606. /*
  1607. * This function is called to create the actual command orb and s/g list
  1608. * out of the scsi command itself.
  1609. */
  1610. static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
  1611. struct sbp2_command_info *command,
  1612. unchar *scsi_cmd,
  1613. unsigned int scsi_use_sg,
  1614. unsigned int scsi_request_bufflen,
  1615. void *scsi_request_buffer,
  1616. enum dma_data_direction dma_dir)
  1617. {
  1618. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1619. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1620. struct sbp2_command_orb *command_orb = &command->command_orb;
  1621. u32 orb_direction;
  1622. /*
  1623. * Set-up our command ORB..
  1624. *
  1625. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1626. * best performance (at least with the devices I have). This means
  1627. * that data_size becomes the number of s/g elements, and
  1628. * page_size should be zero (for unrestricted).
  1629. */
  1630. command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1631. command_orb->next_ORB_lo = 0x0;
  1632. command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
  1633. command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
  1634. command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
  1635. if (dma_dir == DMA_NONE)
  1636. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1637. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1638. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1639. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1640. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1641. else {
  1642. SBP2_WARN("Falling back to DMA_NONE");
  1643. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1644. }
  1645. /* Set-up our pagetable stuff */
  1646. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1647. SBP2_DEBUG("No data transfer");
  1648. command_orb->data_descriptor_hi = 0x0;
  1649. command_orb->data_descriptor_lo = 0x0;
  1650. command_orb->misc |= ORB_SET_DIRECTION(1);
  1651. } else if (scsi_use_sg) {
  1652. SBP2_DEBUG("Use scatter/gather");
  1653. sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
  1654. sgpnt, orb_direction, dma_dir);
  1655. } else {
  1656. SBP2_DEBUG("No scatter/gather");
  1657. sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
  1658. orb_direction, scsi_request_bufflen,
  1659. scsi_request_buffer, dma_dir);
  1660. }
  1661. /* Byte swap command ORB if necessary */
  1662. sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
  1663. /* Put our scsi command in the command ORB */
  1664. memset(command_orb->cdb, 0, 12);
  1665. memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1666. }
  1667. /*
  1668. * This function is called in order to begin a regular SBP-2 command.
  1669. */
  1670. static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
  1671. struct sbp2_command_info *command)
  1672. {
  1673. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1674. struct sbp2_command_orb *command_orb = &command->command_orb;
  1675. struct sbp2_command_orb *last_orb;
  1676. dma_addr_t last_orb_dma;
  1677. u64 addr = scsi_id->sbp2_command_block_agent_addr;
  1678. quadlet_t data[2];
  1679. size_t length;
  1680. unsigned long flags;
  1681. outstanding_orb_incr;
  1682. SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
  1683. command_orb, global_outstanding_command_orbs);
  1684. pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
  1685. sizeof(struct sbp2_command_orb),
  1686. PCI_DMA_TODEVICE);
  1687. pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
  1688. sizeof(command->scatter_gather_element),
  1689. PCI_DMA_BIDIRECTIONAL);
  1690. /*
  1691. * Check to see if there are any previous orbs to use
  1692. */
  1693. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1694. last_orb = scsi_id->last_orb;
  1695. last_orb_dma = scsi_id->last_orb_dma;
  1696. if (!last_orb) {
  1697. /*
  1698. * last_orb == NULL means: We know that the target's fetch agent
  1699. * is not active right now.
  1700. */
  1701. addr += SBP2_ORB_POINTER_OFFSET;
  1702. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1703. data[1] = command->command_orb_dma;
  1704. sbp2util_cpu_to_be32_buffer(data, 8);
  1705. length = 8;
  1706. } else {
  1707. /*
  1708. * last_orb != NULL means: We know that the target's fetch agent
  1709. * is (very probably) not dead or in reset state right now.
  1710. * We have an ORB already sent that we can append a new one to.
  1711. * The target's fetch agent may or may not have read this
  1712. * previous ORB yet.
  1713. */
  1714. pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
  1715. sizeof(struct sbp2_command_orb),
  1716. PCI_DMA_TODEVICE);
  1717. last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
  1718. wmb();
  1719. /* Tells hardware that this pointer is valid */
  1720. last_orb->next_ORB_hi = 0;
  1721. pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
  1722. sizeof(struct sbp2_command_orb),
  1723. PCI_DMA_TODEVICE);
  1724. addr += SBP2_DOORBELL_OFFSET;
  1725. data[0] = 0;
  1726. length = 4;
  1727. }
  1728. scsi_id->last_orb = command_orb;
  1729. scsi_id->last_orb_dma = command->command_orb_dma;
  1730. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1731. SBP2_ORB_DEBUG("write to %s register, command orb %p",
  1732. last_orb ? "DOORBELL" : "ORB_POINTER", command_orb);
  1733. if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
  1734. /*
  1735. * sbp2util_node_write_no_wait failed. We certainly ran out
  1736. * of transaction labels, perhaps just because there were no
  1737. * context switches which gave khpsbpkt a chance to collect
  1738. * free tlabels. Try again in non-atomic context. If necessary,
  1739. * the workqueue job will sleep to guaranteedly get a tlabel.
  1740. * We do not accept new commands until the job is over.
  1741. */
  1742. scsi_block_requests(scsi_id->scsi_host);
  1743. PREPARE_WORK(&scsi_id->protocol_work,
  1744. last_orb ? sbp2util_write_doorbell:
  1745. sbp2util_write_orb_pointer,
  1746. scsi_id);
  1747. schedule_work(&scsi_id->protocol_work);
  1748. }
  1749. }
  1750. /*
  1751. * This function is called in order to begin a regular SBP-2 command.
  1752. */
  1753. static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
  1754. struct scsi_cmnd *SCpnt,
  1755. void (*done)(struct scsi_cmnd *))
  1756. {
  1757. unchar *cmd = (unchar *) SCpnt->cmnd;
  1758. unsigned int request_bufflen = SCpnt->request_bufflen;
  1759. struct sbp2_command_info *command;
  1760. SBP2_DEBUG_ENTER();
  1761. SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
  1762. SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
  1763. /*
  1764. * Allocate a command orb and s/g structure
  1765. */
  1766. command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
  1767. if (!command) {
  1768. return -EIO;
  1769. }
  1770. /*
  1771. * Now actually fill in the comamnd orb and sbp2 s/g list
  1772. */
  1773. sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
  1774. request_bufflen, SCpnt->request_buffer,
  1775. SCpnt->sc_data_direction);
  1776. sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
  1777. "sbp2 command orb", command->command_orb_dma);
  1778. /*
  1779. * Link up the orb, and ring the doorbell if needed
  1780. */
  1781. sbp2_link_orb_command(scsi_id, command);
  1782. return 0;
  1783. }
  1784. /*
  1785. * Translates SBP-2 status into SCSI sense data for check conditions
  1786. */
  1787. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
  1788. {
  1789. SBP2_DEBUG_ENTER();
  1790. /*
  1791. * Ok, it's pretty ugly... ;-)
  1792. */
  1793. sense_data[0] = 0x70;
  1794. sense_data[1] = 0x0;
  1795. sense_data[2] = sbp2_status[9];
  1796. sense_data[3] = sbp2_status[12];
  1797. sense_data[4] = sbp2_status[13];
  1798. sense_data[5] = sbp2_status[14];
  1799. sense_data[6] = sbp2_status[15];
  1800. sense_data[7] = 10;
  1801. sense_data[8] = sbp2_status[16];
  1802. sense_data[9] = sbp2_status[17];
  1803. sense_data[10] = sbp2_status[18];
  1804. sense_data[11] = sbp2_status[19];
  1805. sense_data[12] = sbp2_status[10];
  1806. sense_data[13] = sbp2_status[11];
  1807. sense_data[14] = sbp2_status[20];
  1808. sense_data[15] = sbp2_status[21];
  1809. return sbp2_status[8] & 0x3f; /* return scsi status */
  1810. }
  1811. /*
  1812. * This function deals with status writes from the SBP-2 device
  1813. */
  1814. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1815. int destid, quadlet_t *data, u64 addr,
  1816. size_t length, u16 fl)
  1817. {
  1818. struct sbp2scsi_host_info *hi;
  1819. struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
  1820. struct scsi_cmnd *SCpnt = NULL;
  1821. struct sbp2_status_block *sb;
  1822. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1823. struct sbp2_command_info *command;
  1824. unsigned long flags;
  1825. SBP2_DEBUG_ENTER();
  1826. sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
  1827. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1828. SBP2_ERR("Wrong size of status block");
  1829. return RCODE_ADDRESS_ERROR;
  1830. }
  1831. if (unlikely(!host)) {
  1832. SBP2_ERR("host is NULL - this is bad!");
  1833. return RCODE_ADDRESS_ERROR;
  1834. }
  1835. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1836. if (unlikely(!hi)) {
  1837. SBP2_ERR("host info is NULL - this is bad!");
  1838. return RCODE_ADDRESS_ERROR;
  1839. }
  1840. /*
  1841. * Find our scsi_id structure by looking at the status fifo address
  1842. * written to by the sbp2 device.
  1843. */
  1844. list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
  1845. if (scsi_id_tmp->ne->nodeid == nodeid &&
  1846. scsi_id_tmp->status_fifo_addr == addr) {
  1847. scsi_id = scsi_id_tmp;
  1848. break;
  1849. }
  1850. }
  1851. if (unlikely(!scsi_id)) {
  1852. SBP2_ERR("scsi_id is NULL - device is gone?");
  1853. return RCODE_ADDRESS_ERROR;
  1854. }
  1855. /*
  1856. * Put response into scsi_id status fifo buffer. The first two bytes
  1857. * come in big endian bit order. Often the target writes only a
  1858. * truncated status block, minimally the first two quadlets. The rest
  1859. * is implied to be zeros.
  1860. */
  1861. sb = &scsi_id->status_block;
  1862. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1863. memcpy(sb, data, length);
  1864. sbp2util_be32_to_cpu_buffer(sb, 8);
  1865. /*
  1866. * Ignore unsolicited status. Handle command ORB status.
  1867. */
  1868. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1869. command = NULL;
  1870. else
  1871. command = sbp2util_find_command_for_orb(scsi_id,
  1872. sb->ORB_offset_lo);
  1873. if (command) {
  1874. SBP2_DEBUG("Found status for command ORB");
  1875. pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
  1876. sizeof(struct sbp2_command_orb),
  1877. PCI_DMA_TODEVICE);
  1878. pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
  1879. sizeof(command->scatter_gather_element),
  1880. PCI_DMA_BIDIRECTIONAL);
  1881. SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
  1882. outstanding_orb_decr;
  1883. /*
  1884. * Matched status with command, now grab scsi command pointers
  1885. * and check status.
  1886. */
  1887. /*
  1888. * FIXME: If the src field in the status is 1, the ORB DMA must
  1889. * not be reused until status for a subsequent ORB is received.
  1890. */
  1891. SCpnt = command->Current_SCpnt;
  1892. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1893. sbp2util_mark_command_completed(scsi_id, command);
  1894. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1895. if (SCpnt) {
  1896. if (STATUS_TEST_RS(sb->ORB_offset_hi_misc))
  1897. scsi_status =
  1898. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1899. /*
  1900. * See if the target stored any scsi status information.
  1901. */
  1902. if (STATUS_GET_LEN(sb->ORB_offset_hi_misc) > 1) {
  1903. SBP2_DEBUG("CHECK CONDITION");
  1904. scsi_status = sbp2_status_to_sense_data(
  1905. (unchar *)sb, SCpnt->sense_buffer);
  1906. }
  1907. /*
  1908. * Check to see if the dead bit is set. If so, we'll
  1909. * have to initiate a fetch agent reset.
  1910. */
  1911. if (STATUS_TEST_D(sb->ORB_offset_hi_misc)) {
  1912. SBP2_DEBUG("Dead bit set - "
  1913. "initiating fetch agent reset");
  1914. sbp2_agent_reset(scsi_id, 0);
  1915. }
  1916. SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
  1917. }
  1918. /*
  1919. * Check here to see if there are no commands in-use. If there
  1920. * are none, we know that the fetch agent left the active state
  1921. * _and_ that we did not reactivate it yet. Therefore clear
  1922. * last_orb so that next time we write directly to the
  1923. * ORB_POINTER register. That way the fetch agent does not need
  1924. * to refetch the next_ORB.
  1925. */
  1926. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1927. if (list_empty(&scsi_id->sbp2_command_orb_inuse))
  1928. scsi_id->last_orb = NULL;
  1929. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1930. } else {
  1931. /*
  1932. * It's probably a login/logout/reconnect status.
  1933. */
  1934. if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
  1935. (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
  1936. (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
  1937. (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
  1938. scsi_id->access_complete = 1;
  1939. wake_up_interruptible(&access_wq);
  1940. }
  1941. }
  1942. if (SCpnt) {
  1943. SBP2_DEBUG("Completing SCSI command");
  1944. sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
  1945. command->Current_done);
  1946. SBP2_ORB_DEBUG("command orb completed");
  1947. }
  1948. return RCODE_COMPLETE;
  1949. }
  1950. /**************************************
  1951. * SCSI interface related section
  1952. **************************************/
  1953. /*
  1954. * This routine is the main request entry routine for doing I/O. It is
  1955. * called from the scsi stack directly.
  1956. */
  1957. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1958. void (*done)(struct scsi_cmnd *))
  1959. {
  1960. struct scsi_id_instance_data *scsi_id =
  1961. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  1962. struct sbp2scsi_host_info *hi;
  1963. int result = DID_NO_CONNECT << 16;
  1964. SBP2_DEBUG_ENTER();
  1965. #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
  1966. scsi_print_command(SCpnt);
  1967. #endif
  1968. if (!sbp2util_node_is_available(scsi_id))
  1969. goto done;
  1970. hi = scsi_id->hi;
  1971. if (!hi) {
  1972. SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
  1973. goto done;
  1974. }
  1975. /*
  1976. * Until we handle multiple luns, just return selection time-out
  1977. * to any IO directed at non-zero LUNs
  1978. */
  1979. if (SCpnt->device->lun)
  1980. goto done;
  1981. /*
  1982. * Check for request sense command, and handle it here
  1983. * (autorequest sense)
  1984. */
  1985. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1986. SBP2_DEBUG("REQUEST_SENSE");
  1987. memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
  1988. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  1989. sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
  1990. return 0;
  1991. }
  1992. /*
  1993. * Check to see if we are in the middle of a bus reset.
  1994. */
  1995. if (!hpsb_node_entry_valid(scsi_id->ne)) {
  1996. SBP2_ERR("Bus reset in progress - rejecting command");
  1997. result = DID_BUS_BUSY << 16;
  1998. goto done;
  1999. }
  2000. /*
  2001. * Bidirectional commands are not yet implemented,
  2002. * and unknown transfer direction not handled.
  2003. */
  2004. if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
  2005. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  2006. result = DID_ERROR << 16;
  2007. goto done;
  2008. }
  2009. /*
  2010. * Try and send our SCSI command
  2011. */
  2012. if (sbp2_send_command(scsi_id, SCpnt, done)) {
  2013. SBP2_ERR("Error sending SCSI command");
  2014. sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  2015. SCpnt, done);
  2016. }
  2017. return 0;
  2018. done:
  2019. SCpnt->result = result;
  2020. done(SCpnt);
  2021. return 0;
  2022. }
  2023. /*
  2024. * This function is called in order to complete all outstanding SBP-2
  2025. * commands (in case of resets, etc.).
  2026. */
  2027. static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
  2028. u32 status)
  2029. {
  2030. struct sbp2scsi_host_info *hi = scsi_id->hi;
  2031. struct list_head *lh;
  2032. struct sbp2_command_info *command;
  2033. unsigned long flags;
  2034. SBP2_DEBUG_ENTER();
  2035. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  2036. while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
  2037. SBP2_DEBUG("Found pending command to complete");
  2038. lh = scsi_id->sbp2_command_orb_inuse.next;
  2039. command = list_entry(lh, struct sbp2_command_info, list);
  2040. pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
  2041. sizeof(struct sbp2_command_orb),
  2042. PCI_DMA_TODEVICE);
  2043. pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
  2044. sizeof(command->scatter_gather_element),
  2045. PCI_DMA_BIDIRECTIONAL);
  2046. sbp2util_mark_command_completed(scsi_id, command);
  2047. if (command->Current_SCpnt) {
  2048. command->Current_SCpnt->result = status << 16;
  2049. command->Current_done(command->Current_SCpnt);
  2050. }
  2051. }
  2052. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  2053. return;
  2054. }
  2055. /*
  2056. * This function is called in order to complete a regular SBP-2 command.
  2057. *
  2058. * This can be called in interrupt context.
  2059. */
  2060. static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
  2061. u32 scsi_status, struct scsi_cmnd *SCpnt,
  2062. void (*done)(struct scsi_cmnd *))
  2063. {
  2064. SBP2_DEBUG_ENTER();
  2065. /*
  2066. * Sanity
  2067. */
  2068. if (!SCpnt) {
  2069. SBP2_ERR("SCpnt is NULL");
  2070. return;
  2071. }
  2072. /*
  2073. * If a bus reset is in progress and there was an error, don't
  2074. * complete the command, just let it get retried at the end of the
  2075. * bus reset.
  2076. */
  2077. if (!hpsb_node_entry_valid(scsi_id->ne)
  2078. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  2079. SBP2_ERR("Bus reset in progress - retry command later");
  2080. return;
  2081. }
  2082. /*
  2083. * Switch on scsi status
  2084. */
  2085. switch (scsi_status) {
  2086. case SBP2_SCSI_STATUS_GOOD:
  2087. SCpnt->result = DID_OK << 16;
  2088. break;
  2089. case SBP2_SCSI_STATUS_BUSY:
  2090. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  2091. SCpnt->result = DID_BUS_BUSY << 16;
  2092. break;
  2093. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  2094. SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
  2095. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  2096. #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
  2097. scsi_print_command(SCpnt);
  2098. scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
  2099. #endif
  2100. break;
  2101. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  2102. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  2103. SCpnt->result = DID_NO_CONNECT << 16;
  2104. scsi_print_command(SCpnt);
  2105. break;
  2106. case SBP2_SCSI_STATUS_CONDITION_MET:
  2107. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  2108. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  2109. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  2110. SCpnt->result = DID_ERROR << 16;
  2111. scsi_print_command(SCpnt);
  2112. break;
  2113. default:
  2114. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  2115. SCpnt->result = DID_ERROR << 16;
  2116. }
  2117. /*
  2118. * If a bus reset is in progress and there was an error, complete
  2119. * the command as busy so that it will get retried.
  2120. */
  2121. if (!hpsb_node_entry_valid(scsi_id->ne)
  2122. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  2123. SBP2_ERR("Completing command with busy (bus reset)");
  2124. SCpnt->result = DID_BUS_BUSY << 16;
  2125. }
  2126. /*
  2127. * If a unit attention occurs, return busy status so it gets
  2128. * retried... it could have happened because of a 1394 bus reset
  2129. * or hot-plug...
  2130. * XXX DID_BUS_BUSY is actually a bad idea because it will defy
  2131. * the scsi layer's retry logic.
  2132. */
  2133. #if 0
  2134. if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
  2135. (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
  2136. SBP2_DEBUG("UNIT ATTENTION - return busy");
  2137. SCpnt->result = DID_BUS_BUSY << 16;
  2138. }
  2139. #endif
  2140. /*
  2141. * Tell scsi stack that we're done with this command
  2142. */
  2143. done(SCpnt);
  2144. }
  2145. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  2146. {
  2147. struct scsi_id_instance_data *scsi_id =
  2148. (struct scsi_id_instance_data *)sdev->host->hostdata[0];
  2149. scsi_id->sdev = sdev;
  2150. if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  2151. sdev->inquiry_len = 36;
  2152. return 0;
  2153. }
  2154. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  2155. {
  2156. struct scsi_id_instance_data *scsi_id =
  2157. (struct scsi_id_instance_data *)sdev->host->hostdata[0];
  2158. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  2159. sdev->use_10_for_rw = 1;
  2160. sdev->use_10_for_ms = 1;
  2161. if (sdev->type == TYPE_DISK &&
  2162. scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  2163. sdev->skip_ms_page_8 = 1;
  2164. if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  2165. sdev->fix_capacity = 1;
  2166. if (scsi_id->ne->guid_vendor_id == 0x0010b9 && /* Maxtor's OUI */
  2167. (sdev->type == TYPE_DISK || sdev->type == TYPE_RBC))
  2168. sdev->allow_restart = 1;
  2169. return 0;
  2170. }
  2171. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  2172. {
  2173. ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
  2174. return;
  2175. }
  2176. /*
  2177. * Called by scsi stack when something has really gone wrong. Usually
  2178. * called when a command has timed-out for some reason.
  2179. */
  2180. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  2181. {
  2182. struct scsi_id_instance_data *scsi_id =
  2183. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  2184. struct sbp2scsi_host_info *hi = scsi_id->hi;
  2185. struct sbp2_command_info *command;
  2186. unsigned long flags;
  2187. SBP2_ERR("aborting sbp2 command");
  2188. scsi_print_command(SCpnt);
  2189. if (sbp2util_node_is_available(scsi_id)) {
  2190. /*
  2191. * Right now, just return any matching command structures
  2192. * to the free pool.
  2193. */
  2194. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  2195. command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
  2196. if (command) {
  2197. SBP2_DEBUG("Found command to abort");
  2198. pci_dma_sync_single_for_cpu(hi->host->pdev,
  2199. command->command_orb_dma,
  2200. sizeof(struct sbp2_command_orb),
  2201. PCI_DMA_TODEVICE);
  2202. pci_dma_sync_single_for_cpu(hi->host->pdev,
  2203. command->sge_dma,
  2204. sizeof(command->scatter_gather_element),
  2205. PCI_DMA_BIDIRECTIONAL);
  2206. sbp2util_mark_command_completed(scsi_id, command);
  2207. if (command->Current_SCpnt) {
  2208. command->Current_SCpnt->result = DID_ABORT << 16;
  2209. command->Current_done(command->Current_SCpnt);
  2210. }
  2211. }
  2212. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  2213. /*
  2214. * Initiate a fetch agent reset.
  2215. */
  2216. sbp2_agent_reset(scsi_id, 0);
  2217. sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
  2218. }
  2219. return SUCCESS;
  2220. }
  2221. /*
  2222. * Called by scsi stack when something has really gone wrong.
  2223. */
  2224. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  2225. {
  2226. struct scsi_id_instance_data *scsi_id =
  2227. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  2228. SBP2_ERR("reset requested");
  2229. if (sbp2util_node_is_available(scsi_id)) {
  2230. SBP2_ERR("Generating sbp2 fetch agent reset");
  2231. sbp2_agent_reset(scsi_id, 0);
  2232. }
  2233. return SUCCESS;
  2234. }
  2235. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  2236. struct device_attribute *attr,
  2237. char *buf)
  2238. {
  2239. struct scsi_device *sdev;
  2240. struct scsi_id_instance_data *scsi_id;
  2241. int lun;
  2242. if (!(sdev = to_scsi_device(dev)))
  2243. return 0;
  2244. if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
  2245. return 0;
  2246. lun = ORB_SET_LUN(scsi_id->sbp2_lun);
  2247. return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
  2248. scsi_id->ud->id, lun);
  2249. }
  2250. static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
  2251. static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
  2252. &dev_attr_ieee1394_id,
  2253. NULL
  2254. };
  2255. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  2256. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  2257. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  2258. MODULE_LICENSE("GPL");
  2259. /* SCSI host template */
  2260. static struct scsi_host_template scsi_driver_template = {
  2261. .module = THIS_MODULE,
  2262. .name = "SBP-2 IEEE-1394",
  2263. .proc_name = SBP2_DEVICE_NAME,
  2264. .queuecommand = sbp2scsi_queuecommand,
  2265. .eh_abort_handler = sbp2scsi_abort,
  2266. .eh_device_reset_handler = sbp2scsi_reset,
  2267. .slave_alloc = sbp2scsi_slave_alloc,
  2268. .slave_configure = sbp2scsi_slave_configure,
  2269. .slave_destroy = sbp2scsi_slave_destroy,
  2270. .this_id = -1,
  2271. .sg_tablesize = SG_ALL,
  2272. .use_clustering = ENABLE_CLUSTERING,
  2273. .cmd_per_lun = SBP2_MAX_CMDS,
  2274. .can_queue = SBP2_MAX_CMDS,
  2275. .emulated = 1,
  2276. .sdev_attrs = sbp2_sysfs_sdev_attrs,
  2277. };
  2278. static int sbp2_module_init(void)
  2279. {
  2280. int ret;
  2281. SBP2_DEBUG_ENTER();
  2282. /* Module load debug option to force one command at a time (serializing I/O) */
  2283. if (serialize_io) {
  2284. SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)");
  2285. SBP2_INFO("Try serialize_io=0 for better performance");
  2286. scsi_driver_template.can_queue = 1;
  2287. scsi_driver_template.cmd_per_lun = 1;
  2288. }
  2289. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  2290. (max_sectors * 512) > (128 * 1024))
  2291. max_sectors = 128 * 1024 / 512;
  2292. scsi_driver_template.max_sectors = max_sectors;
  2293. /* Register our high level driver with 1394 stack */
  2294. hpsb_register_highlevel(&sbp2_highlevel);
  2295. ret = hpsb_register_protocol(&sbp2_driver);
  2296. if (ret) {
  2297. SBP2_ERR("Failed to register protocol");
  2298. hpsb_unregister_highlevel(&sbp2_highlevel);
  2299. return ret;
  2300. }
  2301. return 0;
  2302. }
  2303. static void __exit sbp2_module_exit(void)
  2304. {
  2305. SBP2_DEBUG_ENTER();
  2306. hpsb_unregister_protocol(&sbp2_driver);
  2307. hpsb_unregister_highlevel(&sbp2_highlevel);
  2308. }
  2309. module_init(sbp2_module_init);
  2310. module_exit(sbp2_module_exit);