sbp2.c 63 KB

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