sbp2.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  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 (usually storage devices) as regular
  32. * SCSI devices. E.g. mount /dev/sda1, fdisk, mkfs, etc..
  33. *
  34. * See http://www.t10.org/drafts.htm#sbp2 for the final draft of the SBP-2
  35. * specification and for where to purchase the official standard.
  36. *
  37. * TODO:
  38. * - look into possible improvements of the SCSI error handlers
  39. * - handle Unit_Characteristics.mgt_ORB_timeout and .ORB_size
  40. * - handle Logical_Unit_Number.ordered
  41. * - handle src == 1 in status blocks
  42. * - reimplement the DMA mapping in absence of physical DMA so that
  43. * bus_to_virt is no longer required
  44. * - debug the handling of absent physical DMA
  45. * - replace CONFIG_IEEE1394_SBP2_PHYS_DMA by automatic detection
  46. * (this is easy but depends on the previous two TODO items)
  47. * - make the parameter serialize_io configurable per device
  48. * - move all requests to fetch agent registers into non-atomic context,
  49. * replace all usages of sbp2util_node_write_no_wait by true transactions
  50. * - convert to generic DMA mapping API to eliminate dependency on PCI
  51. * Grep for inline FIXME comments below.
  52. */
  53. #include <linux/blkdev.h>
  54. #include <linux/compiler.h>
  55. #include <linux/delay.h>
  56. #include <linux/device.h>
  57. #include <linux/dma-mapping.h>
  58. #include <linux/gfp.h>
  59. #include <linux/init.h>
  60. #include <linux/kernel.h>
  61. #include <linux/list.h>
  62. #include <linux/module.h>
  63. #include <linux/moduleparam.h>
  64. #include <linux/pci.h>
  65. #include <linux/slab.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/stat.h>
  68. #include <linux/string.h>
  69. #include <linux/stringify.h>
  70. #include <linux/types.h>
  71. #include <linux/wait.h>
  72. #include <asm/byteorder.h>
  73. #include <asm/errno.h>
  74. #include <asm/param.h>
  75. #include <asm/scatterlist.h>
  76. #include <asm/system.h>
  77. #include <asm/types.h>
  78. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  79. #include <asm/io.h> /* for bus_to_virt */
  80. #endif
  81. #include <scsi/scsi.h>
  82. #include <scsi/scsi_cmnd.h>
  83. #include <scsi/scsi_dbg.h>
  84. #include <scsi/scsi_device.h>
  85. #include <scsi/scsi_host.h>
  86. #include "csr1212.h"
  87. #include "highlevel.h"
  88. #include "hosts.h"
  89. #include "ieee1394.h"
  90. #include "ieee1394_core.h"
  91. #include "ieee1394_hotplug.h"
  92. #include "ieee1394_transactions.h"
  93. #include "ieee1394_types.h"
  94. #include "nodemgr.h"
  95. #include "sbp2.h"
  96. /*
  97. * Module load parameter definitions
  98. */
  99. /*
  100. * Change max_speed on module load if you have a bad IEEE-1394
  101. * controller that has trouble running 2KB packets at 400mb.
  102. *
  103. * NOTE: On certain OHCI parts I have seen short packets on async transmit
  104. * (probably due to PCI latency/throughput issues with the part). You can
  105. * bump down the speed if you are running into problems.
  106. */
  107. static int sbp2_max_speed = IEEE1394_SPEED_MAX;
  108. module_param_named(max_speed, sbp2_max_speed, int, 0644);
  109. MODULE_PARM_DESC(max_speed, "Force max speed "
  110. "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
  111. /*
  112. * Set serialize_io to 1 if you'd like only one scsi command sent
  113. * down to us at a time (debugging). This might be necessary for very
  114. * badly behaved sbp2 devices.
  115. */
  116. static int sbp2_serialize_io = 1;
  117. module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
  118. MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
  119. "(default = 1, faster = 0)");
  120. /*
  121. * Bump up max_sectors if you'd like to support very large sized
  122. * transfers. Please note that some older sbp2 bridge chips are broken for
  123. * transfers greater or equal to 128KB. Default is a value of 255
  124. * sectors, or just under 128KB (at 512 byte sector size). I can note that
  125. * the Oxsemi sbp2 chipsets have no problems supporting very large
  126. * transfer sizes.
  127. */
  128. static int sbp2_max_sectors = SBP2_MAX_SECTORS;
  129. module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
  130. MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
  131. "(default = " __stringify(SBP2_MAX_SECTORS) ")");
  132. /*
  133. * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
  134. * do an exclusive login, as it's generally unsafe to have two hosts
  135. * talking to a single sbp2 device at the same time (filesystem coherency,
  136. * etc.). If you're running an sbp2 device that supports multiple logins,
  137. * and you're either running read-only filesystems or some sort of special
  138. * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
  139. * File System, or Lustre, then set exclusive_login to zero.
  140. *
  141. * So far only bridges from Oxford Semiconductor are known to support
  142. * concurrent logins. Depending on firmware, four or two concurrent logins
  143. * are possible on OXFW911 and newer Oxsemi bridges.
  144. */
  145. static int sbp2_exclusive_login = 1;
  146. module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
  147. MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
  148. "(default = 1)");
  149. /*
  150. * If any of the following workarounds is required for your device to work,
  151. * please submit the kernel messages logged by sbp2 to the linux1394-devel
  152. * mailing list.
  153. *
  154. * - 128kB max transfer
  155. * Limit transfer size. Necessary for some old bridges.
  156. *
  157. * - 36 byte inquiry
  158. * When scsi_mod probes the device, let the inquiry command look like that
  159. * from MS Windows.
  160. *
  161. * - skip mode page 8
  162. * Suppress sending of mode_sense for mode page 8 if the device pretends to
  163. * support the SCSI Primary Block commands instead of Reduced Block Commands.
  164. *
  165. * - fix capacity
  166. * Tell sd_mod to correct the last sector number reported by read_capacity.
  167. * Avoids access beyond actual disk limits on devices with an off-by-one bug.
  168. * Don't use this with devices which don't have this bug.
  169. *
  170. * - override internal blacklist
  171. * Instead of adding to the built-in blacklist, use only the workarounds
  172. * specified in the module load parameter.
  173. * Useful if a blacklist entry interfered with a non-broken device.
  174. */
  175. static int sbp2_default_workarounds;
  176. module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
  177. MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
  178. ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
  179. ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
  180. ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
  181. ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
  182. ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
  183. ", or a combination)");
  184. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  185. #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  186. /*
  187. * Globals
  188. */
  189. static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
  190. static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
  191. void (*)(struct scsi_cmnd *));
  192. static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
  193. static int sbp2_start_device(struct sbp2_lu *);
  194. static void sbp2_remove_device(struct sbp2_lu *);
  195. static int sbp2_login_device(struct sbp2_lu *);
  196. static int sbp2_reconnect_device(struct sbp2_lu *);
  197. static int sbp2_logout_device(struct sbp2_lu *);
  198. static void sbp2_host_reset(struct hpsb_host *);
  199. static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
  200. u64, size_t, u16);
  201. static int sbp2_agent_reset(struct sbp2_lu *, int);
  202. static void sbp2_parse_unit_directory(struct sbp2_lu *,
  203. struct unit_directory *);
  204. static int sbp2_set_busy_timeout(struct sbp2_lu *);
  205. static int sbp2_max_speed_and_size(struct sbp2_lu *);
  206. static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
  207. static struct hpsb_highlevel sbp2_highlevel = {
  208. .name = SBP2_DEVICE_NAME,
  209. .host_reset = sbp2_host_reset,
  210. };
  211. static struct hpsb_address_ops sbp2_ops = {
  212. .write = sbp2_handle_status_write
  213. };
  214. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  215. static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
  216. u64, size_t, u16);
  217. static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
  218. size_t, u16);
  219. static struct hpsb_address_ops sbp2_physdma_ops = {
  220. .read = sbp2_handle_physdma_read,
  221. .write = sbp2_handle_physdma_write,
  222. };
  223. #endif
  224. /*
  225. * Interface to driver core and IEEE 1394 core
  226. */
  227. static struct ieee1394_device_id sbp2_id_table[] = {
  228. {
  229. .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  230. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
  231. .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
  232. {}
  233. };
  234. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  235. static int sbp2_probe(struct device *);
  236. static int sbp2_remove(struct device *);
  237. static int sbp2_update(struct unit_directory *);
  238. static struct hpsb_protocol_driver sbp2_driver = {
  239. .name = "SBP2 Driver",
  240. .id_table = sbp2_id_table,
  241. .update = sbp2_update,
  242. .driver = {
  243. .name = SBP2_DEVICE_NAME,
  244. .bus = &ieee1394_bus_type,
  245. .probe = sbp2_probe,
  246. .remove = sbp2_remove,
  247. },
  248. };
  249. /*
  250. * Interface to SCSI core
  251. */
  252. static int sbp2scsi_queuecommand(struct scsi_cmnd *,
  253. void (*)(struct scsi_cmnd *));
  254. static int sbp2scsi_abort(struct scsi_cmnd *);
  255. static int sbp2scsi_reset(struct scsi_cmnd *);
  256. static int sbp2scsi_slave_alloc(struct scsi_device *);
  257. static int sbp2scsi_slave_configure(struct scsi_device *);
  258. static void sbp2scsi_slave_destroy(struct scsi_device *);
  259. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
  260. struct device_attribute *, char *);
  261. static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
  262. static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
  263. &dev_attr_ieee1394_id,
  264. NULL
  265. };
  266. static struct scsi_host_template sbp2_shost_template = {
  267. .module = THIS_MODULE,
  268. .name = "SBP-2 IEEE-1394",
  269. .proc_name = SBP2_DEVICE_NAME,
  270. .queuecommand = sbp2scsi_queuecommand,
  271. .eh_abort_handler = sbp2scsi_abort,
  272. .eh_device_reset_handler = sbp2scsi_reset,
  273. .slave_alloc = sbp2scsi_slave_alloc,
  274. .slave_configure = sbp2scsi_slave_configure,
  275. .slave_destroy = sbp2scsi_slave_destroy,
  276. .this_id = -1,
  277. .sg_tablesize = SG_ALL,
  278. .use_clustering = ENABLE_CLUSTERING,
  279. .cmd_per_lun = SBP2_MAX_CMDS,
  280. .can_queue = SBP2_MAX_CMDS,
  281. .emulated = 1,
  282. .sdev_attrs = sbp2_sysfs_sdev_attrs,
  283. };
  284. /*
  285. * List of devices with known bugs.
  286. *
  287. * The firmware_revision field, masked with 0xffff00, is the best indicator
  288. * for the type of bridge chip of a device. It yields a few false positives
  289. * but this did not break correctly behaving devices so far.
  290. */
  291. static const struct {
  292. u32 firmware_revision;
  293. u32 model_id;
  294. unsigned workarounds;
  295. } sbp2_workarounds_table[] = {
  296. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  297. .firmware_revision = 0x002800,
  298. .model_id = 0x001010,
  299. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  300. SBP2_WORKAROUND_MODE_SENSE_8,
  301. },
  302. /* Initio bridges, actually only needed for some older ones */ {
  303. .firmware_revision = 0x000200,
  304. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  305. },
  306. /* Symbios bridge */ {
  307. .firmware_revision = 0xa0b800,
  308. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  309. },
  310. /*
  311. * Note about the following Apple iPod blacklist entries:
  312. *
  313. * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
  314. * matching logic treats 0 as a wildcard, we cannot match this ID
  315. * without rewriting the matching routine. Fortunately these iPods
  316. * do not feature the read_capacity bug according to one report.
  317. * Read_capacity behaviour as well as model_id could change due to
  318. * Apple-supplied firmware updates though.
  319. */
  320. /* iPod 4th generation */ {
  321. .firmware_revision = 0x0a2700,
  322. .model_id = 0x000021,
  323. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  324. },
  325. /* iPod mini */ {
  326. .firmware_revision = 0x0a2700,
  327. .model_id = 0x000023,
  328. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  329. },
  330. /* iPod Photo */ {
  331. .firmware_revision = 0x0a2700,
  332. .model_id = 0x00007e,
  333. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  334. }
  335. };
  336. /**************************************
  337. * General utility functions
  338. **************************************/
  339. #ifndef __BIG_ENDIAN
  340. /*
  341. * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
  342. */
  343. static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
  344. {
  345. u32 *temp = buffer;
  346. for (length = (length >> 2); length--; )
  347. temp[length] = be32_to_cpu(temp[length]);
  348. }
  349. /*
  350. * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
  351. */
  352. static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
  353. {
  354. u32 *temp = buffer;
  355. for (length = (length >> 2); length--; )
  356. temp[length] = cpu_to_be32(temp[length]);
  357. }
  358. #else /* BIG_ENDIAN */
  359. /* Why waste the cpu cycles? */
  360. #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
  361. #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
  362. #endif
  363. static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
  364. /*
  365. * Waits for completion of an SBP-2 access request.
  366. * Returns nonzero if timed out or prematurely interrupted.
  367. */
  368. static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
  369. {
  370. long leftover;
  371. leftover = wait_event_interruptible_timeout(
  372. sbp2_access_wq, lu->access_complete, timeout);
  373. lu->access_complete = 0;
  374. return leftover <= 0;
  375. }
  376. static void sbp2_free_packet(void *packet)
  377. {
  378. hpsb_free_tlabel(packet);
  379. hpsb_free_packet(packet);
  380. }
  381. /*
  382. * This is much like hpsb_node_write(), except it ignores the response
  383. * subaction and returns immediately. Can be used from atomic context.
  384. */
  385. static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
  386. quadlet_t *buf, size_t len)
  387. {
  388. struct hpsb_packet *packet;
  389. packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
  390. if (!packet)
  391. return -ENOMEM;
  392. hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
  393. hpsb_node_fill_packet(ne, packet);
  394. if (hpsb_send_packet(packet) < 0) {
  395. sbp2_free_packet(packet);
  396. return -EIO;
  397. }
  398. return 0;
  399. }
  400. static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
  401. quadlet_t *data, size_t len)
  402. {
  403. /* There is a small window after a bus reset within which the node
  404. * entry's generation is current but the reconnect wasn't completed. */
  405. if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
  406. return;
  407. if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
  408. data, len))
  409. SBP2_ERR("sbp2util_notify_fetch_agent failed.");
  410. /* Now accept new SCSI commands, unless a bus reset happended during
  411. * hpsb_node_write. */
  412. if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
  413. scsi_unblock_requests(lu->shost);
  414. }
  415. static void sbp2util_write_orb_pointer(struct work_struct *work)
  416. {
  417. quadlet_t data[2];
  418. data[0] = ORB_SET_NODE_ID((container_of(work, struct sbp2_lu, protocol_work))->hi->host->node_id);
  419. data[1] = (container_of(work, struct sbp2_lu, protocol_work))->last_orb_dma;
  420. sbp2util_cpu_to_be32_buffer(data, 8);
  421. sbp2util_notify_fetch_agent(container_of(work, struct sbp2_lu, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
  422. }
  423. static void sbp2util_write_doorbell(struct work_struct *work)
  424. {
  425. sbp2util_notify_fetch_agent(container_of(work, struct sbp2_lu, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
  426. }
  427. static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
  428. {
  429. struct sbp2_fwhost_info *hi = lu->hi;
  430. int i;
  431. unsigned long flags, orbs;
  432. struct sbp2_command_info *cmd;
  433. orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
  434. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  435. for (i = 0; i < orbs; i++) {
  436. cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
  437. if (!cmd) {
  438. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  439. return -ENOMEM;
  440. }
  441. cmd->command_orb_dma = pci_map_single(hi->host->pdev,
  442. &cmd->command_orb,
  443. sizeof(struct sbp2_command_orb),
  444. PCI_DMA_TODEVICE);
  445. cmd->sge_dma = pci_map_single(hi->host->pdev,
  446. &cmd->scatter_gather_element,
  447. sizeof(cmd->scatter_gather_element),
  448. PCI_DMA_BIDIRECTIONAL);
  449. INIT_LIST_HEAD(&cmd->list);
  450. list_add_tail(&cmd->list, &lu->cmd_orb_completed);
  451. }
  452. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  453. return 0;
  454. }
  455. static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu)
  456. {
  457. struct hpsb_host *host = lu->hi->host;
  458. struct list_head *lh, *next;
  459. struct sbp2_command_info *cmd;
  460. unsigned long flags;
  461. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  462. if (!list_empty(&lu->cmd_orb_completed))
  463. list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
  464. cmd = list_entry(lh, struct sbp2_command_info, list);
  465. pci_unmap_single(host->pdev, cmd->command_orb_dma,
  466. sizeof(struct sbp2_command_orb),
  467. PCI_DMA_TODEVICE);
  468. pci_unmap_single(host->pdev, cmd->sge_dma,
  469. sizeof(cmd->scatter_gather_element),
  470. PCI_DMA_BIDIRECTIONAL);
  471. kfree(cmd);
  472. }
  473. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  474. return;
  475. }
  476. /*
  477. * Finds the sbp2_command for a given outstanding command ORB.
  478. * Only looks at the in-use list.
  479. */
  480. static struct sbp2_command_info *sbp2util_find_command_for_orb(
  481. struct sbp2_lu *lu, dma_addr_t orb)
  482. {
  483. struct sbp2_command_info *cmd;
  484. unsigned long flags;
  485. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  486. if (!list_empty(&lu->cmd_orb_inuse))
  487. list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
  488. if (cmd->command_orb_dma == orb) {
  489. spin_unlock_irqrestore(
  490. &lu->cmd_orb_lock, flags);
  491. return cmd;
  492. }
  493. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  494. return NULL;
  495. }
  496. /*
  497. * Finds the sbp2_command for a given outstanding SCpnt.
  498. * Only looks at the in-use list.
  499. * Must be called with lu->cmd_orb_lock held.
  500. */
  501. static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
  502. struct sbp2_lu *lu, void *SCpnt)
  503. {
  504. struct sbp2_command_info *cmd;
  505. if (!list_empty(&lu->cmd_orb_inuse))
  506. list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
  507. if (cmd->Current_SCpnt == SCpnt)
  508. return cmd;
  509. return NULL;
  510. }
  511. static struct sbp2_command_info *sbp2util_allocate_command_orb(
  512. struct sbp2_lu *lu,
  513. struct scsi_cmnd *Current_SCpnt,
  514. void (*Current_done)(struct scsi_cmnd *))
  515. {
  516. struct list_head *lh;
  517. struct sbp2_command_info *cmd = NULL;
  518. unsigned long flags;
  519. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  520. if (!list_empty(&lu->cmd_orb_completed)) {
  521. lh = lu->cmd_orb_completed.next;
  522. list_del(lh);
  523. cmd = list_entry(lh, struct sbp2_command_info, list);
  524. cmd->Current_done = Current_done;
  525. cmd->Current_SCpnt = Current_SCpnt;
  526. list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
  527. } else
  528. SBP2_ERR("%s: no orbs available", __FUNCTION__);
  529. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  530. return cmd;
  531. }
  532. /*
  533. * Unmaps the DMAs of a command and moves the command to the completed ORB list.
  534. * Must be called with lu->cmd_orb_lock held.
  535. */
  536. static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
  537. struct sbp2_command_info *cmd)
  538. {
  539. struct hpsb_host *host = lu->ud->ne->host;
  540. if (cmd->cmd_dma) {
  541. if (cmd->dma_type == CMD_DMA_SINGLE)
  542. pci_unmap_single(host->pdev, cmd->cmd_dma,
  543. cmd->dma_size, cmd->dma_dir);
  544. else if (cmd->dma_type == CMD_DMA_PAGE)
  545. pci_unmap_page(host->pdev, cmd->cmd_dma,
  546. cmd->dma_size, cmd->dma_dir);
  547. /* XXX: Check for CMD_DMA_NONE bug */
  548. cmd->dma_type = CMD_DMA_NONE;
  549. cmd->cmd_dma = 0;
  550. }
  551. if (cmd->sge_buffer) {
  552. pci_unmap_sg(host->pdev, cmd->sge_buffer,
  553. cmd->dma_size, cmd->dma_dir);
  554. cmd->sge_buffer = NULL;
  555. }
  556. list_move_tail(&cmd->list, &lu->cmd_orb_completed);
  557. }
  558. /*
  559. * Is lu valid? Is the 1394 node still present?
  560. */
  561. static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
  562. {
  563. return lu && lu->ne && !lu->ne->in_limbo;
  564. }
  565. /*********************************************
  566. * IEEE-1394 core driver stack related section
  567. *********************************************/
  568. static int sbp2_probe(struct device *dev)
  569. {
  570. struct unit_directory *ud;
  571. struct sbp2_lu *lu;
  572. ud = container_of(dev, struct unit_directory, device);
  573. /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
  574. * instead. */
  575. if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
  576. return -ENODEV;
  577. lu = sbp2_alloc_device(ud);
  578. if (!lu)
  579. return -ENOMEM;
  580. sbp2_parse_unit_directory(lu, ud);
  581. return sbp2_start_device(lu);
  582. }
  583. static int sbp2_remove(struct device *dev)
  584. {
  585. struct unit_directory *ud;
  586. struct sbp2_lu *lu;
  587. struct scsi_device *sdev;
  588. ud = container_of(dev, struct unit_directory, device);
  589. lu = ud->device.driver_data;
  590. if (!lu)
  591. return 0;
  592. if (lu->shost) {
  593. /* Get rid of enqueued commands if there is no chance to
  594. * send them. */
  595. if (!sbp2util_node_is_available(lu))
  596. sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
  597. /* scsi_remove_device() may trigger shutdown functions of SCSI
  598. * highlevel drivers which would deadlock if blocked. */
  599. atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
  600. scsi_unblock_requests(lu->shost);
  601. }
  602. sdev = lu->sdev;
  603. if (sdev) {
  604. lu->sdev = NULL;
  605. scsi_remove_device(sdev);
  606. }
  607. sbp2_logout_device(lu);
  608. sbp2_remove_device(lu);
  609. return 0;
  610. }
  611. static int sbp2_update(struct unit_directory *ud)
  612. {
  613. struct sbp2_lu *lu = ud->device.driver_data;
  614. if (sbp2_reconnect_device(lu)) {
  615. /* Reconnect has failed. Perhaps we didn't reconnect fast
  616. * enough. Try a regular login, but first log out just in
  617. * case of any weirdness. */
  618. sbp2_logout_device(lu);
  619. if (sbp2_login_device(lu)) {
  620. /* Login failed too, just fail, and the backend
  621. * will call our sbp2_remove for us */
  622. SBP2_ERR("Failed to reconnect to sbp2 device!");
  623. return -EBUSY;
  624. }
  625. }
  626. sbp2_set_busy_timeout(lu);
  627. sbp2_agent_reset(lu, 1);
  628. sbp2_max_speed_and_size(lu);
  629. /* Complete any pending commands with busy (so they get retried)
  630. * and remove them from our queue. */
  631. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  632. /* Accept new commands unless there was another bus reset in the
  633. * meantime. */
  634. if (hpsb_node_entry_valid(lu->ne)) {
  635. atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
  636. scsi_unblock_requests(lu->shost);
  637. }
  638. return 0;
  639. }
  640. static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
  641. {
  642. struct sbp2_fwhost_info *hi;
  643. struct Scsi_Host *shost = NULL;
  644. struct sbp2_lu *lu = NULL;
  645. lu = kzalloc(sizeof(*lu), GFP_KERNEL);
  646. if (!lu) {
  647. SBP2_ERR("failed to create lu");
  648. goto failed_alloc;
  649. }
  650. lu->ne = ud->ne;
  651. lu->ud = ud;
  652. lu->speed_code = IEEE1394_SPEED_100;
  653. lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
  654. lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
  655. INIT_LIST_HEAD(&lu->cmd_orb_inuse);
  656. INIT_LIST_HEAD(&lu->cmd_orb_completed);
  657. INIT_LIST_HEAD(&lu->lu_list);
  658. spin_lock_init(&lu->cmd_orb_lock);
  659. atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
  660. INIT_WORK(&lu->protocol_work, NULL);
  661. ud->device.driver_data = lu;
  662. hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
  663. if (!hi) {
  664. hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
  665. sizeof(*hi));
  666. if (!hi) {
  667. SBP2_ERR("failed to allocate hostinfo");
  668. goto failed_alloc;
  669. }
  670. hi->host = ud->ne->host;
  671. INIT_LIST_HEAD(&hi->logical_units);
  672. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  673. /* Handle data movement if physical dma is not
  674. * enabled or not supported on host controller */
  675. if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
  676. &sbp2_physdma_ops,
  677. 0x0ULL, 0xfffffffcULL)) {
  678. SBP2_ERR("failed to register lower 4GB address range");
  679. goto failed_alloc;
  680. }
  681. #endif
  682. }
  683. /* Prevent unloading of the 1394 host */
  684. if (!try_module_get(hi->host->driver->owner)) {
  685. SBP2_ERR("failed to get a reference on 1394 host driver");
  686. goto failed_alloc;
  687. }
  688. lu->hi = hi;
  689. list_add_tail(&lu->lu_list, &hi->logical_units);
  690. /* Register the status FIFO address range. We could use the same FIFO
  691. * for targets at different nodes. However we need different FIFOs per
  692. * target in order to support multi-unit devices.
  693. * The FIFO is located out of the local host controller's physical range
  694. * but, if possible, within the posted write area. Status writes will
  695. * then be performed as unified transactions. This slightly reduces
  696. * bandwidth usage, and some Prolific based devices seem to require it.
  697. */
  698. lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  699. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  700. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  701. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  702. if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  703. SBP2_ERR("failed to allocate status FIFO address range");
  704. goto failed_alloc;
  705. }
  706. shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
  707. if (!shost) {
  708. SBP2_ERR("failed to register scsi host");
  709. goto failed_alloc;
  710. }
  711. shost->hostdata[0] = (unsigned long)lu;
  712. if (!scsi_add_host(shost, &ud->device)) {
  713. lu->shost = shost;
  714. return lu;
  715. }
  716. SBP2_ERR("failed to add scsi host");
  717. scsi_host_put(shost);
  718. failed_alloc:
  719. sbp2_remove_device(lu);
  720. return NULL;
  721. }
  722. static void sbp2_host_reset(struct hpsb_host *host)
  723. {
  724. struct sbp2_fwhost_info *hi;
  725. struct sbp2_lu *lu;
  726. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  727. if (!hi)
  728. return;
  729. list_for_each_entry(lu, &hi->logical_units, lu_list)
  730. if (likely(atomic_read(&lu->state) !=
  731. SBP2LU_STATE_IN_SHUTDOWN)) {
  732. atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
  733. scsi_block_requests(lu->shost);
  734. }
  735. }
  736. static int sbp2_start_device(struct sbp2_lu *lu)
  737. {
  738. struct sbp2_fwhost_info *hi = lu->hi;
  739. int error;
  740. lu->login_response = pci_alloc_consistent(hi->host->pdev,
  741. sizeof(struct sbp2_login_response),
  742. &lu->login_response_dma);
  743. if (!lu->login_response)
  744. goto alloc_fail;
  745. lu->query_logins_orb = pci_alloc_consistent(hi->host->pdev,
  746. sizeof(struct sbp2_query_logins_orb),
  747. &lu->query_logins_orb_dma);
  748. if (!lu->query_logins_orb)
  749. goto alloc_fail;
  750. lu->query_logins_response = pci_alloc_consistent(hi->host->pdev,
  751. sizeof(struct sbp2_query_logins_response),
  752. &lu->query_logins_response_dma);
  753. if (!lu->query_logins_response)
  754. goto alloc_fail;
  755. lu->reconnect_orb = pci_alloc_consistent(hi->host->pdev,
  756. sizeof(struct sbp2_reconnect_orb),
  757. &lu->reconnect_orb_dma);
  758. if (!lu->reconnect_orb)
  759. goto alloc_fail;
  760. lu->logout_orb = pci_alloc_consistent(hi->host->pdev,
  761. sizeof(struct sbp2_logout_orb),
  762. &lu->logout_orb_dma);
  763. if (!lu->logout_orb)
  764. goto alloc_fail;
  765. lu->login_orb = pci_alloc_consistent(hi->host->pdev,
  766. sizeof(struct sbp2_login_orb),
  767. &lu->login_orb_dma);
  768. if (!lu->login_orb)
  769. goto alloc_fail;
  770. if (sbp2util_create_command_orb_pool(lu)) {
  771. SBP2_ERR("sbp2util_create_command_orb_pool failed!");
  772. sbp2_remove_device(lu);
  773. return -ENOMEM;
  774. }
  775. /* Wait a second before trying to log in. Previously logged in
  776. * initiators need a chance to reconnect. */
  777. if (msleep_interruptible(1000)) {
  778. sbp2_remove_device(lu);
  779. return -EINTR;
  780. }
  781. if (sbp2_login_device(lu)) {
  782. sbp2_remove_device(lu);
  783. return -EBUSY;
  784. }
  785. sbp2_set_busy_timeout(lu);
  786. sbp2_agent_reset(lu, 1);
  787. sbp2_max_speed_and_size(lu);
  788. error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
  789. if (error) {
  790. SBP2_ERR("scsi_add_device failed");
  791. sbp2_logout_device(lu);
  792. sbp2_remove_device(lu);
  793. return error;
  794. }
  795. return 0;
  796. alloc_fail:
  797. SBP2_ERR("Could not allocate memory for lu");
  798. sbp2_remove_device(lu);
  799. return -ENOMEM;
  800. }
  801. static void sbp2_remove_device(struct sbp2_lu *lu)
  802. {
  803. struct sbp2_fwhost_info *hi;
  804. if (!lu)
  805. return;
  806. hi = lu->hi;
  807. if (lu->shost) {
  808. scsi_remove_host(lu->shost);
  809. scsi_host_put(lu->shost);
  810. }
  811. flush_scheduled_work();
  812. sbp2util_remove_command_orb_pool(lu);
  813. list_del(&lu->lu_list);
  814. if (lu->login_response)
  815. pci_free_consistent(hi->host->pdev,
  816. sizeof(struct sbp2_login_response),
  817. lu->login_response,
  818. lu->login_response_dma);
  819. if (lu->login_orb)
  820. pci_free_consistent(hi->host->pdev,
  821. sizeof(struct sbp2_login_orb),
  822. lu->login_orb,
  823. lu->login_orb_dma);
  824. if (lu->reconnect_orb)
  825. pci_free_consistent(hi->host->pdev,
  826. sizeof(struct sbp2_reconnect_orb),
  827. lu->reconnect_orb,
  828. lu->reconnect_orb_dma);
  829. if (lu->logout_orb)
  830. pci_free_consistent(hi->host->pdev,
  831. sizeof(struct sbp2_logout_orb),
  832. lu->logout_orb,
  833. lu->logout_orb_dma);
  834. if (lu->query_logins_orb)
  835. pci_free_consistent(hi->host->pdev,
  836. sizeof(struct sbp2_query_logins_orb),
  837. lu->query_logins_orb,
  838. lu->query_logins_orb_dma);
  839. if (lu->query_logins_response)
  840. pci_free_consistent(hi->host->pdev,
  841. sizeof(struct sbp2_query_logins_response),
  842. lu->query_logins_response,
  843. lu->query_logins_response_dma);
  844. if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  845. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  846. lu->status_fifo_addr);
  847. lu->ud->device.driver_data = NULL;
  848. if (hi)
  849. module_put(hi->host->driver->owner);
  850. kfree(lu);
  851. }
  852. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  853. /*
  854. * Deal with write requests on adapters which do not support physical DMA or
  855. * have it switched off.
  856. */
  857. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  858. int destid, quadlet_t *data, u64 addr,
  859. size_t length, u16 flags)
  860. {
  861. memcpy(bus_to_virt((u32) addr), data, length);
  862. return RCODE_COMPLETE;
  863. }
  864. /*
  865. * Deal with read requests on adapters which do not support physical DMA or
  866. * have it switched off.
  867. */
  868. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  869. quadlet_t *data, u64 addr, size_t length,
  870. u16 flags)
  871. {
  872. memcpy(data, bus_to_virt((u32) addr), length);
  873. return RCODE_COMPLETE;
  874. }
  875. #endif
  876. /**************************************
  877. * SBP-2 protocol related section
  878. **************************************/
  879. static int sbp2_query_logins(struct sbp2_lu *lu)
  880. {
  881. struct sbp2_fwhost_info *hi = lu->hi;
  882. quadlet_t data[2];
  883. int max_logins;
  884. int active_logins;
  885. lu->query_logins_orb->reserved1 = 0x0;
  886. lu->query_logins_orb->reserved2 = 0x0;
  887. lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
  888. lu->query_logins_orb->query_response_hi =
  889. ORB_SET_NODE_ID(hi->host->node_id);
  890. lu->query_logins_orb->lun_misc =
  891. ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  892. lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  893. lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  894. lu->query_logins_orb->reserved_resp_length =
  895. ORB_SET_QUERY_LOGINS_RESP_LENGTH(
  896. sizeof(struct sbp2_query_logins_response));
  897. lu->query_logins_orb->status_fifo_hi =
  898. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  899. lu->query_logins_orb->status_fifo_lo =
  900. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  901. sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
  902. sizeof(struct sbp2_query_logins_orb));
  903. memset(lu->query_logins_response, 0,
  904. sizeof(struct sbp2_query_logins_response));
  905. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  906. data[1] = lu->query_logins_orb_dma;
  907. sbp2util_cpu_to_be32_buffer(data, 8);
  908. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  909. if (sbp2util_access_timeout(lu, 2*HZ)) {
  910. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  911. return -EIO;
  912. }
  913. if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
  914. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  915. return -EIO;
  916. }
  917. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  918. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  919. return -EIO;
  920. }
  921. sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
  922. sizeof(struct sbp2_query_logins_response));
  923. max_logins = RESPONSE_GET_MAX_LOGINS(
  924. lu->query_logins_response->length_max_logins);
  925. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  926. active_logins = RESPONSE_GET_ACTIVE_LOGINS(
  927. lu->query_logins_response->length_max_logins);
  928. SBP2_INFO("Number of active logins: %d", active_logins);
  929. if (active_logins >= max_logins) {
  930. return -EIO;
  931. }
  932. return 0;
  933. }
  934. static int sbp2_login_device(struct sbp2_lu *lu)
  935. {
  936. struct sbp2_fwhost_info *hi = lu->hi;
  937. quadlet_t data[2];
  938. if (!lu->login_orb)
  939. return -EIO;
  940. if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
  941. SBP2_INFO("Device does not support any more concurrent logins");
  942. return -EIO;
  943. }
  944. /* assume no password */
  945. lu->login_orb->password_hi = 0;
  946. lu->login_orb->password_lo = 0;
  947. lu->login_orb->login_response_lo = lu->login_response_dma;
  948. lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  949. lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  950. /* one second reconnect time */
  951. lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
  952. lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
  953. lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
  954. lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  955. lu->login_orb->passwd_resp_lengths =
  956. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  957. lu->login_orb->status_fifo_hi =
  958. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  959. lu->login_orb->status_fifo_lo =
  960. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  961. sbp2util_cpu_to_be32_buffer(lu->login_orb,
  962. sizeof(struct sbp2_login_orb));
  963. memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
  964. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  965. data[1] = lu->login_orb_dma;
  966. sbp2util_cpu_to_be32_buffer(data, 8);
  967. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  968. /* wait up to 20 seconds for login status */
  969. if (sbp2util_access_timeout(lu, 20*HZ)) {
  970. SBP2_ERR("Error logging into SBP-2 device - timed out");
  971. return -EIO;
  972. }
  973. /* make sure that the returned status matches the login ORB */
  974. if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
  975. SBP2_ERR("Error logging into SBP-2 device - timed out");
  976. return -EIO;
  977. }
  978. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  979. SBP2_ERR("Error logging into SBP-2 device - failed");
  980. return -EIO;
  981. }
  982. sbp2util_cpu_to_be32_buffer(lu->login_response,
  983. sizeof(struct sbp2_login_response));
  984. lu->command_block_agent_addr =
  985. ((u64)lu->login_response->command_block_agent_hi) << 32;
  986. lu->command_block_agent_addr |=
  987. ((u64)lu->login_response->command_block_agent_lo);
  988. lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
  989. SBP2_INFO("Logged into SBP-2 device");
  990. return 0;
  991. }
  992. static int sbp2_logout_device(struct sbp2_lu *lu)
  993. {
  994. struct sbp2_fwhost_info *hi = lu->hi;
  995. quadlet_t data[2];
  996. int error;
  997. lu->logout_orb->reserved1 = 0x0;
  998. lu->logout_orb->reserved2 = 0x0;
  999. lu->logout_orb->reserved3 = 0x0;
  1000. lu->logout_orb->reserved4 = 0x0;
  1001. lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1002. lu->logout_orb->login_ID_misc |=
  1003. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1004. lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1005. lu->logout_orb->reserved5 = 0x0;
  1006. lu->logout_orb->status_fifo_hi =
  1007. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1008. lu->logout_orb->status_fifo_lo =
  1009. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1010. sbp2util_cpu_to_be32_buffer(lu->logout_orb,
  1011. sizeof(struct sbp2_logout_orb));
  1012. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1013. data[1] = lu->logout_orb_dma;
  1014. sbp2util_cpu_to_be32_buffer(data, 8);
  1015. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1016. if (error)
  1017. return error;
  1018. /* wait up to 1 second for the device to complete logout */
  1019. if (sbp2util_access_timeout(lu, HZ))
  1020. return -EIO;
  1021. SBP2_INFO("Logged out of SBP-2 device");
  1022. return 0;
  1023. }
  1024. static int sbp2_reconnect_device(struct sbp2_lu *lu)
  1025. {
  1026. struct sbp2_fwhost_info *hi = lu->hi;
  1027. quadlet_t data[2];
  1028. int error;
  1029. lu->reconnect_orb->reserved1 = 0x0;
  1030. lu->reconnect_orb->reserved2 = 0x0;
  1031. lu->reconnect_orb->reserved3 = 0x0;
  1032. lu->reconnect_orb->reserved4 = 0x0;
  1033. lu->reconnect_orb->login_ID_misc =
  1034. ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1035. lu->reconnect_orb->login_ID_misc |=
  1036. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1037. lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1038. lu->reconnect_orb->reserved5 = 0x0;
  1039. lu->reconnect_orb->status_fifo_hi =
  1040. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1041. lu->reconnect_orb->status_fifo_lo =
  1042. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1043. sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
  1044. sizeof(struct sbp2_reconnect_orb));
  1045. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1046. data[1] = lu->reconnect_orb_dma;
  1047. sbp2util_cpu_to_be32_buffer(data, 8);
  1048. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1049. if (error)
  1050. return error;
  1051. /* wait up to 1 second for reconnect status */
  1052. if (sbp2util_access_timeout(lu, HZ)) {
  1053. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1054. return -EIO;
  1055. }
  1056. /* make sure that the returned status matches the reconnect ORB */
  1057. if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
  1058. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1059. return -EIO;
  1060. }
  1061. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  1062. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1063. return -EIO;
  1064. }
  1065. SBP2_INFO("Reconnected to SBP-2 device");
  1066. return 0;
  1067. }
  1068. /*
  1069. * Set the target node's Single Phase Retry limit. Affects the target's retry
  1070. * behaviour if our node is too busy to accept requests.
  1071. */
  1072. static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
  1073. {
  1074. quadlet_t data;
  1075. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1076. if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1077. SBP2_ERR("%s error", __FUNCTION__);
  1078. return 0;
  1079. }
  1080. static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
  1081. struct unit_directory *ud)
  1082. {
  1083. struct csr1212_keyval *kv;
  1084. struct csr1212_dentry *dentry;
  1085. u64 management_agent_addr;
  1086. u32 unit_characteristics, firmware_revision;
  1087. unsigned workarounds;
  1088. int i;
  1089. management_agent_addr = 0;
  1090. unit_characteristics = 0;
  1091. firmware_revision = 0;
  1092. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1093. switch (kv->key.id) {
  1094. case CSR1212_KV_ID_DEPENDENT_INFO:
  1095. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
  1096. management_agent_addr =
  1097. CSR1212_REGISTER_SPACE_BASE +
  1098. (kv->value.csr_offset << 2);
  1099. else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
  1100. lu->lun = ORB_SET_LUN(kv->value.immediate);
  1101. break;
  1102. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1103. /* FIXME: This is ignored so far.
  1104. * See SBP-2 clause 7.4.8. */
  1105. unit_characteristics = kv->value.immediate;
  1106. break;
  1107. case SBP2_FIRMWARE_REVISION_KEY:
  1108. firmware_revision = kv->value.immediate;
  1109. break;
  1110. default:
  1111. /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
  1112. * Its "ordered" bit has consequences for command ORB
  1113. * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
  1114. break;
  1115. }
  1116. }
  1117. workarounds = sbp2_default_workarounds;
  1118. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1119. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1120. if (sbp2_workarounds_table[i].firmware_revision &&
  1121. sbp2_workarounds_table[i].firmware_revision !=
  1122. (firmware_revision & 0xffff00))
  1123. continue;
  1124. if (sbp2_workarounds_table[i].model_id &&
  1125. sbp2_workarounds_table[i].model_id != ud->model_id)
  1126. continue;
  1127. workarounds |= sbp2_workarounds_table[i].workarounds;
  1128. break;
  1129. }
  1130. if (workarounds)
  1131. SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
  1132. "(firmware_revision 0x%06x, vendor_id 0x%06x,"
  1133. " model_id 0x%06x)",
  1134. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1135. workarounds, firmware_revision,
  1136. ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
  1137. ud->model_id);
  1138. /* We would need one SCSI host template for each target to adjust
  1139. * max_sectors on the fly, therefore warn only. */
  1140. if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1141. (sbp2_max_sectors * 512) > (128 * 1024))
  1142. SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
  1143. "max transfer size. WARNING: Current max_sectors "
  1144. "setting is larger than 128KB (%d sectors)",
  1145. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1146. sbp2_max_sectors);
  1147. /* If this is a logical unit directory entry, process the parent
  1148. * to get the values. */
  1149. if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
  1150. struct unit_directory *parent_ud = container_of(
  1151. ud->device.parent, struct unit_directory, device);
  1152. sbp2_parse_unit_directory(lu, parent_ud);
  1153. } else {
  1154. lu->management_agent_addr = management_agent_addr;
  1155. lu->workarounds = workarounds;
  1156. if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
  1157. lu->lun = ORB_SET_LUN(ud->lun);
  1158. }
  1159. }
  1160. #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
  1161. /*
  1162. * This function is called in order to determine the max speed and packet
  1163. * size we can use in our ORBs. Note, that we (the driver and host) only
  1164. * initiate the transaction. The SBP-2 device actually transfers the data
  1165. * (by reading from the DMA area we tell it). This means that the SBP-2
  1166. * device decides the actual maximum data it can transfer. We just tell it
  1167. * the speed that it needs to use, and the max_rec the host supports, and
  1168. * it takes care of the rest.
  1169. */
  1170. static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
  1171. {
  1172. struct sbp2_fwhost_info *hi = lu->hi;
  1173. u8 payload;
  1174. lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
  1175. if (lu->speed_code > sbp2_max_speed) {
  1176. lu->speed_code = sbp2_max_speed;
  1177. SBP2_INFO("Reducing speed to %s",
  1178. hpsb_speedto_str[sbp2_max_speed]);
  1179. }
  1180. /* Payload size is the lesser of what our speed supports and what
  1181. * our host supports. */
  1182. payload = min(sbp2_speedto_max_payload[lu->speed_code],
  1183. (u8) (hi->host->csr.max_rec - 1));
  1184. /* If physical DMA is off, work around limitation in ohci1394:
  1185. * packet size must not exceed PAGE_SIZE */
  1186. if (lu->ne->host->low_addr_space < (1ULL << 32))
  1187. while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
  1188. payload)
  1189. payload--;
  1190. SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
  1191. NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
  1192. hpsb_speedto_str[lu->speed_code],
  1193. SBP2_PAYLOAD_TO_BYTES(payload));
  1194. lu->max_payload_size = payload;
  1195. return 0;
  1196. }
  1197. static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
  1198. {
  1199. quadlet_t data;
  1200. u64 addr;
  1201. int retval;
  1202. unsigned long flags;
  1203. /* cancel_delayed_work(&lu->protocol_work); */
  1204. if (wait)
  1205. flush_scheduled_work();
  1206. data = ntohl(SBP2_AGENT_RESET_DATA);
  1207. addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
  1208. if (wait)
  1209. retval = hpsb_node_write(lu->ne, addr, &data, 4);
  1210. else
  1211. retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
  1212. if (retval < 0) {
  1213. SBP2_ERR("hpsb_node_write failed.\n");
  1214. return -EIO;
  1215. }
  1216. /* make sure that the ORB_POINTER is written on next command */
  1217. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1218. lu->last_orb = NULL;
  1219. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1220. return 0;
  1221. }
  1222. static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
  1223. struct sbp2_fwhost_info *hi,
  1224. struct sbp2_command_info *cmd,
  1225. unsigned int scsi_use_sg,
  1226. struct scatterlist *sgpnt,
  1227. u32 orb_direction,
  1228. enum dma_data_direction dma_dir)
  1229. {
  1230. cmd->dma_dir = dma_dir;
  1231. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1232. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1233. /* special case if only one element (and less than 64KB in size) */
  1234. if ((scsi_use_sg == 1) &&
  1235. (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
  1236. cmd->dma_size = sgpnt[0].length;
  1237. cmd->dma_type = CMD_DMA_PAGE;
  1238. cmd->cmd_dma = pci_map_page(hi->host->pdev,
  1239. sgpnt[0].page, sgpnt[0].offset,
  1240. cmd->dma_size, cmd->dma_dir);
  1241. orb->data_descriptor_lo = cmd->cmd_dma;
  1242. orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
  1243. } else {
  1244. struct sbp2_unrestricted_page_table *sg_element =
  1245. &cmd->scatter_gather_element[0];
  1246. u32 sg_count, sg_len;
  1247. dma_addr_t sg_addr;
  1248. int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
  1249. dma_dir);
  1250. cmd->dma_size = scsi_use_sg;
  1251. cmd->sge_buffer = sgpnt;
  1252. /* use page tables (s/g) */
  1253. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1254. orb->data_descriptor_lo = cmd->sge_dma;
  1255. /* loop through and fill out our SBP-2 page tables
  1256. * (and split up anything too large) */
  1257. for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
  1258. sg_len = sg_dma_len(sgpnt);
  1259. sg_addr = sg_dma_address(sgpnt);
  1260. while (sg_len) {
  1261. sg_element[sg_count].segment_base_lo = sg_addr;
  1262. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1263. sg_element[sg_count].length_segment_base_hi =
  1264. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1265. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1266. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1267. } else {
  1268. sg_element[sg_count].length_segment_base_hi =
  1269. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1270. sg_len = 0;
  1271. }
  1272. sg_count++;
  1273. }
  1274. }
  1275. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1276. sbp2util_cpu_to_be32_buffer(sg_element,
  1277. (sizeof(struct sbp2_unrestricted_page_table)) *
  1278. sg_count);
  1279. }
  1280. }
  1281. static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
  1282. struct sbp2_fwhost_info *hi,
  1283. struct sbp2_command_info *cmd,
  1284. struct scatterlist *sgpnt,
  1285. u32 orb_direction,
  1286. unsigned int scsi_request_bufflen,
  1287. void *scsi_request_buffer,
  1288. enum dma_data_direction dma_dir)
  1289. {
  1290. cmd->dma_dir = dma_dir;
  1291. cmd->dma_size = scsi_request_bufflen;
  1292. cmd->dma_type = CMD_DMA_SINGLE;
  1293. cmd->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
  1294. cmd->dma_size, cmd->dma_dir);
  1295. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1296. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1297. /* handle case where we get a command w/o s/g enabled
  1298. * (but check for transfers larger than 64K) */
  1299. if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
  1300. orb->data_descriptor_lo = cmd->cmd_dma;
  1301. orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
  1302. } else {
  1303. /* The buffer is too large. Turn this into page tables. */
  1304. struct sbp2_unrestricted_page_table *sg_element =
  1305. &cmd->scatter_gather_element[0];
  1306. u32 sg_count, sg_len;
  1307. dma_addr_t sg_addr;
  1308. orb->data_descriptor_lo = cmd->sge_dma;
  1309. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1310. /* fill out our SBP-2 page tables; split up the large buffer */
  1311. sg_count = 0;
  1312. sg_len = scsi_request_bufflen;
  1313. sg_addr = cmd->cmd_dma;
  1314. while (sg_len) {
  1315. sg_element[sg_count].segment_base_lo = sg_addr;
  1316. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1317. sg_element[sg_count].length_segment_base_hi =
  1318. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1319. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1320. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1321. } else {
  1322. sg_element[sg_count].length_segment_base_hi =
  1323. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1324. sg_len = 0;
  1325. }
  1326. sg_count++;
  1327. }
  1328. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1329. sbp2util_cpu_to_be32_buffer(sg_element,
  1330. (sizeof(struct sbp2_unrestricted_page_table)) *
  1331. sg_count);
  1332. }
  1333. }
  1334. static void sbp2_create_command_orb(struct sbp2_lu *lu,
  1335. struct sbp2_command_info *cmd,
  1336. unchar *scsi_cmd,
  1337. unsigned int scsi_use_sg,
  1338. unsigned int scsi_request_bufflen,
  1339. void *scsi_request_buffer,
  1340. enum dma_data_direction dma_dir)
  1341. {
  1342. struct sbp2_fwhost_info *hi = lu->hi;
  1343. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1344. struct sbp2_command_orb *orb = &cmd->command_orb;
  1345. u32 orb_direction;
  1346. /*
  1347. * Set-up our command ORB.
  1348. *
  1349. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1350. * best performance (at least with the devices I have). This means
  1351. * that data_size becomes the number of s/g elements, and
  1352. * page_size should be zero (for unrestricted).
  1353. */
  1354. orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1355. orb->next_ORB_lo = 0x0;
  1356. orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
  1357. orb->misc |= ORB_SET_SPEED(lu->speed_code);
  1358. orb->misc |= ORB_SET_NOTIFY(1);
  1359. if (dma_dir == DMA_NONE)
  1360. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1361. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1362. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1363. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1364. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1365. else {
  1366. SBP2_INFO("Falling back to DMA_NONE");
  1367. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1368. }
  1369. /* set up our page table stuff */
  1370. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1371. orb->data_descriptor_hi = 0x0;
  1372. orb->data_descriptor_lo = 0x0;
  1373. orb->misc |= ORB_SET_DIRECTION(1);
  1374. } else if (scsi_use_sg)
  1375. sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
  1376. orb_direction, dma_dir);
  1377. else
  1378. sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
  1379. scsi_request_bufflen,
  1380. scsi_request_buffer, dma_dir);
  1381. sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
  1382. memset(orb->cdb, 0, 12);
  1383. memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1384. }
  1385. static void sbp2_link_orb_command(struct sbp2_lu *lu,
  1386. struct sbp2_command_info *cmd)
  1387. {
  1388. struct sbp2_fwhost_info *hi = lu->hi;
  1389. struct sbp2_command_orb *last_orb;
  1390. dma_addr_t last_orb_dma;
  1391. u64 addr = lu->command_block_agent_addr;
  1392. quadlet_t data[2];
  1393. size_t length;
  1394. unsigned long flags;
  1395. pci_dma_sync_single_for_device(hi->host->pdev, cmd->command_orb_dma,
  1396. sizeof(struct sbp2_command_orb),
  1397. PCI_DMA_TODEVICE);
  1398. pci_dma_sync_single_for_device(hi->host->pdev, cmd->sge_dma,
  1399. sizeof(cmd->scatter_gather_element),
  1400. PCI_DMA_BIDIRECTIONAL);
  1401. /* check to see if there are any previous orbs to use */
  1402. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1403. last_orb = lu->last_orb;
  1404. last_orb_dma = lu->last_orb_dma;
  1405. if (!last_orb) {
  1406. /*
  1407. * last_orb == NULL means: We know that the target's fetch agent
  1408. * is not active right now.
  1409. */
  1410. addr += SBP2_ORB_POINTER_OFFSET;
  1411. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1412. data[1] = cmd->command_orb_dma;
  1413. sbp2util_cpu_to_be32_buffer(data, 8);
  1414. length = 8;
  1415. } else {
  1416. /*
  1417. * last_orb != NULL means: We know that the target's fetch agent
  1418. * is (very probably) not dead or in reset state right now.
  1419. * We have an ORB already sent that we can append a new one to.
  1420. * The target's fetch agent may or may not have read this
  1421. * previous ORB yet.
  1422. */
  1423. pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
  1424. sizeof(struct sbp2_command_orb),
  1425. PCI_DMA_TODEVICE);
  1426. last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
  1427. wmb();
  1428. /* Tells hardware that this pointer is valid */
  1429. last_orb->next_ORB_hi = 0;
  1430. pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
  1431. sizeof(struct sbp2_command_orb),
  1432. PCI_DMA_TODEVICE);
  1433. addr += SBP2_DOORBELL_OFFSET;
  1434. data[0] = 0;
  1435. length = 4;
  1436. }
  1437. lu->last_orb = &cmd->command_orb;
  1438. lu->last_orb_dma = cmd->command_orb_dma;
  1439. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1440. if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
  1441. /*
  1442. * sbp2util_node_write_no_wait failed. We certainly ran out
  1443. * of transaction labels, perhaps just because there were no
  1444. * context switches which gave khpsbpkt a chance to collect
  1445. * free tlabels. Try again in non-atomic context. If necessary,
  1446. * the workqueue job will sleep to guaranteedly get a tlabel.
  1447. * We do not accept new commands until the job is over.
  1448. */
  1449. scsi_block_requests(lu->shost);
  1450. PREPARE_WORK(&lu->protocol_work,
  1451. last_orb ? sbp2util_write_doorbell:
  1452. sbp2util_write_orb_pointer
  1453. /* */);
  1454. schedule_work(&lu->protocol_work);
  1455. }
  1456. }
  1457. static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
  1458. void (*done)(struct scsi_cmnd *))
  1459. {
  1460. unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
  1461. unsigned int request_bufflen = SCpnt->request_bufflen;
  1462. struct sbp2_command_info *cmd;
  1463. cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
  1464. if (!cmd)
  1465. return -EIO;
  1466. sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
  1467. request_bufflen, SCpnt->request_buffer,
  1468. SCpnt->sc_data_direction);
  1469. sbp2_link_orb_command(lu, cmd);
  1470. return 0;
  1471. }
  1472. /*
  1473. * Translates SBP-2 status into SCSI sense data for check conditions
  1474. */
  1475. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
  1476. unchar *sense_data)
  1477. {
  1478. /* OK, it's pretty ugly... ;-) */
  1479. sense_data[0] = 0x70;
  1480. sense_data[1] = 0x0;
  1481. sense_data[2] = sbp2_status[9];
  1482. sense_data[3] = sbp2_status[12];
  1483. sense_data[4] = sbp2_status[13];
  1484. sense_data[5] = sbp2_status[14];
  1485. sense_data[6] = sbp2_status[15];
  1486. sense_data[7] = 10;
  1487. sense_data[8] = sbp2_status[16];
  1488. sense_data[9] = sbp2_status[17];
  1489. sense_data[10] = sbp2_status[18];
  1490. sense_data[11] = sbp2_status[19];
  1491. sense_data[12] = sbp2_status[10];
  1492. sense_data[13] = sbp2_status[11];
  1493. sense_data[14] = sbp2_status[20];
  1494. sense_data[15] = sbp2_status[21];
  1495. return sbp2_status[8] & 0x3f;
  1496. }
  1497. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1498. int destid, quadlet_t *data, u64 addr,
  1499. size_t length, u16 fl)
  1500. {
  1501. struct sbp2_fwhost_info *hi;
  1502. struct sbp2_lu *lu = NULL, *lu_tmp;
  1503. struct scsi_cmnd *SCpnt = NULL;
  1504. struct sbp2_status_block *sb;
  1505. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1506. struct sbp2_command_info *cmd;
  1507. unsigned long flags;
  1508. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1509. SBP2_ERR("Wrong size of status block");
  1510. return RCODE_ADDRESS_ERROR;
  1511. }
  1512. if (unlikely(!host)) {
  1513. SBP2_ERR("host is NULL - this is bad!");
  1514. return RCODE_ADDRESS_ERROR;
  1515. }
  1516. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1517. if (unlikely(!hi)) {
  1518. SBP2_ERR("host info is NULL - this is bad!");
  1519. return RCODE_ADDRESS_ERROR;
  1520. }
  1521. /* Find the unit which wrote the status. */
  1522. list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
  1523. if (lu_tmp->ne->nodeid == nodeid &&
  1524. lu_tmp->status_fifo_addr == addr) {
  1525. lu = lu_tmp;
  1526. break;
  1527. }
  1528. }
  1529. if (unlikely(!lu)) {
  1530. SBP2_ERR("lu is NULL - device is gone?");
  1531. return RCODE_ADDRESS_ERROR;
  1532. }
  1533. /* Put response into lu status fifo buffer. The first two bytes
  1534. * come in big endian bit order. Often the target writes only a
  1535. * truncated status block, minimally the first two quadlets. The rest
  1536. * is implied to be zeros. */
  1537. sb = &lu->status_block;
  1538. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1539. memcpy(sb, data, length);
  1540. sbp2util_be32_to_cpu_buffer(sb, 8);
  1541. /* Ignore unsolicited status. Handle command ORB status. */
  1542. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1543. cmd = NULL;
  1544. else
  1545. cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
  1546. if (cmd) {
  1547. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1548. cmd->command_orb_dma,
  1549. sizeof(struct sbp2_command_orb),
  1550. PCI_DMA_TODEVICE);
  1551. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1552. cmd->sge_dma,
  1553. sizeof(cmd->scatter_gather_element),
  1554. PCI_DMA_BIDIRECTIONAL);
  1555. /* Grab SCSI command pointers and check status. */
  1556. /*
  1557. * FIXME: If the src field in the status is 1, the ORB DMA must
  1558. * not be reused until status for a subsequent ORB is received.
  1559. */
  1560. SCpnt = cmd->Current_SCpnt;
  1561. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1562. sbp2util_mark_command_completed(lu, cmd);
  1563. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1564. if (SCpnt) {
  1565. u32 h = sb->ORB_offset_hi_misc;
  1566. u32 r = STATUS_GET_RESP(h);
  1567. if (r != RESP_STATUS_REQUEST_COMPLETE) {
  1568. SBP2_INFO("resp 0x%x, sbp_status 0x%x",
  1569. r, STATUS_GET_SBP_STATUS(h));
  1570. scsi_status =
  1571. r == RESP_STATUS_TRANSPORT_FAILURE ?
  1572. SBP2_SCSI_STATUS_BUSY :
  1573. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1574. }
  1575. if (STATUS_GET_LEN(h) > 1)
  1576. scsi_status = sbp2_status_to_sense_data(
  1577. (unchar *)sb, SCpnt->sense_buffer);
  1578. if (STATUS_TEST_DEAD(h))
  1579. sbp2_agent_reset(lu, 0);
  1580. }
  1581. /* Check here to see if there are no commands in-use. If there
  1582. * are none, we know that the fetch agent left the active state
  1583. * _and_ that we did not reactivate it yet. Therefore clear
  1584. * last_orb so that next time we write directly to the
  1585. * ORB_POINTER register. That way the fetch agent does not need
  1586. * to refetch the next_ORB. */
  1587. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1588. if (list_empty(&lu->cmd_orb_inuse))
  1589. lu->last_orb = NULL;
  1590. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1591. } else {
  1592. /* It's probably status after a management request. */
  1593. if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
  1594. (sb->ORB_offset_lo == lu->login_orb_dma) ||
  1595. (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
  1596. (sb->ORB_offset_lo == lu->logout_orb_dma)) {
  1597. lu->access_complete = 1;
  1598. wake_up_interruptible(&sbp2_access_wq);
  1599. }
  1600. }
  1601. if (SCpnt)
  1602. sbp2scsi_complete_command(lu, scsi_status, SCpnt,
  1603. cmd->Current_done);
  1604. return RCODE_COMPLETE;
  1605. }
  1606. /**************************************
  1607. * SCSI interface related section
  1608. **************************************/
  1609. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1610. void (*done)(struct scsi_cmnd *))
  1611. {
  1612. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1613. struct sbp2_fwhost_info *hi;
  1614. int result = DID_NO_CONNECT << 16;
  1615. if (unlikely(!sbp2util_node_is_available(lu)))
  1616. goto done;
  1617. hi = lu->hi;
  1618. if (unlikely(!hi)) {
  1619. SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
  1620. goto done;
  1621. }
  1622. /* Multiple units are currently represented to the SCSI core as separate
  1623. * targets, not as one target with multiple LUs. Therefore return
  1624. * selection time-out to any IO directed at non-zero LUNs. */
  1625. if (unlikely(SCpnt->device->lun))
  1626. goto done;
  1627. /* handle the request sense command here (auto-request sense) */
  1628. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1629. memcpy(SCpnt->request_buffer, SCpnt->sense_buffer,
  1630. SCpnt->request_bufflen);
  1631. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  1632. sbp2scsi_complete_command(lu, SBP2_SCSI_STATUS_GOOD, SCpnt,
  1633. done);
  1634. return 0;
  1635. }
  1636. if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
  1637. SBP2_ERR("Bus reset in progress - rejecting command");
  1638. result = DID_BUS_BUSY << 16;
  1639. goto done;
  1640. }
  1641. /* Bidirectional commands are not yet implemented,
  1642. * and unknown transfer direction not handled. */
  1643. if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
  1644. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  1645. result = DID_ERROR << 16;
  1646. goto done;
  1647. }
  1648. if (sbp2_send_command(lu, SCpnt, done)) {
  1649. SBP2_ERR("Error sending SCSI command");
  1650. sbp2scsi_complete_command(lu,
  1651. SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  1652. SCpnt, done);
  1653. }
  1654. return 0;
  1655. done:
  1656. SCpnt->result = result;
  1657. done(SCpnt);
  1658. return 0;
  1659. }
  1660. static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
  1661. {
  1662. struct sbp2_fwhost_info *hi = lu->hi;
  1663. struct list_head *lh;
  1664. struct sbp2_command_info *cmd;
  1665. unsigned long flags;
  1666. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1667. while (!list_empty(&lu->cmd_orb_inuse)) {
  1668. lh = lu->cmd_orb_inuse.next;
  1669. cmd = list_entry(lh, struct sbp2_command_info, list);
  1670. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1671. cmd->command_orb_dma,
  1672. sizeof(struct sbp2_command_orb),
  1673. PCI_DMA_TODEVICE);
  1674. pci_dma_sync_single_for_cpu(hi->host->pdev, cmd->sge_dma,
  1675. sizeof(cmd->scatter_gather_element),
  1676. PCI_DMA_BIDIRECTIONAL);
  1677. sbp2util_mark_command_completed(lu, cmd);
  1678. if (cmd->Current_SCpnt) {
  1679. cmd->Current_SCpnt->result = status << 16;
  1680. cmd->Current_done(cmd->Current_SCpnt);
  1681. }
  1682. }
  1683. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1684. return;
  1685. }
  1686. /*
  1687. * Complete a regular SCSI command. Can be called in atomic context.
  1688. */
  1689. static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
  1690. struct scsi_cmnd *SCpnt,
  1691. void (*done)(struct scsi_cmnd *))
  1692. {
  1693. if (!SCpnt) {
  1694. SBP2_ERR("SCpnt is NULL");
  1695. return;
  1696. }
  1697. switch (scsi_status) {
  1698. case SBP2_SCSI_STATUS_GOOD:
  1699. SCpnt->result = DID_OK << 16;
  1700. break;
  1701. case SBP2_SCSI_STATUS_BUSY:
  1702. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  1703. SCpnt->result = DID_BUS_BUSY << 16;
  1704. break;
  1705. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  1706. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  1707. break;
  1708. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  1709. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  1710. SCpnt->result = DID_NO_CONNECT << 16;
  1711. scsi_print_command(SCpnt);
  1712. break;
  1713. case SBP2_SCSI_STATUS_CONDITION_MET:
  1714. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  1715. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  1716. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  1717. SCpnt->result = DID_ERROR << 16;
  1718. scsi_print_command(SCpnt);
  1719. break;
  1720. default:
  1721. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  1722. SCpnt->result = DID_ERROR << 16;
  1723. }
  1724. /* If a bus reset is in progress and there was an error, complete
  1725. * the command as busy so that it will get retried. */
  1726. if (!hpsb_node_entry_valid(lu->ne)
  1727. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  1728. SBP2_ERR("Completing command with busy (bus reset)");
  1729. SCpnt->result = DID_BUS_BUSY << 16;
  1730. }
  1731. /* Tell the SCSI stack that we're done with this command. */
  1732. done(SCpnt);
  1733. }
  1734. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  1735. {
  1736. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1737. lu->sdev = sdev;
  1738. sdev->allow_restart = 1;
  1739. if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  1740. sdev->inquiry_len = 36;
  1741. return 0;
  1742. }
  1743. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  1744. {
  1745. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1746. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  1747. sdev->use_10_for_rw = 1;
  1748. if (sdev->type == TYPE_DISK &&
  1749. lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  1750. sdev->skip_ms_page_8 = 1;
  1751. if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  1752. sdev->fix_capacity = 1;
  1753. return 0;
  1754. }
  1755. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  1756. {
  1757. ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
  1758. return;
  1759. }
  1760. /*
  1761. * Called by scsi stack when something has really gone wrong.
  1762. * Usually called when a command has timed-out for some reason.
  1763. */
  1764. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  1765. {
  1766. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1767. struct sbp2_fwhost_info *hi = lu->hi;
  1768. struct sbp2_command_info *cmd;
  1769. unsigned long flags;
  1770. SBP2_INFO("aborting sbp2 command");
  1771. scsi_print_command(SCpnt);
  1772. if (sbp2util_node_is_available(lu)) {
  1773. sbp2_agent_reset(lu, 1);
  1774. /* Return a matching command structure to the free pool. */
  1775. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1776. cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
  1777. if (cmd) {
  1778. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1779. cmd->command_orb_dma,
  1780. sizeof(struct sbp2_command_orb),
  1781. PCI_DMA_TODEVICE);
  1782. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1783. cmd->sge_dma,
  1784. sizeof(cmd->scatter_gather_element),
  1785. PCI_DMA_BIDIRECTIONAL);
  1786. sbp2util_mark_command_completed(lu, cmd);
  1787. if (cmd->Current_SCpnt) {
  1788. cmd->Current_SCpnt->result = DID_ABORT << 16;
  1789. cmd->Current_done(cmd->Current_SCpnt);
  1790. }
  1791. }
  1792. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1793. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  1794. }
  1795. return SUCCESS;
  1796. }
  1797. /*
  1798. * Called by scsi stack when something has really gone wrong.
  1799. */
  1800. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  1801. {
  1802. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1803. SBP2_INFO("reset requested");
  1804. if (sbp2util_node_is_available(lu)) {
  1805. SBP2_INFO("generating sbp2 fetch agent reset");
  1806. sbp2_agent_reset(lu, 1);
  1807. }
  1808. return SUCCESS;
  1809. }
  1810. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  1811. struct device_attribute *attr,
  1812. char *buf)
  1813. {
  1814. struct scsi_device *sdev;
  1815. struct sbp2_lu *lu;
  1816. if (!(sdev = to_scsi_device(dev)))
  1817. return 0;
  1818. if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
  1819. return 0;
  1820. return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)lu->ne->guid,
  1821. lu->ud->id, ORB_SET_LUN(lu->lun));
  1822. }
  1823. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  1824. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  1825. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  1826. MODULE_LICENSE("GPL");
  1827. static int sbp2_module_init(void)
  1828. {
  1829. int ret;
  1830. if (sbp2_serialize_io) {
  1831. sbp2_shost_template.can_queue = 1;
  1832. sbp2_shost_template.cmd_per_lun = 1;
  1833. }
  1834. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1835. (sbp2_max_sectors * 512) > (128 * 1024))
  1836. sbp2_max_sectors = 128 * 1024 / 512;
  1837. sbp2_shost_template.max_sectors = sbp2_max_sectors;
  1838. hpsb_register_highlevel(&sbp2_highlevel);
  1839. ret = hpsb_register_protocol(&sbp2_driver);
  1840. if (ret) {
  1841. SBP2_ERR("Failed to register protocol");
  1842. hpsb_unregister_highlevel(&sbp2_highlevel);
  1843. return ret;
  1844. }
  1845. return 0;
  1846. }
  1847. static void __exit sbp2_module_exit(void)
  1848. {
  1849. hpsb_unregister_protocol(&sbp2_driver);
  1850. hpsb_unregister_highlevel(&sbp2_highlevel);
  1851. }
  1852. module_init(sbp2_module_init);
  1853. module_exit(sbp2_module_exit);