sbp2.c 81 KB

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