sbp2.c 64 KB

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