sbp2.c 77 KB

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