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