sbp2.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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,
  440. &cmd->command_orb,
  441. sizeof(struct sbp2_command_orb),
  442. DMA_TO_DEVICE);
  443. cmd->sge_dma = dma_map_single(&hi->host->device,
  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, cmd->command_orb_dma,
  464. sizeof(struct sbp2_command_orb),
  465. DMA_TO_DEVICE);
  466. dma_unmap_single(&host->device, cmd->sge_dma,
  467. sizeof(cmd->scatter_gather_element),
  468. DMA_BIDIRECTIONAL);
  469. kfree(cmd);
  470. }
  471. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  472. return;
  473. }
  474. /*
  475. * Finds the sbp2_command for a given outstanding command ORB.
  476. * Only looks at the in-use list.
  477. */
  478. static struct sbp2_command_info *sbp2util_find_command_for_orb(
  479. struct sbp2_lu *lu, dma_addr_t orb)
  480. {
  481. struct sbp2_command_info *cmd;
  482. unsigned long flags;
  483. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  484. if (!list_empty(&lu->cmd_orb_inuse))
  485. list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
  486. if (cmd->command_orb_dma == orb) {
  487. spin_unlock_irqrestore(
  488. &lu->cmd_orb_lock, flags);
  489. return cmd;
  490. }
  491. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  492. return NULL;
  493. }
  494. /*
  495. * Finds the sbp2_command for a given outstanding SCpnt.
  496. * Only looks at the in-use list.
  497. * Must be called with lu->cmd_orb_lock held.
  498. */
  499. static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
  500. struct sbp2_lu *lu, void *SCpnt)
  501. {
  502. struct sbp2_command_info *cmd;
  503. if (!list_empty(&lu->cmd_orb_inuse))
  504. list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
  505. if (cmd->Current_SCpnt == SCpnt)
  506. return cmd;
  507. return NULL;
  508. }
  509. static struct sbp2_command_info *sbp2util_allocate_command_orb(
  510. struct sbp2_lu *lu,
  511. struct scsi_cmnd *Current_SCpnt,
  512. void (*Current_done)(struct scsi_cmnd *))
  513. {
  514. struct list_head *lh;
  515. struct sbp2_command_info *cmd = NULL;
  516. unsigned long flags;
  517. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  518. if (!list_empty(&lu->cmd_orb_completed)) {
  519. lh = lu->cmd_orb_completed.next;
  520. list_del(lh);
  521. cmd = list_entry(lh, struct sbp2_command_info, list);
  522. cmd->Current_done = Current_done;
  523. cmd->Current_SCpnt = Current_SCpnt;
  524. list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
  525. } else
  526. SBP2_ERR("%s: no orbs available", __FUNCTION__);
  527. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  528. return cmd;
  529. }
  530. /*
  531. * Unmaps the DMAs of a command and moves the command to the completed ORB list.
  532. * Must be called with lu->cmd_orb_lock held.
  533. */
  534. static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
  535. struct sbp2_command_info *cmd)
  536. {
  537. struct hpsb_host *host = lu->ud->ne->host;
  538. if (cmd->cmd_dma) {
  539. if (cmd->dma_type == CMD_DMA_SINGLE)
  540. dma_unmap_single(&host->device, cmd->cmd_dma,
  541. cmd->dma_size, cmd->dma_dir);
  542. else if (cmd->dma_type == CMD_DMA_PAGE)
  543. dma_unmap_page(&host->device, cmd->cmd_dma,
  544. cmd->dma_size, cmd->dma_dir);
  545. /* XXX: Check for CMD_DMA_NONE bug */
  546. cmd->dma_type = CMD_DMA_NONE;
  547. cmd->cmd_dma = 0;
  548. }
  549. if (cmd->sge_buffer) {
  550. dma_unmap_sg(&host->device, cmd->sge_buffer,
  551. cmd->dma_size, cmd->dma_dir);
  552. cmd->sge_buffer = NULL;
  553. }
  554. list_move_tail(&cmd->list, &lu->cmd_orb_completed);
  555. }
  556. /*
  557. * Is lu valid? Is the 1394 node still present?
  558. */
  559. static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
  560. {
  561. return lu && lu->ne && !lu->ne->in_limbo;
  562. }
  563. /*********************************************
  564. * IEEE-1394 core driver stack related section
  565. *********************************************/
  566. static int sbp2_probe(struct device *dev)
  567. {
  568. struct unit_directory *ud;
  569. struct sbp2_lu *lu;
  570. ud = container_of(dev, struct unit_directory, device);
  571. /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
  572. * instead. */
  573. if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
  574. return -ENODEV;
  575. lu = sbp2_alloc_device(ud);
  576. if (!lu)
  577. return -ENOMEM;
  578. sbp2_parse_unit_directory(lu, ud);
  579. return sbp2_start_device(lu);
  580. }
  581. static int sbp2_remove(struct device *dev)
  582. {
  583. struct unit_directory *ud;
  584. struct sbp2_lu *lu;
  585. struct scsi_device *sdev;
  586. ud = container_of(dev, struct unit_directory, device);
  587. lu = ud->device.driver_data;
  588. if (!lu)
  589. return 0;
  590. if (lu->shost) {
  591. /* Get rid of enqueued commands if there is no chance to
  592. * send them. */
  593. if (!sbp2util_node_is_available(lu))
  594. sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
  595. /* scsi_remove_device() may trigger shutdown functions of SCSI
  596. * highlevel drivers which would deadlock if blocked. */
  597. atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
  598. scsi_unblock_requests(lu->shost);
  599. }
  600. sdev = lu->sdev;
  601. if (sdev) {
  602. lu->sdev = NULL;
  603. scsi_remove_device(sdev);
  604. }
  605. sbp2_logout_device(lu);
  606. sbp2_remove_device(lu);
  607. return 0;
  608. }
  609. static int sbp2_update(struct unit_directory *ud)
  610. {
  611. struct sbp2_lu *lu = ud->device.driver_data;
  612. if (sbp2_reconnect_device(lu)) {
  613. /* Reconnect has failed. Perhaps we didn't reconnect fast
  614. * enough. Try a regular login, but first log out just in
  615. * case of any weirdness. */
  616. sbp2_logout_device(lu);
  617. if (sbp2_login_device(lu)) {
  618. /* Login failed too, just fail, and the backend
  619. * will call our sbp2_remove for us */
  620. SBP2_ERR("Failed to reconnect to sbp2 device!");
  621. return -EBUSY;
  622. }
  623. }
  624. sbp2_set_busy_timeout(lu);
  625. sbp2_agent_reset(lu, 1);
  626. sbp2_max_speed_and_size(lu);
  627. /* Complete any pending commands with busy (so they get retried)
  628. * and remove them from our queue. */
  629. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  630. /* Accept new commands unless there was another bus reset in the
  631. * meantime. */
  632. if (hpsb_node_entry_valid(lu->ne)) {
  633. atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
  634. scsi_unblock_requests(lu->shost);
  635. }
  636. return 0;
  637. }
  638. static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
  639. {
  640. struct sbp2_fwhost_info *hi;
  641. struct Scsi_Host *shost = NULL;
  642. struct sbp2_lu *lu = NULL;
  643. lu = kzalloc(sizeof(*lu), GFP_KERNEL);
  644. if (!lu) {
  645. SBP2_ERR("failed to create lu");
  646. goto failed_alloc;
  647. }
  648. lu->ne = ud->ne;
  649. lu->ud = ud;
  650. lu->speed_code = IEEE1394_SPEED_100;
  651. lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
  652. lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
  653. INIT_LIST_HEAD(&lu->cmd_orb_inuse);
  654. INIT_LIST_HEAD(&lu->cmd_orb_completed);
  655. INIT_LIST_HEAD(&lu->lu_list);
  656. spin_lock_init(&lu->cmd_orb_lock);
  657. atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
  658. INIT_WORK(&lu->protocol_work, NULL);
  659. ud->device.driver_data = lu;
  660. hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
  661. if (!hi) {
  662. hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
  663. sizeof(*hi));
  664. if (!hi) {
  665. SBP2_ERR("failed to allocate hostinfo");
  666. goto failed_alloc;
  667. }
  668. hi->host = ud->ne->host;
  669. INIT_LIST_HEAD(&hi->logical_units);
  670. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  671. /* Handle data movement if physical dma is not
  672. * enabled or not supported on host controller */
  673. if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
  674. &sbp2_physdma_ops,
  675. 0x0ULL, 0xfffffffcULL)) {
  676. SBP2_ERR("failed to register lower 4GB address range");
  677. goto failed_alloc;
  678. }
  679. #endif
  680. }
  681. /* Prevent unloading of the 1394 host */
  682. if (!try_module_get(hi->host->driver->owner)) {
  683. SBP2_ERR("failed to get a reference on 1394 host driver");
  684. goto failed_alloc;
  685. }
  686. lu->hi = hi;
  687. list_add_tail(&lu->lu_list, &hi->logical_units);
  688. /* Register the status FIFO address range. We could use the same FIFO
  689. * for targets at different nodes. However we need different FIFOs per
  690. * target in order to support multi-unit devices.
  691. * The FIFO is located out of the local host controller's physical range
  692. * but, if possible, within the posted write area. Status writes will
  693. * then be performed as unified transactions. This slightly reduces
  694. * bandwidth usage, and some Prolific based devices seem to require it.
  695. */
  696. lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  697. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  698. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  699. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  700. if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  701. SBP2_ERR("failed to allocate status FIFO address range");
  702. goto failed_alloc;
  703. }
  704. shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
  705. if (!shost) {
  706. SBP2_ERR("failed to register scsi host");
  707. goto failed_alloc;
  708. }
  709. shost->hostdata[0] = (unsigned long)lu;
  710. if (!scsi_add_host(shost, &ud->device)) {
  711. lu->shost = shost;
  712. return lu;
  713. }
  714. SBP2_ERR("failed to add scsi host");
  715. scsi_host_put(shost);
  716. failed_alloc:
  717. sbp2_remove_device(lu);
  718. return NULL;
  719. }
  720. static void sbp2_host_reset(struct hpsb_host *host)
  721. {
  722. struct sbp2_fwhost_info *hi;
  723. struct sbp2_lu *lu;
  724. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  725. if (!hi)
  726. return;
  727. list_for_each_entry(lu, &hi->logical_units, lu_list)
  728. if (likely(atomic_read(&lu->state) !=
  729. SBP2LU_STATE_IN_SHUTDOWN)) {
  730. atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
  731. scsi_block_requests(lu->shost);
  732. }
  733. }
  734. static int sbp2_start_device(struct sbp2_lu *lu)
  735. {
  736. struct sbp2_fwhost_info *hi = lu->hi;
  737. int error;
  738. lu->login_response = dma_alloc_coherent(&hi->host->device,
  739. sizeof(struct sbp2_login_response),
  740. &lu->login_response_dma, GFP_KERNEL);
  741. if (!lu->login_response)
  742. goto alloc_fail;
  743. lu->query_logins_orb = dma_alloc_coherent(&hi->host->device,
  744. sizeof(struct sbp2_query_logins_orb),
  745. &lu->query_logins_orb_dma, GFP_KERNEL);
  746. if (!lu->query_logins_orb)
  747. goto alloc_fail;
  748. lu->query_logins_response = dma_alloc_coherent(&hi->host->device,
  749. sizeof(struct sbp2_query_logins_response),
  750. &lu->query_logins_response_dma, GFP_KERNEL);
  751. if (!lu->query_logins_response)
  752. goto alloc_fail;
  753. lu->reconnect_orb = dma_alloc_coherent(&hi->host->device,
  754. sizeof(struct sbp2_reconnect_orb),
  755. &lu->reconnect_orb_dma, GFP_KERNEL);
  756. if (!lu->reconnect_orb)
  757. goto alloc_fail;
  758. lu->logout_orb = dma_alloc_coherent(&hi->host->device,
  759. sizeof(struct sbp2_logout_orb),
  760. &lu->logout_orb_dma, GFP_KERNEL);
  761. if (!lu->logout_orb)
  762. goto alloc_fail;
  763. lu->login_orb = dma_alloc_coherent(&hi->host->device,
  764. sizeof(struct sbp2_login_orb),
  765. &lu->login_orb_dma, GFP_KERNEL);
  766. if (!lu->login_orb)
  767. goto alloc_fail;
  768. if (sbp2util_create_command_orb_pool(lu)) {
  769. SBP2_ERR("sbp2util_create_command_orb_pool failed!");
  770. sbp2_remove_device(lu);
  771. return -ENOMEM;
  772. }
  773. /* Wait a second before trying to log in. Previously logged in
  774. * initiators need a chance to reconnect. */
  775. if (msleep_interruptible(1000)) {
  776. sbp2_remove_device(lu);
  777. return -EINTR;
  778. }
  779. if (sbp2_login_device(lu)) {
  780. sbp2_remove_device(lu);
  781. return -EBUSY;
  782. }
  783. sbp2_set_busy_timeout(lu);
  784. sbp2_agent_reset(lu, 1);
  785. sbp2_max_speed_and_size(lu);
  786. error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
  787. if (error) {
  788. SBP2_ERR("scsi_add_device failed");
  789. sbp2_logout_device(lu);
  790. sbp2_remove_device(lu);
  791. return error;
  792. }
  793. return 0;
  794. alloc_fail:
  795. SBP2_ERR("Could not allocate memory for lu");
  796. sbp2_remove_device(lu);
  797. return -ENOMEM;
  798. }
  799. static void sbp2_remove_device(struct sbp2_lu *lu)
  800. {
  801. struct sbp2_fwhost_info *hi;
  802. if (!lu)
  803. return;
  804. hi = lu->hi;
  805. if (lu->shost) {
  806. scsi_remove_host(lu->shost);
  807. scsi_host_put(lu->shost);
  808. }
  809. flush_scheduled_work();
  810. sbp2util_remove_command_orb_pool(lu);
  811. list_del(&lu->lu_list);
  812. if (lu->login_response)
  813. dma_free_coherent(&hi->host->device,
  814. sizeof(struct sbp2_login_response),
  815. lu->login_response,
  816. lu->login_response_dma);
  817. if (lu->login_orb)
  818. dma_free_coherent(&hi->host->device,
  819. sizeof(struct sbp2_login_orb),
  820. lu->login_orb,
  821. lu->login_orb_dma);
  822. if (lu->reconnect_orb)
  823. dma_free_coherent(&hi->host->device,
  824. sizeof(struct sbp2_reconnect_orb),
  825. lu->reconnect_orb,
  826. lu->reconnect_orb_dma);
  827. if (lu->logout_orb)
  828. dma_free_coherent(&hi->host->device,
  829. sizeof(struct sbp2_logout_orb),
  830. lu->logout_orb,
  831. lu->logout_orb_dma);
  832. if (lu->query_logins_orb)
  833. dma_free_coherent(&hi->host->device,
  834. sizeof(struct sbp2_query_logins_orb),
  835. lu->query_logins_orb,
  836. lu->query_logins_orb_dma);
  837. if (lu->query_logins_response)
  838. dma_free_coherent(&hi->host->device,
  839. sizeof(struct sbp2_query_logins_response),
  840. lu->query_logins_response,
  841. lu->query_logins_response_dma);
  842. if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  843. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  844. lu->status_fifo_addr);
  845. lu->ud->device.driver_data = NULL;
  846. if (hi)
  847. module_put(hi->host->driver->owner);
  848. kfree(lu);
  849. }
  850. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  851. /*
  852. * Deal with write requests on adapters which do not support physical DMA or
  853. * have it switched off.
  854. */
  855. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  856. int destid, quadlet_t *data, u64 addr,
  857. size_t length, u16 flags)
  858. {
  859. memcpy(bus_to_virt((u32) addr), data, length);
  860. return RCODE_COMPLETE;
  861. }
  862. /*
  863. * Deal with read requests on adapters which do not support physical DMA or
  864. * have it switched off.
  865. */
  866. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  867. quadlet_t *data, u64 addr, size_t length,
  868. u16 flags)
  869. {
  870. memcpy(data, bus_to_virt((u32) addr), length);
  871. return RCODE_COMPLETE;
  872. }
  873. #endif
  874. /**************************************
  875. * SBP-2 protocol related section
  876. **************************************/
  877. static int sbp2_query_logins(struct sbp2_lu *lu)
  878. {
  879. struct sbp2_fwhost_info *hi = lu->hi;
  880. quadlet_t data[2];
  881. int max_logins;
  882. int active_logins;
  883. lu->query_logins_orb->reserved1 = 0x0;
  884. lu->query_logins_orb->reserved2 = 0x0;
  885. lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
  886. lu->query_logins_orb->query_response_hi =
  887. ORB_SET_NODE_ID(hi->host->node_id);
  888. lu->query_logins_orb->lun_misc =
  889. ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  890. lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  891. lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  892. lu->query_logins_orb->reserved_resp_length =
  893. ORB_SET_QUERY_LOGINS_RESP_LENGTH(
  894. sizeof(struct sbp2_query_logins_response));
  895. lu->query_logins_orb->status_fifo_hi =
  896. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  897. lu->query_logins_orb->status_fifo_lo =
  898. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  899. sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
  900. sizeof(struct sbp2_query_logins_orb));
  901. memset(lu->query_logins_response, 0,
  902. sizeof(struct sbp2_query_logins_response));
  903. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  904. data[1] = lu->query_logins_orb_dma;
  905. sbp2util_cpu_to_be32_buffer(data, 8);
  906. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  907. if (sbp2util_access_timeout(lu, 2*HZ)) {
  908. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  909. return -EIO;
  910. }
  911. if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
  912. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  913. return -EIO;
  914. }
  915. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  916. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  917. return -EIO;
  918. }
  919. sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
  920. sizeof(struct sbp2_query_logins_response));
  921. max_logins = RESPONSE_GET_MAX_LOGINS(
  922. lu->query_logins_response->length_max_logins);
  923. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  924. active_logins = RESPONSE_GET_ACTIVE_LOGINS(
  925. lu->query_logins_response->length_max_logins);
  926. SBP2_INFO("Number of active logins: %d", active_logins);
  927. if (active_logins >= max_logins) {
  928. return -EIO;
  929. }
  930. return 0;
  931. }
  932. static int sbp2_login_device(struct sbp2_lu *lu)
  933. {
  934. struct sbp2_fwhost_info *hi = lu->hi;
  935. quadlet_t data[2];
  936. if (!lu->login_orb)
  937. return -EIO;
  938. if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
  939. SBP2_INFO("Device does not support any more concurrent logins");
  940. return -EIO;
  941. }
  942. /* assume no password */
  943. lu->login_orb->password_hi = 0;
  944. lu->login_orb->password_lo = 0;
  945. lu->login_orb->login_response_lo = lu->login_response_dma;
  946. lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  947. lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  948. /* one second reconnect time */
  949. lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
  950. lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
  951. lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
  952. lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  953. lu->login_orb->passwd_resp_lengths =
  954. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  955. lu->login_orb->status_fifo_hi =
  956. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  957. lu->login_orb->status_fifo_lo =
  958. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  959. sbp2util_cpu_to_be32_buffer(lu->login_orb,
  960. sizeof(struct sbp2_login_orb));
  961. memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
  962. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  963. data[1] = lu->login_orb_dma;
  964. sbp2util_cpu_to_be32_buffer(data, 8);
  965. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  966. /* wait up to 20 seconds for login status */
  967. if (sbp2util_access_timeout(lu, 20*HZ)) {
  968. SBP2_ERR("Error logging into SBP-2 device - timed out");
  969. return -EIO;
  970. }
  971. /* make sure that the returned status matches the login ORB */
  972. if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
  973. SBP2_ERR("Error logging into SBP-2 device - timed out");
  974. return -EIO;
  975. }
  976. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  977. SBP2_ERR("Error logging into SBP-2 device - failed");
  978. return -EIO;
  979. }
  980. sbp2util_cpu_to_be32_buffer(lu->login_response,
  981. sizeof(struct sbp2_login_response));
  982. lu->command_block_agent_addr =
  983. ((u64)lu->login_response->command_block_agent_hi) << 32;
  984. lu->command_block_agent_addr |=
  985. ((u64)lu->login_response->command_block_agent_lo);
  986. lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
  987. SBP2_INFO("Logged into SBP-2 device");
  988. return 0;
  989. }
  990. static int sbp2_logout_device(struct sbp2_lu *lu)
  991. {
  992. struct sbp2_fwhost_info *hi = lu->hi;
  993. quadlet_t data[2];
  994. int error;
  995. lu->logout_orb->reserved1 = 0x0;
  996. lu->logout_orb->reserved2 = 0x0;
  997. lu->logout_orb->reserved3 = 0x0;
  998. lu->logout_orb->reserved4 = 0x0;
  999. lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1000. lu->logout_orb->login_ID_misc |=
  1001. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1002. lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1003. lu->logout_orb->reserved5 = 0x0;
  1004. lu->logout_orb->status_fifo_hi =
  1005. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1006. lu->logout_orb->status_fifo_lo =
  1007. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1008. sbp2util_cpu_to_be32_buffer(lu->logout_orb,
  1009. sizeof(struct sbp2_logout_orb));
  1010. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1011. data[1] = lu->logout_orb_dma;
  1012. sbp2util_cpu_to_be32_buffer(data, 8);
  1013. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1014. if (error)
  1015. return error;
  1016. /* wait up to 1 second for the device to complete logout */
  1017. if (sbp2util_access_timeout(lu, HZ))
  1018. return -EIO;
  1019. SBP2_INFO("Logged out of SBP-2 device");
  1020. return 0;
  1021. }
  1022. static int sbp2_reconnect_device(struct sbp2_lu *lu)
  1023. {
  1024. struct sbp2_fwhost_info *hi = lu->hi;
  1025. quadlet_t data[2];
  1026. int error;
  1027. lu->reconnect_orb->reserved1 = 0x0;
  1028. lu->reconnect_orb->reserved2 = 0x0;
  1029. lu->reconnect_orb->reserved3 = 0x0;
  1030. lu->reconnect_orb->reserved4 = 0x0;
  1031. lu->reconnect_orb->login_ID_misc =
  1032. ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1033. lu->reconnect_orb->login_ID_misc |=
  1034. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1035. lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1036. lu->reconnect_orb->reserved5 = 0x0;
  1037. lu->reconnect_orb->status_fifo_hi =
  1038. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1039. lu->reconnect_orb->status_fifo_lo =
  1040. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1041. sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
  1042. sizeof(struct sbp2_reconnect_orb));
  1043. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1044. data[1] = lu->reconnect_orb_dma;
  1045. sbp2util_cpu_to_be32_buffer(data, 8);
  1046. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1047. if (error)
  1048. return error;
  1049. /* wait up to 1 second for reconnect status */
  1050. if (sbp2util_access_timeout(lu, HZ)) {
  1051. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1052. return -EIO;
  1053. }
  1054. /* make sure that the returned status matches the reconnect ORB */
  1055. if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
  1056. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1057. return -EIO;
  1058. }
  1059. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  1060. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1061. return -EIO;
  1062. }
  1063. SBP2_INFO("Reconnected to SBP-2 device");
  1064. return 0;
  1065. }
  1066. /*
  1067. * Set the target node's Single Phase Retry limit. Affects the target's retry
  1068. * behaviour if our node is too busy to accept requests.
  1069. */
  1070. static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
  1071. {
  1072. quadlet_t data;
  1073. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1074. if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1075. SBP2_ERR("%s error", __FUNCTION__);
  1076. return 0;
  1077. }
  1078. static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
  1079. struct unit_directory *ud)
  1080. {
  1081. struct csr1212_keyval *kv;
  1082. struct csr1212_dentry *dentry;
  1083. u64 management_agent_addr;
  1084. u32 unit_characteristics, firmware_revision;
  1085. unsigned workarounds;
  1086. int i;
  1087. management_agent_addr = 0;
  1088. unit_characteristics = 0;
  1089. firmware_revision = 0;
  1090. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1091. switch (kv->key.id) {
  1092. case CSR1212_KV_ID_DEPENDENT_INFO:
  1093. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
  1094. management_agent_addr =
  1095. CSR1212_REGISTER_SPACE_BASE +
  1096. (kv->value.csr_offset << 2);
  1097. else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
  1098. lu->lun = ORB_SET_LUN(kv->value.immediate);
  1099. break;
  1100. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1101. /* FIXME: This is ignored so far.
  1102. * See SBP-2 clause 7.4.8. */
  1103. unit_characteristics = kv->value.immediate;
  1104. break;
  1105. case SBP2_FIRMWARE_REVISION_KEY:
  1106. firmware_revision = kv->value.immediate;
  1107. break;
  1108. default:
  1109. /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
  1110. * Its "ordered" bit has consequences for command ORB
  1111. * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
  1112. break;
  1113. }
  1114. }
  1115. workarounds = sbp2_default_workarounds;
  1116. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1117. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1118. if (sbp2_workarounds_table[i].firmware_revision &&
  1119. sbp2_workarounds_table[i].firmware_revision !=
  1120. (firmware_revision & 0xffff00))
  1121. continue;
  1122. if (sbp2_workarounds_table[i].model_id &&
  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,
  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, sgpnt, scsi_use_sg,
  1247. 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, scsi_request_buffer,
  1292. cmd->dma_size, cmd->dma_dir);
  1293. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1294. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1295. /* handle case where we get a command w/o s/g enabled
  1296. * (but check for transfers larger than 64K) */
  1297. if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
  1298. orb->data_descriptor_lo = cmd->cmd_dma;
  1299. orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
  1300. } else {
  1301. /* The buffer is too large. Turn this into page tables. */
  1302. struct sbp2_unrestricted_page_table *sg_element =
  1303. &cmd->scatter_gather_element[0];
  1304. u32 sg_count, sg_len;
  1305. dma_addr_t sg_addr;
  1306. orb->data_descriptor_lo = cmd->sge_dma;
  1307. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1308. /* fill out our SBP-2 page tables; split up the large buffer */
  1309. sg_count = 0;
  1310. sg_len = scsi_request_bufflen;
  1311. sg_addr = cmd->cmd_dma;
  1312. while (sg_len) {
  1313. sg_element[sg_count].segment_base_lo = sg_addr;
  1314. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1315. sg_element[sg_count].length_segment_base_hi =
  1316. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1317. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1318. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1319. } else {
  1320. sg_element[sg_count].length_segment_base_hi =
  1321. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1322. sg_len = 0;
  1323. }
  1324. sg_count++;
  1325. }
  1326. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1327. sbp2util_cpu_to_be32_buffer(sg_element,
  1328. (sizeof(struct sbp2_unrestricted_page_table)) *
  1329. sg_count);
  1330. }
  1331. }
  1332. static void sbp2_create_command_orb(struct sbp2_lu *lu,
  1333. struct sbp2_command_info *cmd,
  1334. unchar *scsi_cmd,
  1335. unsigned int scsi_use_sg,
  1336. unsigned int scsi_request_bufflen,
  1337. void *scsi_request_buffer,
  1338. enum dma_data_direction dma_dir)
  1339. {
  1340. struct sbp2_fwhost_info *hi = lu->hi;
  1341. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1342. struct sbp2_command_orb *orb = &cmd->command_orb;
  1343. u32 orb_direction;
  1344. /*
  1345. * Set-up our command ORB.
  1346. *
  1347. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1348. * best performance (at least with the devices I have). This means
  1349. * that data_size becomes the number of s/g elements, and
  1350. * page_size should be zero (for unrestricted).
  1351. */
  1352. orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1353. orb->next_ORB_lo = 0x0;
  1354. orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
  1355. orb->misc |= ORB_SET_SPEED(lu->speed_code);
  1356. orb->misc |= ORB_SET_NOTIFY(1);
  1357. if (dma_dir == DMA_NONE)
  1358. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1359. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1360. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1361. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1362. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1363. else {
  1364. SBP2_INFO("Falling back to DMA_NONE");
  1365. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1366. }
  1367. /* set up our page table stuff */
  1368. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1369. orb->data_descriptor_hi = 0x0;
  1370. orb->data_descriptor_lo = 0x0;
  1371. orb->misc |= ORB_SET_DIRECTION(1);
  1372. } else if (scsi_use_sg)
  1373. sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
  1374. orb_direction, dma_dir);
  1375. else
  1376. sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
  1377. scsi_request_bufflen,
  1378. scsi_request_buffer, dma_dir);
  1379. sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
  1380. memset(orb->cdb, 0, 12);
  1381. memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1382. }
  1383. static void sbp2_link_orb_command(struct sbp2_lu *lu,
  1384. struct sbp2_command_info *cmd)
  1385. {
  1386. struct sbp2_fwhost_info *hi = lu->hi;
  1387. struct sbp2_command_orb *last_orb;
  1388. dma_addr_t last_orb_dma;
  1389. u64 addr = lu->command_block_agent_addr;
  1390. quadlet_t data[2];
  1391. size_t length;
  1392. unsigned long flags;
  1393. dma_sync_single_for_device(&hi->host->device, cmd->command_orb_dma,
  1394. sizeof(struct sbp2_command_orb),
  1395. DMA_TO_DEVICE);
  1396. dma_sync_single_for_device(&hi->host->device, cmd->sge_dma,
  1397. sizeof(cmd->scatter_gather_element),
  1398. DMA_BIDIRECTIONAL);
  1399. /* check to see if there are any previous orbs to use */
  1400. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1401. last_orb = lu->last_orb;
  1402. last_orb_dma = lu->last_orb_dma;
  1403. if (!last_orb) {
  1404. /*
  1405. * last_orb == NULL means: We know that the target's fetch agent
  1406. * is not active right now.
  1407. */
  1408. addr += SBP2_ORB_POINTER_OFFSET;
  1409. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1410. data[1] = cmd->command_orb_dma;
  1411. sbp2util_cpu_to_be32_buffer(data, 8);
  1412. length = 8;
  1413. } else {
  1414. /*
  1415. * last_orb != NULL means: We know that the target's fetch agent
  1416. * is (very probably) not dead or in reset state right now.
  1417. * We have an ORB already sent that we can append a new one to.
  1418. * The target's fetch agent may or may not have read this
  1419. * previous ORB yet.
  1420. */
  1421. dma_sync_single_for_cpu(&hi->host->device, last_orb_dma,
  1422. sizeof(struct sbp2_command_orb),
  1423. DMA_TO_DEVICE);
  1424. last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
  1425. wmb();
  1426. /* Tells hardware that this pointer is valid */
  1427. last_orb->next_ORB_hi = 0;
  1428. dma_sync_single_for_device(&hi->host->device, last_orb_dma,
  1429. sizeof(struct sbp2_command_orb),
  1430. DMA_TO_DEVICE);
  1431. addr += SBP2_DOORBELL_OFFSET;
  1432. data[0] = 0;
  1433. length = 4;
  1434. }
  1435. lu->last_orb = &cmd->command_orb;
  1436. lu->last_orb_dma = cmd->command_orb_dma;
  1437. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1438. if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
  1439. /*
  1440. * sbp2util_node_write_no_wait failed. We certainly ran out
  1441. * of transaction labels, perhaps just because there were no
  1442. * context switches which gave khpsbpkt a chance to collect
  1443. * free tlabels. Try again in non-atomic context. If necessary,
  1444. * the workqueue job will sleep to guaranteedly get a tlabel.
  1445. * We do not accept new commands until the job is over.
  1446. */
  1447. scsi_block_requests(lu->shost);
  1448. PREPARE_WORK(&lu->protocol_work,
  1449. last_orb ? sbp2util_write_doorbell:
  1450. sbp2util_write_orb_pointer);
  1451. schedule_work(&lu->protocol_work);
  1452. }
  1453. }
  1454. static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
  1455. void (*done)(struct scsi_cmnd *))
  1456. {
  1457. unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
  1458. unsigned int request_bufflen = SCpnt->request_bufflen;
  1459. struct sbp2_command_info *cmd;
  1460. cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
  1461. if (!cmd)
  1462. return -EIO;
  1463. sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
  1464. request_bufflen, SCpnt->request_buffer,
  1465. SCpnt->sc_data_direction);
  1466. sbp2_link_orb_command(lu, cmd);
  1467. return 0;
  1468. }
  1469. /*
  1470. * Translates SBP-2 status into SCSI sense data for check conditions
  1471. */
  1472. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
  1473. unchar *sense_data)
  1474. {
  1475. /* OK, it's pretty ugly... ;-) */
  1476. sense_data[0] = 0x70;
  1477. sense_data[1] = 0x0;
  1478. sense_data[2] = sbp2_status[9];
  1479. sense_data[3] = sbp2_status[12];
  1480. sense_data[4] = sbp2_status[13];
  1481. sense_data[5] = sbp2_status[14];
  1482. sense_data[6] = sbp2_status[15];
  1483. sense_data[7] = 10;
  1484. sense_data[8] = sbp2_status[16];
  1485. sense_data[9] = sbp2_status[17];
  1486. sense_data[10] = sbp2_status[18];
  1487. sense_data[11] = sbp2_status[19];
  1488. sense_data[12] = sbp2_status[10];
  1489. sense_data[13] = sbp2_status[11];
  1490. sense_data[14] = sbp2_status[20];
  1491. sense_data[15] = sbp2_status[21];
  1492. return sbp2_status[8] & 0x3f;
  1493. }
  1494. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1495. int destid, quadlet_t *data, u64 addr,
  1496. size_t length, u16 fl)
  1497. {
  1498. struct sbp2_fwhost_info *hi;
  1499. struct sbp2_lu *lu = NULL, *lu_tmp;
  1500. struct scsi_cmnd *SCpnt = NULL;
  1501. struct sbp2_status_block *sb;
  1502. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1503. struct sbp2_command_info *cmd;
  1504. unsigned long flags;
  1505. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1506. SBP2_ERR("Wrong size of status block");
  1507. return RCODE_ADDRESS_ERROR;
  1508. }
  1509. if (unlikely(!host)) {
  1510. SBP2_ERR("host is NULL - this is bad!");
  1511. return RCODE_ADDRESS_ERROR;
  1512. }
  1513. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1514. if (unlikely(!hi)) {
  1515. SBP2_ERR("host info is NULL - this is bad!");
  1516. return RCODE_ADDRESS_ERROR;
  1517. }
  1518. /* Find the unit which wrote the status. */
  1519. list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
  1520. if (lu_tmp->ne->nodeid == nodeid &&
  1521. lu_tmp->status_fifo_addr == addr) {
  1522. lu = lu_tmp;
  1523. break;
  1524. }
  1525. }
  1526. if (unlikely(!lu)) {
  1527. SBP2_ERR("lu is NULL - device is gone?");
  1528. return RCODE_ADDRESS_ERROR;
  1529. }
  1530. /* Put response into lu status fifo buffer. The first two bytes
  1531. * come in big endian bit order. Often the target writes only a
  1532. * truncated status block, minimally the first two quadlets. The rest
  1533. * is implied to be zeros. */
  1534. sb = &lu->status_block;
  1535. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1536. memcpy(sb, data, length);
  1537. sbp2util_be32_to_cpu_buffer(sb, 8);
  1538. /* Ignore unsolicited status. Handle command ORB status. */
  1539. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1540. cmd = NULL;
  1541. else
  1542. cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
  1543. if (cmd) {
  1544. dma_sync_single_for_cpu(&hi->host->device, cmd->command_orb_dma,
  1545. sizeof(struct sbp2_command_orb),
  1546. DMA_TO_DEVICE);
  1547. dma_sync_single_for_cpu(&hi->host->device, cmd->sge_dma,
  1548. sizeof(cmd->scatter_gather_element),
  1549. DMA_BIDIRECTIONAL);
  1550. /* Grab SCSI command pointers and check status. */
  1551. /*
  1552. * FIXME: If the src field in the status is 1, the ORB DMA must
  1553. * not be reused until status for a subsequent ORB is received.
  1554. */
  1555. SCpnt = cmd->Current_SCpnt;
  1556. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1557. sbp2util_mark_command_completed(lu, cmd);
  1558. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1559. if (SCpnt) {
  1560. u32 h = sb->ORB_offset_hi_misc;
  1561. u32 r = STATUS_GET_RESP(h);
  1562. if (r != RESP_STATUS_REQUEST_COMPLETE) {
  1563. SBP2_INFO("resp 0x%x, sbp_status 0x%x",
  1564. r, STATUS_GET_SBP_STATUS(h));
  1565. scsi_status =
  1566. r == RESP_STATUS_TRANSPORT_FAILURE ?
  1567. SBP2_SCSI_STATUS_BUSY :
  1568. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1569. }
  1570. if (STATUS_GET_LEN(h) > 1)
  1571. scsi_status = sbp2_status_to_sense_data(
  1572. (unchar *)sb, SCpnt->sense_buffer);
  1573. if (STATUS_TEST_DEAD(h))
  1574. sbp2_agent_reset(lu, 0);
  1575. }
  1576. /* Check here to see if there are no commands in-use. If there
  1577. * are none, we know that the fetch agent left the active state
  1578. * _and_ that we did not reactivate it yet. Therefore clear
  1579. * last_orb so that next time we write directly to the
  1580. * ORB_POINTER register. That way the fetch agent does not need
  1581. * to refetch the next_ORB. */
  1582. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1583. if (list_empty(&lu->cmd_orb_inuse))
  1584. lu->last_orb = NULL;
  1585. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1586. } else {
  1587. /* It's probably status after a management request. */
  1588. if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
  1589. (sb->ORB_offset_lo == lu->login_orb_dma) ||
  1590. (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
  1591. (sb->ORB_offset_lo == lu->logout_orb_dma)) {
  1592. lu->access_complete = 1;
  1593. wake_up_interruptible(&sbp2_access_wq);
  1594. }
  1595. }
  1596. if (SCpnt)
  1597. sbp2scsi_complete_command(lu, scsi_status, SCpnt,
  1598. cmd->Current_done);
  1599. return RCODE_COMPLETE;
  1600. }
  1601. /**************************************
  1602. * SCSI interface related section
  1603. **************************************/
  1604. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1605. void (*done)(struct scsi_cmnd *))
  1606. {
  1607. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1608. struct sbp2_fwhost_info *hi;
  1609. int result = DID_NO_CONNECT << 16;
  1610. if (unlikely(!sbp2util_node_is_available(lu)))
  1611. goto done;
  1612. hi = lu->hi;
  1613. if (unlikely(!hi)) {
  1614. SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
  1615. goto done;
  1616. }
  1617. /* Multiple units are currently represented to the SCSI core as separate
  1618. * targets, not as one target with multiple LUs. Therefore return
  1619. * selection time-out to any IO directed at non-zero LUNs. */
  1620. if (unlikely(SCpnt->device->lun))
  1621. goto done;
  1622. /* handle the request sense command here (auto-request sense) */
  1623. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1624. memcpy(SCpnt->request_buffer, SCpnt->sense_buffer,
  1625. SCpnt->request_bufflen);
  1626. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  1627. sbp2scsi_complete_command(lu, SBP2_SCSI_STATUS_GOOD, SCpnt,
  1628. done);
  1629. return 0;
  1630. }
  1631. if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
  1632. SBP2_ERR("Bus reset in progress - rejecting command");
  1633. result = DID_BUS_BUSY << 16;
  1634. goto done;
  1635. }
  1636. /* Bidirectional commands are not yet implemented,
  1637. * and unknown transfer direction not handled. */
  1638. if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
  1639. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  1640. result = DID_ERROR << 16;
  1641. goto done;
  1642. }
  1643. if (sbp2_send_command(lu, SCpnt, done)) {
  1644. SBP2_ERR("Error sending SCSI command");
  1645. sbp2scsi_complete_command(lu,
  1646. SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  1647. SCpnt, done);
  1648. }
  1649. return 0;
  1650. done:
  1651. SCpnt->result = result;
  1652. done(SCpnt);
  1653. return 0;
  1654. }
  1655. static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
  1656. {
  1657. struct sbp2_fwhost_info *hi = lu->hi;
  1658. struct list_head *lh;
  1659. struct sbp2_command_info *cmd;
  1660. unsigned long flags;
  1661. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1662. while (!list_empty(&lu->cmd_orb_inuse)) {
  1663. lh = lu->cmd_orb_inuse.next;
  1664. cmd = list_entry(lh, struct sbp2_command_info, list);
  1665. dma_sync_single_for_cpu(&hi->host->device, cmd->command_orb_dma,
  1666. sizeof(struct sbp2_command_orb),
  1667. DMA_TO_DEVICE);
  1668. dma_sync_single_for_cpu(&hi->host->device, cmd->sge_dma,
  1669. sizeof(cmd->scatter_gather_element),
  1670. DMA_BIDIRECTIONAL);
  1671. sbp2util_mark_command_completed(lu, cmd);
  1672. if (cmd->Current_SCpnt) {
  1673. cmd->Current_SCpnt->result = status << 16;
  1674. cmd->Current_done(cmd->Current_SCpnt);
  1675. }
  1676. }
  1677. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1678. return;
  1679. }
  1680. /*
  1681. * Complete a regular SCSI command. Can be called in atomic context.
  1682. */
  1683. static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
  1684. struct scsi_cmnd *SCpnt,
  1685. void (*done)(struct scsi_cmnd *))
  1686. {
  1687. if (!SCpnt) {
  1688. SBP2_ERR("SCpnt is NULL");
  1689. return;
  1690. }
  1691. switch (scsi_status) {
  1692. case SBP2_SCSI_STATUS_GOOD:
  1693. SCpnt->result = DID_OK << 16;
  1694. break;
  1695. case SBP2_SCSI_STATUS_BUSY:
  1696. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  1697. SCpnt->result = DID_BUS_BUSY << 16;
  1698. break;
  1699. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  1700. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  1701. break;
  1702. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  1703. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  1704. SCpnt->result = DID_NO_CONNECT << 16;
  1705. scsi_print_command(SCpnt);
  1706. break;
  1707. case SBP2_SCSI_STATUS_CONDITION_MET:
  1708. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  1709. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  1710. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  1711. SCpnt->result = DID_ERROR << 16;
  1712. scsi_print_command(SCpnt);
  1713. break;
  1714. default:
  1715. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  1716. SCpnt->result = DID_ERROR << 16;
  1717. }
  1718. /* If a bus reset is in progress and there was an error, complete
  1719. * the command as busy so that it will get retried. */
  1720. if (!hpsb_node_entry_valid(lu->ne)
  1721. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  1722. SBP2_ERR("Completing command with busy (bus reset)");
  1723. SCpnt->result = DID_BUS_BUSY << 16;
  1724. }
  1725. /* Tell the SCSI stack that we're done with this command. */
  1726. done(SCpnt);
  1727. }
  1728. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  1729. {
  1730. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1731. lu->sdev = sdev;
  1732. sdev->allow_restart = 1;
  1733. if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  1734. sdev->inquiry_len = 36;
  1735. return 0;
  1736. }
  1737. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  1738. {
  1739. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1740. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  1741. sdev->use_10_for_rw = 1;
  1742. if (sdev->type == TYPE_DISK &&
  1743. lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  1744. sdev->skip_ms_page_8 = 1;
  1745. if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  1746. sdev->fix_capacity = 1;
  1747. return 0;
  1748. }
  1749. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  1750. {
  1751. ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
  1752. return;
  1753. }
  1754. /*
  1755. * Called by scsi stack when something has really gone wrong.
  1756. * Usually called when a command has timed-out for some reason.
  1757. */
  1758. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  1759. {
  1760. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1761. struct sbp2_fwhost_info *hi = lu->hi;
  1762. struct sbp2_command_info *cmd;
  1763. unsigned long flags;
  1764. SBP2_INFO("aborting sbp2 command");
  1765. scsi_print_command(SCpnt);
  1766. if (sbp2util_node_is_available(lu)) {
  1767. sbp2_agent_reset(lu, 1);
  1768. /* Return a matching command structure to the free pool. */
  1769. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1770. cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
  1771. if (cmd) {
  1772. dma_sync_single_for_cpu(&hi->host->device,
  1773. cmd->command_orb_dma,
  1774. sizeof(struct sbp2_command_orb),
  1775. DMA_TO_DEVICE);
  1776. dma_sync_single_for_cpu(&hi->host->device, cmd->sge_dma,
  1777. sizeof(cmd->scatter_gather_element),
  1778. DMA_BIDIRECTIONAL);
  1779. sbp2util_mark_command_completed(lu, cmd);
  1780. if (cmd->Current_SCpnt) {
  1781. cmd->Current_SCpnt->result = DID_ABORT << 16;
  1782. cmd->Current_done(cmd->Current_SCpnt);
  1783. }
  1784. }
  1785. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1786. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  1787. }
  1788. return SUCCESS;
  1789. }
  1790. /*
  1791. * Called by scsi stack when something has really gone wrong.
  1792. */
  1793. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  1794. {
  1795. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1796. SBP2_INFO("reset requested");
  1797. if (sbp2util_node_is_available(lu)) {
  1798. SBP2_INFO("generating sbp2 fetch agent reset");
  1799. sbp2_agent_reset(lu, 1);
  1800. }
  1801. return SUCCESS;
  1802. }
  1803. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  1804. struct device_attribute *attr,
  1805. char *buf)
  1806. {
  1807. struct scsi_device *sdev;
  1808. struct sbp2_lu *lu;
  1809. if (!(sdev = to_scsi_device(dev)))
  1810. return 0;
  1811. if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
  1812. return 0;
  1813. return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)lu->ne->guid,
  1814. lu->ud->id, ORB_SET_LUN(lu->lun));
  1815. }
  1816. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  1817. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  1818. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  1819. MODULE_LICENSE("GPL");
  1820. static int sbp2_module_init(void)
  1821. {
  1822. int ret;
  1823. if (sbp2_serialize_io) {
  1824. sbp2_shost_template.can_queue = 1;
  1825. sbp2_shost_template.cmd_per_lun = 1;
  1826. }
  1827. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1828. (sbp2_max_sectors * 512) > (128 * 1024))
  1829. sbp2_max_sectors = 128 * 1024 / 512;
  1830. sbp2_shost_template.max_sectors = sbp2_max_sectors;
  1831. hpsb_register_highlevel(&sbp2_highlevel);
  1832. ret = hpsb_register_protocol(&sbp2_driver);
  1833. if (ret) {
  1834. SBP2_ERR("Failed to register protocol");
  1835. hpsb_unregister_highlevel(&sbp2_highlevel);
  1836. return ret;
  1837. }
  1838. return 0;
  1839. }
  1840. static void __exit sbp2_module_exit(void)
  1841. {
  1842. hpsb_unregister_protocol(&sbp2_driver);
  1843. hpsb_unregister_highlevel(&sbp2_highlevel);
  1844. }
  1845. module_init(sbp2_module_init);
  1846. module_exit(sbp2_module_exit);