sbp2.c 63 KB

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