sbp2.c 67 KB

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