sbp2.c 79 KB

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