sbp2.c 73 KB

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